diff --git a/src/desktop/LinuxApplicationInventory.test.ts b/src/desktop/LinuxApplicationInventory.test.ts index 7e07779a41..f970c541ef 100644 --- a/src/desktop/LinuxApplicationInventory.test.ts +++ b/src/desktop/LinuxApplicationInventory.test.ts @@ -255,6 +255,8 @@ describe.skipIf(process.platform === "win32")( { before: "[Other]\nName=Other\n", extra: "", invalidLine: 1 }, { header: "\u00a0[Desktop Entry]", invalidLine: 1 }, { header: "[Desktop Entry]\u00a0", invalidLine: 1 }, + { before: "", extra: "\u00a0Name=Injected\n", invalidLine: 3 }, + { before: "", extra: "\u00a0\n", invalidLine: 3 }, { before: "", extra: "", diff --git a/src/domain/desktop/desktopEntry.test.ts b/src/domain/desktop/desktopEntry.test.ts index 04a073c06b..298d43a02c 100644 --- a/src/domain/desktop/desktopEntry.test.ts +++ b/src/domain/desktop/desktopEntry.test.ts @@ -113,6 +113,43 @@ Terminal=true }); }, ); + it.each(["\u00a0", "\u3000", "\ufeff"])( + "refuses a key indented with %j, which GLib keeps inside the key", + (padding) => { + const parsed = parseDesktopEntry( + `[Desktop Entry]\nType=Application\n${padding}Name=Injected\n`, + ); + expect(parsed).toMatchObject({ + ok: true, + value: { name: null, malformed_line: 3 }, + }); + }, + ); + + it.each(["\u00a0", "\u3000", "\ufeff"])( + "does not read a %j line as a blank or a comment", + (padding) => { + const blank = parseDesktopEntry( + `[Desktop Entry]\nType=Application\nName=Notes\n${padding}\n`, + ); + expect(blank).toMatchObject({ ok: true, value: { malformed_line: 4 } }); + const comment = parseDesktopEntry( + `[Desktop Entry]\nType=Application\nName=Notes\n${padding}# Hidden=true\n`, + ); + expect(comment).toMatchObject({ ok: true, value: { malformed_line: 4 } }); + }, + ); + + it("keeps accepting keys indented with ASCII spaces and tabs", () => { + const parsed = parseDesktopEntry( + "[Desktop Entry]\nType=Application\n \tName=Notes\n Exec=notes\n \t # note\n", + ); + expect(parsed).toMatchObject({ + ok: true, + value: { name: "Notes", exec: "notes" }, + }); + expect(parsed.ok && parsed.value).not.toHaveProperty("malformed_line"); + }); }); describe("tokenizeDesktopExec", () => { diff --git a/src/domain/desktop/desktopEntry.ts b/src/domain/desktop/desktopEntry.ts index 0fb7e711ad..52a942d745 100644 --- a/src/domain/desktop/desktopEntry.ts +++ b/src/domain/desktop/desktopEntry.ts @@ -36,6 +36,10 @@ export interface DesktopEntry { const GROUP = new RegExp(String.raw`^(\s*)${DESKTOP_GROUP}(\s*)$`, "u"); const GLIB_GROUP_LEADING_SPACE = /^[ \t\f\r]*$/u; const GLIB_GROUP_TRAILING_SPACE = /^[ \t]*$/u; +// GLib skips only its ASCII whitespace before a key or a comment. A record that +// starts with any other Unicode space keeps that space in the key, so the line +// is malformed rather than parsed with the space removed. +const GLIB_LINE_LEADING_SPACE = /^[ \t\f\r]*/u; const INVALID_GROUP_NAME_CHARACTER = /[[\]\u0000-\u001f\u007f]/u; const LINE_WITH_SEPARATOR = new RegExp(`(${DESKTOP_LINE_BREAK})`, "u"); // Only LF separates records; Unicode line/paragraph separators are value data. @@ -124,7 +128,7 @@ const readDesktopEntryFields = (text: string) => { let malformedLine: number | undefined; for (let index = 0; index < parts.length; index += 2) { const raw = parts[index]!; - const line = raw.trim(); + const line = raw.replace(GLIB_LINE_LEADING_SPACE, ""); if (line.length === 0 || line.startsWith("#")) continue; const [, leading = "", header, trailing = ""] = GROUP.exec(raw) ?? []; if (header !== undefined) { @@ -141,7 +145,7 @@ const readDesktopEntryFields = (text: string) => { continue; } // Full-line trimming is syntax-only: string-value suffixes are data. - const entry = ENTRY.exec(raw.trimStart()); + const entry = ENTRY.exec(line); if (group === undefined || entry?.groups === undefined) { malformedLine ??= index / 2 + 1; continue;