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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
量子位
aimingoo的专栏
aimingoo的专栏
V
V2EX
Vercel News
Vercel News
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
U
Unit 42
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
W
WeLiveSecurity
博客园 - Franky
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Schneier on Security
Schneier on Security
I
InfoQ
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks

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 Vue CLI Security Fix to Mitigate NPM Binary Planting Node.js API Security Vulnerabilities with Path Traversal in files-bucket-server Will You Accept These GPT 4o Secure Coding Recommendations? Command Injection Vulnerability in interactive-git-checkout npm package An Introduction to SSRF Bypasses and Denylist Failures Disclosing a Command Injection Vulnerability in `git-checkout-tool` Prisma Raw Query Leads to SQL Injection? Yes and No Flawed Git Promises Library on npm Leads to Command Injection Vulnerability Regex Gone Wrong: How parse-duration npm Package Can Crash Your Node.js App How I found an XSS in the Nuxt MDC Library for Markdown Content Holes in the Safety Net: Bypassing SSRF Protection in safe-axios How to Parse URLs from Markdown to HTML Securely? NPM Ignore Scripts Best Practices as Security Mitigation for Malicious Packages Where to find npm vulnerabilities? How to Hunt for IDOR Vulnerabilities To Exploit Security Misconfiguration? How to Avoid JWT Security Mistakes in Node.js Can a Node.js Secure Code Review Find Future Vulnerabilities? The Okta bcrypt Security Incident and The Bun vs Node.js Angle in Secure By Design NodeJS Path Traversal Vulnerability Scanner Do not use secrets in environment variables and here's how to do it better How to use npm audit How to use yarn audit Raw SQL Queries are Actually Better for Security Than ORMs? Node API Security Is Node.js Secure? URL Regex Validation: what can go wrong? Uncovering a Prototype Pollution Regression in the core Node.js project Deno CLI Vulnerability Repeats npm mistakes: CVE-2024-37150 Security skills for JavaScript developers Understanding and Preventing Prototype Pollution in Node.js How to protect against a security breach in React Server Components IDOR Vulnerability: What is it and how to prevent it? The security vulnerability of serving images via a route as opposed to static middleware in Node.js Why is it considered a bad practice to write raw SQL commands? JS Security Concepts for JavaScript Developers Secure Coding Practices in Node.js Against Path Traversal Vulnerabilities Secure JavaScript Coding Practices Against Command Injection Vulnerabilities To IDOR or Not to IDOR: Insecure Direct Object Reference in JavaScript Applications Explained npm vulnerabilities: reviewing the security of your dependencies Disclosing code injection vulnerabilities in safe-eval-2 npm package Introducing Node.js Security Permissions Model, Threat Model, and Security Releases Common Node.js Security Issues and How to Mitigate Them How JavaScript developers should embrace npm security The XZ backdoor CVE-2024-3094: a JavaScript perspective Node.js Security Best Practices The Case for Node.js Secure Configuration Protecting Against Common Node.js Vulnerabilities Input Validation Security Best Practices for Node.js A Node.js Vulnerability Scanner to Avoid Security Risks of EOL Runtime Versions JavaScript Security Issues in Node.js Applications OWASP Node.js Authentication, Authorization and Cryptography Practices Secure JavaScript Coding to Avoid Insecure Direct Object References (IDOR) North Korea malware on npm and Ledger connect-kit crypto heist 10 Best Practices for Secure Code Review of Node.js code Node.js and OWASP Top Ten Command Injection: Don't Let Your App Go 'BOOM' Secure Code Review Tips to Defend Against Vulnerable Node.js Code Destroyed by Dashes: How Two Hyphens Cause Argument Injection Vulnerability in blamer npm Package Securing Your Node.js Apps by Analyzing Real-World Command Injection Examples An Introduction to Command Injection Vulnerabilities in Node.js and JavaScript
OWASP Node.js Best Practices Guide
2024-01-15 · via NodeJS Security & NodeJS Secure Coding's Blog

Most developers would agree that properly securing Node.js applications is crucial, yet often overlooked.

By following OWASP’s comprehensive best practices guide, you can effectively harden your Node.js apps against the most common vulnerabilities and attacks.

In this guide, you’ll learn practical techniques across authentication, authorization, cryptography, input validation, and more to implement robust security defenses in your Node.js code.

Introduction to OWASP Node.js Security Best Practices

What is OWASP?

The Open Web Application Security Project (OWASP) is a nonprofit foundation focused on improving software security. With over 220 local chapters worldwide, OWASP brings together security experts to develop open source standards, guides, tools, and resources for developers.

