Integrity checks according to policies can be circumvented in Node.js 20 and Node.js 18
Node.js's experimental policy feature, which enforces integrity checks on loaded modules, can be bypassed because the application can replace internal C++ bindings used by the Hash class to return a forged checksum. An attacker who can run arbitrary code in a policy-restricted context can thus load modified modules that inherit the trusted module's permissions, potentially escalating privileges.
- Triaged in 4 days
- Fixed in 18 days
- Paid in 18 days
- Disclosed after 48 days
Summary by the program
Integrity checks according to policies can be circumvented (Medium) - (CVE-2023-38552) When the Node.js policy feature checks the integrity of a resource against a trusted manifest, the application can intercept the operation and return a forged checksum to node's policy implementation, thus effectively disabling the integrity check.
Impacts: This vulnerability affects all users using the experimental policy mechanism in all active release lines: 18.x and, 20.x. Please note that at the time this CVE is issued, the policy mechanism is an experimental feature of Node.js.
Thanks to Tobias Nießen who reported and created the security patch.
Full Security Advisory: https://nodejs.org/en/blog/vulnerability/october-2023-security-releases
The report
Summary: When Node.js is checking the integrity of a resource against a trusted manifest, the application can intercept the operation and return a forged checksum to node's policy implementation, thus effectively disabling the integrity check.
Description: Node.js uses the Hash class of the built-in crypto module to compute a cryptographic hash of each resource. The implementation protects itself against modifications of the Hash class prototype by the application, however, the Hash class internally relies on several C++ bindings that the application can replace at runtime.
Consider the following policy.json file:
{
"resources": {
"./main.js": {
"integrity": true,
"dependencies": {
"./protected.js": true,
"crypto": true
}
},
"./protected.js": {
"integrity": "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
"dependencies": true
}
}
}
The file main.js may contain arbitrary code, but it cannot access, for example, the built-in fs module. The file protected.js, on the other hand, has a strict integrity requirement but can access arbitrary modules. The main.js file may require protected.js, provided that the integrity of protected.js is verified by Node.js.
The file main.js can thus contain arbitrary code. Let the contents be:
const h = require('crypto').createHash('sha384');
const fakeDigest = h.digest();
const kHandle = Object.getOwnPropertySymbols(h)
.find((s) => s.description === 'kHandle');
h[kHandle].constructor.prototype.digest = () => fakeDigest;
require('./protected.js');
The file protected.js does not match the integrity value specified in policy.json:
console.log(require('fs').readFileSync('/etc/passwd').length);
Running main.js with the policy enabled succeeds despite the integrity mismatch, and the application reads /etc/passwd:
$ node --experimental-policy=policy.json main.js
3224
This vulnerability is exploitable in the default build configuration of Node.js, and only requires the user to enable the policy feature when starting Node.js.
I provided a patch, which has been merged into the main branch as commit e673c0362979f9cb2c74fc6876c45ae9be1fe853, into the v20.x release line as commit a4cb7fc7c04869f051e270ed192a679d2d108328, and into the v18.x release line as commit 1c538938ccadfd35fbc699d8e85102736cd5945c, all of which have been released on October 13th, 2023.
Impact
As per the Node.js documentation at the time the issue was reported, "Policies are a security feature intended to allow guarantees about what code Node.js is able to load" and "The policy manifest will be used to enforce constraints on code loaded by Node.js." The current revision of the documentation adds that "policies guarantee the file integrity when a module is loaded using require(), import() or new Module()."
The presented vulnerability invalidates these statements. Code may be executed through require() even if the code has been modified. The modified code inherits all permissions of the supposedly trusted code, which potentially allows the attacker to escalate their permissions as demonstrated above.
Context
Node.js ships an experimental policy mechanism, enabled at startup with --experimental-policy. A policy is described in a manifest file, typically policy.json, which lists resources and, for each, whether its integrity must be verified and which dependencies it may load. The Node.js documentation at the time of the report described policies as "a security feature intended to allow guarantees about what code Node.js is able to load" and stated that "The policy manifest will be used to enforce constraints on code loaded by Node.js." A later revision adds that "policies guarantee the file integrity when a module is loaded using require(), import() or new Module()."
The vulnerability, tracked as CVE-2023-38552 and rated Medium, breaks exactly that guarantee: when the policy feature checks the integrity of a resource against the trusted manifest, the application can intercept the operation and return a forged checksum to Node's policy implementation, effectively disabling the integrity check. It affects all users of the experimental policy mechanism on the active release lines 18.x and 20.x, and is exploitable in the default build configuration of Node.js. The only precondition is that the user enables the policy feature when starting Node.js.
Setup
The scenario needs a policy manifest and two scripts. The manifest declares ./main.js with integrity: true and dependencies on ./protected.js and crypto, and declares ./protected.js with a strict integrity value and unrestricted dependencies:
{
"resources": {
"./main.js": {
"integrity": true,
"dependencies": {
"./protected.js": true,
"crypto": true
}
},
"./protected.js": {
"integrity": "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
"dependencies": true
}
}
}
The roles are deliberately asymmetric. main.js may contain arbitrary code but cannot access, for example, the built-in fs module. protected.js has a strict integrity requirement but can access arbitrary modules. main.js is allowed to require protected.js, provided that Node.js verifies the integrity of protected.js first. The whole attack lives in that gap: untrusted code that is allowed to run, and a trusted module whose integrity check it can subvert.
Finding it
Node.js computes a cryptographic hash of each resource using the Hash class of the built-in crypto module. The implementation protects itself against modifications of the Hash class prototype by the application. However, the Hash class internally relies on several C++ bindings that the application can replace at runtime. The Hash class internally relies on C++ bindings that the application can replace at runtime, and the code in main.js reaches the internal handle through a symbol on the hash object. If the policy implementation calls digest() on such an object, the application can make it return an attacker-chosen value instead of the real hash.
Exploitation
Step 1: Prepare the untrusted entry point
The contents of main.js are arbitrary code. The researcher uses them to obtain a Hash instance, compute a real digest once, locate the internal handle symbol, and then overwrite digest on the prototype of the handle's constructor so that every later call returns the captured digest:
const h = require('crypto').createHash('sha384');
const fakeDigest = h.digest();
const kHandle = Object.getOwnPropertySymbols(h)
.find((s) => s.description === 'kHandle');
h[kHandle].constructor.prototype.digest = () => fakeDigest;
require('./protected.js');
The first line creates a SHA-384 hash object. The second computes its digest, which becomes the forged checksum returned later. The third and fourth lines find the symbol whose description is kHandle among the hash object's own property symbols, and replace digest on the prototype of that handle's constructor with a function that always returns fakeDigest. The final line requires the protected module, which triggers the policy integrity check.
Step 2: Provide a protected module that does not match the manifest
The file protected.js does not match the integrity value specified in policy.json:
console.log(require('fs').readFileSync('/etc/passwd').length);
This is code that main.js itself is not permitted to run, because main.js has no access to fs. It is exactly the kind of code the integrity check exists to keep out, since the file on disk differs from the trusted manifest entry.
Step 3: Run with the policy enabled
Running main.js with the policy enabled succeeds despite the integrity mismatch, and the application reads /etc/passwd:
$ node --experimental-policy=policy.json main.js
3224
The command starts Node.js with the manifest, loads main.js, which tampers with the hash prototype and then requires protected.js. The policy check on protected.js receives the forged digest, accepts the module, and the module prints the length of /etc/passwd. The output 3224 is the result of that read, demonstrating that the modified code executed with the permissions of the supposedly trusted code.
Root cause
The policy implementation verifies integrity by computing a hash through the crypto module's Hash class. The class guards its own prototype against modification by the application, but it delegates the actual work to C++ bindings that the application can replace at runtime. The internal handle reachable from a Hash instance exposes a constructor whose prototype carries digest, and that prototype is writable from application code. Overwriting digest therefore changes what the policy check sees, without touching the guarded Hash prototype itself. The check compares the manifest value against a value the attacker controls, so it can never fail. The existing defence, protecting the Hash class prototype, did not cover the underlying binding surface.
Impact
The vulnerability invalidates the documented guarantees of the policy feature. Code may be executed through require() even if the code has been modified, and the modified code inherits all permissions of the supposedly trusted code. As the researcher demonstrates, this potentially allows an attacker to escalate their permissions: code confined to main.js gains the module access of protected.js, including fs. The impact is limited to deployments that use the experimental policy mechanism on Node.js 18.x or 20.x, and the policy feature must be enabled at startup. The policy feature was experimental at the time the CVE was issued.
Fix
The researcher provided a patch. It was merged into the main branch as commit [redacted], into the v20.x release line as commit [redacted], and into the v18.x release line as commit [redacted]. All of these were released on 13 October 2023. The report does not describe the contents of the patch beyond this.
- Other internal handles exposed on built-in objects: look for own property symbols with descriptive names on objects returned by built-in APIs, and check whether the prototype of the handle's constructor carries methods used by security checks. A positive result is a security-relevant method that can be replaced from application code and whose replacement is accepted by the check.
- Other consumers of
cryptohashing inside Node.js security features: any check that callsdigest()on aHashinstance created in the same process as application code is a candidate. A positive result is a check that passes for a resource whose on-disk contents differ from the trusted value. - Prototype tampering on classes that wrap native bindings more generally: the pattern of guarding the JavaScript class while leaving the binding reachable is the signal. A positive result is a guarded class whose behaviour can still be changed through an object it returns.
- Policy manifests with mixed trust: place the tampering code in a resource with
integrity: truebut restricted dependencies, and the target in a resource with a strict integrity value and unrestricted dependencies, as in the report. A positive result is execution of the protected module despite a mismatch.
This kind of bug hides in security features implemented in JavaScript on top of native bindings, especially experimental ones. During recon, look for features that are opt-in at startup, documented as providing guarantees, and implemented by calling into crypto or other native modules from the same process that runs untrusted code. The signal is a check whose result depends on an object the application can reach and modify, such as a hash object returned by a public API.
The researcher showed enough impact by building a minimal, self-contained demonstration: a manifest with a clear trust boundary, an untrusted entry point that only tampers with the hash object, and a protected module that reads a file the untrusted code could not otherwise access. The output of the command proves both the bypass and the resulting permission escalation, which is what makes the case for a security fix rather than a hardening suggestion.
What happened, in plain words
Node.js is a program that runs JavaScript code outside a web browser. It is used to build servers, command-line tools and many other things. It comes with a feature called policies, which is meant to act like a strict doorman: you write a list of rules saying which files the program is allowed to load, and for some files you also record a fingerprint of their contents. When the program tries to load such a file, Node.js checks the file against the recorded fingerprint. If the file has been changed, the fingerprint will not match and the file should be refused.
This report shows that the doorman can be tricked. A file that is allowed to run but is not trusted can reach into the machinery Node.js uses to compute fingerprints and replace it with a fake one. From then on, every fingerprint check returns whatever the attacker wants, so a modified file is accepted as if it were the original. The attacker's code then runs with the permissions of the trusted file, which in the demonstration means reading a file on the computer that the untrusted code was never supposed to touch.
The bug is tracked as CVE-2023-38552 and was rated Medium. It affects Node.js versions 18.x and 20.x, but only when the policy feature is switched on at startup. At the time the CVE was issued, the policy feature was still experimental, meaning it was not yet considered finished or stable. The researcher, Tobias Nießen, also wrote the fix.
Background: the pieces involved
Before walking through the attack, here are the ideas it relies on.
Node.js and modules. A Node.js program is usually split into files called modules. One file loads another with a function called require(). For example, require('./protected.js') means "load the file protected.js and give me what it exports". Node.js also has built-in modules, such as crypto (cryptographic functions) and fs (file system access), which a program can require by name.
Policies and manifests. A policy is a set of rules stored in a file, usually called policy.json. This file is the manifest. For each resource (a file the program might load) the manifest can say two things:
- whether the file's integrity must be checked, and
- which dependencies the file is allowed to load.
You switch the feature on when starting Node.js with the flag --experimental-policy=policy.json. The Node.js documentation at the time described policies as "a security feature intended to allow guarantees about what code Node.js is able to load" and said that "The policy manifest will be used to enforce constraints on code loaded by Node.js." A later revision of the documentation adds that "policies guarantee the file integrity when a module is loaded using require(), import() or new Module()." In other words, the feature promises that a file whose contents do not match the manifest will not run.
Hashes and checksums. A hash (also called a digest or checksum) is a short string of characters computed from a file's contents. Change even one byte of the file and the hash changes completely. That is why a hash works as a fingerprint: if the fingerprint of the file on disk matches the fingerprint recorded in the manifest, the file is almost certainly unchanged. Node.js computes these fingerprints with the Hash class from its built-in crypto module. The name sha384 in the manifest refers to one particular hashing method.
Prototypes and symbols. JavaScript objects can inherit behaviour from other objects called prototypes. If you change a method on a prototype, every object that inherits from it gets the changed version. A symbol is a special kind of property name that is hard to collide with by accident. Node.js uses symbols to attach internal, hidden-looking properties to objects. The attack in this report abuses exactly that: it finds a hidden property on a hash object and changes a method on the prototype behind it.
C++ bindings. Node.js is written partly in JavaScript and partly in C++. Some JavaScript objects are thin wrappers around C++ code. The JavaScript side can be protected, but the C++ side is still reachable through the wrapper, and that is where the gap was.
The vulnerability class. This is an "insufficient verification of data authenticity" problem: the system does check authenticity, but the check can be made to approve data that is not authentic. A simple comparison: a bouncer checks your ID against a list, but you are allowed to hold the list, so you write your own name on it.
The setup
The demonstration needs three files: a manifest and two scripts. The manifest declares ./main.js with integrity: true and dependencies on ./protected.js and crypto, and declares ./protected.js with a strict integrity value and unrestricted dependencies:
{
"resources": {
"./main.js": {
"integrity": true,
"dependencies": {
"./protected.js": true,
"crypto": true
}
},
"./protected.js": {
"integrity": "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
"dependencies": true
}
}
}
Line by line:
- The outer braces open a JSON object, and
"resources"is the list of files the policy talks about. "./main.js"is the first resource. Its"integrity": truemeans its integrity is checked, but no specific fingerprint is pinned here.- Its
"dependencies"object lists whatmain.jsmay load:"./protected.js"and the built-incryptomodule. Both are set totrue, meaning allowed. "./protected.js"is the second resource. Its"integrity"is a long string starting withsha384-, which is the exact fingerprint the file must have.- Its
"dependencies": truemeans it may load anything it likes.
The roles are deliberately lopsided. main.js may contain arbitrary code but cannot access, for example, the built-in fs module, so it cannot read files. protected.js has a strict integrity requirement but can access arbitrary modules, including fs. main.js is allowed to require protected.js, provided that Node.js verifies the integrity of protected.js first. The whole attack lives in that gap: untrusted code that is allowed to run, and a trusted module whose integrity check it can subvert.
Finding it
Node.js computes a cryptographic hash of each resource using the Hash class of the built-in crypto module. The implementation protects itself against modifications of the Hash class prototype by the application. However, the Hash class internally relies on several C++ bindings that the application can replace at runtime.
The Hash class internally relies on C++ bindings that the application can replace at runtime. The JavaScript object returned by createHash() exposes an internal handle, and the prototype of that handle's constructor can be reached and modified from ordinary application code. If the policy implementation calls digest() on such an object, the application can make it return an attacker-chosen value instead of the real hash. In everyday terms: the guard checks the seal on a parcel, but the seal-making machine is sitting in the same room as the visitor, and the visitor can swap its output.
Exploitation
Step 1: Prepare the untrusted entry point
The contents of main.js are arbitrary code. The researcher uses them to obtain a Hash instance, compute a real digest once, locate the internal handle symbol, and then overwrite digest on the prototype of the handle's constructor so that every later call returns the captured digest:
const h = require('crypto').createHash('sha384');
const fakeDigest = h.digest();
const kHandle = Object.getOwnPropertySymbols(h)
.find((s) => s.description === 'kHandle');
h[kHandle].constructor.prototype.digest = () => fakeDigest;
require('./protected.js');
Line by line:
const h = require('crypto').createHash('sha384');loads the built-incryptomodule and creates a hash objecththat uses the SHA-384 method. This is the same kind of object Node.js uses when it checks a file's fingerprint.const fakeDigest = h.digest();computes the hash of nothing (an empty input) and stores the result infakeDigest. This is the value that will later be handed to the policy check in place of the real fingerprint.const kHandle = Object.getOwnPropertySymbols(h).find((s) => s.description === 'kHandle');asks the hash object for its own symbol-named properties and picks the one whose description iskHandle. That symbol is the hidden door to the internal C++ handle.h[kHandle].constructor.prototype.digest = () => fakeDigest;follows that door to the handle, then to its constructor, then to the constructor's prototype, and replaces thedigestmethod there with a function that always returnsfakeDigest. From now on, any hash object built on that prototype reports the fake value.require('./protected.js');loads the protected module. This is what triggers the policy integrity check, and the check now receives the forged value.
Why this order? The tampering has to happen before the protected file is loaded, because the check runs at load time. Computing a real digest first gives the attacker a plausible-looking string to return, so the check has something to compare against.
Step 2: Provide a protected module that does not match the manifest
The file protected.js does not match the integrity value specified in policy.json:
console.log(require('fs').readFileSync('/etc/passwd').length);
Line by line:
require('fs')loads the built-in file system module.main.jsis not allowed to do this, butprotected.jsis..readFileSync('/etc/passwd')reads the file/etc/passwdin one go. On Unix-like systems this file lists user accounts, and it is a classic target in demonstrations because it is readable by ordinary users and clearly outside the application's own files..lengthgives the number of characters read, andconsole.log(...)prints that number.
This is code that main.js itself is not permitted to run, because main.js has no access to fs. It is exactly the kind of code the integrity check exists to keep out, since the file on disk differs from the trusted manifest entry. The attacker's goal is to get this file to run anyway.
Step 3: Run with the policy enabled
Running main.js with the policy enabled succeeds despite the integrity mismatch, and the application reads /etc/passwd:
$ node --experimental-policy=policy.json main.js
3224
Line by line:
nodestarts Node.js.--experimental-policy=policy.jsonswitches on the experimental policy feature and points it at the manifest.main.jsis the file to run first.3224is the output: the length of/etc/passwdas read byprotected.js.
The command starts Node.js with the manifest, loads main.js, which tampers with the hash prototype and then requires protected.js. The policy check on protected.js receives the forged digest, accepts the module, and the module prints the length of /etc/passwd. The output 3224 is the result of that read, demonstrating that the modified code executed with the permissions of the supposedly trusted code.
Root cause
The policy implementation verifies integrity by computing a hash through the crypto module's Hash class. The class guards its own prototype against modification by the application, but it delegates the actual work to C++ bindings that the application can replace at runtime. The internal handle reachable from a Hash instance exposes a constructor whose prototype carries digest, and that prototype is writable from application code. Overwriting digest therefore changes what the policy check sees, without touching the guarded Hash prototype itself. The check compares the manifest value against a value the attacker controls, so it can never fail. The existing defence, protecting the Hash class prototype, did not cover the underlying binding surface.
A comparison: a bank checks signatures, and its own signature-checking machine is locked in a glass case. But the machine has a cable running out of the case to a socket on the wall, and the visitor can unplug the cable and plug in their own device. The lock on the case was real; it just did not cover the cable.
Impact
The vulnerability invalidates the documented guarantees of the policy feature. Code may be executed through require() even if the code has been modified, and the modified code inherits all permissions of the supposedly trusted code. As the researcher demonstrates, this potentially allows an attacker to escalate their permissions: code confined to main.js gains the module access of protected.js, including fs. The impact is limited to deployments that use the experimental policy mechanism on Node.js 18.x or 20.x, and the policy feature must be enabled at startup. The policy feature was experimental at the time the CVE was issued.
In plain terms: policies are meant to be a fence around untrusted code. This bug lets the untrusted code open a gate in the fence, walk into the trusted area, and use everything stored there.
Fix
The researcher provided a patch. It was merged into the main branch as commit [redacted], into the v20.x release line as commit [redacted], and into the v18.x release line as commit [redacted]. All of these were released on 13 October 2023. The report does not describe the contents of the patch beyond this.
Each one is a way of asking the same question: is there a security check whose answer depends on something the application can reach and change?
- Other internal handles exposed on built-in objects: look for own property symbols with descriptive names on objects returned by built-in APIs, and check whether the prototype of the handle's constructor carries methods used by security checks. A positive result is a security-relevant method that can be replaced from application code and whose replacement is accepted by the check.
- Other consumers of
cryptohashing inside Node.js security features: any check that callsdigest()on aHashinstance created in the same process as application code is a candidate. A positive result is a check that passes for a resource whose on-disk contents differ from the trusted value. - Prototype tampering on classes that wrap native bindings more generally: the pattern of guarding the JavaScript class while leaving the binding reachable is the signal. A positive result is a guarded class whose behaviour can still be changed through an object it returns.
- Policy manifests with mixed trust: place the tampering code in a resource with
integrity: truebut restricted dependencies, and the target in a resource with a strict integrity value and unrestricted dependencies, as in the report. A positive result is execution of the protected module despite a mismatch.
This kind of bug hides in security features implemented in JavaScript on top of native bindings, especially experimental ones. During recon, look for features that are opt-in at startup, documented as providing guarantees, and implemented by calling into crypto or other native modules from the same process that runs untrusted code. The signal is a check whose result depends on an object the application can reach and modify, such as a hash object returned by a public API.
The researcher showed enough impact by building a minimal, self-contained demonstration: a manifest with a clear trust boundary, an untrusted entry point that only tampers with the hash object, and a protected module that reads a file the untrusted code could not otherwise access. The output of the command proves both the bypass and the resulting permission escalation, which is what makes the case for a security fix rather than a hardening suggestion.
Glossary
- API: a defined set of functions a program can call to use another piece of software, like a menu of services.
- Binding: the glue code that lets JavaScript call functions written in another language, here C++.
- Checksum: a short value computed from data, used to detect changes; also called a hash or digest.
- Commit: a recorded change in a version control system such as Git, identified by a long hexadecimal string.
- Constructor: the function that creates objects of a particular kind in JavaScript.
- CVE: a public identifier for a known security vulnerability, here CVE-2023-38552.
- Dependency: a module that another module loads.
- Digest: the output of a hash function; see checksum.
- Experimental feature: a feature that is available but not yet considered stable or fully supported.
fs: Node.js's built-in module for reading and writing files.- Hash: a fixed-length value computed from data such that any change to the data changes the value; used as a fingerprint.
- Integrity check: verifying that a file's contents match a trusted fingerprint before using it.
- Manifest: the file, here
policy.json, that lists resources and their rules. - Module: a file of code that can be loaded by another file.
- Node.js: a runtime that executes JavaScript outside a browser.
- Policy: Node.js's experimental feature for restricting which code may be loaded and verifying file integrity.
- Prototype: an object from which other JavaScript objects inherit properties and methods; changing it affects all objects that inherit from it.
require(): the Node.js function that loads a module.- Resource: in a policy manifest, a file the program may load.
- SHA-384: a specific hashing method producing a 384-bit digest.
- Symbol: a unique property name in JavaScript, often used for internal properties.
- Vulnerability: a weakness in software that can be used to make it behave in unintended ways.