-
Notifications
You must be signed in to change notification settings - Fork 27
Enable Function sink again, with support for eval #861
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
Open
hansott
wants to merge
8
commits into
main
Choose a base branch
from
dynamic-code-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−27
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ba373cf
Enable Function sink again, with support for eval
hansott 36aed4f
Update error messages
hansott 606a31c
Parallel downloads + version check
hansott ba8b62f
Prefix warning messages with AIKIDO:
hansott 5b646fc
Add tests for eval (direct and indirect)
hansott 6b2251c
Refactor
hansott bbcfffe
Upload native addon
hansott c2bc027
Merge branch 'main' of github.com:AikidoSec/node-RASP into dynamic-co…
hansott 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,4 +58,5 @@ jobs: | |
| path: | | ||
| build/ | ||
| library/internals/ | ||
| library/node_internals/ | ||
| library/agent/hooks/instrumentation/wasm/ | ||
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,4 @@ | ||
| # Ignore everything in this directory# Ignore everything in this directory | ||
| * | ||
| # Except this file | ||
| !.gitignore |
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 |
|---|---|---|
| @@ -1,42 +1,107 @@ | ||
| import { join } from "node:path"; | ||
| import { getInstance } from "../agent/AgentSingleton"; | ||
| import { getContext } from "../agent/Context"; | ||
| import { Hooks } from "../agent/hooks/Hooks"; | ||
| import { InterceptorResult } from "../agent/hooks/InterceptorResult"; | ||
| import { inspectArgs } from "../agent/hooks/wrapExport"; | ||
| import { Wrapper } from "../agent/Wrapper"; | ||
| import { getLibraryRoot } from "../helpers/getLibraryRoot"; | ||
| import { getMajorNodeVersion } from "../helpers/getNodeVersion"; | ||
| import { checkContextForJsInjection } from "../vulnerabilities/js-injection/checkContextForJsInjection"; | ||
| import { existsSync } from "node:fs"; | ||
|
|
||
| export class Function implements Wrapper { | ||
| private inspectFunction(args: any[]) { | ||
| const context = getContext(); | ||
| private inspectFunction(args: unknown[]): InterceptorResult { | ||
| if (args.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!context || !Array.isArray(args) || args.length === 0) { | ||
| const code = args[0]; | ||
| if (!code || typeof code !== "string") { | ||
| return undefined; | ||
| } | ||
|
|
||
| const findLastStringArg = (args: any[]) => { | ||
| for (let i = args.length - 1; i >= 0; --i) { | ||
| if (typeof args[i] === "string") { | ||
| return args[i]; | ||
| } | ||
| } | ||
| const context = getContext(); | ||
| if (!context) { | ||
| return undefined; | ||
| }; | ||
| } | ||
|
|
||
| return checkContextForJsInjection({ | ||
| js: code, | ||
| operation: "new Function/eval", | ||
| context, | ||
| }); | ||
| } | ||
|
|
||
| const lastStringArg = findLastStringArg(args); | ||
| private loadNativeAddon() { | ||
| const majorVersion = getMajorNodeVersion(); | ||
| const arch = process.arch; | ||
| const platform = process.platform; | ||
|
|
||
| if (lastStringArg) { | ||
| return checkContextForJsInjection({ | ||
| js: lastStringArg, | ||
| operation: "new Function", | ||
| context, | ||
| }); | ||
| const nodeInternalsDir = join(getLibraryRoot(), "node_internals"); | ||
| const binaryPath = join( | ||
| nodeInternalsDir, | ||
| `zen-internals-node-${platform}-${arch}-node${majorVersion}.node` | ||
| ); | ||
| if (!existsSync(binaryPath)) { | ||
| // oxlint-disable-next-line no-console | ||
| console.warn( | ||
hansott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AIKIDO: Cannot find native addon for Node.js ${majorVersion} on ${platform}-${arch}. Function sink will not be instrumented.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| return undefined; | ||
| const bindings: { | ||
| setCodeGenerationCallback: ( | ||
| callback: (code: string) => string | undefined | ||
| ) => void; | ||
| } = require(binaryPath); | ||
| if (!bindings || typeof bindings.setCodeGenerationCallback !== "function") { | ||
| // oxlint-disable-next-line no-console | ||
| console.warn( | ||
hansott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `AIKIDO: Native addon for Node.js ${majorVersion} on ${platform}-${arch} is invalid. Function sink will not be instrumented.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| return bindings; | ||
| } | ||
|
|
||
| wrap(hooks: Hooks) { | ||
| hooks.addGlobal("Function", { | ||
| kind: "eval_op", | ||
| inspectArgs: this.inspectFunction, | ||
| wrap(_: Hooks) { | ||
| const bindings = this.loadNativeAddon(); | ||
| if (!bindings) { | ||
| return; | ||
| } | ||
|
|
||
| bindings.setCodeGenerationCallback((code: string) => { | ||
| const agent = getInstance(); | ||
| if (!agent) { | ||
| return; | ||
| } | ||
|
|
||
| const context = getContext(); | ||
timokoessler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!context) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| inspectArgs( | ||
| [code], | ||
| this.inspectFunction, | ||
| context, | ||
| agent, | ||
| { | ||
| name: "Function/eval", | ||
| type: "global", | ||
| }, | ||
| "<compile>", | ||
| "eval_op" | ||
| ); | ||
| } catch (error) { | ||
| // In blocking mode, onInspectionInterceptorResult would throw to block the operation | ||
| // To block the code generation, we need to return a string that will be used for the thrown error message | ||
| return (error as Error).message; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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.
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.
Maybe rename to FunctionSink to not override global Function in this file?