-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: Fix CSP eval violation and add a test to protect against future violations #32291
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
Merged
+185
−113
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c51417
Bump @microlink/react-json-view
robbie-c 9ec422c
Add CI check to ensure that we don't accidentally add an eval to the …
robbie-c 396fc6c
Actually parse the js
robbie-c 35d45c8
Update frontend/bin/check-toolbar-csp-eval.mjs
robbie-c fdda340
Update frontend/bin/check-toolbar-csp-eval.mjs
robbie-c 64b6367
Update UI snapshots for `chromium` (5)
github-actions[bot] 91197f2
Update UI snapshots for `chromium` (5)
github-actions[bot] 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,102 @@ | ||
| import * as parser from "@babel/parser"; | ||
| import traverse from "@babel/traverse"; | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| // Parse dist/toolbar.js and check for anything that might trigger CSP script-src violations | ||
|
|
||
| // The threat model here is: | ||
| // * We're not trying to protect against an attacker hiding an eval like window["ev" + "al"]("..."). | ||
| // * This is OK because any actual violations will be caught by the customer's actual CSP rules. | ||
| // * We are just trying to prevent false positives in the CSP report. | ||
| // * This script is just meant to protect against us accidentally adding evals, which wouldn't be maliciously hidden. | ||
|
|
||
|
|
||
| // Identifiers that become code when the *first* argument is a string | ||
| const UNSAFE_TIMERS = new Set(["setTimeout", "setInterval", "setImmediate", "execScript"]); | ||
|
|
||
| const FUNCTION_CTORS = new Set([ | ||
| "Function", | ||
| "AsyncFunction", | ||
| "GeneratorFunction", | ||
| "AsyncGeneratorFunction", | ||
| ]); | ||
|
|
||
| function main() { | ||
| const filePath = 'dist/toolbar.js'; | ||
| const absPath = path.resolve(process.cwd(), filePath); | ||
| const source = fs.readFileSync(absPath, "utf-8"); | ||
|
|
||
| const ast = parser.parse(source, { | ||
| sourceType: "unambiguous", | ||
| }); | ||
|
|
||
| const evals = []; | ||
|
|
||
| traverse.default(ast, { | ||
| CallExpression(p) { | ||
| const callee = p.get("callee"); | ||
|
|
||
| // eval(...) | ||
| if (callee.isIdentifier({name: "eval"})) { | ||
| if (p.node.loc) { | ||
| evals.push({ | ||
| type: 'eval()', | ||
| start: p.node.loc.start | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // window['eval'](...) | ||
| else if ( | ||
| callee.isMemberExpression() && | ||
| callee.node.computed && | ||
| callee.get("object").isIdentifier({name: "window"}) && | ||
| callee.get("property").isStringLiteral({value: "eval"}) | ||
| ) { | ||
| if (p.node.loc) { | ||
| evals.push({type: 'window["eval"]()', start: p.node.loc.start}); | ||
| } | ||
| } | ||
|
|
||
| // === 3. setTimeout + other functions with a string argument | ||
| else if ( | ||
| (callee.isIdentifier() && UNSAFE_TIMERS.has(callee.node.name)) || | ||
| (callee.isMemberExpression() && | ||
| !callee.node.computed && | ||
| callee.get("object").isIdentifier({name: "window"}) && | ||
| callee.get("property").isIdentifier() && | ||
| UNSAFE_TIMERS.has(callee.get("property").node.name)) | ||
| ) { | ||
| const firstArg = p.get("arguments")[0]; | ||
| if (firstArg?.isStringLiteral()) { | ||
| evals.push({type: `${callee.node.name}(string)`, start: p.node.loc}); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| NewExpression(p) { | ||
| const callee = p.get("callee"); | ||
|
|
||
| // new Function(...) + other Function constructors | ||
| if (callee.isIdentifier() && FUNCTION_CTORS.has(callee.node.name)) { | ||
| evals.push({ | ||
| type: `new ${callee.node.name}()`, | ||
| start: p.node.loc.start | ||
| }); | ||
|
|
||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (evals.length > 0) { | ||
| evals.forEach(({type, start: {line, column}}) => | ||
| console.log(`${type}: line ${line}:${column}`) | ||
| ); | ||
|
|
||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| main() |
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.
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.
Nice