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

推荐订阅源

WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
C
Check Point Blog
宝玉的分享
宝玉的分享
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
量子位
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
J
Java Code Geeks
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
美团技术团队
H
Help Net Security
B
Blog
博客园_首页

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
Protecting Against Common Node.js Vulnerabilities
2024-03-01 · via NodeJS Security & NodeJS Secure Coding's Blog

As a Node.js developer, keeping your applications secure is crucial. Node applications can be vulnerable to issues like command injection, path traversal, and insecure APIs. In this post, we’ll explore some common Node.js security pitfalls and how to avoid them.

Defending Against Command Injection

Command injection is one of the most common and dangerous vulnerabilities in Node. A key way this happens is by concatenating unsanitized user input into commands executed by a child process. For example:

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

const userInput = process.argv[2];

exec(`git checkout ${userInput}`);

If a malicious user provides input like ; rm -rf /, it could delete critical files. Instead, avoid concatenating user input into commands. Consider using the execFile API which only allows specifying arguments, not arbitrary commands.

The Fastify framework provides some built-in security controls like request and response schema validation that will be helpful to mitigate malicious input. That said, always be careful what input you pass directly to the OS. A schema validation is not enough to prevent command injection.

Defending Against Path Traversal

Another common issue is path traversal attacks, where a user can access files outside an expected directory.

Her’s an example of a more classic use-case of an API endpoint for serving PDF files from disk:

const express = require('express');

const fs = require('fs');

const app = express();

app.get('/users/:userId/files/:fileId', (req, res) => {

const userId = req.params.userId;

const fileId = req.params.fileId;

// In real scenario, look up file path for given user and file ID combo

const filePath = `/users/${userId}/files/${fileId}.pdf`;

fs.readFile(filePath, (err, fileContents) => {

if (err) {

return res.status(400).send('Error reading file');

}

res.contentType('application/pdf');

res.send(fileContents);

});

});

app.listen(3000);

Conducting Secure Code Reviews

Many vulnerabilities can be caught early by conducting peer code reviews before releasing software. When reviewing code, watch for areas where user input is incorporated without sanitization.

Analyze all external data entry points like APIs, CLI arguments, uploaded files, etc. I recommend following these 10 best practices for secure code review of Node.js code to help you get started.

Also carefully review uses of privileges like executing commands, writing files, accessing APIs, etc. Ask questions any time elevated privileges are used. Could this area be exploited? What’s the worst possible input? Code reviews catch issues proactively while code is still easy to change.

Assessing Your Node.js Security Knowledge

Continuously assess your own knowledge of Node.js application security. Do a regular security self-assessment to identify gaps in your understanding. Check that you understand all aspects of the Node.js security model like scope, sandboxing, input validation, secure API usage, etc.

For example, can you find the security issue in this code?

const fastify = require('fastify')();

fastify.get('/downloads', (req, res) => {

const userFile = req.query.file;

if (userFile.includes('..') || userFile.includes('/')) {

return res.status(400).send({error: 'Illegal directory traversal attempt'});

}

const fileAbsolutePath = decodeURIComponent(`/downloads/files/${userFile}`);

fs.readFile(fileAbsolutePath, (err, data) => {

if (err) {

return res.status(500).send('Error accessing file');

}

res.type('application/octet-stream');

res.send(data);

});

});

fastify.listen(3000);

If you aren’t sure about the issue or how to fix it, you want to take a look at my book Node.js Secure Coding: Prevention and Exploitation of Path Traversal Vulnerabilities.

Key Vulnerable Node.js APIs to Avoid

Some Node.js APIs have security issues if used improperly. Be cautious with APIs like vm.runInContext, child_process.exec, path.join() and others. Similarly, calling OS commands, writing files, and accessing sensitive environment variables can lead to vulnerabilities if not handled carefully. Consider more secure alternatives like execFile and access controls around privileged operations.

Security must be every Node.js developer’s priority. Follow best practices around input validation, access controls, and peer code reviews. Continuously build your Node.js security knowledge through hands-on training. Making security a habit will help protect both your applications and your users.