From e98a767428bf8b5fcdfa5c86d3d2021672334626 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 03:56:54 +0800 Subject: [PATCH] fix(desktop): exclude configured APT status from repository sources (#11932) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> --- src/desktop/AptRepositoryInventory.test.ts | 4 ++ src/desktop/AptRepositoryInventory.ts | 9 ++- src/desktop/DebianPackageInventory.test.ts | 62 +++++++++++++++- src/desktop/DebianPackageInventory.ts | 4 +- src/desktop/LibraryPackageInventory.ts | 9 ++- src/domain/desktop/linuxPackages.test.ts | 84 ++++++++++++---------- src/domain/desktop/linuxPackages.ts | 39 ++++++++-- 7 files changed, 160 insertions(+), 51 deletions(-) diff --git a/src/desktop/AptRepositoryInventory.test.ts b/src/desktop/AptRepositoryInventory.test.ts index 686e6f49d2..e619eb947b 100644 --- a/src/desktop/AptRepositoryInventory.test.ts +++ b/src/desktop/AptRepositoryInventory.test.ts @@ -349,6 +349,7 @@ describe.skipIf( lists: `${root}/user's\nlists/`, trustedParts: `${root}/trusted\nparts/`, trusted: `${root}/trusted\nkeys.gpg`, + status: `${root}/installed\nstatus`, }; const configured: QueryRunner = (command, argv, signal) => runHostQuery( @@ -360,6 +361,8 @@ describe.skipIf( `Dir::Etc::TrustedParts=${selected.trustedParts}`, "-o", `Dir::Etc::Trusted=${selected.trusted}`, + "-o", + `Dir::State::status=${selected.status}`, ...argv, ], signal, @@ -383,6 +386,7 @@ describe.skipIf( lists: `${root}/state/user lists/`, trustedParts: "/etc/apt/trusted.gpg.d/", trusted: "/etc/apt/trusted.gpg", + status: "/var/lib/dpkg/status", }); expect( await resolveAptLocations( diff --git a/src/desktop/AptRepositoryInventory.ts b/src/desktop/AptRepositoryInventory.ts index 1f162b2b53..c0295e1d08 100644 --- a/src/desktop/AptRepositoryInventory.ts +++ b/src/desktop/AptRepositoryInventory.ts @@ -14,6 +14,7 @@ import { type AptSourceStanza, } from "../domain/desktop/aptRepositories.js"; import type { AptRepository } from "../domain/desktop/desktopCapture.js"; +import { DEFAULT_DPKG_STATUS_SOURCE } from "../domain/desktop/linuxPackages.js"; import { withGpgvKeyCheck, type HeldKey, @@ -22,17 +23,20 @@ import { import { hostFileKind, readHostFile } from "./HostFileRead.js"; import { succeeded, type QueryRunner } from "./HostQuery.js"; -/** Where apt keeps its trusted keys and downloaded lists. */ +/** Where apt keeps trusted keys, downloaded lists and installed state. */ export interface AptLocations { readonly trustedParts: string; readonly trusted: string; readonly lists: string; + /** Omitted by injected hosts that use APT's default installed database. */ + readonly status?: string; } export const APT_LOCATIONS: AptLocations = { trustedParts: "/etc/apt/trusted.gpg.d", trusted: "/etc/apt/trusted.gpg", lists: "/var/lib/apt/lists", + status: DEFAULT_DPKG_STATUS_SOURCE, }; /** @@ -57,6 +61,8 @@ export const resolveAptLocations = async ( "Dir::Etc::TrustedParts/d", "TRUSTED", "Dir::Etc::Trusted/f", + "STATUS", + "Dir::State::status/f", ], signal, ), @@ -70,6 +76,7 @@ export const resolveAptLocations = async ( lists: value("LISTS") ?? fallback.lists, trustedParts: value("PARTS") ?? fallback.trustedParts, trusted: value("TRUSTED") ?? fallback.trusted, + status: value("STATUS") ?? fallback.status ?? DEFAULT_DPKG_STATUS_SOURCE, }; }; diff --git a/src/desktop/DebianPackageInventory.test.ts b/src/desktop/DebianPackageInventory.test.ts index 3a3ca5fb30..1f18a63242 100644 --- a/src/desktop/DebianPackageInventory.test.ts +++ b/src/desktop/DebianPackageInventory.test.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { existsSync } from "node:fs"; import { access, chmod, @@ -27,7 +28,11 @@ import { import { exportInstall } from "../domain/desktop/appExportInstall.js"; import { elfImage } from "../domain/desktop/elfImage.fixture.js"; import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; -import type { QueryOutcome, QueryRunner } from "./HostQuery.js"; +import { + runHostQuery, + type QueryOutcome, + type QueryRunner, +} from "./HostQuery.js"; import { inspectLinuxApplication } from "./LinuxApplicationInventory.js"; const NOBLE = "http://ports.ubuntu.com/ubuntu-ports noble/main arm64 Packages"; @@ -880,6 +885,61 @@ const writerFixture = async () => { return { root, runQuery, inspect }; }; +it + .skipIf( + process.platform !== "linux" || + !existsSync("/usr/bin/apt-cache") || + !existsSync("/usr/bin/apt-config"), + ) + .each(["installed status", "installed status ", "installed\nstatus"])( + "does not turn APT's configured database %j into a vendor repository", + async (name) => { + const { root, runQuery, inspect } = await writerFixture(); + const status = join(root, name); + const lists = join(root, "private-lists"); + await mkdir(lists); + await writeFile( + status, + `Package: libreoffice-writer\nStatus: install ok installed\nArchitecture: arm64\nVersion: ${WRITER_VERSION}\nDescription: Owned installed-package fixture\n`, + ); + let repositoryQueries = 0; + const query: QueryRunner = async (command, argv, signal) => { + if (command === "apt-cache" || command === "apt-config") + return runHostQuery( + `/usr/bin/${command}`, + [ + "-o", + `Dir::State::status=${status}`, + "-o", + `Dir::State::lists=${lists}`, + "-o", + "Dir::Etc::sourcelist=-", + "-o", + "Dir::Etc::sourceparts=-", + "-o", + "APT::Architecture=arm64", + ...argv, + ], + signal, + ); + if (command === "apt-get") repositoryQueries++; + return runQuery(command, argv, signal); + }; + const { installed } = await inspect( + { desktopEntry: "libreoffice-writer" }, + { run: query }, + ); + expect(installed).toMatchObject({ + name: "libreoffice-writer", + version: WRITER_VERSION, + sources: [], + source_origins: [], + apt_repositories: [], + }); + expect(repositoryQueries).toBe(0); + }, +); + describe.skipIf(process.platform === "win32")("desktop entry package", () => { it("records the package that owns the launched entry, and the program's when it differs", async () => { const { inspect } = await writerFixture(); diff --git a/src/desktop/DebianPackageInventory.ts b/src/desktop/DebianPackageInventory.ts index 5dcea02bd1..11aeaf54a4 100644 --- a/src/desktop/DebianPackageInventory.ts +++ b/src/desktop/DebianPackageInventory.ts @@ -158,7 +158,7 @@ const installFacts = async ( ...(libraries?.library_packages ?? []), ...(program === undefined ? [] : [program]), ], - { origins, declared }, + { origins, declared, statusFile: context.host.apt.status }, runQuery, context.signal, ); @@ -303,7 +303,7 @@ export const debianPackage = async ( "No apt package lists are present, so whether the installed version comes from a package archive is unknown.", ); const sources = - policy === undefined ? [] : [...parseAptPolicySources(policy)]; + policy === undefined ? [] : [...parseAptPolicySources(policy, apt.status)]; // A local install's library packages are matched to the archive by origin too. const origins = await policyOrigins(context); const installed = { diff --git a/src/desktop/LibraryPackageInventory.ts b/src/desktop/LibraryPackageInventory.ts index cb8b29a911..268e053d6e 100644 --- a/src/desktop/LibraryPackageInventory.ts +++ b/src/desktop/LibraryPackageInventory.ts @@ -232,6 +232,7 @@ const packageSources = async ( names: readonly string[], runQuery: QueryRunner, signal: AbortSignal | undefined, + statusFile?: string, ): Promise | undefined> => { const sources = new Map(); for (let start = 0; start < names.length; start += QUERY_BATCH) { @@ -243,7 +244,10 @@ const packageSources = async ( ), ); if (output === undefined) return undefined; - for (const [name, listed] of parseAptPolicyPackageSources(output)) + for (const [name, listed] of parseAptPolicyPackageSources( + output, + statusFile, + )) sources.set(name, listed); } return sources; @@ -255,6 +259,7 @@ export const installablePackages = async ( available: { readonly origins: ReadonlyMap | undefined; readonly declared: ReadonlySet; + readonly statusFile?: string | undefined; }, runQuery: QueryRunner, signal: AbortSignal | undefined, @@ -262,7 +267,7 @@ export const installablePackages = async ( const sources = names.length === 0 ? new Map() - : await packageSources(names, runQuery, signal); + : await packageSources(names, runQuery, signal, available.statusFile); return sources === undefined ? undefined : installableLibraryPackages(names, { ...available, sources }); diff --git a/src/domain/desktop/linuxPackages.test.ts b/src/domain/desktop/linuxPackages.test.ts index f787d20019..fd11f65a11 100644 --- a/src/domain/desktop/linuxPackages.test.ts +++ b/src/domain/desktop/linuxPackages.test.ts @@ -162,45 +162,53 @@ describe("parseAptPolicyOrigins", () => { }); describe("parseAptPolicyPackageSources", () => { - it("lists every version's sources of each package, as Ubuntu 24.04 prints several at once", () => { - const updates = - "http://ports.ubuntu.com/ubuntu-ports noble-updates/main arm64 Packages"; - const security = - "http://ports.ubuntu.com/ubuntu-ports noble-security/main arm64 Packages"; - const noble = - "http://ports.ubuntu.com/ubuntu-ports noble/main arm64 Packages"; - const typora = "https://downloads.typora.io/linux ./ Packages"; - // `apt-cache policy libnss3 typora nosuchpkg`; apt prints nothing for an unknown name. - const output = [ - "libnss3:", - " Installed: 2:3.98-1ubuntu0.3", - " Candidate: 2:3.98-1ubuntu0.3", - " Version table:", - " *** 2:3.98-1ubuntu0.3 500", - ` 500 ${updates}`, - ` 500 ${security}`, - " 100 /var/lib/dpkg/status", - " 2:3.98-1build1 500", - ` 500 ${noble}`, - "typora:", - " Installed: 1.14.9-1", - " Candidate: 1.14.9-1", - " Version table:", - " *** 1.14.9-1 500", - ` 500 ${typora}`, - " 100 /var/lib/dpkg/status", - " 1.14.8-1 500", - ` 500 ${typora}`, - "", - ].join("\n"); + it.each([ + "/var/lib/dpkg/status", + "/custom/installed status", + "/custom/installed status ", + "/custom/installed\nstatus", + ])( + "lists every version's sources of each package, excluding status database %j", + (statusFile) => { + const updates = + "http://ports.ubuntu.com/ubuntu-ports noble-updates/main arm64 Packages"; + const security = + "http://ports.ubuntu.com/ubuntu-ports noble-security/main arm64 Packages"; + const noble = + "http://ports.ubuntu.com/ubuntu-ports noble/main arm64 Packages"; + const typora = "https://downloads.typora.io/linux ./ Packages"; + // `apt-cache policy libnss3 typora nosuchpkg`; apt prints nothing for an unknown name. + const output = [ + "libnss3:", + " Installed: 2:3.98-1ubuntu0.3", + " Candidate: 2:3.98-1ubuntu0.3", + " Version table:", + " *** 2:3.98-1ubuntu0.3 500", + ` 500 ${updates}`, + ` 500 ${security}`, + ` 100 ${statusFile}`, + " 2:3.98-1build1 500", + ` 500 ${noble}`, + "typora:", + " Installed: 1.14.9-1", + " Candidate: 1.14.9-1", + " Version table:", + " *** 1.14.9-1 500", + ` 500 ${typora}`, + ` 100 ${statusFile}`, + " 1.14.8-1 500", + ` 500 ${typora}`, + "", + ].join("\n"); - expect(parseAptPolicyPackageSources(output)).toEqual( - new Map([ - ["libnss3", [updates, security, noble]], - ["typora", [typora]], - ]), - ); - }); + expect(parseAptPolicyPackageSources(output, statusFile)).toEqual( + new Map([ + ["libnss3", [updates, security, noble]], + ["typora", [typora]], + ]), + ); + }, + ); }); describe("packageFromPath", () => { diff --git a/src/domain/desktop/linuxPackages.ts b/src/domain/desktop/linuxPackages.ts index 164f1795d9..2f0dcf33be 100644 --- a/src/domain/desktop/linuxPackages.ts +++ b/src/domain/desktop/linuxPackages.ts @@ -61,7 +61,30 @@ export const parseDpkgStatus = ( const POLICY_VERSION = /^ (?:\*\*\*| {3}) (?\S+) -?\d+$/u; const POLICY_SOURCE = /^ {8}-?\d+ (?.+)$/u; -const DPKG_STATUS_SOURCE = "/var/lib/dpkg/status"; +/** Default only: apt-config can select a different installed-package database. */ +export const DEFAULT_DPKG_STATUS_SOURCE = "/var/lib/dpkg/status"; + +/** Skip the known status row before line framing can split its filesystem path. */ +function* policyLines(output: string, statusFile: string): Generator { + let start = 0; + while (start < output.length) { + const end = output.indexOf("\n", start); + const line = output.slice(start, end < 0 ? undefined : end); + const prefix = /^ {8}-?\d+ /u.exec(line)?.[0]; + const sourceStart = start + (prefix?.length ?? 0); + const sourceEnd = sourceStart + statusFile.length; + if ( + prefix !== undefined && + output.startsWith(statusFile, sourceStart) && + (sourceEnd === output.length || output[sourceEnd] === "\n") + ) { + start = sourceEnd + 1; + continue; + } + yield line; + start = end < 0 ? output.length : end + 1; + } +} /** The source a policy line names, without any user name or password in its URI. */ const policySource = (pattern: RegExp, line: string): string | undefined => { @@ -73,18 +96,20 @@ const policySource = (pattern: RegExp, line: string): string | undefined => { * The package sources `apt-cache policy` lists for the installed version, * excluding the dpkg status database every installed version carries. */ -export const parseAptPolicySources = (output: string): readonly string[] => { +export const parseAptPolicySources = ( + output: string, + statusFile = DEFAULT_DPKG_STATUS_SOURCE, +): readonly string[] => { const sources: string[] = []; let installed = false; - for (const line of output.split("\n")) { + for (const line of policyLines(output, statusFile)) { const version = POLICY_VERSION.exec(line); if (version !== null) { installed = line.startsWith(" ***"); continue; } const source = policySource(POLICY_SOURCE, line); - if (installed && source !== undefined && source !== DPKG_STATUS_SOURCE) - sources.push(source); + if (installed && source !== undefined) sources.push(source); } return sources; }; @@ -98,10 +123,11 @@ const POLICY_PACKAGE = /^(?\S+):$/u; */ export const parseAptPolicyPackageSources = ( output: string, + statusFile = DEFAULT_DPKG_STATUS_SOURCE, ): ReadonlyMap => { const packages = new Map(); let current: string[] | undefined; - for (const line of output.split("\n")) { + for (const line of policyLines(output, statusFile)) { const name = POLICY_PACKAGE.exec(line)?.groups?.name; if (name !== undefined) { current = []; @@ -112,7 +138,6 @@ export const parseAptPolicyPackageSources = ( if ( current !== undefined && source !== undefined && - source !== DPKG_STATUS_SOURCE && !current.includes(source) ) current.push(source);