diff --git a/src/application/releaseExposure/ReleaseExposureService.test.ts b/src/application/releaseExposure/ReleaseExposureService.test.ts index 4b937c1e4d..6ec7b44aac 100644 --- a/src/application/releaseExposure/ReleaseExposureService.test.ts +++ b/src/application/releaseExposure/ReleaseExposureService.test.ts @@ -1,3 +1,4 @@ +import { isAbsolute } from "node:path"; import { describe, expect, it, vi } from "vitest"; import { ArtifactReaderFailure } from "../../resources/ArtifactReader.js"; @@ -12,6 +13,7 @@ import { electronFuseName, electronFuseState, ELECTRON_FUSE_STATE_BYTES, + type ElectronFuseReport, type ElectronFuseSlice, } from "../../domain/electron/electronFuses.js"; import type { GoBinary } from "../../domain/go/goBinary.js"; @@ -667,6 +669,7 @@ describe("ReleaseExposureService fuse image identity", () => { }); it("accepts an app bundle's fuse read when its file is an inventoried image", async () => { + const framework = "/work/App.app/framework"; const bundle = scan({ target: { selected_path: "/work/App.app", @@ -679,15 +682,13 @@ describe("ReleaseExposureService fuse image identity", () => { const status = async (sha256: string) => { const { service, desktop, electronFuses } = owners({ ...bundle, - native_images: [ - { path: "/work/App.app/framework", format: "elf", sha256 }, - ], + native_images: [{ path: framework, format: "elf", sha256 }], }); desktop.inspect.mockResolvedValue( err(new AnalysisInputError("inspect_desktop_application")), ); electronFuses.inspect.mockResolvedValue( - ok(evidence(fuseReport([slice(0)]))), + ok(evidence(fuseReport([slice(0)], { resolved_path: framework }))), ); const result = await service.inspect({ path: "/work/App.app" }); if (!result.ok) throw result.error; @@ -698,25 +699,6 @@ describe("ReleaseExposureService fuse image identity", () => { expect(await status(SHA)).toBe("not_observed"); expect(await status(CHANGED)).toBe("unknown"); }); - - it("does not attach fuse facts when the resolved file left the inventory", async () => { - const { service, electronFuses } = owners( - scan({ - native_images: [{ path: "/work/app", format: "elf", sha256: CHANGED }], - fuse_candidates: [{ path: "/work/app", role: "native-image" }], - }), - ); - electronFuses.inspect.mockResolvedValue( - ok(evidence(fuseReport([slice(0)]))), - ); - const result = await service.inspect({ path: "/work/app" }); - if (!result.ok) throw result.error; - expect( - releaseExposureReportSchema - .parse(result.value.normalized_result) - .checks.find((item) => item.id === "electron.run_as_node")?.status, - ).toBe("unknown"); - }); }); describe("ReleaseExposureService fuse slice coverage", () => { @@ -1054,27 +1036,32 @@ describe("ReleaseExposureService packaged native images", () => { }); }); -describe("ReleaseExposureService Electron layout paths", () => { +describe("ReleaseExposureService Electron discovery identity", () => { it.each([ - ["Windows", "C:\\ship\\App", "\\"], - ["POSIX", "/ship/App", "/"], + ["Windows", "C:\\ship\\App", "C:\\real\\App", "\\"], + ["POSIX", "/work/app", "/private/work/app", "/"], ])( - "keeps a %s named host without a sentinel unknown beside a safe helper", - async (_, folder, separator) => { - const host = `${folder}${separator}App.exe`; - const { service, desktop, electronFuses } = owners( - directoryScan([host, `${folder}${separator}helper.exe`]), - ); + "settles the named %s host under a canonical layout and a caller path with a trailing separator", + async (_, selected, canonical, separator) => { + const path = `${selected}${separator}`; + const host = `${path}App.exe`; + const scanned = directoryScan([host, `${path}helper.exe`]); + const { service, desktop, electronFuses } = owners({ + ...scanned, + target: { ...scanned.target, selected_path: path }, + }); const report = electronDesktopReport(); desktop.inspect.mockResolvedValue( ok( evidence({ ...report, + root: { ...report.root, path, canonical_path: canonical }, files: [ { ...report.files[0], - path: "resources/app.asar", - local_path: [folder, "resources", "app.asar"].join(separator), + local_path: [canonical, "resources", "app.asar"].join( + separator, + ), }, ], }), @@ -1084,82 +1071,19 @@ describe("ReleaseExposureService Electron layout paths", () => { ok( evidence( fuseReport( - (input as { path?: string }).path === host ? [] : [slice(0)], + (input as { path?: string }).path === host ? [slice(0)] : [], ), ), ), ); - expect((await runAsNode(service))?.status).toBe("unknown"); + + // The fake reader supplies either layout dialect. Input admission still uses this host. + const selectedInput = isAbsolute(path) ? path : "/work/app"; + expect((await runAsNode(service, selectedInput))?.status).toBe( + "not_observed", + ); }, ); -}); - -describe("ReleaseExposureService Electron discovery identity", () => { - it("joins hosts through a symlinked parent the desktop owner resolved", async () => { - const host = "/work/app/App.exe"; - const { service, desktop, electronFuses } = owners( - directoryScan([host, "/work/app/helper.exe"]), - ); - const report = electronDesktopReport(); - desktop.inspect.mockResolvedValue( - ok( - evidence({ - ...report, - root: { ...report.root, canonical_path: "/private/work/app" }, - files: [ - { - ...report.files[0], - local_path: "/private/work/app/resources/app.asar", - }, - ], - }), - ), - ); - electronFuses.inspect.mockImplementation(async (input) => - ok( - evidence( - fuseReport( - (input as { path?: string }).path === host ? [] : [slice(0)], - ), - ), - ), - ); - expect((await runAsNode(service))?.status).toBe("unknown"); - }); - - it("joins hosts when the selected directory has a trailing slash", async () => { - const host = "/work/app/App.exe"; - const scanned = directoryScan([host, "/work/app/helper.exe"]); - const { service, desktop, electronFuses } = owners({ - ...scanned, - target: { ...scanned.target, selected_path: "/work/app/" }, - }); - const report = electronDesktopReport(); - desktop.inspect.mockResolvedValue( - ok( - evidence({ - ...report, - root: { ...report.root, canonical_path: "/private/work/app" }, - files: [ - { - ...report.files[0], - local_path: "/private/work/app/resources/app.asar", - }, - ], - }), - ), - ); - electronFuses.inspect.mockImplementation(async (input) => - ok( - evidence( - fuseReport( - (input as { path?: string }).path === host ? [] : [slice(0)], - ), - ), - ), - ); - expect((await runAsNode(service, "/work/app/"))?.status).toBe("unknown"); - }); it("keeps safe fuses unknown when discovery stopped after one layout", async () => { const { service, desktop, electronFuses } = owners( @@ -1922,23 +1846,27 @@ describe("ReleaseExposureService asar: false bundle with partial inventory", () }); describe("ReleaseExposureService app bundle with a sidecar", () => { - const noSentinel = ok(evidence(fuseReport([slice(0, { sentinels: 0 })]))); const unreadable = err(new AnalysisInputError("inspect_electron_fuses")); const FRAMEWORK = "/work/App.app/Contents/Frameworks/Electron Framework.framework/Electron Framework"; const SIDECAR = "/work/App.app/Contents/Resources/sidecar"; - const safe = ok(evidence(fuseReport([slice(0)]))); - const runAsNodeOn = ok( - evidence( - fuseReport([slice(0, { state: ELECTRON_FUSE_STATE_BYTES.enabled })]), - ), + const framework = (body: ElectronFuseSlice) => + ok(evidence(fuseReport([body], { resolved_path: FRAMEWORK }))); + const sidecar = (body: ElectronFuseSlice) => + ok( + evidence(fuseReport([body], { resolved_path: SIDECAR, sha256: CHANGED })), + ); + const safeFramework = framework(slice(0)); + const safeSidecar = sidecar(slice(0)); + const runAsNodeOn = sidecar( + slice(0, { state: ELECTRON_FUSE_STATE_BYTES.enabled }), ); const bundleWithSidecar = () => { const parts = owners({ ...appScan, native_images: [ { path: FRAMEWORK, format: "mach-o", sha256: SHA }, - { path: SIDECAR, format: "mach-o" }, + { path: SIDECAR, format: "mach-o", sha256: CHANGED }, ], }); parts.desktop.inspect.mockResolvedValue( @@ -1952,7 +1880,9 @@ describe("ReleaseExposureService app bundle with a sidecar", () => { it("reports RunAsNode exposed on the sidecar and reads the framework once", async () => { const { service, electronFuses } = bundleWithSidecar(); electronFuses.inspect.mockImplementation(async (input) => - (input as { path: string }).path === SIDECAR ? runAsNodeOn : safe, + (input as { path: string }).path === SIDECAR + ? runAsNodeOn + : safeFramework, ); const check = await runAsNode(service, "/work/App.app"); expect(check?.status).toBe("exposed"); @@ -1964,7 +1894,7 @@ describe("ReleaseExposureService app bundle with a sidecar", () => { }); it.each([ - ["sentinel-free", noSentinel], + ["sentinel-free", framework(slice(0, { sentinels: 0 }))], [ "unsupported", err( @@ -1980,7 +1910,7 @@ describe("ReleaseExposureService app bundle with a sidecar", () => { async (_, framework) => { const { service, electronFuses } = bundleWithSidecar(); electronFuses.inspect.mockImplementation(async (input) => - (input as { path: string }).path === SIDECAR ? safe : framework, + (input as { path: string }).path === SIDECAR ? safeSidecar : framework, ); expect((await runAsNode(service, "/work/App.app"))?.status).toBe( "unknown", @@ -1991,7 +1921,7 @@ describe("ReleaseExposureService app bundle with a sidecar", () => { it("keeps safe outer fuses unknown when the sidecar cannot be read", async () => { const { service, electronFuses } = bundleWithSidecar(); electronFuses.inspect.mockImplementation(async (input) => - (input as { path: string }).path === SIDECAR ? unreadable : safe, + (input as { path: string }).path === SIDECAR ? unreadable : safeFramework, ); expect((await runAsNode(service, "/work/App.app"))?.status).toBe("unknown"); }); @@ -2922,7 +2852,10 @@ const slice = ( ], }); -const fuseReport = (slices: readonly ElectronFuseSlice[]) => { +const fuseReport = ( + slices: readonly ElectronFuseSlice[], + artifact: Partial = {}, +) => { const container = slices.length > 1 ? "mach-o-universal" : "mach-o"; return { artifact: { @@ -2931,6 +2864,7 @@ const fuseReport = (slices: readonly ElectronFuseSlice[]) => { sha256: SHA, bytes: 4096, format: container, + ...artifact, }, slices, ...describeElectronFuseFindings(slices, container), diff --git a/src/application/releaseExposure/ReleaseExposureService.ts b/src/application/releaseExposure/ReleaseExposureService.ts index 1e5a3f8f9d..23fd1a069d 100644 --- a/src/application/releaseExposure/ReleaseExposureService.ts +++ b/src/application/releaseExposure/ReleaseExposureService.ts @@ -215,9 +215,14 @@ export class ReleaseExposureService { image.sha256 === undefined ? [] : [[image.path, image.sha256] as const], ), ); - const fuseImages: FuseImageFact[] = []; + const fuseImages: CollectedFuseImage[] = []; for (const candidate of prepared.scan.fuse_candidates) { - if (bundleReadCovers(candidate, digests, fuseImages)) continue; + if ( + fuseImages.some( + (image) => image.inventoriedBundleFile === candidate.path, + ) + ) + continue; fuseImages.push( await settle( () => this.#fuseImage(candidate, digests, options), @@ -332,7 +337,7 @@ export class ReleaseExposureService { candidate: ReleaseExposureFuseCandidate, digests: ReadonlyMap, options: ExecutionOptions | undefined, - ): Promise> { + ): Promise> { let result: Result; try { result = await this.owners.electronFuses.inspect( @@ -456,12 +461,17 @@ const completedOwnerEvidence = (error: AnalysisError): Evidence | undefined => { return partial.ok ? partial.value : undefined; }; +interface CollectedFuseImage extends FuseImageFact { + /** The exact inventoried regular file whose validated bytes a bundle read covered. */ + readonly inventoriedBundleFile?: string; +} + /** Settle one fuse image from its owner's Evidence. */ const fuseImageFact = ( candidate: ReleaseExposureFuseCandidate, digests: ReadonlyMap, value: Evidence, -): FuseImageFact => { +): CollectedFuseImage => { const report = electronFuseReportSchema.safeParse(value.normalized_result); if (!report.success) return { @@ -470,7 +480,13 @@ const fuseImageFact = ( path: candidate.path, reason: "Electron fuse report did not match its schema.", }; - if (fuseImageChanged(candidate, digests, report.data.artifact.sha256)) + const bundleFile = + candidate.role === "app-bundle" + ? report.data.artifact.resolved_path + : undefined; + const expected = digests.get(bundleFile ?? candidate.path); + // A digest captured for another path cannot bind this read to inventory. + if (digests.size > 0 && expected !== report.data.artifact.sha256) return { kind: "failed", role: candidate.role, @@ -481,7 +497,9 @@ const fuseImageFact = ( kind: "report", role: candidate.role, path: candidate.path, - sha256: report.data.artifact.sha256, + ...(bundleFile !== undefined && expected !== undefined + ? { inventoriedBundleFile: bundleFile } + : {}), implications: fuseImplications(report.data), }; }; @@ -587,38 +605,6 @@ const ownerCloseFailure = ( ); }; -/** - * An app bundle resolves to a file through paths the inventory may spell - * differently, so its digest must match some inventoried native image. - */ -const fuseImageChanged = ( - candidate: ReleaseExposureFuseCandidate, - digests: ReadonlyMap, - sha256: string, -): boolean => { - const expected = digests.get(candidate.path); - if (expected !== undefined) return expected !== sha256; - return digests.size > 0 && ![...digests.values()].includes(sha256); -}; - -/** - * The bundle read lands on the framework file, which the sample may also list. - * The bundle candidate comes first, so that image needs no second read. - */ -const bundleReadCovers = ( - candidate: ReleaseExposureFuseCandidate, - digests: ReadonlyMap, - images: readonly FuseImageFact[], -): boolean => { - const digest = digests.get(candidate.path); - return ( - digest !== undefined && - images.some( - (image) => image.role === "app-bundle" && image.sha256 === digest, - ) - ); -}; - const imageChanged = ( image: ReleaseExposureNativeImage, sha256: string, diff --git a/src/domain/releaseExposure.ts b/src/domain/releaseExposure.ts index e4f2f2973d..0ab0f79c4a 100644 --- a/src/domain/releaseExposure.ts +++ b/src/domain/releaseExposure.ts @@ -413,8 +413,6 @@ export interface FuseImageFact { readonly kind: "unsupported" | "failed" | "report"; readonly role: "app-bundle" | "native-image"; readonly path?: string; - /** Digest of the file the reader resolved, on a report. */ - readonly sha256?: string; readonly reason?: string; readonly implications?: readonly { readonly subject: string; diff --git a/tests/boundary/filesystem/releaseExposure.test.ts b/tests/boundary/filesystem/releaseExposure.test.ts index 343f59d65a..98b7d012ea 100644 --- a/tests/boundary/filesystem/releaseExposure.test.ts +++ b/tests/boundary/filesystem/releaseExposure.test.ts @@ -8,7 +8,13 @@ import { denoElf, } from "../../../src/artifacts/deno/DenoStandalone.fixture.js"; +import { ReleaseExposureService } from "../../../src/application/releaseExposure/ReleaseExposureService.js"; +import { ReleaseExposureReader } from "../../../src/artifacts/ReleaseExposureReader.js"; +import { createDesktopApplicationService } from "../../../src/composition/desktop.js"; +import { createElectronFuseService } from "../../../src/composition/electronFuses.js"; +import { createGoBinaryService } from "../../../src/composition/go.js"; import { createReleaseExposureService } from "../../../src/composition/releaseExposure.js"; +import { createRustBinaryService } from "../../../src/composition/rust.js"; import { DEFAULT_ASAR_MAX_HEADER_BYTES } from "../../../src/domain/analysisResourceLimits.js"; import { AnalysisResourceConstraintError } from "../../../src/domain/analysisErrorCore.js"; import { releaseExposureReportSchema } from "../../../src/domain/releaseExposure.js"; @@ -191,43 +197,83 @@ describe("release exposure filesystem boundary", () => { }); describe("release exposure app bundle boundary", () => { - it("reports a sidecar's RunAsNode fuse in an app bundle with safe outer fuses", async () => { - const root = await createTestTempDirectory("rea-release-exposure-app-"); - const app = join(root, "Sample.app"); - const framework = join( - app, - "Contents", - "Frameworks", - "Electron Framework.framework", - ); - const resources = join(app, "Contents", "Resources"); - await mkdir(framework, { recursive: true }); - await mkdir(resources, { recursive: true }); - const disabled = 0x30; - const enabled = 0x31; - await writeFile( - join(framework, "Electron Framework"), - thinFuseImage(Array.from({ length: 6 }, () => disabled)).bytes, - ); - const sidecar = join(resources, "sidecar"); - await writeFile( - sidecar, - thinFuseImage([enabled, disabled, disabled, disabled, disabled, disabled]) - .bytes, - ); - const service = createReleaseExposureService(); - try { - const result = await service.inspect({ path: app }); - if (!result.ok) throw result.error; - const check = releaseExposureReportSchema - .parse(result.value.normalized_result) - .checks.find((item) => item.id === "electron.run_as_node"); - expect(check?.status).toBe("exposed"); - expect(JSON.stringify(check?.locations)).toContain("sidecar"); - } finally { - await service.close(); - } - }); + it.each(["unchanged", "replaced", "identical"] as const)( + "binds fuse findings to their inventoried paths with %s framework bytes", + async (scenario) => { + const root = await createTestTempDirectory("rea-release-exposure-app-"); + const app = join(root, "Sample.app"); + const framework = join( + app, + "Contents", + "Frameworks", + "Electron Framework.framework", + ); + const resources = join(app, "Contents", "Resources"); + const revision = join(framework, "Versions", "A"); + await mkdir(revision, { recursive: true }); + await mkdir(resources, { recursive: true }); + const disabled = 0x30; + const enabled = 0x31; + const frameworkImage = join(revision, "Electron Framework"); + const sidecar = join(resources, "sidecar"); + const exposed = thinFuseImage([ + enabled, + disabled, + disabled, + disabled, + disabled, + disabled, + ]).bytes; + await writeFile( + frameworkImage, + scenario === "identical" + ? exposed + : thinFuseImage(Array.from({ length: 6 }, () => disabled)).bytes, + ); + await writeFile(sidecar, exposed); + const fuses = createElectronFuseService(); + const service = new ReleaseExposureService({ + reader: new ReleaseExposureReader(), + desktop: createDesktopApplicationService(), + electronFuses: { + inspect: async (input, options) => { + if ( + (input as { path: string }).path === app && + scenario === "replaced" + ) + await writeFile(frameworkImage, exposed); + return fuses.inspect(input, options); + }, + close: () => fuses.close(), + }, + goBinaries: createGoBinaryService(), + rustBinaries: createRustBinaryService(), + }); + try { + const result = await service.inspect({ path: app }); + if (!result.ok) throw result.error; + const check = releaseExposureReportSchema + .parse(result.value.normalized_result) + .checks.find((item) => item.id === "electron.run_as_node"); + expect(check?.status).toBe("exposed"); + expect(check?.locations).toEqual( + (scenario === "identical" ? [app, sidecar] : [sidecar]).map( + (path) => ({ + kind: "artifact-path", + path, + }), + ), + ); + const changed = + "The file changed after inventory, so its findings were not attached."; + expect(check?.observation.samples.includes(changed)).toBe( + scenario === "replaced", + ); + } finally { + await service.close(); + } + }, + ); }); describe("release exposure ASAR header admission", () => {