Permission model improperly protects against path traversal in Node.js 20
The Node.js permission model's path resolution could be bypassed because it dynamically called a user-overridable path.resolve function. An attacker could overwrite path.resolve to skip normalisation, allowing file reads and writes outside the permitted directories.
- Triaged in 2 hours
- Fixed in 6 days
- Paid in 6 days
- Disclosed after 36 days
Summary by the program
Permission model improperly protects against path traversal (High) - (CVE-2023-39331) A previously disclosed vulnerability (CVE-2023-30584) was patched insufficiently. The new path traversal vulnerability arises because the implementation does not protect itself against the application overwriting built-in utility functions with user-defined implementations.
Impacts: This vulnerability affects all users using the experimental permission model in Node.js 20.x. Please note that at the time this CVE is issued, the permission model 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: A previously disclosed vulnerability (CVE-2023-30584) was patched insufficiently in commit 205f1e6. The new path traversal vulnerability arises because the implementation does not protect itself against the application overwriting built-in utility functions with user-defined implementations.
Description: The function possiblyTransformPath calls pathModule.resolve(path), where pathModule is the result of require('path'). Application code may replace the value of the require('path').resolveproperty with a user-defined function that does not resolve /../ within any given path. Because possiblyTransformPath retrieves the value of the pathModule.resolve property dynamically, it will use the user-defined function instead of the built-in function and will thus fail to fully resolve the path given by the application. The vulnerability can be prevented by maintaining a reference to the original value of pathModule.resolve for use in possiblyTransformPath, assuming that the original implementation of the resolve() function is not subject to any such vulnerabilities itself.
Steps To Reproduce:
Temporarily assigning path.resolve = (s) => s disables the resolution of /../ within the permission model implementation.
$ node --experimental-permission --allow-fs-read=/tmp/ -p "path.resolve = (s) => s; fs.readFileSync('/tmp/../etc/passwd')"
<Buffer 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 6d 6f ... 3174 more bytes>
Supporting Material/References:
- Original HackerOne report 2092852 of this vulnerability
- HackerOne report 1952978 for the previous path traversal vulnerability (CVE-2023-30584)
- Vulnerable implementation of
possiblyTransformPath
Suggested patch
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index b7354e30e9..4971656d0a 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -710,2 +710,3 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
// The permission model needs the absolute path for the fs_permission
+const resolvePath = pathModule.resolve;
function possiblyTransformPath(path) {
@@ -713,3 +714,3 @@ function possiblyTransformPath(path) {
if (typeof path === 'string') {
- return pathModule.resolve(path);
+ return resolvePath(path);
}
This patch assumes that pathModule.resolve() itself is not susceptible to having its behavior altered in a security-critical way through user-defined properties.
This patch was merged into the main branch of Node.js as commit 32bcf4ca and into the Node.js 20 release line as commit cd352751.
Impact
The impact is almost identical with that of CVE-2023-30584. Applications may use this vulnerability to read and write files and directories that the user has not granted access to.
Context
Node.js 20 ships an experimental permission model, enabled with the --experimental-permission flag. When it is active, the runtime restricts what the process may do with the filesystem: reads and writes are only allowed under paths the operator has explicitly granted, for example with --allow-fs-read=/tmp/. The model is enforced inside the runtime's own filesystem layer, so application code that calls fs.readFileSync is checked against the granted paths before the read happens.
One of the checks has to turn the path supplied by the application into an absolute path before comparing it with the allowlist. That normalisation step is what this report is about. The affected code lives in lib/internal/fs/utils.js, in the function possiblyTransformPath.
This is a follow-up to an earlier path traversal issue, CVE-2023-30584, which was fixed in commit 205f1e6. The fix for that CVE was insufficient, and the new issue was assigned CVE-2023-39331. The researcher, Tobias Nießen, both reported the problem and wrote the security patch.
Setup
The precondition is a Node.js 20.x process running with the experimental permission model enabled and at least one filesystem path granted. In the reproduction the granted path is /tmp/ for reads:
--experimental-permission --allow-fs-read=/tmp/
No account, role or remote service is involved. The attacker in this model is application code running inside the permission-restricted process, for example code from a dependency, a plugin or any other untrusted script that the host application evaluates. The only tool needed is the node binary itself.
Finding it
possiblyTransformPath calls pathModule.resolve(path), where pathModule is the object returned by require('path'). The property is read dynamically at call time, so a user-defined replacement is used instead of the built-in function.
Application code can assign to require('path').resolve and replace the built-in function with anything it likes. Since possiblyTransformPath looks the property up on every call rather than holding a reference to the original function, it will pick up the replacement. A replacement that does not resolve /../ segments leaves the traversal in the path, and the permission check then compares the wrong string against the allowlist.
The researcher notes that the problem can be prevented by keeping a reference to the original value of pathModule.resolve and using that reference inside possiblyTransformPath, on the assumption that the original resolve() implementation is not itself subject to a similar weakness.
Exploitation
Step 1: overwrite the built-in resolver and read outside the allowlist
It assigns a pass-through function to path.resolve and then reads a file outside the granted directory through a .. segment:
$ node --experimental-permission --allow-fs-read=/tmp/ -p "path.resolve = (s) => s; fs.readFileSync('/tmp/../etc/passwd')"
<Buffer 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 6d 6f ... 3174 more bytes>
The parts that matter:
--experimental-permission --allow-fs-read=/tmp/turns on the permission model and grants read access only under/tmp/.path.resolve = (s) => sreplaces the resolver with a function that returns its argument unchanged. No normalisation of..happens.fs.readFileSync('/tmp/../etc/passwd')asks for a file that is outside the granted directory once the traversal is resolved.
The command returns the contents of /etc/passwd as a buffer. The hex dump begins with 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a, which is the first line of the file, and the output continues for another 3174 bytes. The permission model was supposed to reject this read, but the attacker's replacement function left the traversal unresolved.
Root cause
The vulnerable implementation of possiblyTransformPath is linked in the report at lib/internal/fs/utils.js, lines 711 to 718 of the referenced revision. The function calls pathModule.resolve(path), reading the resolve property off the path module object at the moment of the call. Because that object is shared with application code, the property can be reassigned, and the permission model ends up using an attacker-controlled function to compute the absolute path it validates.
The earlier fix for CVE-2023-30584 was insufficient. The suggested patch assumes that pathModule.resolve() itself is not susceptible to having its behaviour altered in a security-critical way through user-defined properties.
Impact
The report states that the impact is almost identical to that of CVE-2023-30584: applications may use this vulnerability to read and write files and directories that the user has not granted access to. The report says the vulnerability affects all users using the experimental permission model in Node.js 20.x. The report notes that at the time the CVE was issued, the permission model was an experimental feature of Node.js.
Fix
The researcher supplied a patch that captures the original resolver once, at module load, and uses the captured reference inside possiblyTransformPath:
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index b7354e30e9..4971656d0a 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -710,2 +710,3 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
// The permission model needs the absolute path for the fs_permission
+const resolvePath = pathModule.resolve;
function possiblyTransformPath(path) {
@@ -713,3 +714,3 @@ function possiblyTransformPath(path) {
if (typeof path === 'string') {
- return pathModule.resolve(path);
+ return resolvePath(path);
}
With const resolvePath = pathModule.resolve; evaluated at load time, later reassignment of pathModule.resolve by application code no longer affects the permission check, because the function calls the saved reference instead of looking the property up again. The patch was merged into the main branch as commit 32bcf4ca and into the Node.js 20 release line as commit cd352751.
Variants to test
- Replace other utility functions the security check depends on, not just
path.resolve. Any property read dynamically from a shared module object at check time is a candidate. A positive result is a restricted operation succeeding on a path or resource outside the granted set. - Assign a resolver that returns a path inside the allowlist while the real target is outside it, rather than a plain pass-through. A positive result is the same out-of-allowlist read or write with a check that appears to see an allowed path.
- Try the same substitution against write permissions and directory operations, since the report says both reads and writes are affected. A positive result is a write or directory access outside the granted paths.
- Look for other places in the permission model that call into mutable built-ins, such as path joining or normalisation helpers, and substitute them the same way. A positive result is any permission decision that changes when the built-in is replaced.
Hunting notes
This class of bug hides in security checks that call shared, mutable library objects at decision time instead of capturing the function reference when the module loads. In Node.js the path module object is the obvious example, but the pattern applies to any runtime where application code shares a module registry with the enforcement code.
During review, the signal is a permission or sandbox check that reads a property such as pathModule.resolve inside the check itself. Grep for calls of the form module.property(...) inside enforcement code and ask whether application code can reassign that property before the check runs. A second signal is a previous traversal fix: when a CVE in this area is patched, check whether the patch hardened the path handling but still depends on a replaceable function.
The researcher demonstrated impact with a single command that reads /etc/passwd outside the granted /tmp/ directory and shows the file contents in the output. The report also links the original HackerOne report 2092852 for this vulnerability, report 1952978 for the earlier CVE-2023-30584, and the full security advisory.
What happened, in plain words
Node.js is a program that lets people run JavaScript code outside a web browser, typically on a server. Version 20 of Node.js added an experimental feature called the permission model. Think of it as a security guard standing at the door of a building: the guard has a list of rooms the visitor is allowed to enter, and anyone trying to open a door that is not on the list is stopped. In Node.js terms, the guard checks which files and folders the running code is allowed to read or write.
For example, /tmp/../etc/passwd contains .., which means "go up one folder". Cleaned up, that path becomes /etc/passwd, which is outside the allowed folder /tmp/. The cleaning is done by a built-in helper function called resolve.
The problem: that helper lives on a shared object that the running application code can reach and replace. If the application swaps the helper for a fake one that does no cleaning, the guard compares the wrong text against its list and lets the read through. The researcher, Tobias Nießen, found this, reported it, and also wrote the fix. The issue is tracked as CVE-2023-39331. It is a follow-up to an earlier, similar bug, CVE-2023-30584, whose fix did not go far enough.
Context
What Node.js and the permission model are
Node.js runs JavaScript code, usually on a server. Normally, code running in Node.js can read and write files the operating system user is allowed to access. That is fine when you trust the code, but risky when you run code you did not write, such as a plugin or a third-party library.
To limit that risk, Node.js 20 introduced an experimental permission model. "Experimental" means the feature is still being tested and may change. You switch it on with a command-line flag, --experimental-permission. Once it is on, the runtime acts like the security guard described above: file reads and writes are only allowed under folders the person starting the program has explicitly granted, for example with --allow-fs-read=/tmp/.
So when application code calls fs.readFileSync (a function that reads a file), the runtime checks the requested path against the granted list before the read happens.
Why the path has to be cleaned first
One of those checks must turn the path supplied by the application into an absolute path before comparing it with the allowlist. An absolute path is a full path starting from the root of the filesystem, like /etc/passwd, rather than a relative one like ../etc/passwd. Cleaning matters because .. can climb out of an allowed folder.
That cleaning step is what this report is about. The affected code lives in a file called lib/internal/fs/utils.js, in a function named possiblyTransformPath.
The earlier bug and this one
This is a follow-up to an earlier path traversal issue, CVE-2023-30584, which was fixed in commit 205f1e6. A commit is a saved change in a code repository. The fix for that earlier CVE was insufficient, and the new issue was assigned CVE-2023-39331. The researcher, Tobias Nießen, both reported the problem and wrote the security patch.
Setup
What has to be true for the bug to matter
The precondition is a Node.js 20.x process running with the experimental permission model enabled and at least one filesystem path granted. In the reproduction the granted path is /tmp/ for reads:
--experimental-permission --allow-fs-read=/tmp/
Line by line:
--experimental-permissionswitches on the permission model, the security guard.--allow-fs-read=/tmp/tells the guard that reading is allowed only under the/tmp/folder.
No account, role or remote service is involved. The attacker in this model is application code running inside the permission-restricted process, for example code from a dependency, a plugin or any other untrusted script that the host application evaluates. The only tool needed is the node binary itself.
Finding it
How the check was written
possiblyTransformPath calls pathModule.resolve(path), where pathModule is the object returned by require('path'). In Node.js, require('path') loads a built-in module that offers helpers for working with file paths, and resolve is the helper that turns a path into its cleaned, absolute form.
The property is read dynamically at call time, so a user-defined replacement is used instead of the built-in function. "Dynamically" means the code looks up the resolve property on the object every time it runs, rather than keeping a private copy of the original function.
Why that is a problem
"Mutable" means its contents can be changed while the program runs. Application code can assign to require('path').resolve and replace the built-in function with anything it likes. Since possiblyTransformPath looks the property up on every call rather than holding a reference to the original function, it will pick up the replacement. A replacement that does not resolve /../ segments leaves the traversal in the path, and the permission check then compares the wrong string against the allowlist.
An everyday comparison: imagine a form at a building entrance that says "ask the receptionist for the current list of allowed rooms". If a visitor can swap the receptionist's list for a fake one, the form still looks official, but the answer is wrong. The fix is to write the correct list down once, at the start, and use that copy.
The researcher notes that the problem can be prevented by keeping a reference to the original value of pathModule.resolve and using that reference inside possiblyTransformPath, on the assumption that the original resolve() implementation is not itself subject to a similar weakness.
Exploitation
Step 1: overwrite the built-in resolver and read outside the allowlist
It assigns a pass-through function to path.resolve and then reads a file outside the granted directory through a .. segment:
$ node --experimental-permission --allow-fs-read=/tmp/ -p "path.resolve = (s) => s; fs.readFileSync('/tmp/../etc/passwd')"
<Buffer 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 6d 6f ... 3174 more bytes>
The parts that matter:
--experimental-permission --allow-fs-read=/tmp/turns on the permission model and grants read access only under/tmp/.path.resolve = (s) => sreplaces the resolver with a function that returns its argument unchanged. No normalisation of..happens.fs.readFileSync('/tmp/../etc/passwd')asks for a file that is outside the granted directory once the traversal is resolved.
Line by line, in plain words:
nodestarts the Node.js program.--experimental-permission --allow-fs-read=/tmp/is the security guard, with/tmp/as the only allowed reading area.-ptells Node.js to run the code that follows and print the result.path.resolve = (s) => stakes the built-in cleaning helper and replaces it with a fake one. The fake takes a stringsand returns it unchanged, so..is never cleaned up.fs.readFileSync('/tmp/../etc/passwd')asks to read a file. The path starts inside the allowed folder but uses..to climb out to/etc/passwd, a file on Unix-like systems that lists user accounts.
The command returns the contents of /etc/passwd as a buffer. A buffer is how Node.js hands back raw binary data. The hex dump begins with 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a, which is the first line of the file, and the output continues for another 3174 bytes. The permission model was supposed to reject this read, but the attacker's replacement function left the traversal unresolved.
Root cause
The vulnerable implementation of possiblyTransformPath is linked in the report at lib/internal/fs/utils.js, lines 711 to 718 of the referenced revision. The function calls pathModule.resolve(path), reading the resolve property off the path module object at the moment of the call. Because that object is shared with application code, the property can be reassigned, and the permission model ends up using an attacker-controlled function to compute the absolute path it validates.
The earlier fix for CVE-2023-30584 was insufficient. The suggested patch assumes that pathModule.resolve() itself is not susceptible to having its behaviour altered in a security-critical way through user-defined properties.
Impact
The report states that the impact is almost identical to that of CVE-2023-30584: applications may use this vulnerability to read and write files and directories that the user has not granted access to. The report says the vulnerability affects all users using the experimental permission model in Node.js 20.x. The report notes that at the time the CVE was issued, the permission model was an experimental feature of Node.js.
Fix
The researcher supplied a patch that captures the original resolver once, at module load, and uses the captured reference inside possiblyTransformPath:
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index b7354e30e9..4971656d0a 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -710,2 +710,3 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
// The permission model needs the absolute path for the fs_permission
+const resolvePath = pathModule.resolve;
function possiblyTransformPath(path) {
@@ -713,3 +714,3 @@ function possiblyTransformPath(path) {
if (typeof path === 'string') {
- return pathModule.resolve(path);
+ return resolvePath(path);
}
Line by line:
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.jssays which file the change touches.index b7354e30e9..4971656d0a 100644is bookkeeping that identifies the old and new versions of the file.--- a/lib/internal/fs/utils.jsmarks the old version and+++ b/lib/internal/fs/utils.jsthe new one.@@ -710,2 +710,3 @@says where in the file the change sits and how many lines are involved.// The permission model needs the absolute path for the fs_permissionis an existing comment, shown for context.+const resolvePath = pathModule.resolve;is the new line. It saves the original cleaning function in a variable calledresolvePathonce, when the module loads.function possiblyTransformPath(path) {is the function being fixed.- return pathModule.resolve(path);is the old line, removed. It looked the helper up on the shared object every time.+ return resolvePath(path);is the new line, which calls the saved copy instead.
With const resolvePath = pathModule.resolve; evaluated at load time, later reassignment of pathModule.resolve by application code no longer affects the permission check, because the function calls the saved reference instead of looking the property up again. The patch was merged into the main branch as commit 32bcf4ca and into the Node.js 20 release line as commit cd352751.
Variants to test
- Replace other utility functions the security check depends on, not just
path.resolve. Any property read dynamically from a shared module object at check time is a candidate. A positive result is a restricted operation succeeding on a path or resource outside the granted set. - Assign a resolver that returns a path inside the allowlist while the real target is outside it, rather than a plain pass-through. A positive result is the same out-of-allowlist read or write with a check that appears to see an allowed path.
- Try the same substitution against write permissions and directory operations, since the report says both reads and writes are affected. A positive result is a write or directory access outside the granted paths.
- Look for other places in the permission model that call into mutable built-ins, such as path joining or normalisation helpers, and substitute them the same way. A positive result is any permission decision that changes when the built-in is replaced.
Hunting notes
This class of bug hides in security checks that call shared, mutable library objects at decision time instead of capturing the function reference when the module loads. In Node.js the path module object is the obvious example, but the pattern applies to any runtime where application code shares a module registry with the enforcement code.
During review, the signal is a permission or sandbox check that reads a property such as pathModule.resolve inside the check itself. Grep for calls of the form module.property(...) inside enforcement code and ask whether application code can reassign that property before the check runs. A second signal is a previous traversal fix: when a CVE in this area is patched, check whether the patch hardened the path handling but still depends on a replaceable function.
The researcher demonstrated impact with a single command that reads /etc/passwd outside the granted /tmp/ directory and shows the file contents in the output. The report also links the original HackerOne report 2092852 for this vulnerability, report 1952978 for the earlier CVE-2023-30584, and the full security advisory.
Glossary
- Node.js: a program that runs JavaScript code outside a web browser, commonly on servers.
- Permission model: an experimental Node.js 20 feature that restricts which files and folders the running code may read or write.
- Experimental feature: a feature still being tested that may change in future versions.
- Flag: an option given on the command line to change how a program starts, such as
--experimental-permission. - Allowlist: a list of things that are explicitly permitted.
- Filesystem: the way a computer organises files and folders.
- Path: the text that names the location of a file or folder, such as
/tmp/example.txt. - Absolute path: a full path starting from the root of the filesystem, like
/etc/passwd. - Relative path: a path interpreted from the current location, often using
..to go up a folder. - Path traversal: a bug where
..or similar tricks let someone reach files outside the intended folder. - Normalisation: cleaning a path into a standard, absolute form so it can be compared reliably.
resolve: the built-in Node.js helper that turns a path into its cleaned, absolute form.require('path'): the way Node.js code loads the built-in module that offers path helpers.- Module: a self-contained piece of code that other code can load and use.
- Mutable object: an object whose contents can be changed while the program runs.
- Property: a named value stored on an object, such as
resolveon the path module. - Dynamic lookup: reading a property from an object at the moment it is needed, rather than keeping a saved copy.
- Reference: a saved pointer to a function or value that stays the same even if the original property is later replaced.
- Function: a reusable block of code that takes input and produces a result.
- Pass-through function: a function that returns its input unchanged, doing no processing.
fs.readFileSync: a Node.js function that reads a file and returns its contents.- Buffer: how Node.js hands back raw binary data.
- Hex dump: a display of binary data as pairs of hexadecimal digits.
- CVE: a public identifier for a known security vulnerability.
- Commit: a saved change in a code repository.
- Patch: a proposed code change that fixes a problem.
- Diff: a display showing lines added to and removed from a file.
- Severity: a rating of how serious a vulnerability is, here high.
- Sandbox: an isolated environment that limits what code inside it can do.
- Grep: a tool for searching text for a pattern.
- Security advisory: an official notice describing a vulnerability and its fix.