diff --git a/apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts b/apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts index e7c89b0bb..5fa3b852a 100644 --- a/apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts +++ b/apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts @@ -177,6 +177,21 @@ describe("check-license-compat", () => { expect(parseExceptions(raw(body))).toEqual([{ crate: "x", allow: ["MIT"] }]); }); + it("does not let a '#' inside a string hide an exceptions form (#1601)", () => { + const body = 'licenses = { allow = ["#"], exceptions = [{ crate = "x", allow = ["GPL-2.0"] }] }\n'; + expect(() => parseExceptions(raw(body))).toThrow(/inline table/); + expect(() => parseExceptions(raw(body.replace(/\n/g, "\r\n")))).toThrow(/inline table/); + expect(() => parseExceptions(raw("licenses = { allow = ['#'], exceptions = [] }\n"))).toThrow(/inline table/); + expect(() => parseExceptions(raw('licenses = { allow = ["a\\"#"], "exceptions" = [] }\n'))).toThrow(); + }); + + it("still strips real trailing comments after strings (#1601)", () => { + expect(parseExceptions(raw('allow = ["#"] # exceptions = [{ crate = "x", allow = ["GPL-2.0"] }]\n'))).toEqual([]); + expect( + parseExceptions(raw('exceptions = [\n { crate = "x", allow = ["MIT"] }, # "q\n]\n')), + ).toEqual([{ crate: "x", allow: ["MIT"] }]); + }); + it("returns the sqlcipher-src entry of the repo deny.toml", () => { expect(parseExceptions(path.resolve(__dirname, "../../../../deny.toml"))).toEqual([ { crate: "sqlcipher-src", allow: ["blessing", "WTFPL"] }, diff --git a/scripts/generate-third-party-notices.mjs b/scripts/generate-third-party-notices.mjs index aece22d3e..98c9b27c2 100644 --- a/scripts/generate-third-party-notices.mjs +++ b/scripts/generate-third-party-notices.mjs @@ -179,6 +179,29 @@ function hasEscapedQuotedKey(line) { return false; } +// Removes a trailing comment: a `#` only starts one outside a basic string +// ("…" with backslash escapes) and a literal string ('…'). +function stripTomlComment(line) { + let i = 0; + while (i < line.length) { + const c = line[i]; + if (c === "#") return line.slice(0, i); + if (c === "'") { + const e = line.indexOf("'", i + 1); + if (e < 0) return line; + i = e + 1; + } else if (c === '"') { + let j = i + 1; + while (j < line.length && line[j] !== '"') j += line[j] === "\\" ? 2 : 1; + if (j >= line.length) return line; + i = j + 1; + } else { + i++; + } + } + return line; +} + // Per-Crate-Ausnahmen aus `[licenses] exceptions = [{ crate, allow }]` // (ADR 0135). cargo-about kennt sie als `[] accepted = [...]`. function parseExceptions(licensesFilePath) { @@ -186,7 +209,7 @@ function parseExceptions(licensesFilePath) { const content = fs .readFileSync(licensesFilePath, "utf8") .split(/\r?\n/) - .map((l) => l.replace(/#.*$/, "")) + .map(stripTomlComment) .join("\n"); // Other valid TOML spellings of the same data are rejected (fail closed): // the repo uses the `exceptions = [ … ]` block only. @@ -246,9 +269,11 @@ function parseExceptions(licensesFilePath) { } out.push({ crate: m[1], allow }); } + // Comments are already stripped above (string-aware), so a `#` left here + // sits inside a string and must not be dropped. // Anything left over (besides comments, commas, whitespace) is an entry the // parser does not understand: fail instead of silently skipping it. - const rest = block[1].replace(entryRe, "").replace(/#[^\n]*/g, "").trim(); + const rest = block[1].replace(entryRe, "").trim(); if (rest.replace(/[\s,]+/g, "") !== "") { fail( `Nicht lesbare Ausnahme in ${licensesFilePath} (erwartet: { crate = "…", allow = ["…"] }): ${rest}`,