DevOps.com

  • Latest
    • Articles
    • Features
    • Most Read
    • News
    • News Releases
  • Topics
    • AI
    • Continuous Delivery
    • Continuous Testing
    • Cloud
    • Culture
    • DataOps
    • DevSecOps
    • Enterprise DevOps
    • Leadership Suite
    • DevOps Practice
    • ROELBOB
    • DevOps Toolbox
    • IT as Code
  • Videos/Podcasts
    • Techstrong.tv Podcast
    • Techstrong.tv Video Podcast
    • Techstrong.tv - Twitch
    • DevOps Unbound
  • Webinars
    • Upcoming
    • On-Demand Webinars
  • Library
  • Events
    • Upcoming Events
    • On-Demand Events
  • Sponsored Communities
    • CloudBees
    • IT as Code
    • Rocket on DevOps.com
    • Traceable on DevOps.com
    • Quali on DevOps.com
  • Related Sites
    • Techstrong Group
    • Container Journal
    • Security Boulevard
    • Techstrong Research
    • DevOps Chat
    • DevOps Dozen
    • DevOps TV
    • Techstrong TV
    • Techstrong.tv Podcast
    • Techstrong.tv Video Podcast
    • Techstrong.tv - Twitch
  • Media Kit
  • About
  • Sponsor
  • AI
  • Cloud
  • Continuous Delivery
  • Continuous Testing
  • DataOps
  • DevSecOps
  • DevOps Onramp
  • Platform Engineering
  • Low-Code/No-Code
  • IT as Code
  • More
    • Application Performance Management/Monitoring
    • Culture
    • Enterprise DevOps
    • ROELBOB

Home » Blogs » A DevSecOps Process for Node.js Projects

Node.js ZAP SBOM

A DevSecOps Process for Node.js Projects

By: Gilad David Maayan on January 23, 2023 Leave a Comment

Node.js is an open source development platform for running JavaScript code on the server side. Node is useful for developing applications that require a persistent browser-server connection and is often used for real-time applications such as chat, social applications, or news feeds. 

DevSecOps is a way for development, security, and operations teams to work together throughout the project life cycle. It is often implemented by adding security to existing DevOps teams and agile processes.

TechStrong Con 2023Sponsorships Available

The idea behind this collaboration is to incorporate security early in the development process. This is called shifting security left. As security moves to the “left” of the development process, application vulnerabilities and bugs are discovered and addressed faster. This leads to more secure product releases with a reduced need to apply patches and troubleshoot in production. Applying DevSecOps to Node.js development can improve both the efficiency and security of Node.js projects.

Common Node.js Attack Vectors

Here are a few cyber threats that affect many Node.js applications.

Cross-Site Scripting (XSS)

XSS attacks inject manipulated client-side JavaScript code. Web apps are vulnerable to XSS if they don’t validate inputted hostnames from the DNS. Attackers can use forms to enter malicious scripts and hack into the system. In some cases, entering code into the filename can trigger cross-site scripting in the target browser. 

A hacker can use your file upload form to hack into your system. There are many ways for this vulnerability to lead to a system compromise. Though usually mitigated by modern front-end frameworks, in some cases it may happen that putting javascript into the filename will trigger XSS in a user’s browser, allowing the theft of session IDs. In other cases, an attacker might get sensitive server information by uploading a file to the Node.js server and running it by exploiting a path traversal vulnerability.

Code Injection

Developers are responsible for writing secure application code, but it’s impossible to fully guarantee the code’s security when using open source components. A code injection attack involves injecting malicious code into the target system and forcing the application to execute it, allowing attackers to access the codebase. Inadequate data validation often results in code injection vulnerabilities.

Default Cookie Names

Cookies help web apps identify specific users by storing user actions in the underlying infrastructure (e.g., an eCommerce shopping cart that remembers the items a customer selects). 

Developers should use custom names for cookies (rather than defaults) to prevent attackers from identifying and targeting cookies and extracting data. 

Brute Force Attacks

Brute force attacks often affect Node.js applications. An attacker randomly generates millions of passwords to find one that works, logging in to the application. You can prevent brute force attacks by strengthening the authentication mechanisms, limiting the number of permitted login attempts from a single IP, and encrypting stored passwords (using bcrypt.js).

Cross-Site Resource Forgery (CSRF)

