diff --git a/scripts/verify/baselines/resource-constraint-constructor.json b/scripts/verify/baselines/resource-constraint-constructor.json index c101f9f3ab..7c95c60f4e 100644 --- a/scripts/verify/baselines/resource-constraint-constructor.json +++ b/scripts/verify/baselines/resource-constraint-constructor.json @@ -120,7 +120,7 @@ "src/native/pe/PeResourcesProvider.ts": 1, "src/native/pwntools/PwntoolsDecoder.ts": 1, "src/native/pwntools/PwntoolsFailures.ts": 2, - "src/native/windows/WindowsShortcutProvider.ts": 2, + "src/native/windows/WindowsShortcutProvider.ts": 1, "src/pcap/PcapOutput.ts": 3, "src/pcap/PcapSnapshot.ts": 1, "src/process/ArtifactAnalysisResources.ts": 2, diff --git a/src/native/windows/WindowsShortcut.errors.ts b/src/native/windows/WindowsShortcut.errors.ts index 7045542a7c..7c2a45b9b9 100644 --- a/src/native/windows/WindowsShortcut.errors.ts +++ b/src/native/windows/WindowsShortcut.errors.ts @@ -1,3 +1,5 @@ +import { ParseFailureError } from "../../domain/parseFailure.js"; + /** Caller-selected budgets that bound Shell Link expansion. */ export type WindowsShortcutSelectedBoundary = | "id_list_items" @@ -5,13 +7,22 @@ export type WindowsShortcutSelectedBoundary = | "string_bytes"; /** Resource boundary exceeded while walking a Shell Link file. */ -export class WindowsShortcutResourceFailure extends Error { +export class WindowsShortcutResourceFailure extends ParseFailureError<"limit"> { + declare readonly failure: ParseFailureError<"limit">["failure"] & { + readonly limit: { readonly name: WindowsShortcutSelectedBoundary }; + }; + constructor( - readonly boundary: WindowsShortcutSelectedBoundary, - readonly maximum: number, - message: string, + boundary: WindowsShortcutSelectedBoundary, + maximum: number, + detail: string, ) { - super(message); + super({ + kind: "limit", + format: "windows-shortcut", + detail, + limit: { name: boundary, maximum }, + }); this.name = "WindowsShortcutResourceFailure"; } } diff --git a/src/native/windows/WindowsShortcutParser.ts b/src/native/windows/WindowsShortcutParser.ts index e451f826d2..975c6dda64 100644 --- a/src/native/windows/WindowsShortcutParser.ts +++ b/src/native/windows/WindowsShortcutParser.ts @@ -650,6 +650,7 @@ const note = ( if (stop) walk.stopped = true; }; +/** Charge the retained source text bytes, excluding framing. */ const charge = (walk: Walk, bytes: number): void => { walk.stringBytes += bytes; if (walk.stringBytes > walk.maxStringBytes) @@ -695,7 +696,7 @@ const readAnsiAt = ( ); return null; } - charge(walk, end + 1); + charge(walk, end); return slice.subarray(0, end).toString("latin1"); }; @@ -728,7 +729,7 @@ const readUtf16At = ( ); return null; } - charge(walk, end + 2); + charge(walk, end); return slice.subarray(0, end).toString("utf16le"); }; diff --git a/src/native/windows/WindowsShortcutProvider.ts b/src/native/windows/WindowsShortcutProvider.ts index 51af3c2c67..0ee7f0f3f6 100644 --- a/src/native/windows/WindowsShortcutProvider.ts +++ b/src/native/windows/WindowsShortcutProvider.ts @@ -1,5 +1,8 @@ import { HOST_PLATFORM } from "../../platform/HostPlatform.js"; -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { lstat, open } from "node:fs/promises"; import { projectSelectedArtifactFileFailure, @@ -258,12 +261,11 @@ const SELECTED_BUDGETS = { } >; -const budgetRemediation = ( - input: InspectWindowsShortcutInput, - boundary: WindowsShortcutSelectedBoundary, -): string => { - const { argument, maximum, unit } = SELECTED_BUDGETS[boundary]; - const selected = input[argument]; +const budgetRemediation = ({ + name, + maximum: selected, +}: WindowsShortcutResourceFailure["failure"]["limit"]): string => { + const { argument, maximum, unit } = SELECTED_BUDGETS[name]; const recovery = selected >= maximum ? `This release has no override above the ${String(maximum)} ${unit} ceiling; select a smaller shortcut.` @@ -285,17 +287,11 @@ const windowsShortcutFailurePrimary = ( ); if (identityFailure !== undefined) return identityFailure; if (cause instanceof WindowsShortcutResourceFailure) - return new AnalysisResourceConstraintError( - OPERATION, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: cause.boundary, maximum: cause.maximum }, - { - cause, - retryable: false, - remediationAction: budgetRemediation(input, cause.boundary), - }, - ); + return parseLimitConstraint(OPERATION, "capacity", cause, { + path: input.path, + retryable: false, + remediationAction: budgetRemediation(cause.failure.limit), + }); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; if ( diff --git a/tests/boundary/filesystem/windowsShortcut.test.ts b/tests/boundary/filesystem/windowsShortcut.test.ts index c163eef1cf..e2af19be8e 100644 --- a/tests/boundary/filesystem/windowsShortcut.test.ts +++ b/tests/boundary/filesystem/windowsShortcut.test.ts @@ -2,6 +2,7 @@ import { createHash } from "node:crypto"; import { mkdir, symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; import { createWindowsShortcutService } from "../../../src/composition/binaryDiagnostics.js"; import { WindowsShortcutProvider } from "../../../src/native/windows/WindowsShortcutProvider.js"; import { @@ -179,30 +180,102 @@ describe("Windows shortcut stable filesystem boundary", () => { coverage: { status: "partial", failing_offset: 92 }, }); }); +}); - it("returns typed resource and cancellation failures", async () => { - const root = await createTestTempDirectory("rea-windows-shortcut-budget-"); - const path = join(root, "shortcut.lnk"); - await writeFile(path, powershellHiddenIconSpoofShortcut()); +it.each([false, true])( + "counts retained LinkInfo text separately from its terminators (unicode: %s)", + async (unicode) => { + const root = await createTestTempDirectory("rea-shortcut-text-budget-"); + const path = join(root, "notes.lnk"); + const target = "C:\\notes.txt"; + await writeFile( + path, + windowsShortcutBytes({ + flags: LINK_FLAG.HasLinkInfo, + idList: null, + linkInfo: { unicode, label: "", localBasePath: target }, + extraData: [], + }), + ); const service = createWindowsShortcutService(); - const tooSmall = await service.inspect({ path, max_file_bytes: 76 }); - if (tooSmall.ok) throw new Error("Expected resource budget failure"); - expect(tooSmall.error._tag).toBe("AnalysisResourceConstraintError"); - const items = await service.inspect({ path, max_id_list_items: 1 }); - if (items.ok) throw new Error("Expected IDList budget failure"); - expect(items.error).toMatchObject({ + try { + const retainedBytes = + Buffer.byteLength(target, "latin1") + + (unicode ? Buffer.byteLength(target, "utf16le") : 0); + const admitted = await service.inspect({ + path, + max_string_bytes: retainedBytes, + }); + if (!admitted.ok) throw admitted.error; + expect(admitted.value.normalized_result).toMatchObject({ + link_info: { + local_base_path: target, + local_base_path_unicode: unicode ? target : null, + }, + coverage: { status: "complete" }, + }); + const maximum = retainedBytes - 1; + const limited = await service.inspect({ + path, + max_string_bytes: maximum, + }); + if (limited.ok) + throw new Error("Expected retained-text capacity failure"); + const projected = projectAnalysisError(limited.error); + expect(projected).toMatchObject({ + code: "resource_constraint", + details: { resource: "capacity" }, + remediation: { + action: expect.stringContaining( + `Effective max_string_bytes is ${String(maximum)} bytes`, + ), + }, + retryable: false, + }); + expect(projected.details?.reported_limits).toEqual({ + boundary: "string_bytes", + maximum, + }); + } finally { + await service.close(); + } + }, +); + +it("returns typed resource and cancellation failures", async () => { + const root = await createTestTempDirectory("rea-windows-shortcut-budget-"); + const path = join(root, "shortcut.lnk"); + await writeFile(path, powershellHiddenIconSpoofShortcut()); + const service = createWindowsShortcutService(); + const tooSmall = await service.inspect({ path, max_file_bytes: 76 }); + if (tooSmall.ok) throw new Error("Expected resource budget failure"); + expect(tooSmall.error._tag).toBe("AnalysisResourceConstraintError"); + for (const [argument, boundary] of [ + ["max_id_list_items", "id_list_items"], + ["max_extra_data_blocks", "extra_data_blocks"], + ] as const) { + const limited = await service.inspect({ path, [argument]: 1 }); + if (limited.ok) throw new Error(`Expected ${boundary} capacity failure`); + expect(limited.error).toMatchObject({ _tag: "AnalysisResourceConstraintError", - reportedLimits: { boundary: "id_list_items", maximum: 1 }, - remediationAction: expect.stringContaining("max_id_list_items"), + cause: { + failure: { + kind: "limit", + format: "windows-shortcut", + limit: { name: boundary, maximum: 1 }, + }, + }, + reportedLimits: { boundary, maximum: 1 }, + remediationAction: expect.stringContaining(`Effective ${argument} is 1`), retryable: false, }); - const cancelled = await service.inspect( - { path }, - { signal: AbortSignal.abort() }, - ); - if (cancelled.ok) throw new Error("Expected cancellation"); - expect(cancelled.error._tag).toBe("AnalysisCancelledError"); - }); + } + const cancelled = await service.inspect( + { path }, + { signal: AbortSignal.abort() }, + ); + if (cancelled.ok) throw new Error("Expected cancellation"); + expect(cancelled.error._tag).toBe("AnalysisCancelledError"); }); it.each([