Files
reverse-engineering-anything/tests/acceptance/windowsShortcutCli.test.ts
morluto 89c1dd3c32 fix(errors): refuse malformed target bytes as a target, not invalid input (#14195)
* fix(errors): refuse malformed target bytes as a target, not invalid input

parseFailureAnalysisError is the shared owner that turns a byte-format
parser's ParseFailure into a public refusal. It mapped malformed and
truncated bytes to an AnalysisInputError on the path field. The envelope
then reported invalid_request, with inspect_input_contract recovery and
"correct the listed fields". The arguments were valid; the target bytes
were the problem. Readers that wrap the ParseFailureError in an
ArtifactReaderFailure, and ArtifactOperationError reason=malformed, were
classified the same way.

Add AnalysisMalformedTargetError. It keeps the parser's kind, format,
detail, offset and field, and projects as unsupported_target with
details.format_failure. Its recovery is to choose another operation or a
well-formed copy of the target. Route both parser entry points through it
and reclassify ArtifactOperationError reason=malformed. Argument and
selection errors (missing files, directories, symlinks, JSON arguments)
stay invalid_request.

Fixes #14170

* docs(maven): describe duplicate variant names as malformed metadata

Duplicate Gradle variant names go through malformedInput, which now
reports unsupported_target with details.format_failure: malformed.
Point the earlier statement at that section instead of the old
invalid_format code.

* test(errors): expect malformed-target refusals from CLI byte-format readers

Truncated IPS, ELF, PE and shortcut bytes now project as unsupported_target with format_failure=malformed. Update the CLI acceptance cases that still expected invalid_request.

* fix(errors): keep a cancelled read a cancellation whatever its abort reason

projectOwnedArtifactInputFailure read the reader failure's cause without
checking the reader's own reason. A cancelled read carries the caller's
opaque abort reason as that cause, so aborting with a ParseFailureError
turned the cancellation into a non-retryable malformed-target refusal
before the shared cancellation projection ran. Only a reader whose own
reason is malformed or truncated now yields a malformed target.

The ELF dependency checkpoint cancellation test now also aborts with a
parser failure as the reason, through the public provider read.

* test(pe): expect malformed-target refusals from the PE filesystem boundaries

Short non-PE bytes now refuse the target rather than the request field.
The missing-file, directory, symlink and child-path controls keep their
caller-input expectations.

* fix(elf): refuse malformed ELF note bytes as a target, not invalid input

inspect_elf_notes shared this PR's root cause: its parser reported
structural violations as a reader "format" failure, which the shared
stable-artifact projection treats as a request-field error, so truncated
or non-ELF bytes still returned invalid_request. The parser now raises
malformed or truncated ParseFailureErrors, which the shared projection
reports as a malformed target with format_failure and format facts.

Consumers that interpreted the old reader reason now use the parser's
facts: debug-link build-id observation keys on ParseFailureError kinds,
and binary hardening drops its rewrap of note-parser format failures
into BinaryFormatFailure, which the parser's own failure supersedes.

* test(errors): own the byte readers' malformed-target projection in one CLI table

The PR repeated the same malformed -> unsupported_target mapping in a
dedicated CLI test per command and again in each provider's filesystem
boundary test. The stable-artifact parser-failure CLI table now owns it:
every byte reader it covers (now including ELF dependencies, debug link,
unwind ranges and native initializers) refuses a truncated ELF header as
a malformed target with operation, path, format_failure, retained file
identity and no argument recovery.

Removed as redundant with that table: the truncated or non-ELF/non-PE
CLI tests for ELF debug link, dependencies, unwind ranges and notes,
native initializers, and PE imports and certificates; and the
malformed-file blocks in thirteen filesystem boundary tests, which keep
their directory, symlink, missing-file and child-path controls. Format
specific refusals (a truncated IPS body, a wrong shortcut CLSID, DWARF
units) keep their own tests, as do the owner tests in
parseFailureAnalysisError and ArtifactOperationFailure.

* test(errors): expect malformed target bytes as unsupported_target in acceptance tests

The first full acceptance run of this PR found five expectations still
asserting invalid_request for target-byte failures: the timing test's
truncated ELF, a truncated DTB, duplicate Rust dependency JSON, and PE32
and PE64 load-config images without an MZ header. Assert the
unsupported_target code, with format_failure malformed where the
non-MZ PE path matches the already-updated PE imports test.
2026-11-24 00:44:40 +00:00

307 lines
11 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { expect } from "vitest";
import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../src/contracts/native/binaryDiagnosticsToolContracts.js";
import {
powershellHiddenIconSpoofShortcut,
TRACKER_MAC,
truncatedStringDataShortcut,
uuidV1Bytes,
windowsShortcutBytes,
wrongClsidShortcut,
} from "../fixtures/binaryDiagnostics/windowsShortcut.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
const shortcutContract = (() => {
const contract = BINARY_DIAGNOSTICS_TOOL_CONTRACTS.find(
(item) => item.name === "inspect_windows_shortcut",
);
if (contract === undefined)
throw new Error("Missing inspect_windows_shortcut contract");
return contract;
})();
cliTest.for([
{ target: "C:\\Tools\\\x83A.exe", icon: "C:\\Tools\\\x83a.exe" },
{ target: "C:\\Tools\\\x82\x60.exe", icon: "C:\\Tools\\\x82\x81.exe" },
])(
"preserves opaque ANSI path facts through compiled CLI and stdio MCP: $target",
async ({ target, icon }, { cli }) => {
const root = await createTestTempDirectory("rea-shortcut-ansi-cli-");
const path = join(root, "shortcut.lnk");
const bytes = windowsShortcutBytes({
unicode: false,
idList: null,
relativePath: "",
linkInfo: { localBasePath: target },
extraData: [],
iconLocation: icon,
});
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const call = await cli.sharedMcpCall!();
const response = await call("inspect_windows_shortcut", { path });
expect(response.isError, JSON.stringify(response)).not.toBe(true);
for (const value of [result.json, response.structuredContent]) {
const evidence = shortcutContract.outputSchema.parse(value);
const report = evidence.normalized_result;
expect(report.artifact.sha256).toBe(
createHash("sha256").update(bytes).digest("hex"),
);
expect(report.link_info?.local_base_path).toBe(target);
expect(report.string_data.icon_location?.value).toBe(icon);
expect(report.coverage.status).toBe("complete");
expect(report.observations).toEqual(
expect.arrayContaining([
{
kind: "icon_location_differs_from_target",
evidence_kind: "derived",
value: null,
},
{
kind: "target_extension",
evidence_kind: "derived",
extension: ".exe",
},
]),
);
}
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);
cliTest(
"preserves unreadable environment target uncertainty through CLI and stdio MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-shortcut-environment-cli-");
const iconLocation = "C:\\Tools\\icon.exe";
const call = await cli.sharedMcpCall?.();
if (call === undefined) throw new Error("Expected a stdio MCP session");
for (const {
kind,
localBasePath,
expectedDifference,
expectedExtension,
} of [
{
kind: "environment",
localBasePath: "C:\\Tools\\other.dll",
expectedDifference: null,
expectedExtension: null,
},
{
kind: "environment",
localBasePath: iconLocation,
expectedDifference: false,
expectedExtension: null,
},
{
kind: "icon_environment",
localBasePath: "C:\\Tools\\other.dll",
expectedDifference: true,
expectedExtension: ".dll",
},
] as const) {
const valid = windowsShortcutBytes({
idList: null,
relativePath: "",
linkInfo: { localBasePath },
iconLocation,
extraData: [{ kind, unicode: iconLocation }],
});
const terminal = valid.length - 4;
const blockOffset = terminal - 0x314;
const tail = Buffer.from([0xab, 0xcd]);
const bytes = Buffer.concat([
valid.subarray(0, terminal),
tail,
valid.subarray(terminal),
]);
bytes.writeUInt32LE(0x314 + tail.length, blockOffset);
const path = join(root, `${kind}-${String(expectedDifference)}.lnk`);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const response = await call("inspect_windows_shortcut", { path });
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
for (const value of [result.json, response.structuredContent]) {
const { normalized_result: report } =
shortcutContract.outputSchema.parse(value);
expect(report.coverage).toMatchObject({
status: "partial",
failing_offset: blockOffset,
});
expect(report.extra_data.blocks[0]?.decoded.kind).toBe("raw");
expect(report.observations).toEqual(
expect.arrayContaining([
{
kind: "icon_location_differs_from_target",
evidence_kind: "derived",
value: expectedDifference,
},
{
kind: "target_extension",
evidence_kind: "derived",
extension: expectedExtension,
},
]),
);
}
}
},
);
cliTest(
"preserves pre-epoch shortcut timestamps through CLI and stdio MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-shortcut-time-cli-");
const path = join(root, "time.lnk");
const uuid = uuidV1Bytes(122192927999999999n, 1, TRACKER_MAC);
await writeFile(
path,
windowsShortcutBytes({
creationTime: 116444735999999999n,
accessTime: 0n,
writeTime: 116444736000000001n,
extraData: [
{ kind: "tracker", droid: [uuid, uuid], birthDroid: [uuid, uuid] },
],
}),
);
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const call = await cli.sharedMcpCall?.();
if (call === undefined) throw new Error("Expected a stdio MCP session");
const response = await call("inspect_windows_shortcut", { path });
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
for (const value of [result.json, response.structuredContent]) {
const evidence = shortcutContract.outputSchema.parse(value);
const report = evidence.normalized_result;
expect(evidence.subject?.local_path).toBe(path);
expect(report.header).toMatchObject({
creation_time: {
raw: "116444735999999999",
iso8601: "1969-12-31T23:59:59.999Z",
},
access_time: { raw: "0", iso8601: null },
write_time: {
raw: "116444736000000001",
iso8601: "1970-01-01T00:00:00.000Z",
},
});
const tracker = report.extra_data.blocks[0]?.decoded;
if (tracker?.kind !== "tracker")
throw new Error("Expected TrackerDataBlock");
for (const time of [
...tracker.droid_uuid_v1,
...tracker.birth_droid_uuid_v1,
])
expect(time).toMatchObject({
timestamp_100ns: "122192927999999999",
timestamp_iso8601: "1969-12-31T23:59:59.999Z",
});
expect(report.coverage.status).toBe("complete");
}
},
);
cliTest(
"reports Shell Link facts through CLI and stdio MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-windows-shortcut-cli-");
const path = join(root, "shortcut.lnk");
await writeFile(path, powershellHiddenIconSpoofShortcut());
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = shortcutContract.outputSchema.parse(result.json);
expect(evidence.normalized_result.header.show_command.decoded).toBe(
"minimized",
);
expect(evidence.normalized_result.string_data.arguments?.value).toContain(
"-WindowStyle Hidden",
);
const call = await cli.sharedMcpCall?.();
if (call === undefined) throw new Error("Expected a stdio MCP session");
const mcp = await call("inspect_windows_shortcut", { path });
expect(mcp.isError, JSON.stringify(mcp.content)).not.toBe(true);
const mcpEvidence = shortcutContract.outputSchema.parse(
mcp.structuredContent,
);
expect(mcpEvidence.normalized_result.observations).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "icon_location_differs_from_target",
value: true,
}),
]),
);
},
);
cliTest(
"resolves a relative Shell Link path against the working directory",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-windows-shortcut-cli-rel-");
await writeFile(
join(root, "shortcut.lnk"),
powershellHiddenIconSpoofShortcut(),
);
const result = await cli.run({
arguments: ["inspect-windows-shortcut", "shortcut.lnk", "--json"],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = shortcutContract.outputSchema.parse(result.json);
expect(evidence.subject?.local_path).toBe(join(root, "shortcut.lnk"));
},
);
cliTest("reports a wrong CLSID as a malformed target", async ({ cli }) => {
const root = await createTestTempDirectory("rea-windows-shortcut-cli-bad-");
const path = join(root, "broken.lnk");
await writeFile(path, wrongClsidShortcut());
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
details: {
operation: "inspect_windows_shortcut",
path,
format_failure: "malformed",
},
});
});
cliTest(
"keeps truncated StringData as a partial inventory",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-windows-shortcut-cli-trunc-",
);
const path = join(root, "truncated.lnk");
await writeFile(path, truncatedStringDataShortcut());
const result = await cli.run({
arguments: ["inspect-windows-shortcut", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const report = shortcutContract.outputSchema.parse(
result.json,
).normalized_result;
expect(report.coverage.status).toBe("partial");
expect(report.coverage.failing_offset).toEqual(expect.any(Number));
},
);