





















Node.js is a popular runtime environment for building server-side applications. However, like any other technology, Node.js security issues can result in vulnerabilities that expose your applications to potential threats.
In this blog post, I’ll review some common Node.js security issues and how to mitigate them. Hopefully these insights will help you secure your Node.js applications and write secure code.
The security threat is that malicious actors or even misconfigured clients can overload a Node.js server with HTTP requests, rendering it unavailable to legitimate users. One example of prior art is the Slowloris attack, which sends partial HTTP requests to keep connections open indefinitely.
Key takeaways:
headersTimeout, requestTimeout) to drop idle or slow connections.http.Server properties like agent.maxSockets to restrict the number of open connections per host or total.The threat with DNS rebinding is attackers exploiting a running debugging inspector with DNS rebinding to gain unauthorized access to your application.
This in fact was a real-world attack on the Node.js debugging inspector in 2018, where an attacker could execute arbitrary code on the server by sending a malicious HTTP request. It resulted in a CVE-2018-12120 security advisory in which All versions prior to Node.js 6.15.0: Debugger port 5858 listens on any interface by default.
Key takeaways:
--inspect) in production environments.SIGUSR1 Signal: Implement a process.on('SIGUSR1', ...) listener to disable the inspector on the SIGUSR1 signal.Imagine the threat of publishing sensitive information accidentally when publishing your Node.js package. This could include private keys, passwords, or other confidential data that you as a developer often keep around in your project files.
No doubt you probably have worked before with .env, config.json or other configuration files that contain sensitive information. If you accidentally publish these files as part of your npm package then you could expose this information on the public npm registry.
Key takeaways:
--dry-run: Before publishing, test with npm publish --dry-run to verify the list of files being published..gitignore and .npmignore files to exclude sensitive files from publication.package.json: Review the files property in package.json to ensure it only lists intended files.The threat here is that attackers can potentially steal sensitive information (like passwords) by analyzing your application’s response times.
Key takeaways:
crypto.timingSafeEqual to ensure a constant execution time regardless of the value.scrypt function for secure password comparisons (it’s available as part of the Node.js crypto built-in core module).Command injection vulnerabilities remain a significant threat in Node.js applications due to insecure coding practices. These vulnerabilities arise when user input or untrusted data is directly used to construct system commands.
The threat revolves around attackers who exploit command injection vulnerabilities to execute arbitrary commands on your server. This can lead to severe consequences and past analysis of real-world command injection examples show the consequences are severe and include data exfiltration, file system manipulation and other system disruptions.
A common culprit for command injection vulnerabilities is the child_process.exec API. This function executes a shell command and returns the output as a buffer. However, using it without proper sanitization poses a security risk.
For example, consider:
const command = `cat /etc/passwd | grep ${username}`;
child_process.exec(command, (error, stdout, stderr) => {
// ... process output
});
In this example, the username variable could be controlled by an attacker. If the attacker provides a specially crafted username containing malicious commands, those commands could be executed on the server.
Here’s how to prevent command injection vulnerabilities:
Input Validation and Sanitization: Validate and sanitize all user input before using it to construct commands. Remove potentially harmful characters or commands from the input.
Alternatives to child_process.exec: Consider safer alternatives like child_process.spawn with proper argument escaping or dedicated libraries for specific tasks.
Example (using a secure API):
const username = sanitizeUserInput(username);
const command = `cat /etc/passwd`;
const args = ['grep', username];
child_process.spawn(command, args, (error, stdout, stderr) => {
// ... process output
});
Command injection vulnerabilities remain a significant threat in Node.js applications and CLI tools, as evident by npm packages like blamer which demonstrated the secure coding complexity when two hyphens caused an argument injection vulnerability.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。