diff --git a/docs/specs/desktop-app-environments/vendor-deb-files/spec.md b/docs/specs/desktop-app-environments/vendor-deb-files/spec.md index d87d2c9c0e..c6c198a17d 100644 --- a/docs/specs/desktop-app-environments/vendor-deb-files/spec.md +++ b/docs/specs/desktop-app-environments/vendor-deb-files/spec.md @@ -25,8 +25,9 @@ capture nor the bundle holds the package. entries of each relationship field as the file declares them. Otherwise `package_file_reason` says why: dpkg reports no installed version of the application, the path is not a regular file, `dpkg-deb` is missing or cannot - read it, it lacks one of the three fields, it holds another package, version - or architecture, or it could not be read. A descriptor whose close fails + read it, the reported fields contain invalid UTF-8 or lack one of the three + identity fields, it holds another package, version or architecture, or it + could not be read. A descriptor whose close fails fails the capture with its owner. The file is never copied into the capture folder. - Library packages are walked as for a vendor repository's package also for a diff --git a/src/desktop/DebianPackageFile.test.ts b/src/desktop/DebianPackageFile.test.ts index 4b1a33da3a..df6e513e87 100644 --- a/src/desktop/DebianPackageFile.test.ts +++ b/src/desktop/DebianPackageFile.test.ts @@ -34,9 +34,11 @@ const inspect = async ( const facts = await inspectPackageFile( path, installed, - async (command, argv) => { + async (command, argv, _signal, options) => { calls.push([command, ...argv]); - return reply; + return typeof reply === "object" && !options?.preserveStdoutBytes + ? { stdout: reply.stdout, exitCode: reply.exitCode } + : reply; }, undefined, ); @@ -76,6 +78,32 @@ describe("inspectPackageFile", () => { }); }); + it("withholds package-file evidence for undecodable control fields", async () => { + const stdoutBuffer = Buffer.concat([ + Buffer.from(DPKG_DEB_OUTPUT.trimEnd()), + Buffer.from([0xff, 0x0a]), + ]); + const invalid = await inspect({ + stdout: stdoutBuffer.toString("utf8"), + stdoutBuffer, + exitCode: 0, + }); + expect(invalid.facts).toEqual({ + package_file_reason: `dpkg-deb returned invalid UTF-8 in the control fields of ${invalid.path}`, + }); + + const valid = await inspect({ + stdout: "", + stdoutBuffer: Buffer.from(DPKG_DEB_OUTPUT), + exitCode: 0, + }); + expect(valid.facts.package_file).toMatchObject({ + package: INSTALLED.name, + version: INSTALLED.version, + architecture: INSTALLED.architecture, + }); + }); + it("records why instead when the file holds another version", async () => { const { path, facts } = await inspect( { stdout: DPKG_DEB_OUTPUT, exitCode: 0 }, diff --git a/src/desktop/DebianPackageFile.ts b/src/desktop/DebianPackageFile.ts index e6fafe21f7..338d610c62 100644 --- a/src/desktop/DebianPackageFile.ts +++ b/src/desktop/DebianPackageFile.ts @@ -26,7 +26,7 @@ import { hostFileKind, isRefusedRead, } from "./HostFileRead.js"; -import type { QueryRunner } from "./HostQuery.js"; +import { queryUtf8Text, type QueryRunner } from "./HostQuery.js"; import { errorMessage } from "../kernel/errorMessage.js"; type PackageFileFacts = Pick< @@ -103,18 +103,23 @@ export const inspectPackageFile = async ( // dpkg-deb and the hash read the file apart; its identity must hold across both. const before = await stat(path).catch(() => undefined); if (before === undefined) return refused(`${path} could not be read`); - const outcome = await runQuery( + const outcome = await queryUtf8Text( + runQuery, "dpkg-deb", ["--field", path, ...PACKAGE_FILE_FIELDS], signal, ); - if (outcome === "missing") + if (outcome.kind === "missing") return refused( `dpkg-deb is not installed, so the control fields of ${path} were not read`, ); - if (outcome === "failed" || outcome.exitCode !== 0) + if (outcome.kind === "undecodable") + return refused( + `dpkg-deb returned invalid UTF-8 in the control fields of ${path}`, + ); + if (outcome.kind !== "read") return refused(`dpkg-deb could not read ${path} as a Debian package`); - const fields = parseDpkgDebFields(outcome.stdout); + const fields = parseDpkgDebFields(outcome.text); if (fields === undefined) return refused( `dpkg-deb did not report the Package, Version and Architecture of ${path}`, diff --git a/src/desktop/HostQuery.ts b/src/desktop/HostQuery.ts index 98c633753f..ba00fe27bf 100644 --- a/src/desktop/HostQuery.ts +++ b/src/desktop/HostQuery.ts @@ -76,11 +76,13 @@ export const queryUtf8Text = async ( args: readonly string[], signal: AbortSignal | undefined, ): Promise< - { kind: "read"; text: string } | { kind: "undecodable" | "unavailable" } + | { kind: "read"; text: string } + | { kind: "missing" | "undecodable" | "unavailable" } > => { const outcome = await runQuery(command, args, signal, { preserveStdoutBytes: true, }); + if (outcome === "missing") return { kind: "missing" }; if (typeof outcome !== "object" || outcome.exitCode !== 0) return { kind: "unavailable" }; if (outcome.stdoutBuffer !== undefined && !isStrictUtf8(outcome.stdoutBuffer))