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

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
博客园 - 聂微东
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
小众软件
小众软件
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
量子位
月光博客
月光博客
博客园 - 【当耐特】
博客园 - 叶小钗

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
Secure JavaScript Coding Practices Against Command Inject...
2024-05-17 · via NodeJS Security & NodeJS Secure Coding's Blog

Command injection vulnerabilities are a severe security threat in Node.js applications. They arise when user-controlled input is used to construct system commands, allowing attackers to execute arbitrary code on your server.

Here, we explore secure coding practices to prevent such vulnerabilities and analyze real-world examples (CVE-2024-21488, CVE-2019-25158 as a couple of recent examples and reference points) to understand the risks and mitigation strategies.

1. Prefer Secure Command APIs

child_process.execFile: This function executes a specific binary file, providing a safer alternative to child_process.exec as it avoids shell interpretation of arguments.

const { execFile } = require('child_process');

const filePath = 'path/to/safe_script.sh';

const args = ['argument1', 'argument2'];

execFile(filePath, args, (error, stdout, stderr) => {

// handle results

});

child_process.spawn: This function offers more granular control over command execution, allowing separate arguments and environment variable specification.

const { spawn } = require('child_process');

const command = 'ls';

const args = ['-l', '/tmp'];

const childProcess = spawn(command, args);

childProcess.stdout.on('data', (data) => {

console.log(`stdout: ${data}`);

});

childProcess.stderr.on('data', (data) => {

console.error(`stderr: ${data}`);

});

childProcess.on('close', (code) => {

console.log(`child process exited with code ${code}`);

});

2. Avoid Insecure APIs

child_process.exec: This function executes a shell command and is vulnerable to injection if user input is directly included in the command string. Avoid using child_process.exec unless absolutely necessary.

3. Isolate Commands from Arguments

Construct commands as separate strings from user input and arguments. This prevents malicious code injection through manipulation of spaces or special characters.

// Sanitize user input

const sanitizedUserInput = sanitizeUserInput(userInput);

// Prepare user input to be escaped via command line arguments

// ❌ Insecure: Directly concatenating user input

const safeCommand = `some_command ${sanitizedUserInput}`;

// ✅ Secure: Using separate arguments

const safeCommand = child_process.execFile('some_command', [ '--someFlag', sanitizedUserInput]);

4. Avoid Shell When Possible

If possible, avoid using a shell environment when spawning child processes. This reduces the risk of shell interpretation issues.

const { spawn } = require('child_process');

spawn('ls', ['-l', '/tmp']);

5. Override Environment Variables

When spawning child processes, consider overriding environment variables to prevent leaking sensitive information from your parent process environment.

const { spawn } = require('child_process');

// Override sensitive variable

const env = { ...process.env, MY_SECRET: 'redacted' };

spawn('some_command', [], { env });

Real-World Command-Injection Examples:

The provided CVEs illustrate the consequences of insecure coding practices:

  • CVE-2024-21488: The network package uses child_process.exec without proper input sanitization, allowing attackers to inject arbitrary commands through user input.

  • CVE-2019-25158: The tts-api package is vulnerable to command injection via the onSpeechDone function due to potentially unsafe usage of shell commands.

By adopting secure coding practices and avoiding vulnerable APIs like child_process.exec, you can significantly reduce the risk of command injection vulnerabilities in your Node.js applications.

Stay updated on best practices and consider using libraries with a strong focus on secure coding principles.