From 9b8916152badd33249ac0045ad4ee072863935ef Mon Sep 17 00:00:00 2001 From: 64579525 <17373302683@163.com> Date: Mon, 12 Oct 2026 16:52:05 +0800 Subject: [PATCH] fix(artifacts): normalize source loss during root hashing --- src/application/BinaryTargetResolver.ts | 17 +----- .../inventory/hashStableRootArtifact.ts | 22 ++++--- .../boundary/filesystem/inesArtifact.test.ts | 57 +++++++++++++++++++ 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts index ba2cd3602..aa503083d 100644 --- a/src/application/BinaryTargetResolver.ts +++ b/src/application/BinaryTargetResolver.ts @@ -4,19 +4,12 @@ import { nesContainerFormat, parseInesLayout, } from "../domain/inesContainer.js"; -import { - access, - lstat, - realpath, - stat, - type FileHandle, -} from "node:fs/promises"; +import { access, realpath, stat, type FileHandle } from "node:fs/promises"; import { extname, isAbsolute, resolve } from "node:path"; import { BinaryTargetError } from "../domain/configurationErrors.js"; import { openRegularFile, - sameRegularFileState, verifyRegularFileState, } from "../filesystem/RegularFile.js"; import { AnalysisCancelledError } from "../domain/analysisErrorCore.js"; @@ -390,12 +383,8 @@ const sha256Handle = async ( throw new BinaryTargetError(path, "target grew while being read"); } throwIfTargetResolutionCancelled(signal); - const [opened, currentPath] = await Promise.all([handle.stat(), lstat(path)]); - if ( - !sameRegularFileState(initial, opened) || - !sameRegularFileState(initial, currentPath) || - position !== initial.size - ) + await verifyRegularFileState(path, { handle, initial }); + if (position !== initial.size) throw new BinaryTargetError(path, "target changed while being read"); return hash.digest("hex"); }; diff --git a/src/artifacts/inventory/hashStableRootArtifact.ts b/src/artifacts/inventory/hashStableRootArtifact.ts index 40a8a5fd6..930798fe8 100644 --- a/src/artifacts/inventory/hashStableRootArtifact.ts +++ b/src/artifacts/inventory/hashStableRootArtifact.ts @@ -1,4 +1,3 @@ -import { lstat } from "node:fs/promises"; import type { Stats } from "node:fs"; import type { FileHandle } from "node:fs/promises"; import { readFileHandleChunks } from "../../filesystem/readFileHandleChunks.js"; @@ -9,7 +8,8 @@ import { NonRegularFileReadError, RegularFileAdmissionFailure, openRegularFile, - sameRegularFileState, + RegularFileChangedError, + verifyRegularFileState, } from "../../filesystem/RegularFile.js"; import { abortIfNeeded, @@ -105,12 +105,18 @@ export const hashStableRootArtifactHandle = async ( signal, ); abortIfNeeded(signal); - const [opened, currentPath] = await Promise.all([handle.stat(), lstat(path)]); - if ( - !sameRegularFileState(initial, opened) || - !sameRegularFileState(initial, currentPath) || - digest.bytes !== initial.size - ) + try { + await verifyRegularFileState(path, { handle, initial }); + } catch (cause: unknown) { + if (cause instanceof RegularFileChangedError) + throw new ArtifactReaderFailure( + "integrity", + `Root artifact changed during inventory: ${path}`, + { cause: cause.cause ?? cause }, + ); + throw cause; + } + if (digest.bytes !== initial.size) throw new ArtifactReaderFailure( "integrity", `Root artifact changed during inventory: ${path}`, diff --git a/tests/boundary/filesystem/inesArtifact.test.ts b/tests/boundary/filesystem/inesArtifact.test.ts index 0af93b545..dd1fc2945 100644 --- a/tests/boundary/filesystem/inesArtifact.test.ts +++ b/tests/boundary/filesystem/inesArtifact.test.ts @@ -314,6 +314,63 @@ it.each(["inventory", "target"])( }, ); +it.each([ + { route: "inventory", hashPass: 1 }, + { route: "inventory", hashPass: 2 }, + { route: "target", hashPass: 1 }, +])( + "reports source loss in $route hash pass $hashPass as a changed input", + async ({ route, hashPass }) => { + const root = await createTestTempDirectory("rea-ines-hash-loss-"); + const path = join(root, "source.nes"); + await writeFile(path, inesFixture()); + const sample = await open(path, "r"); + const prototype = Object.getPrototypeOf(sample) as Pick< + typeof sample, + "read" + >; + await sample.close(); + const originalRead = prototype.read; + let observedPasses = 0; + const read = vi.spyOn(prototype, "read").mockImplementation(async function ( + this: typeof sample, + ...args + ) { + const result = await originalRead.call(this, ...args); + const readArguments: readonly unknown[] = args; + const [buffer, , , position] = readArguments; + if ( + Buffer.isBuffer(buffer) && + buffer.byteLength > 8192 && + (position === 0 || position === 0n) + ) { + observedPasses += 1; + if (observedPasses === hashPass) { + read.mockRestore(); + await unlink(path); + } + } + return result; + }); + try { + if (route === "inventory") { + await expect(inventoryArtifact(path)).rejects.toMatchObject({ + reason: "integrity", + cause: { code: "ENOENT" }, + }); + } else { + expect(await parseBinaryTarget(path)).toMatchObject({ + ok: false, + error: { reason: expect.stringContaining("changed") }, + }); + } + expect(observedPasses).toBe(hashPass); + } finally { + read.mockRestore(); + } + }, +); + it("releases an extraction-owned source descriptor on repeated close", async () => { const root = await createTestTempDirectory("rea-ines-owned-source-"); const path = join(root, "source.nes");