One of their most well-known projects is the OWASP Top 10 list which outlines the most critical web application security risks. While originally focused on vulnerabilities in web apps built with languages like PHP and Java, OWASP has expanded their recommendations to include modern frameworks like Node.js.

Understanding the Importance of Node.js Security

Node.js has grown tremendously in popularity over the past decade, with its event-driven, non-blocking I/O model making it well-suited for building fast and scalable network applications. However, like any technology, Node.js comes with certain security risks that developers need to be aware of.

Some common Node.js security vulnerabilities include:

  • Insecure dependencies: this easily relatable for JavaScript developers who get presented with “X number of vulnerabilities found” when running npm install or yarn install. This is a common issue with Node.js applications, as developers often use third-party packages without checking for vulnerabilities.
  • Cross-site scripting (XSS) - Even though modern frontend frameworks like React, Vue, and Angular have built-in XSS protection, it’s still possible to introduce XSS vulnerabilities in Server-side JavaScript applications built on Node.js and other run-times.
  • Broken authentication - Getting authentication right is hard, and Node.js is no exception. Developers often make mistakes when implementing authentication and session management, which can lead to security vulnerabilities. Often the debate is where do I store the JWT token? In a cookie or in local storage? Maybe the answer is neither? The best for most is probably the use of a secure cookie with the httpOnly flag set to true and manage cookie sessions with a server-side session store.
  • Injection attacks - Many types of injection attacks exist, but perhaps the most devastating is Command Injection where an attacker can execute arbitrary commands on the host operating system. This is a common issue with Node.js applications that use child processes to execute commands.
  • Insecure data exposure - Node.js developers might inadvertently expose sensitive data by exposing configuration files for example and sometimes even source code. Learn to adopt Best practices for bootstrapping a Node.js application configuration.

Following best practices recommended by OWASP can help mitigate these Node.js security vulnerabilities.

Overview of Node.js Security Best Practices from OWASP Top 10

The OWASP Top 10 provides a framework for developers to understand and address the most critical web application security risks. As Node.js continues to grow in popularity, it’s important to evaluate how these risks apply specifically to Node.js applications.

OWASP’s Node.js security guide covers five main areas:

  • Authentication: Implementing proper authentication and session management using modern and up to date libraries like Lucia, NextJS Auth, or outsource entirely to SaaS like Clerk, UserBase and others.
  • Authorization: Role-based authorization checks for route access and resource permissions. This can easily get out of hand and become a mess of code, so it’s best to use a library like CASL to manage authorization. I recommend looking into SaaS services like Permit.io and others.
  • Cryptography: Encrypting/hashing sensitive data with crypto packages like bcrypt and crypto-js, or the built-in crypto module using scrypt.
  • Data Validation: Sanitizing and validating all input data to prevent XSS, code injection and other attacks. This is a common issue with Node.js applications that use the child_process API to execute commands.
  • Security Logging: Logging security events like failed logins to identify potential attacks.

While these risks apply to web apps in general, some are more prevalent or impactful for Node.js. For example, denial-of-service attacks may impact a Node.js runtime much more due to the Node.js practices of limited resource control, making it vulnerable to DoS from unmanaged event loops or memory leaks. A regular expression denial of service attack is a perfect example of the practical risk.

Mapping OWASP Top 10 to Node.js Vulnerabilities

Delving deeper into the OWASP Top 10, we can map specific Node.js vulnerabilities for each risk:

Injection Attacks

  • NoSQL injection: MongoDB queries with untrusted user input is the book example of this vulnerability, with user input flowing directly into a MongoDB query such as users.findOne() and takes advantage of the fact that MongoDB queries are JSON-like objects and can be easily manipulated by an attacker to inject malicious JavaScript code.

  • Command injection from uncontrolled user input: A prime example of this is the insecure use of the child_process API to execute commands. This is a common issue with Node.js applications that use the child_process API to execute commands. You can learn a lot about this from reading the code security audit of the blamer npm package that was found vulnerable to argument injection.

Sensitive Data Exposure

  • Configuration mistakes exposing sensitive data: most prominently in Node.js applications is the issue of environment variables configuration being an anti-pattern.

  • Verbose error messages leaking system details: one clear example of this is the use of the NODE_ENV environment variable to control the verbosity of error messages. For example, if you don’t set NODE_ENV to production then you’ll get verbose error messages generated in the Express framework when exceptions happen, that can leak sensitive information about your system.

Broken Access Control

  • Insecure direct object references from misconfigured access control lists: watch for the Clerk SaaS service to release their security disclosure finding of their @clerk/nexjs package that was found vulnerable to insecure direct object references (IDOR).

  • Overly permissive Cross-Origin Resource Sharing (CORS) misconfiguration

