fix(desktop): preserve APT one-line source token boundaries (#12692)

fix(desktop): honor APT one-line separators and comments
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 02:15:18 +00:00
1 parent ddd31b0289
commit 60707abd94
2 files changed
+53 -7

No files matched your search

@@ -175,6 +175,36 @@ describe("parseOneLineSources", () => {
}),
]);
});
it("preserves non-ASCII whitespace inside and at the edges of APT list words", () => {
const uri = "https://vendor.invalid/edge\u00a0embedded\u00a0";
const suite = "\u00a0stable";
const component = "main\u00a0";
expect(
parseOneLineSources(`deb\t${uri} ${suite} ${component}\n`, TYPORA_LIST),
).toMatchObject([
{
uris: [{ uri, credentials: false }],
suites: [suite],
components: [component],
},
]);
});
it("keeps a hash inside an option and strips the trailing comment", () => {
expect(
parseOneLineSources(
"deb [signed-by=/usr/share/keyrings/vendor#1.gpg] https://vendor.invalid/archive stable main # extra",
TYPORA_LIST,
),
).toMatchObject([
{
uris: [{ uri: "https://vendor.invalid/archive", credentials: false }],
suites: ["stable"],
components: ["main"],
signedBy: ["/usr/share/keyrings/vendor#1.gpg"],
},
]);
});
});
describe("parseDeb822Sources", () => {
+23 -7
View File
@@ -170,7 +170,20 @@ function* sourceLines(text: string | Buffer): Generator<string | undefined> {
}
const ONE_LINE =
/^deb(?:\s+\[(?<options>[^\]]*)\])?\s+(?<uri>\S+)\s+(?<suite>\S+)(?<components>(?:\s+\S+)*)\s*$/u;
/^[ \t\r]*deb[ \t\v][ \t\v\f\r]*(?:\[(?<options>[^\]]*)\][ \t\v\f\r]+)?(?<fields>.*)$/su;
/** APT cuts a one-line comment only outside unmatched square brackets. */
const withoutOneLineComment = (line: string): string => {
if (!line.includes("#")) return line;
let brackets = 0;
for (let index = 0; index < line.length; index += 1) {
const character = line[index];
if (character === "[") brackets += 1;
else if (character === "]") brackets -= 1;
else if (character === "#" && brackets <= 0) return line.slice(0, index);
}
return line;
};
/** The `deb` lines of a one-line `sources.list` file; `deb-src` lines and comments are skipped. */
export const parseOneLineSources = (
@@ -179,10 +192,13 @@ export const parseOneLineSources = (
): readonly AptSourceStanza[] =>
[...sourceLines(text)].flatMap((line, index) => {
if (line === undefined) return [];
const groups = ONE_LINE.exec(line.replace(/#.*$/u, "").trim())?.groups;
if (groups?.uri === undefined || groups.suite === undefined) return [];
const groups = ONE_LINE.exec(withoutOneLineComment(line))?.groups;
if (groups === undefined) return [];
const [uri, suite, ...components] = fieldValues(groups.fields);
if (uri === undefined || suite === undefined || uri.startsWith("["))
return [];
const options = new Map(
(groups.options ?? "").split(/\s+/u).flatMap((option) => {
fieldValues(groups.options).flatMap((option) => {
const equals = option.indexOf("=");
return equals > 0
? [[option.slice(0, equals), option.slice(equals + 1)] as const]
@@ -193,9 +209,9 @@ export const parseOneLineSources = (
stanza(
{ file, position: index + 1 },
{
uris: [groups.uri],
suites: [groups.suite],
components: fieldValues(groups.components),
uris: [uri],
suites: [suite],
components,
options,
oneLine: true,
},