Dependency Vulnerabilities — Security

Dependencies are packages someone else wrote that your project uses. An advisory (a public notice about a hole) names a range of vulnerable versions, the…

The code you wrote yourself may be only a few hundred lines. What sits in node_modules (the folder where npm installs packages) is far bigger, and someone else wrote all of it. If one of those packages has a security hole, your app has it too, even when your own code did nothing wrong.

Those packages someone else wrote are called dependencies. In JavaScript you install them through npm, and in PHP through Composer. Picture a restaurant that uses a sauce from a supplier. One day the supplier announces a recall: "batches 1.0.0 to 1.2.5 are contaminated, replace them with batch 1.2.6". The restaurant checks the notice against the stock list in its storeroom, then replaces the affected sauce. The sauce is a package, the notice is called an advisory, the stock list is the lockfile, and the clerk who checks one against the other is npm audit.

Advisory, lockfile and npm audit

This is the same coffee shop project as in the Lab. Its package.json (the file that lists the project's dependencies) has "dayjs": "^1.11.13", "jsonwebtoken": "^8.5.1" and "minimist": "^1.2.5" (the meaning of ^ comes below). Its lockfile records 1.11.13, 8.5.1 and 1.2.5. This is the real npm audit output for that project:

$ npm audit
# npm audit report

jsonwebtoken  <=8.5.1
Severity: high
jsonwebtoken unrestricted key type could lead to legacy keys usage  - https://github.com/advisories/GHSA-8cf7-32gw-wr33
jsonwebtoken's insecure implementation of key retrieval function could lead to Forgeable Public/Private Tokens from RSA to HMAC - https://github.com/advisories/GHSA-hjrf-2m68-5959
jsonwebtoken vulnerable to signature validation bypass due to insecure default algorithm in jwt.verify() - https://github.com/advisories/GHSA-qwph-4952-7xr6
fix available via `npm audit fix --force`
Will install [email protected], which is a breaking change
node_modules/jsonwebtoken

minimist  1.0.0 - 1.2.5
Severity: critical
Prototype Pollution in minimist - https://github.com/advisories/GHSA-xvch-5gv4-984h
fix available via `npm audit fix`
node_modules/minimist

2 vulnerabilities (1 high, 1 critical)

Vulnerable or not: compare versions as numbers

Versions are written in semver (semantic versioning): MAJOR.MINOR.PATCH. Patch goes up for a bug fix, minor for a new feature that breaks no old code, and major for a change that can break old code (a breaking change). To tell whether a version is in a vulnerable range, compare each part as a number. This function does that:

function checkVulnerableVersion(version, vulnerableRange) {
  const toNum = (v) => {
    const [major, minor, patch] = v.split(".").map(Number);
    return major * 1000000 + minor * 1000 + patch;
  };
  const ver = toNum(version);
  return ver >= toNum(vulnerableRange.min) && ver <= toNum(vulnerableRange.max);
}

console.log(checkVulnerableVersion("1.2.5", { min: "1.0.0", max: "1.2.5" })); // true
console.log(checkVulnerableVersion("1.2.6", { min: "1.0.0", max: "1.2.5" })); // false

This table shows the results for the range 1.0.0 to 1.2.5:

version toNum Within 1.0.0 to 1.2.5?
0.9.9 9,009 no, below min
1.0.0 1,000,000 yes, exactly at min
1.2.5 1,002,005 yes, exactly at max
1.2.6 1,002,006 no, above max

The range in package.json decides the fix

package.json doesn't hold one version, but a range that may be installed:

npm audit fix only installs a fix that is still inside the range. minimist's fix is in 1.2.6, and 1.2.6 is inside ^1.2.5. So npm audit fix can install it without changing package.json.

jsonwebtoken is different. Its range is ^8.5.1, yet no 8.x version is safe: the fix starts at 9.0.0, a new major. That's why npm says npm audit fix --force and "which is a breaking change". --force changes package.json to the new major. Before you merge (combine that change into your code), read its changelog, the list of changes in each version (jsonwebtoken 9 changed some default values). Then run the tests.

Composer, Dependabot and CI

⚠️ Versions compared wrong, fixes installed wrong

TL;DR: Dependencies are packages someone else wrote that your project uses. An advisory (a public notice about a hole) names a range of vulnerable versions, the lockfile records the exact installed version, and npm audit (or composer audit) checks one against the other. A version is vulnerable when min <= version <= max (between the lower and upper ends of the vulnerable range), and both ends are vulnerable too. Versions are compared part by part as numbers, with split(".") and Number. A fix that's still inside the ^ range in package.json is installed by npm audit fix: ^ lets the minor and the patch (the second and third numbers of the version) go up. A fix that needs a new major (the first number of the version goes up) needs --force, a read of the changelog (the list of changes in each version), and your tests. Commit the lockfile, use npm ci and npm audit --audit-level=high in CI, and turn on Dependabot (a GitHub feature that watches your dependencies).

⚡ Someone else's code, your hole too

Meaning: The packages in node_modules (the folder where npm installs packages) are called dependencies. They are written by someone else, and their holes come into your app too. An advisory announces which versions have a hole. The lockfile records the exact installed version. npm audit (or composer audit) checks one against the other.

⚡ Compare as numbers

Write:

const [major, minor, patch] = v.split(".").map(Number);
return major * 1000000 + minor * 1000 + patch;
Guess first: what is "1.10.0" > "1.9.0" as text?

false. Text is compared character by character, and at the third character, "1" is smaller than "9". Split with split(".") and compare the numbers.

Meaning: This code turns the version v, such as "1.2.5", into one number you can compare. Then check the vulnerable range with >= and <=, because the lower end (min) and the upper end (max) are vulnerable too.

⚡ The range in package.json decides the fix

Meaning: ^1.2.5 in package.json (the file that lists the project's dependencies) means the version may go up to, not including, 2.0.0. npm audit fix only installs a fix that's still inside that range. If the fix is in a new major (the first number of the version goes up), npm asks for --force. Moving to a new major is a breaking change (a change that can break old code), so run your tests first.

⚡ Guard it in CI

Meaning: In CI (the server that tests and builds your code automatically), install the dependencies with npm ci. Then run npm audit --audit-level=high so CI fails on any finding with a high or critical danger level. Dependabot (a GitHub feature) opens a pull request (a proposed code change) with the fixed version.

What you will learn