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

推荐订阅源

J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
V
V2EX
N
Netflix TechBlog - Medium
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
量子位
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
月光博客
月光博客
F
Fortinet All Blogs

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
Node.js Security Best Practices
3. Follow authentication and authorization best practices · 2024-03-15 · via NodeJS Security & NodeJS Secure Coding's Blog

Building secure Node.js applications requires a multifaceted approach, encompassing various security best practices that safeguard your server-side code from vulnerabilities. This guide goes into these crucial practices, ranging from coding practices to general Node.js application security that will help empower you to develop robust and secure Node.js applications.

1. Node.js secure coding

The first step for a secure Node.js application is to write secure code. This includes following best practices for secure coding, such as input validation, output encoding, string parameterization and secure error handling.

Some of these security best practices might sound familiar, or even obvious but for example, most developers would consider mitigating command injection vulnerabilities using input sanitization and validation. However, these are helpful tactics, but other secure coding practices like string parameterization that splits a command from its command-line arguments, are de-facto the secure coding practice to follow.

I wrote two comprehensive books that teach developers how to follow secure coding conventions in Node.js in order to mitigate security vulnerabilities: path traversal and command injection.

2. Secure your dependencies

Maintain up-to-date dependencies is crucial in terms of overall Node.js application security as well as dependency health. Regularly update your application’s dependencies to benefit from security patches and bug fixes. Utilize tools like Snyk to identify vulnerabilities in your dependencies and address them promptly.

npm install url-parse serve @node-red/runtime

added 176 packages, and audited 177 packages in 16s

12 high severity vulnerabilities

You should also responsibly scrutinize third-party packages. Before introducing a new dependency, meticulously evaluate its reputation, maintenance status, and potential security risks.

Did you know that crypto-mining malware was found in a dependency that traced back to North-Korean hackers?. Keep a close eye on your dependencies.

Aim for robust and battle-tested authentication algorithms. Employ strong authentication mechanisms like password hashing with secure algorithms (e.g., bcrypt) and of course follow practices like implementing authentication layers via two-factor authentication (2FA) to enhance user account security.

What you need to know to avoid common security pitfalls when implementing authentication:

  • Do not store plain text passwords.
  • Do not use weak hashing algorithms. If you want to use modern secure hashing algorithms like scrypt or argon2, those are too, vetted securely to use.
  • Do not implement password limits like a maximum of 16 characters or requiring special characters. Instead, encourage the use of passphrases and lengthy generated passwords from password managers.

You can read more about secure Node.js authentication and cryptography for a comprehensive guide on this topic.

4. Secure sensitive data

With regards to encryption at rest, make sure to avoid leaking sensitive information. This includes using secure key management practices, including key rotation and proper key storage using dedicated key management systems. But really, at the first place, just avoid hard-coding sensitive data in your application source code or storing credentials in plain text configuration files or environment variables.

If you wonder what practices to avoid then you should read my guide on environment variables and security anti-patterns.

There’s another more in-depth concept to look into which is key management: most cloud vendors offer a key management service that can help you manage your keys securely. For example, AWS Key Management Service (KMS) or Google Cloud Key Management Service (KMS) are good examples. You can also use open-source solutions like HashiCorp Vault or the dotenv’s team dotenvx which builds on the practice of using .env files for configuration and secrets management.

5. Prioritize error handling and logging

Handle errors gracefully and securely. For example, if you’re using the Express web application and don’t start your Node.js servers with NODE_ENV=production then you might be leaking sensitive information to the client. This is because the default error handling middleware in Express will expose stack traces and other sensitive information to the client when errors are left unhandled and uncaught. This is because the default NODE_ENV is development and not production. Do you remember to set it to production in your CI or your Node.js deployment workflows?

Implement robust error handling to prevent sensitive information disclosure and application crashes in the event of unexpected errors. Avoid returning detailed error messages that might personally identify users or expose details that would be useful to attackers.

Follow secure logging practices, such as making sure to monitor application behavior and potential security incidents (using a tool like Sentry that captures error traces and could prove handy). Avoid logging sensitive information like passwords or personally identifiable information (PII).

Input validation and sanitization are helpful security practices. With that regard, utilize libraries like joi validator.js to validate user input and sanitize it to help mitigate malicious code injection attacks, regular expression denial of service, and other security vulnerabilities.

Don’t fall into the trap of attempting to sanitize input to mitigate code injection attacks like like XSS (Cross-Site Scripting) and SQL injection, because these require different mitigation strategies such as output encoding and query parameterization, applied respectively.

You’d probably want to also make use of a library like zod or ajv to specifically maintain your request/reply schema and validate it. This is a good practice to ensure that your application is not only secure but also robust and reliable.

6. Secure your Node.js runtime

A handful of practices fall into the category of securing your Node.js runtime. These include:

  • Minimize privileges: Run your Node.js application with the least privileged user account necessary to minimize the potential impact of security breaches.
  • Keep the Node.js runtime updated: Maintain the latest security updates for your operating system, as well as your Node.js version, and other server-side software to address known vulnerabilities. If you’re using containers to deploy your Node.js application, make sure to keep your container images updated.

Continued learning is essential to stay ahead of the curve. Stay informed about emerging security threats and vulnerabilities specific to Node.js development. Don’t blindly ignore security vulnerability results. They’re there for a reason, and yes it may be frustrating and daunting to find whether they are applicable or not, but there are frameworks, practices and technological solutions to help with that.

I also recommend participating in the Node.js ecosystem security working group and other friendly DevSecOps and secure coding communities to stay updated on the latest best practices.

What’s next?

If you’re keen to follow secure coding practices in Node.js, then you should consider reading my books on Node.js Secure Coding which teach you about insecure code patterns from real-world examples of vulnerable npm packages and how the security incidents unfolded for them.

By diligently adhering to these comprehensive best practices, you can significantly enhance the security posture of your Node.js applications and safeguard your users’ data and privacy. Remember that security is an ongoing process and it’s essential to continuously re-evaluate your security measures and adapt to the threats and vulnerabilities that emerge over time.