Insecure Use of Language/Framework API

This category covers the following Node.js APIs:

Insecure Use of Buffer()

Why is this important?

Node.js, like any other programming language, has dangerous functions. If these functions are not used properly, it can have a catastrophic impact on your app. The new Buffer() function can allow attackers to get full access to your production environment. The new Buffer() function has been deprecated since Node.js v6.0.0.

Read below to find out how to fix this issue in your code.

Fixing Insecure Use of new Buffer()

Option A: Replace new Buffer() with secure Buffer APIs

  1. Go through the issues that GuardRails identified in the PR.
  2. Replace new Buffer() with secure alternatives to achieve the same outcome. For example, use Buffer.from() or Buffer.alloc(). Two examples are shown below:

    //Instead of this:
    new Buffer(, "utf8");
    new Buffer(req.body.length);
    // Use this:
    Buffer.from(, "utf8");
  3. Test it and ensure the functionality works as expected

  4. Ship it 🚢 and relax 🌴

Option B: Use Safe-Buffer

  1. Install Safe-Buffer.

    npm install --save safe-buffer
  2. Add the following line to the top of your Node.js modules.

    var Buffer = require("safe-buffer").Buffer;
  3. Test it and ensure your app is still working as expected.

  4. Ship it 🚢 and relax 🌴

More information: