Repository navigation
fix(security): patch RSA verification and glob nesting vulnerabilities #2918
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Temporary npm security backports | ||
|
|
||
| The UI and documentation postinstall hooks run `patch-security-dependencies.mjs`. | ||
| The UI Docker dependency stage copies this script before installing packages. | ||
|
|
||
| - `braces@3.0.3`: limit parser nesting to 100 brace or parenthesis blocks. This | ||
| bounds the AST consumed by recursive compilation, expansion and stringification. | ||
| Ordinary patterns remain compatible; excessive nesting throws `SyntaxError`. | ||
| Advisory: [GHSA-vfj7-8cjw-p6xm](https://github.com/advisories/GHSA-vfj7-8cjw-p6xm). | ||
| Upstream discussion: [micromatch/braces#70](https://github.com/micromatch/braces/issues/70). | ||
| - `node-forge@1.4.0`: validate the exact number of nested DigestAlgorithm elements | ||
| when verifying RSA signatures, including optional algorithm parameters. | ||
| Advisory: [GHSA-86w9-cpqp-85rv](https://github.com/advisories/GHSA-86w9-cpqp-85rv). | ||
| Backport source: [digitalbazaar/forge#1152](https://github.com/digitalbazaar/forge/pull/1152). | ||
|
|
||
| Each installed copy listed in the npm lockfile is patched. Unexpected versions | ||
| or source contents fail installation. Repeated runs verify the existing patch. | ||
| Do not use `--ignore-scripts` for production installations. | ||
|
|
||
| Run from `ui/` and `docs/` after `npm ci`: | ||
|
|
||
| ```sh | ||
| node --test ../scripts/npm/*.test.mjs | ||
| ``` | ||
|
|
||
| Remove a backport and install a verified fixed version when upstream publishes | ||
| it. Lockfile scanners continue reporting the published version; these hooks | ||
| mitigate the installed code without concealing findings or altering versions. |
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 { existsSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { pathToFileURL } from "node:url"; | ||
|
|
||
| export const braceBefore = " stack.push(block);"; | ||
| export const braceAfter = ` if (stack.length >= 101) { | ||
| throw new SyntaxError('Pattern nesting exceeds maximum depth (100)'); | ||
| } | ||
| stack.push(block);`; | ||
| export const forgeBefore = " obj.value.length !== 2) {"; | ||
| export const forgeAfter = ` obj.value.length !== 2 || | ||
| obj.value[0].value.length !== | ||
| (('parameters' in capture) ? 2 : 1)) {`; | ||
|
|
||
| export function patchSource(source, before, after, expected, label) { | ||
| // Recognize the complete patched form before searching its shared suffix. | ||
| if (source.split(after).length - 1 === expected && | ||
| !source.split(after).join("").includes(before)) return source; | ||
| if (source.includes(after) || source.split(before).length - 1 !== expected) { | ||
| throw new Error(`Unexpected ${label} source; review the security backport`); | ||
| } | ||
| return source.split(before).join(after); | ||
| } | ||
|
|
||
| export function patchDependencies(directory = process.cwd()) { | ||
| const lock = JSON.parse(readFileSync(resolve(directory, "package-lock.json"), "utf8")); | ||
| const patches = { | ||
| braces: { version: "3.0.3", file: "lib/parse.js", before: braceBefore, after: braceAfter, count: 2 }, | ||
| "node-forge": { version: "1.4.0", file: "lib/rsa.js", before: forgeBefore, after: forgeAfter, count: 1 }, | ||
| }; | ||
| for (const [packagePath, metadata] of Object.entries(lock.packages)) { | ||
| const name = packagePath.split("node_modules/").at(-1); | ||
| const patch = patches[name]; | ||
| if (!patch || !existsSync(resolve(directory, packagePath))) continue; | ||
| if (metadata.version !== patch.version) { | ||
| throw new Error(`Review ${name} ${metadata.version} and remove or update its security backport`); | ||
| } | ||
| const installed = JSON.parse(readFileSync(resolve(directory, packagePath, "package.json"), "utf8")); | ||
| if (installed.name !== name || installed.version !== patch.version) { | ||
| throw new Error(`Unexpected installed ${name} version`); | ||
| } | ||
| const file = resolve(directory, packagePath, patch.file); | ||
| const source = readFileSync(file, "utf8"); | ||
| const result = patchSource(source, patch.before, patch.after, patch.count, name); | ||
| if (result !== source) writeFileSync(file, result); | ||
| console.log(`Security backport verified: ${packagePath}`); | ||
| } | ||
| } | ||
|
|
||
| if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { | ||
| patchDependencies(); | ||
| } |
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,65 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import test from "node:test"; | ||
|
|
||
| const root = resolve(import.meta.dirname, "../.."); | ||
| const workflow = name => readFileSync(resolve(root, ".github/workflows", name), "utf8"); | ||
| const automatic = workflow("prebuild-caipe-ui.yml"); | ||
| const manual = workflow("prebuild-manual.yml"); | ||
| const block = manual.match(/^ {10}script: \|\n((?: {12}[^\n]*\n|\n)+)/m); | ||
| assert.ok(block, "manual prebuild metadata script must exist"); | ||
| const script = block[1].split("\n").map(line => line.slice(12)).join("\n"); | ||
| const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; | ||
| const resolvePr = new AsyncFunction("process", "context", "github", "core", script); | ||
|
|
||
| async function manualEligibility(file, target) { | ||
| const outputs = {}; | ||
| await resolvePr( | ||
| { env: { PR_NUMBER: "1", TARGET: target } }, | ||
| { repo: { owner: "example", repo: "example" }, actor: "test-user" }, | ||
| { | ||
| rest: { | ||
| repos: { getCollaboratorPermissionLevel: async () => ({ data: { permission: "write" } }) }, | ||
| pulls: { | ||
| get: async () => ({ data: { | ||
| number: 1, state: "open", | ||
| head: { repo: { full_name: "example/example" }, ref: "prebuild/example", sha: "head" }, | ||
| base: { ref: "main", sha: "base" }, | ||
| } }), | ||
| listFiles: () => {}, | ||
| }, | ||
| }, | ||
| paginate: async () => [{ filename: file }], | ||
| }, | ||
| { setOutput: (key, value) => { outputs[key] = value; }, info: () => {}, setFailed: assert.fail }, | ||
| ); | ||
| return outputs.caipe_ui_changed; | ||
| } | ||
|
|
||
| test("automatic prebuild trigger and inner eligibility include shared npm scripts", () => { | ||
| assert.match(automatic, /- 'scripts\/npm\/\*\*'/); | ||
| const comparison = automatic.match(/if git diff --name-only[^]*?\| grep -q \.; then/); | ||
| assert.ok(comparison, "prebuild changed-file comparison must exist"); | ||
| assert.match(comparison[0], /^\s+scripts\/npm\s+\\$/m); | ||
| }); | ||
|
|
||
| for (const target of ["all", "docker", "caipe-ui"]) { | ||
| test(`manual ${target} prebuild selects UI for a scripts-only change`, async () => { | ||
| assert.equal(await manualEligibility("scripts/npm/example.mjs", target), "true"); | ||
| assert.equal(await manualEligibility("scripts/npm/nested/example.mjs", target), "true"); | ||
| }); | ||
| } | ||
|
|
||
| for (const file of ["docs/example.md", "scripts/example.mjs", "scripts/npm-example/example.mjs"]) { | ||
| test(`manual prebuild excludes unrelated ${file} changes from UI selection`, async () => { | ||
| assert.equal(await manualEligibility(file, "all"), "false"); | ||
| }); | ||
| } | ||
|
|
||
| test("UI retains the RBAC regression command alongside the security postinstall hook", () => { | ||
| const { scripts } = JSON.parse(readFileSync(resolve(root, "ui/package.json"), "utf8")); | ||
| assert.match(scripts["test:e2e:rbac-regression"], /^WORKFLOWS_ENABLED=true playwright test /); | ||
| assert.match(scripts["test:e2e:rbac-regression"], /--config=playwright\.rbac\.config\.ts$/); | ||
| assert.match(scripts.postinstall, /patch-security-dependencies\.mjs/); | ||
| }); |
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,65 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { createRequire } from "node:module"; | ||
| import { resolve } from "node:path"; | ||
| import test from "node:test"; | ||
| import { braceBefore, braceAfter, forgeBefore, forgeAfter, patchSource } from "./patch-security-dependencies.mjs"; | ||
|
|
||
| const require = createRequire(resolve(process.cwd(), "package.json")); | ||
| const braces = require("braces"); | ||
|
|
||
| test("normal brace expansion and matching remain compatible", () => { | ||
| assert.deepEqual(braces.expand("{primary,secondary}/{1..3}"), | ||
| ["primary/1", "primary/2", "primary/3", "secondary/1", "secondary/2", "secondary/3"]); | ||
| assert.match("primary/2", new RegExp(braces.compile("{primary,secondary}/{1..3}"))); | ||
| assert.deepEqual(braces.expand("\\{literal\\}"), ["{literal}"]); | ||
| }); | ||
|
|
||
| for (const [open, close] of [["{", "}"], ["(", ")"], ["{(", ")}"]]) { | ||
| for (const operation of ["parse", "compile", "expand"]) { | ||
| test(`${operation} rejects excessive ${open} nesting before recursive traversal`, () => { | ||
| const pattern = open.repeat(3500) + "example" + close.repeat(3500); | ||
| // Some inputs exceed the length limit; both guards must fail with SyntaxError. | ||
| assert.throws(() => braces[operation](pattern), SyntaxError); | ||
| const bounded = open.repeat(101) + "example" + close.repeat(101); | ||
| assert.throws(() => braces[operation](bounded), /maximum depth/); | ||
| assert.doesNotThrow(() => braces[operation](open.repeat(10) + "example" + close.repeat(10))); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| test("patching is idempotent and rejects partial patches or source drift", () => { | ||
| for (const [before, after, count] of [[braceBefore, braceAfter, 2], [forgeBefore, forgeAfter, 1]]) { | ||
| const original = Array(count).fill(before).join("\n"); | ||
| const patched = patchSource(original, before, after, count, "example"); | ||
| assert.equal(patchSource(patched, before, after, count, "example"), patched); | ||
| assert.throws(() => patchSource("unexpected source", before, after, count, "example")); | ||
| assert.throws(() => patchSource(patched + before, before, after, count, "example")); | ||
| } | ||
| }); | ||
|
|
||
| let forge; | ||
| try { forge = require("node-forge"); } catch (error) { | ||
| if (error.code !== "MODULE_NOT_FOUND") throw error; | ||
| } | ||
|
|
||
| test("RSA verifies valid signatures and rejects extra DigestAlgorithm elements", { skip: !forge }, () => { | ||
| const { publicKey, privateKey } = forge.pki.rsa.generateKeyPair({ bits: 1024, e: 65537 }); | ||
| const hash = forge.md.sha256.create().update("example message"); | ||
| const digest = hash.digest().getBytes(); | ||
| assert.equal(publicKey.verify(digest, privateKey.sign(hash)), true); | ||
| const { Class, Type, create, oidToDer, toDer } = forge.asn1; | ||
| const sequence = values => create(Class.UNIVERSAL, Type.SEQUENCE, true, values); | ||
| const oid = create(Class.UNIVERSAL, Type.OID, false, oidToDer(forge.pki.oids.sha256).getBytes()); | ||
| const nil = create(Class.UNIVERSAL, Type.NULL, false, ""); | ||
| const extra = create(Class.UNIVERSAL, Type.OCTETSTRING, false, "unexpected"); | ||
| const digestValue = create(Class.UNIVERSAL, Type.OCTETSTRING, false, digest); | ||
| for (const algorithm of [[oid], [oid, nil]]) { | ||
| const encoded = toDer(sequence([sequence(algorithm), digestValue])).getBytes(); | ||
| assert.equal(publicKey.verify(digest, privateKey.sign(encoded, "NONE")), true); | ||
| } | ||
| for (const algorithm of [[oid, nil, extra], [oid, nil, nil]]) { | ||
| const encoded = toDer(sequence([sequence(algorithm), digestValue])).getBytes(); | ||
| const signature = privateKey.sign(encoded, "NONE"); | ||
| assert.throws(() => publicKey.verify(digest, signature), /valid RSASSA-PKCS1-v1_5/); | ||
| } | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.