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
- An advisory is a public notice about one package: which versions have a hole, how dangerous it is (severity: low, moderate, high, critical), and which version fixes it. Each advisory has an ID, such as
GHSA-xvch-5gv4-984hin the GitHub Advisory Database (GitHub's collection of advisories). - The lockfile (
package-lock.json,composer.lock) records the exact version of every installed package, dependencies of dependencies included.package-lock.jsonalso records each package'sintegrityhash, as in SRI. npm auditchecks the lockfile against the advisory database.
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)
minimist 1.0.0 - 1.2.5: the package name and the range of vulnerable versions. The minus sign here means "up to", and both ends are vulnerable too.Severity: critical: the highest danger level among that package's advisories.- One line per advisory: its title and a link to the details.
fix available via ...: the command that installs the fix. The difference betweennpm audit fixand--forceis in the section "The range in package.json decides the fix".dayjsdoesn't appear: its version is in no advisory.- When there are findings,
npm auditexits with code 1 (a code that marks a failed command). So when CI (the server that tests and builds your code automatically) runs it, that step fails too. When it's clean,npm auditprintsfound 0 vulnerabilitiesand exits with code 0.
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
v.split(".")breaks"1.2.5"into["1", "2", "5"], and.map(Number)turns those into the numbers[1, 2, 5].const [major, minor, patch]gives those three numbers names.toNumjoins the three numbers into one number you can compare: 1.2.5 becomes 1,002,005. This works as long as each part stays under 1,000.>=and<=makeminandmaxthemselves count as vulnerable.
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:
^1.2.5(caret): from 1.2.5 up to, not including, 2.0.0. The major stays, minor and patch may go up.~1.2.5(tilde): from 1.2.5 up to, not including, 1.3.0. Only the patch may go up.1.2.5: exactly that version.
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
composer auditdoes the same forcomposer.lockin a PHP project.- Dependabot is a GitHub feature that watches your dependencies, warns you when a new advisory appears, and opens a pull request (a proposed code change) with the fixed version.
- In CI, install the dependencies with
npm ci, which installs exactly what the lockfile says and fails when the lockfile andpackage.jsondon't match. Then runnpm audit --audit-level=high, so that CI step fails on any high or critical finding.
⚠️ Versions compared wrong, fixes installed wrong
- Comparing versions as text.
"1.10.0" > "1.9.0"isfalse, because text is compared character by character, and at the third character, "1" is smaller than "9". Split withsplit("."), then compare the numbers. - Forgetting the ends of the range.
minandmaxthemselves are vulnerable: use>=and<=, not>and<. - Running
npm audit fix --forcewithout tests. That command can move to a new major, and your old code can break. - Pinning an exact version without
^. Pinning means locking one version. With"minimist": "1.2.5", the range is only that one version, sonpm audit fixcan do nothing. npm says "Will install [email protected], which is outside the stated dependency range".
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.