Security Misconfigurations

  • Outdated npm packages with known vulnerabilities: often a frustration JavaScript developers have to deal with, but also easily mitigated with the use of tools like Snyk and others that automate the process of identifying vulnerable dependencies and fixing them with automated pull requests to the GitHub repository.

  • Improper system hardening: an easy reference is the insecure practices used to build Node.js Docker containers. For example, the use of the latest tag, or the use of root user in the container, or the use of npm install to install dependencies in the container.

Broken Authentication

  • Weak credentials and improper session management

  • Overly permissive CORS misconfiguration

Cross-site Scripting (XSS)

  • Dynamically generated pages allowing unchecked user input

  • Improper output encoding

Insecure Deserialization

  • Remote code execution via serialized user-supplied data

Using Components with Known Vulnerabilities

  • Runtime dependencies with publicly disclosed vulnerabilities

Insufficient Logging & Monitoring

  • Lack of visibility into system activity enabling threats to persist

Mitigating OWASP Top 10 Risks in Node.js

Node.js developers can apply various methods to mitigate OWASP Top 10 risks:

Input Validation

  • Validate and sanitize all user input on the server-side to prevent injection attacks. Use validation libraries like Joi or Validator.

Authentication Best Practices

  • Implement multi-factor authentication. Manage user sessions securely. Harden password policies.

Encryption

  • Encrypt sensitive data. Ensure secure TLS settings.

Access Control

  • Implement permission checks. Rate limit API requests.

Patching

  • Keep npm packages updated. Monitor advisories closely.

Monitoring

  • Log activity carefully. Set alerts for suspicious or blocked activity.

Node.js Frameworks

  • Use frameworks like Fastify securely. Follow framework security best practices.

Security Testing

  • Conduct extensive testing, risk assessments and code audits to identify vulnerabilities.

By understanding the OWASP Top 10 risks within the context of Node.js, developers can better orient security efforts around mitigating these critical threats. The outlined techniques offer a starting point to address risks in practice. Additional Node.js security best practices should be layered on to strengthen protection.

Hands-On with OWASP NodeGoat: A Practical OWASP NodeGoat Tutorial

OWASP NodeGoat is an intentionally vulnerable Node.js web application created by OWASP to teach developers about common security vulnerabilities and how to avoid them. This hands-on tutorial will walk through setting up NodeGoat, working through some of its vulnerabilities, and securing the application by applying OWASP-recommended best practices.

Setting Up OWASP NodeGoat

To get started with NodeGoat:

  • Install Node.js and npm on your machine

  • Clone the NodeGoat repository from GitHub

  • Navigate to the NodeGoat directory and install dependencies with npm install

  • Start the application with npm start

  • Access the running app at http://localhost:4000 in your browser

The NodeGoat application is structured as an Express web app with various vulnerable routes and functionality that you will exploit and then secure throughout this tutorial.

Working Through Common Vulnerabilities

Some of the OWASP Top 10 vulnerabilities you can explore hands-on using NodeGoat include:

  • Command Injection: Execute system commands via vulnerable route

  • Insecure Deserialization: Change app behavior by tampering with serialized user object

  • XSS: Store persistent XSS payload in database and trigger on admin page

  • Broken Authentication: Access admin section by brute forcing weak passwords

For each vulnerability, you can walk through how an attacker could exploit it, see the impact, and then apply fixes like input validation, using safe serialization methods, and enabling rate limiting to prevent brute force attacks.

Securing NodeGoat: Applying Best Practices

As you work through securing NodeGoat, you will learn how to implement various security best practices recommended by OWASP:

  • Validate & sanitize all input coming from routes, forms, APIs and databases

  • Use parameterized queries to prevent SQL injection

  • Hash and salt stored passwords to encrypt sensitive data

  • Apply the principle of least privilege for role-based authorization

  • Enable rate limiting to prevent DDoS and brute force attacks

Fixing the vulnerabilities in NodeGoat provides excellent hands-on experience in improving node.js security and learning OWASP JavaScript security best practices.

Next Steps for Strengthening Node.js Security

To take your Node.js security knowledge further:

  • Read the full OWASP Node.js Security Checklist

  • Use tools like node.js vulnerability scanner to test your apps

  • Practice exploits and fixes with OWASP NodeGoat tutorial

  • Stay updated as guidelines evolve by joining the OWASP community

Continuous education is key for writing secure code as new threats emerge. OWASP provides excellent resources to level up your skills.