diff --git a/src/domain/desktop/aptRepositories.test.ts b/src/domain/desktop/aptRepositories.test.ts index 45b81b48d4..8f87f54da8 100644 --- a/src/domain/desktop/aptRepositories.test.ts +++ b/src/domain/desktop/aptRepositories.test.ts @@ -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", () => { diff --git a/src/domain/desktop/aptRepositories.ts b/src/domain/desktop/aptRepositories.ts index 7cb9c61b74..d6d99686cb 100644 --- a/src/domain/desktop/aptRepositories.ts +++ b/src/domain/desktop/aptRepositories.ts @@ -170,7 +170,20 @@ function* sourceLines(text: string | Buffer): Generator { } const ONE_LINE = - /^deb(?:\s+\[(?[^\]]*)\])?\s+(?\S+)\s+(?\S+)(?(?:\s+\S+)*)\s*$/u; + /^[ \t\r]*deb[ \t\v][ \t\v\f\r]*(?:\[(?[^\]]*)\][ \t\v\f\r]+)?(?.*)$/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, },