Fix potential OS command injection in webserver.js#32
Open
DongZifan wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Command Injection Reproduction Notes for
webserver.jsSummary
Hello,
I am writing to report potential Path Traversal and OS Command Injection vulnerabilities in the following file:
jsdf/pce/46c3ecb/example/webserver.jsThe first issue appears when user-controlled request paths are used to construct filesystem paths without verifying that the final resolved path remains inside the intended serving directory. Under certain conditions, specially crafted request paths may allow files outside the configured web root to be accessed.
The second issue appears when file paths are used to construct shell command strings that are later executed via child_process.execSync(...). Under certain conditions, specially crafted filenames may allow unintended command execution on the host system. This could potentially lead to command injection risks, depending on how the input path and filename are validated and processed.
Root Cause
The path traversal issue is caused by unsafe filesystem path construction in the following code:
Because the final path is not checked against
serveDir, a request such as/../secret_outside.txtmay escape the intended serving directory.The command injection issue is caused by unsafe shell command construction in the following function:
Reproduction Material
A minimal reproduction script is provided in:
poc_webserver.jspoc_webserver.js
This Proof of Concept (PoC) is intended to demonstrate that external input can reach dangerous filesystem access and command execution logic through the vulnerable code path.
What the PoC Does
The PoC performs a minimal end-to-end trigger of the vulnerable code path:
webserver.js./../secret_outside.txt.poc&calc&rem.getMimeType(filepath).filepathvalue is concatenated into a shell command and passed toexecSync(...).In the provided example, the injected filename is crafted so that, on Windows, successful command execution opens the Calculator application. This serves as a visible indicator that external input can reach the OS command execution sink without proper sanitization.
Example Payload
The PoC uses the following path traversal payload:
The PoC uses the following command injection filename:
How to Run
Run from the project root:
Expected Output
When the PoC runs against the vulnerable version, you should see output similar to:
In the vulnerable version, after the crafted request is processed, the server can read a file outside the intended public directory. In addition, after the malicious filename is requested, the local machine will launch the Calculator application as a benign demonstration effect.
This shows that attacker-controlled request paths can influence filesystem access behavior, and attacker-controlled filenames can influence command execution behavior through the vulnerable
getMimeType()path.Patch Explanation
This branch also includes a patched version of
webserver.jsintended to mitigate the path traversal and command injection risks described above.What the patch changes
The patch adds path validation before request paths are used for filesystem access.
In the vulnerable version, user-controlled request paths such as:
/../secret_outside.txtcould reach filesystem APIs without validating whether the final path remained inside
serveDir.The patched version resolves the request path against
serveDirand rejects paths that escape the intended serving directory.The patch also changes MIME type detection so that file paths are no longer concatenated into a shell command string.
In the vulnerable version, attacker-controlled filenames such as:
poc&calc&remcould reach
child_process.execSync(...)through string concatenation.The patched version uses
execFileSync(...)with an argument array so the file path is passed as a single argument instead of being interpreted by the shell.Validation introduced by the patch
For request paths, the patch checks that:
the value is decoded safely
the final resolved filesystem path is inside
serveDirpaths escaping the serving directory are rejected before filesystem access
For MIME type detection, the patch avoids shell command string construction by replacing:
with:
If validation fails, the request is rejected before it reaches file reading logic. If a filename contains shell metacharacters, those characters are treated as part of the filename argument rather than as shell syntax.# Command Injection Reproduction Notes for
webserver.jsSummary
Hello,
I am writing to report potential Path Traversal and OS Command Injection vulnerabilities in the following file:
jsdf/pce/46c3ecb/example/webserver.jsThe first issue appears when user-controlled request paths are used to construct filesystem paths without verifying that the final resolved path remains inside the intended serving directory. Under certain conditions, specially crafted request paths may allow files outside the configured web root to be accessed.
The second issue appears when file paths are used to construct shell command strings that are later executed via child_process.execSync(...). Under certain conditions, specially crafted filenames may allow unintended command execution on the host system. This could potentially lead to command injection risks, depending on how the input path and filename are validated and processed.
Root Cause
The path traversal issue is caused by unsafe filesystem path construction in the following code:
Because the final path is not checked against
serveDir, a request such as/../secret_outside.txtmay escape the intended serving directory.The command injection issue is caused by unsafe shell command construction in the following function:
Reproduction Material
A minimal reproduction script is provided in:
poc_webserver.jsThis Proof of Concept (PoC) is intended to demonstrate that external input can reach dangerous filesystem access and command execution logic through the vulnerable code path.
What the PoC Does
The PoC performs a minimal end-to-end trigger of the vulnerable code path:
webserver.js./../secret_outside.txt.poc&calc&rem.getMimeType(filepath).filepathvalue is concatenated into a shell command and passed toexecSync(...).In the provided example, the injected filename is crafted so that, on Windows, successful command execution opens the Calculator application. This serves as a visible indicator that external input can reach the OS command execution sink without proper sanitization.
Example Payload
The PoC uses the following path traversal payload:
The PoC uses the following command injection filename:
How to Run
Run from the project root:
Expected Output
When the PoC runs against the vulnerable version, you should see output similar to:
In the vulnerable version, after the crafted request is processed, the server can read a file outside the intended public directory. In addition, after the malicious filename is requested, the local machine will launch the Calculator application as a benign demonstration effect.
This shows that attacker-controlled request paths can influence filesystem access behavior, and attacker-controlled filenames can influence command execution behavior through the vulnerable
getMimeType()path.Patch Explanation
This branch also includes a patched version of
webserver.jsintended to mitigate the path traversal and command injection risks described above.What the patch changes
The patch adds path validation before request paths are used for filesystem access.
In the vulnerable version, user-controlled request paths such as:
/../secret_outside.txtcould reach filesystem APIs without validating whether the final path remained inside
serveDir.The patched version resolves the request path against
serveDirand rejects paths that escape the intended serving directory.The patch also changes MIME type detection so that file paths are no longer concatenated into a shell command string.
In the vulnerable version, attacker-controlled filenames such as:
poc&calc&remcould reach
child_process.execSync(...)through string concatenation.The patched version uses
execFileSync(...)with an argument array so the file path is passed as a single argument instead of being interpreted by the shell.Validation introduced by the patch
For request paths, the patch checks that:
the value is decoded safely
the final resolved filesystem path is inside
serveDirpaths escaping the serving directory are rejected before filesystem access
For MIME type detection, the patch avoids shell command string construction by replacing:
with:
If validation fails, the request is rejected before it reaches file reading logic. If a filename contains shell metacharacters, those characters are treated as part of the filename argument rather than as shell syntax.