This type of session hijacking forces an authenticated user to execute malicious actions on the target web application. The attacker hijacks a legitimate user’s session to bypass security controls and change the application’s state. CSRF can force users to transfer funds or change their account details, potentially compromising the web application’s security. 

Node.js Security Best Practices

Implement Strong Authentication

A compromised, weak, or incomplete authentication mechanism is a common vulnerability. Many developers think of authentication as something that “just works”. However, authentication can easily be bypassed by attackers if not implemented correctly.

In Node.js, you have the option of using the default Node.js authentication solution. There are a few things to keep in mind: 

  • Do not use Node.js’s built-in password library when generating passwords, use Bcrypt. 
  • Limit failed login attempts and do not notify users if their username or password is incorrect. Instead, return a generic “Invalid Credentials” error. 
  • Have a robust session management strategy. 
  • Implement two factor authentication (2FA). This can be done using modules like node-2fa.

Alternatively, use one of many third-party solutions available for application authentication, which may be superior to built-in Node.js functionality. 

Avoid Errors That Reveal Too Much

There are many things to consider when building error handling functionality: 

  • Don’t give the user any details they don’t really need. Never return the entire error object to the client. It may contain information that you do not want to disclose, such as paths, other libraries you are using, or secrets. 
  • Wrap the path in a catch clause so that Node.js doesn’t crash if the request triggers an error. This prevents the app from crashing continuously, allowing attackers to send malicious requests that cause the app to crash.
  • Don’t expose your Node.js app directly to the internet. Use a component like a load balancer, cloud firewall or gateway. This means DoS attacks will be one step removed from your Node.js application.

Using Security Linters to Capture Vulnerabilities in Code

Code linters help identify various issues in code before compiling. They let you detect the most common issues and ensure you follow best practices.

Code linters have their own rules, which you can customize according to your requirements. Before using a linter, make sure you enable rules related to security vulnerability detection in the linter configuration.

Server-side Logging and Monitoring

A good logging library provides developers with powerful troubleshooting and monitoring capabilities. On the other hand, unnecessary or excessive logging affects application performance and consumes more resources. Therefore, you should only use reasonable log levels when deploying applications to production environments.

When constructing log messages, they must be in a format that can be read and understood by humans and machines. Logging ambiguous messages can lead to misunderstandings among developers. An error message must not only describe the problem but also advise what the user can or should do to fix it.

It is also important to ensure that your logging mechanism captures all relevant information such as IP addresses, usernames, and actions taken. 

Never retrieve or store sensitive information in application logs. This violates compliance standards such as PCI and GDPR. However, there are times when you need to record this information for troubleshooting – in this case, it is recommended to mask or anonymize sensitive information before writing it to the log.

Implementing Node.js Security in a DevSecOps Organization

Node.js security best practices are well known and security awareness is growing among Node.js developers. However, many organizations don’t have a clear organizational structure for implementing development best practices. Here are a few steps your organization can take to transition a Node.js development team to a full DevOps model:

  1. Adding mandatory guard rails for developers

For example, adding static application security testing (SAST) within the IDE and on every commit. Linters should be deployed centrally with a consistent configuration for all developers in the team.

  1. Automated security testing in staging environments

During the CI/CD process, when a Node.js application is deployed to a staging environment, there should be automated tests that can identify security misconfigurations and integration-related vulnerabilities. Dynamic application security testing (DAST) tools are ideal for this purpose.

  1. Runtime protection

No DevSecOps process is complete without a safety net to identify security issues in production. This requires the use of tools like web application firewalls (WAFs), which can detect and block malicious traffic, and runtime application self-protection (RASP), which can proactively respond to attacks. Both these tools provide feedback to DevOps teams to help them identify and remediate vulnerabilities in the underlying application.

By implementing these three stages, you can be sure that Node.js security is not just an ad-hoc practice but a formalized, coordinated process involving developers, operations teams and security experts. 

Conclusion

In this article, I explained the basics of DevSecOps and how to secure Node.js:

  • Implement strong authentication—In Node.js, you have the option of using the default Node.js authentication solution.
  • Avoid errors that reveal too much—Don’t give users more details than they actually need when displaying errors.
  • Using security linters to capture vulnerabilities in code—Code linters help identify various issues in code before compiling.
  • Server-side logging and monitoring—A good logging library provides developers with powerful troubleshooting and monitoring capabilities.

