-
Notifications
You must be signed in to change notification settings - Fork 35
feat(probes): add prototype pollution detection for __proto__ usage #512
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
7amed3li
wants to merge
7
commits into
NodeSecure:master
Choose a base branch
from
7amed3li:feat/prototype-pollution-detection
base: master
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.
+120
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71ae16e
feat(probes): add prototype pollution detection probe
7amed3li d250019
fix: resolve type mismatch in externalIdentifierLookup causing build …
7amed3li 3040502
refactor: address review feedback on prototype pollution probe
7amed3li a32c966
Update workspaces/js-x-ray/src/probes/isPrototypePollution.ts
7amed3li 03e37ec
Update workspaces/js-x-ray/test/probes/isPrototypePollution.spec.ts
7amed3li d9ac53c
refactor(probes): address review feedback for prototype pollution probe
7amed3li 65b47c3
refactor(probes): fix ProbeRunner tracing and spec lint issues
7amed3li 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@nodesecure/js-x-ray": minor | ||
| --- | ||
|
|
||
| feat: add prototype pollution detection probe |
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,52 @@ | ||
| // Import Third-party Dependencies | ||
| import type { ESTree } from "meriyah"; | ||
| import { getMemberExpressionIdentifier } from "@nodesecure/estree-ast-utils"; | ||
|
|
||
| // Import Internal Dependencies | ||
| import { SourceFile } from "../SourceFile.ts"; | ||
| import { generateWarning } from "../warnings.ts"; | ||
|
|
||
| function validateNode( | ||
| node: ESTree.Node | ||
| ): [boolean, string?] { | ||
| if (node.type === "Literal" && node.value === "__proto__") { | ||
| return [true, "literal"]; | ||
| } | ||
|
|
||
| if (node.type === "MemberExpression") { | ||
| const parts = [...getMemberExpressionIdentifier(node)]; | ||
|
|
||
| if (parts.at(-1) === "__proto__") { | ||
| return [true, parts.join(".")]; | ||
| } | ||
| } | ||
|
|
||
| return [false]; | ||
| } | ||
|
|
||
| function main( | ||
| node: ESTree.Literal | ESTree.MemberExpression, | ||
| options: { | ||
| sourceFile: SourceFile; | ||
| data?: string; | ||
| signals: { Skip: symbol; }; | ||
| } | ||
| ) { | ||
| const { sourceFile, data, signals } = options; | ||
|
|
||
| sourceFile.warnings.push( | ||
| generateWarning("prototype-pollution", { | ||
| value: data === "literal" ? "__proto__" : data!, | ||
| location: node.loc ?? null | ||
| }) | ||
| ); | ||
|
|
||
| return data === "literal" ? undefined : signals.Skip; | ||
| } | ||
|
|
||
| export default { | ||
| name: "isPrototypePollution", | ||
| validateNode, | ||
| main, | ||
| breakOnMatch: false | ||
| }; | ||
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
54 changes: 54 additions & 0 deletions
54
workspaces/js-x-ray/test/probes/isPrototypePollution.spec.ts
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,54 @@ | ||
| // Import Node.js Dependencies | ||
| import { test, type TestContext } from "node:test"; | ||
|
|
||
| // Import Internal Dependencies | ||
| import { AstAnalyser } from "../../src/AstAnalyser.ts"; | ||
|
|
||
| test("should detect prototype pollution via __proto__ property access", (t: TestContext) => { | ||
| const str = ` | ||
| const obj = {}; | ||
| obj.__proto__.polluted = true; | ||
| `; | ||
|
|
||
| const analyser = new AstAnalyser(); | ||
| const { warnings } = analyser.analyse(str); | ||
|
|
||
| t.assert.strictEqual(warnings.length, 1); | ||
| t.assert.partialDeepStrictEqual(warnings[0], { | ||
| kind: "prototype-pollution", | ||
| value: "obj.__proto__" | ||
| }); | ||
| }); | ||
|
|
||
| test("should detect prototype pollution via computed property access", (t: TestContext) => { | ||
| const str = ` | ||
| const obj = {}; | ||
| obj["__proto__"].polluted = true; | ||
| `; | ||
|
|
||
| const analyser = new AstAnalyser(); | ||
| const { warnings } = analyser.analyse(str); | ||
|
|
||
| t.assert.strictEqual(warnings.length, 1); | ||
| t.assert.partialDeepStrictEqual(warnings[0], { | ||
| kind: "prototype-pollution", | ||
| value: "obj.__proto__" | ||
| }); | ||
| }); | ||
|
|
||
| test("should detect prototype pollution via __proto__ literal", (t: TestContext) => { | ||
| const str = ` | ||
| const key = "__proto__"; | ||
| const obj = {}; | ||
| obj[key] = true; | ||
| `; | ||
|
|
||
| const analyser = new AstAnalyser(); | ||
| const { warnings } = analyser.analyse(str); | ||
|
|
||
| t.assert.strictEqual(warnings.length, 1); | ||
| t.assert.partialDeepStrictEqual(warnings[0], { | ||
| kind: "prototype-pollution", | ||
| value: "__proto__" | ||
| }); | ||
| }); |
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.