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
23 changes: 15 additions & 8 deletions apps/smart-ssh-community/frontend/src/licenseCompatCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ describe("check-license-compat", () => {
["quoted exceptions", "licenses.'exceptions' = [{ crate = \"x\", allow = [\"GPL-2.0\"] }]\n", /quoted key/],
["spaced dots", 'licenses . "exceptions" = [{ crate = "x", allow = ["GPL-2.0"] }]\n', /quoted key/],
["inline table", 'licenses = { exceptions = [{ crate = "x", allow = ["GPL-2.0"] }] }\n', /inline table/],
["escaped exceptions key", 'licenses."exc\\u0065ptions" = [{ crate = "x", allow = ["GPL-2.0"] }]\n', /quoted key with escape sequence/],
["escaped exceptions key with quoted licenses", '"licenses"."exc\\u0065ptions" = [{ crate = "x", allow = ["GPL-2.0"] }]\n', /quoted key with escape sequence/],
["escaped array of tables", '[[licenses."exc\\u0065ptions"]]\ncrate = "x"\n', /quoted key with escape sequence/],
["escaped key in inline table", 'licenses = { "exc\\u0065ptions" = [{ crate = "x", allow = ["GPL-2.0"] }] }\n', /quoted key with escape sequence/],
];

it.each(FORMS)("rejects %s, also with comment and CRLF", (_n, body, re) => {
Expand All @@ -158,18 +162,21 @@ describe("check-license-compat", () => {
}
});

it(
"check-license-compat exits non-zero naming the form",
() => {
for (const [, body, re] of FORMS) {
const bad = spawnSync(process.execPath, [SCRIPT, raw(body)], { encoding: "utf8" });
expect(bad.status).not.toBe(0);
expect(bad.stderr).toMatch(re);
}
it.each(FORMS)(
"check-license-compat exits non-zero naming the form: %s",
(_n, body, re) => {
const bad = spawnSync(process.execPath, [SCRIPT, raw(body)], { encoding: "utf8" });
expect(bad.status).not.toBe(0);
expect(bad.stderr).toMatch(re);
},
30_000,
);

it("accepts a value string containing a backslash", () => {
const body = 'exceptions = [\n { crate = "x", allow = ["MIT"] },\n]\nnote = "a\\\\b \\u0041"\n';
expect(parseExceptions(raw(body))).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
42 changes: 42 additions & 0 deletions scripts/generate-third-party-notices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ function parseAllowList(licensesFilePath) {
return entries;
}

// True when a basic-string key ("…" containing a backslash) appears in key
// position: before `=`/`.` or, in a table header, before `]`/`.`. Value
// strings with backslashes are not keys and stay accepted. Fail closed:
// escaped spellings are not decoded, just rejected.
function hasEscapedQuotedKey(line) {
const isHeader = /^\s*\[/.test(line);
let i = 0;
while (i < line.length) {
const c = line[i];
if (c === "'") {
const e = line.indexOf("'", i + 1);
if (e < 0) return false;
i = e + 1;
} else if (c === '"') {
let j = i + 1;
let escaped = false;
while (j < line.length && line[j] !== '"') {
if (line[j] === "\\") {
escaped = true;
j++;
}
j++;
}
if (escaped) {
const next = line.slice(j + 1).match(/^\s*(.)/)?.[1];
if (next === "=" || next === "." || (isHeader && next === "]")) return true;
}
i = j + 1;
} else {
i++;
}
}
return false;
}

// Per-Crate-Ausnahmen aus `[licenses] exceptions = [{ crate, allow }]`
// (ADR 0135). cargo-about kennt sie als `[<crate>] accepted = [...]`.
function parseExceptions(licensesFilePath) {
Expand All @@ -158,6 +193,13 @@ function parseExceptions(licensesFilePath) {
// General rule: any line using `exceptions` as a key (bare or quoted) other
// than the supported `exceptions = [` block is rejected, so future spellings
// fail closed too.
for (const line of content.split("\n")) {
if (hasEscapedQuotedKey(line)) {
fail(
`Nicht unterstützte Schreibweise der Ausnahmen in ${licensesFilePath} (quoted key with escape sequence): ${line.trim()} — bitte den Block "exceptions = [ … ]" unter [licenses] verwenden.`,
);
}
}
const keyRe = /(^|[\s.{,\[])(["']?)exceptions\2\s*(=|\]|\.|$)/;
for (const line of content.split("\n")) {
if (/^[ \t]*exceptions[ \t]*=[ \t]*\[/.test(line)) continue;
Expand Down
Loading