I hope this will be useful as you implement Node.js security in your DevSecOps organization.

Recent Posts By Gilad David Maayan
  • Why MLOps Hurts: 3 Pain Points for DevOps Teams
  • AppSec in the Cloud: What DevOps Teams Should Know
  • Will DevSecOps Replace the Security Operations Center?
More from Gilad David Maayan
Related Posts
  • A DevSecOps Process for Node.js Projects
  • Veracode Makes DevSecOps Transition Easier for Developers
  • Scribe Security Unveils Pair of Tools to Secure Software Supply Chains
    Related Categories
  • Blogs
  • DevOps and Open Technologies
  • DevOps Onramp
  • DevOps Practice
  • DevOps Toolbox
  • DevSecOps
  • DevSecOps
    Related Topics
  • application development
  • devsecops
  • JavaScript
  • node.js
  • web development
Show more
Show less

Filed Under: Blogs, DevOps and Open Technologies, DevOps Onramp, DevOps Practice, DevOps Toolbox, DevSecOps, DevSecOps Tagged With: application development, devsecops, JavaScript, node.js, web development

Sponsored Content
Featured eBook
The State of Open Source Vulnerabilities 2020

The State of Open Source Vulnerabilities 2020

Open source components have become an integral part of today’s software applications — it’s impossible to keep up with the hectic pace of release cycles without them. As open source usage continues to grow, so does the number of eyes focused on open source security research, resulting in a record-breaking ... Read More
« Five Great DevOps Job Opportunities

Techstrong TV – Live

Click full-screen to enable volume control
Watch latest episodes and shows

Upcoming Webinars

Ask the Experts: Simplify Data Services Orchestration in Your Apps
Monday, January 23, 2023 - 3:00 pm EST
Finding the Right Cloud for the Right Workload
Tuesday, January 24, 2023 - 11:00 am EST
Goin' Mobile in DevOps Workflows
Tuesday, January 24, 2023 - 11:00 am EST

Latest from DevOps.com

A DevSecOps Process for Node.js Projects
January 23, 2023 | Gilad David Maayan
Five Great DevOps Job Opportunities
January 23, 2023 | Mike Vizard
Digital Experience and the Future of Observability
January 20, 2023 | Nik Koutsoukos
6 Ways To Empower Developers and Increase Productivity
January 20, 2023 | Bill Doerrfeld
8-Bit Floating Point for AI/ML? | Amazon and Microsoft Shed Tech Jobs
January 19, 2023 | Richi Jennings

GET THE TOP STORIES OF THE WEEK

Download Free eBook

DevOps: Mastering the Human Element
DevOps: Mastering the Human Element

Most Read on DevOps.com

Top Three DevOps Tools for Cloud Infrastructure
January 18, 2023 | Mike Vizard
Okta Makes No-Code Integration Tool for Identity Platform Av...
January 17, 2023 | Mike Vizard
The API Economy: Increasing Potential to Monetize Public API...
January 17, 2023 | Bill Doerrfeld
8-Bit Floating Point for AI/ML? | Amazon and Microsoft Shed ...
January 19, 2023 | Richi Jennings
Why Can’t We Be Friends? Improving Developer-Tester Alignmen...
January 18, 2023 | Suhail Ansari

On-Demand Webinars

DevOps.com Webinar ReplaysDevOps.com Webinar Replays

Sponsored Content

The Google Cloud DevOps Awards: Apply Now!

January 10, 2023 | Brenna Washington

Why a Low-Code Platform Should Have Pro-Code Capabilities

March 24, 2021 | Andrew Manby

AWS Well-Architected Framework Elevates Agility

December 17, 2020 | JT Giri

Practical Approaches to Long-Term Cloud-Native Security

December 5, 2019 | Chris Tozzi

Why AWS Cost Optimization Is About More than Saving Money

December 3, 2019 | Chris Tozzi

  • Home
  • About DevOps.com
  • Meet our Authors
  • Write for DevOps.com
  • Media Kit
  • Sponsor Info
  • Copyright
  • TOS
  • Privacy Policy

Powered by Techstrong Group, Inc.

© 2023 ·Techstrong Group, Inc.All rights reserved.