-
Notifications
You must be signed in to change notification settings - Fork 140
Security: Arbitrary Code Execution via runInThisContext #1166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,20 @@ | ||||||||||||||||||||||
| import { Script } from "vm"; | ||||||||||||||||||||||
| import "sicp"; | ||||||||||||||||||||||
| import { readFileSync, createWriteStream } from "fs"; | ||||||||||||||||||||||
| import { resolve, dirname } from "path"; | ||||||||||||||||||||||
| ("use strict"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let writer = createWriteStream("test_node_env/result.txt"); | ||||||||||||||||||||||
| const args = process.argv.slice(2); | ||||||||||||||||||||||
| const programPath = args[0]; | ||||||||||||||||||||||
| const allowedDir = resolve(dirname(process.argv[1]), "test_node_env"); | ||||||||||||||||||||||
| const resolvedPath = resolve(programPath); | ||||||||||||||||||||||
| if (!resolvedPath.startsWith(allowedDir)) { | ||||||||||||||||||||||
| throw new Error("Invalid program path: must be within test_node_env directory"); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path validation logic contains a logic error and a security weakness:
Consider resolving the path to the
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| writer.once("open", function (fd) { | ||||||||||||||||||||||
| let s = new Script(readFileSync(programPath)); | ||||||||||||||||||||||
| let s = new Script(readFileSync(resolvedPath)); | ||||||||||||||||||||||
| let r = s.runInThisContext(); | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The primary objective of this PR, as stated in the title and description, is to prevent arbitrary code execution by avoiding
Suggested change
|
||||||||||||||||||||||
| if (typeof r !== "undefined") { | ||||||||||||||||||||||
| writer.write(JSON.stringify(r)); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include
sepin the imports from thepathmodule. This is necessary to perform a robust directory prefix check that avoids partial name matches (e.g., preventing/app/testfrom matching/app/test_malicious/).