-
Notifications
You must be signed in to change notification settings - Fork 43
Protect node:vm against code injection #887
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
+178
β0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import * as t from "tap"; | ||
| import { Context, runWithContext } from "../agent/Context"; | ||
| import { createTestAgent } from "../helpers/createTestAgent"; | ||
| import { NodeVm } from "./NodeVm"; | ||
|
|
||
| const unsafeContext = { | ||
| remoteAddress: "::1", | ||
| method: "POST", | ||
| url: "http://localhost:4000", | ||
| query: {}, | ||
| headers: {}, | ||
| body: { | ||
| code: "'); require('child_process').execSync('id'); //", | ||
| }, | ||
| cookies: {}, | ||
| routeParams: {}, | ||
| source: "express", | ||
| route: "/posts/:id", | ||
| } satisfies Context; | ||
|
|
||
| const safeContext = { | ||
| ...unsafeContext, | ||
| body: { code: "Hello, world!" }, | ||
| } satisfies Context; | ||
|
|
||
| t.test("it works", async (t) => { | ||
| const agent = createTestAgent(); | ||
|
|
||
| agent.start([new NodeVm()]); | ||
|
|
||
| const vm = require("vm"); | ||
|
|
||
| { | ||
| // @ts-expect-error Not typed | ||
| globalThis.__test = "test"; | ||
| const script = new vm.Script("globalThis.__test = 'modified';"); | ||
| script.runInThisContext(); | ||
| // @ts-expect-error Not typed | ||
| t.equal(globalThis.__test, "modified"); | ||
| // @ts-expect-error Not typed | ||
| delete globalThis.__test; | ||
| } | ||
|
|
||
| { | ||
| const contextObject = { globalVar: 1 }; | ||
| vm.createContext(contextObject); | ||
| vm.runInContext("globalVar *= 2;", contextObject); | ||
| t.same(contextObject.globalVar, 2); | ||
| } | ||
|
|
||
| runWithContext(safeContext, () => { | ||
| t.doesNotThrow(() => { | ||
| new vm.Script(`console.log('${safeContext.body.code}');`); | ||
| }); | ||
| t.doesNotThrow(() => { | ||
| vm.runInThisContext(`console.log('${safeContext.body.code}');`); | ||
| }); | ||
|
|
||
| const contextObject = { globalVar: 1 }; | ||
| vm.createContext(contextObject); | ||
| vm.runInContext("globalVar *= 2;", contextObject); | ||
| t.same(contextObject.globalVar, 2); | ||
|
|
||
| // Call with wrong arguments | ||
| t.throws(() => { | ||
| vm.createScript({}); | ||
| }, /Unexpected identifier/); | ||
| }); | ||
|
|
||
| runWithContext(unsafeContext, () => { | ||
| t.throws(() => { | ||
| new vm.Script(`console.log('${unsafeContext.body.code}');`); | ||
| }, /Zen has blocked a JavaScript injection: new Script/); | ||
|
|
||
| const functionsToTest = [ | ||
| "createScript", | ||
| "runInThisContext", | ||
| "runInNewContext", | ||
| "runInContext", | ||
| "compileFunction", | ||
| ]; | ||
|
|
||
| for (const funcName of functionsToTest) { | ||
| t.throws( | ||
| () => { | ||
| vm[funcName](`console.log('${unsafeContext.body.code}');`); | ||
| }, | ||
| new RegExp(`Zen has blocked a JavaScript injection: ${funcName}`) | ||
| ); | ||
| } | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { getContext } from "../agent/Context"; | ||
| import { Hooks } from "../agent/hooks/Hooks"; | ||
| import { inspectArgs, wrapExport } from "../agent/hooks/wrapExport"; | ||
| import { Wrapper } from "../agent/Wrapper"; | ||
| import { checkContextForJsInjection } from "../vulnerabilities/js-injection/checkContextForJsInjection"; | ||
| import { getInstance } from "../agent/AgentSingleton"; | ||
|
|
||
| export class NodeVm implements Wrapper { | ||
| private inspectCode(args: unknown[], operation: string) { | ||
| const context = getContext(); | ||
| if (!context) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (args.length === 0 || typeof args[0] !== "string") { | ||
| return undefined; | ||
| } | ||
|
|
||
| const code = args[0]; | ||
|
|
||
| return checkContextForJsInjection({ | ||
| js: code, | ||
| operation, | ||
| context, | ||
| }); | ||
| } | ||
|
|
||
| private onConstruct(target: any, args: unknown[]) { | ||
| const agent = getInstance(); | ||
| const context = getContext(); | ||
|
|
||
| if (!agent || !context) { | ||
| return new target(...args); | ||
|
timokoessler marked this conversation as resolved.
|
||
| } | ||
|
|
||
| inspectArgs( | ||
| args, | ||
| () => this.inspectCode(args, "new Script(...)"), | ||
| context, | ||
| agent, | ||
| { | ||
| name: "vm", | ||
| type: "builtin", | ||
| }, | ||
| "new Script(...)", | ||
| "eval_op" | ||
| ); | ||
|
|
||
| return new target(...args); | ||
|
aikido-autofix[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| wrap(hooks: Hooks): void { | ||
| hooks.addBuiltinModule("vm").onRequire((exports, pkgInfo) => { | ||
|
timokoessler marked this conversation as resolved.
|
||
| // We can't use our helper wrapNewInstance because it can not inspect constructor args | ||
| exports.Script = new Proxy(exports.Script, { | ||
| construct: (target, args) => this.onConstruct(target, args), | ||
| }); | ||
|
|
||
| const functionsToWrap = [ | ||
| "createScript", | ||
| "runInThisContext", | ||
| "runInNewContext", | ||
| "runInContext", | ||
| "compileFunction", | ||
| ]; | ||
|
|
||
| for (const functionName of functionsToWrap) { | ||
| if (typeof exports[functionName] === "function") { | ||
| wrapExport(exports, functionName, pkgInfo, { | ||
| kind: "eval_op", | ||
| inspectArgs: (args) => | ||
| this.inspectCode(args, `${functionName}(...)`), | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.