Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] },
Expand Down
29 changes: 27 additions & 2 deletions scripts/generate-third-party-notices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,37 @@ 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 `[<crate>] accepted = [...]`.
function parseExceptions(licensesFilePath) {
// Strip comments first, so commented-out entries are not counted as active.
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.
Expand Down Expand Up @@ -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}`,
Expand Down
Loading