-
Notifications
You must be signed in to change notification settings - Fork 25
build: generate CSP-safe Embind adapters #85
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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,48 @@ | ||
| #!/usr/bin/env node | ||
| "use strict"; | ||
|
|
||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| const distDirectory = path.resolve(process.argv[2] ?? ""); | ||
|
|
||
| if (!process.argv[2] || !fs.existsSync(distDirectory)) { | ||
| console.error("Usage: node tools/csp/check-generated-js.js <dist-directory>"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const javascriptFiles = fs | ||
| .readdirSync(distDirectory) | ||
| .filter((fileName) => fileName.endsWith(".js")) | ||
| .sort(); | ||
|
|
||
| if (javascriptFiles.length === 0) { | ||
| console.error(`No generated JavaScript found in ${distDirectory}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const forbiddenDynamicCode = [ | ||
| ["eval()", /\beval\s*\(/], | ||
| ["Function constructor", /\b(?:new\s+)?Function\s*\(/], | ||
| ["Emscripten Function constructor", /\bnewFunc\s*\(\s*Function\s*,/], | ||
| ]; | ||
|
sedghi marked this conversation as resolved.
|
||
|
|
||
| const violations = []; | ||
|
|
||
| for (const fileName of javascriptFiles) { | ||
| const source = fs.readFileSync(path.join(distDirectory, fileName), "utf8"); | ||
|
|
||
| for (const [description, pattern] of forbiddenDynamicCode) { | ||
| if (pattern.test(source)) { | ||
| violations.push(`${fileName}: ${description}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (violations.length > 0) { | ||
| console.error("Generated codec JavaScript contains CSP-unsafe dynamic code:"); | ||
| violations.forEach((violation) => console.error(`- ${violation}`)); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`CSP-safe generated JavaScript: ${javascriptFiles.length} file(s) checked`); | ||
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 { afterEach, describe, expect, it } from "vitest" | ||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs" | ||
| import { tmpdir } from "node:os" | ||
| import { join } from "node:path" | ||
| import { fileURLToPath } from "node:url" | ||
| import { spawnSync } from "node:child_process" | ||
|
|
||
| const checkerPath = fileURLToPath( | ||
| new URL("./check-generated-js.js", import.meta.url) | ||
| ) | ||
| const temporaryDirectories = [] | ||
|
|
||
| afterEach(() => { | ||
| for (const directory of temporaryDirectories.splice(0)) { | ||
| rmSync(directory, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| function runChecker(source) { | ||
| const directory = mkdtempSync(join(tmpdir(), "codec-csp-check-")) | ||
| temporaryDirectories.push(directory) | ||
| writeFileSync(join(directory, "generated.js"), source) | ||
|
|
||
| return spawnSync(process.execPath, [checkerPath, directory], { | ||
| encoding: "utf8", | ||
| }) | ||
| } | ||
|
|
||
| describe("generated JavaScript CSP checker", () => { | ||
| it.each([ | ||
| ["eval()", 'eval("dynamic code")', "eval()"], | ||
| ["Function()", 'Function("return 1")', "Function constructor"], | ||
| ["new Function()", 'new Function("return 1")', "Function constructor"], | ||
| [ | ||
| "Emscripten Function adapter", | ||
| 'newFunc(Function, "arg", "return arg")', | ||
| "Emscripten Function constructor", | ||
| ], | ||
| ])("rejects %s", (_name, source, expectedViolation) => { | ||
| const result = runChecker(source) | ||
|
|
||
| expect(result.status).toBe(1) | ||
| expect(result.stderr).toContain(expectedViolation) | ||
| }) | ||
|
|
||
| it("accepts generated JavaScript without dynamic code", () => { | ||
| const result = runChecker("const add = (left, right) => left + right") | ||
|
|
||
| expect(result.status).toBe(0) | ||
| expect(result.stdout).toContain("CSP-safe generated JavaScript: 1 file(s) checked") | ||
| }) | ||
| }) |
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,8 @@ | ||
| import { defineConfig } from "vitest/config" | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| name: "csp", | ||
| include: ["*.test.js"], | ||
| }, | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.