Files
reverse-engineering-anything/src/windows/WindowsNativeLoader.test.ts
T
Saim Shafiqueandmorluto 731049ab9c fix(windows): gate transient writes on runtimeWriteTransient capability (#10607)
* fix(windows): gate transient writes on runtimeWriteTransient capability

A source checkout and the published 6.8.0 addon share package version
and native ABI 1, so the loader admits the older addon. JavaScript
staging then fails at source-transfer with Win32 87 because the addon
has no runtime_write_transient operation.

Native inspection now advertises runtimeWriteTransient, the loader
accepts it only as literal true, and writeTransientFile refuses before
dispatch with a typed nonretryable error pointing at an exact-source
rebuild. Older addons stay admitted for the controls they support.
Recovery text no longer presents a same-version tarball as enough for
an unreleased checkout.

Fixes #9286

* test(ghidra): expect the new native-authority remediation wording

The #9286 fix changed the doctor recovery text so a same-version
tarball is no longer presented as enough for an unreleased checkout;
the pinned expectation still asserted the old wording.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-18 15:28:36 -05:00

397 lines
13 KiB
TypeScript

import { createHash } from "node:crypto";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { windowsNativeArtifactFixture } from "../../tests/fixtures/windowsNativeArtifact.js";
import { inspectWindowsNativeReadiness } from "../process/WindowsAuthority.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import {
loadWindowsNativeAuthority,
windowsNativeFailureRemediation,
type WindowsNativeLoadHost,
} from "./WindowsNativeLoader.js";
const fixture = (
capabilities: {
filesystem?: string;
protocolStdin?: boolean;
jobMemoryLimit?: unknown;
runtimeWriteTransient?: unknown;
privateBridgeDescriptorAccess?: unknown;
} = {},
) => {
const { bytes, manifest } = windowsNativeArtifactFixture();
const { filesystem = "NTFS", ...inspectionCapabilities } = capabilities;
const identity = {
requestedPath: "D:\\fixture\\runtime",
finalPath: "\\\\?\\D:\\fixture\\runtime",
filesystem,
volumeSerial: "0123456789abcdef",
fileId: "0123456789abcdef0123456789abcdef",
size: 0,
directory: true,
};
let loads = 0;
const host: WindowsNativeLoadHost = {
platform: "win32",
architecture: "x64",
nodeApiVersion: "8",
packageVersion: "1.0.0",
readManifest: () => manifest,
readArtifact: () => bytes,
loadArtifact: () => {
loads++;
return {
call: () => ({
abiVersion: 1,
nodeApiVersion: 8,
architecture: "x64",
filesystem: identity,
privateDacl: true,
privateBridgeDescriptorAccess: true,
atomicJobAssignment: true,
killOnOwnerClose: true,
...inspectionCapabilities,
}),
};
},
};
return { host, manifest, bytes, loads: () => loads };
};
describe("packaged Windows native compatibility", () => {
it.each(["NTFS", "ReFS"])("admits %s filesystem identities", (filesystem) => {
const result = loadWindowsNativeAuthority(fixture({ filesystem }).host);
expect(result).toMatchObject({
available: true,
authority: { inspection: { filesystem: { filesystem } } },
});
});
it("rejects an unsupported filesystem identity", () => {
expect(
loadWindowsNativeAuthority(fixture({ filesystem: "exFAT" }).host),
).toMatchObject({ available: false, failureStage: "inspection" });
});
it.each([true, false])(
"preserves protocol stdin capability %s alongside existing job controls",
(protocolStdin) => {
const value = fixture({ protocolStdin });
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
available: true,
authority: {
inspection: {
protocolStdin,
atomicJobAssignment: true,
killOnOwnerClose: true,
},
},
});
},
);
it("admits the Job Object memory limit control only as an affirmative observation", () => {
expect(
loadWindowsNativeAuthority(fixture({ jobMemoryLimit: true }).host),
).toMatchObject({
available: true,
authority: { inspection: { jobMemoryLimit: true } },
});
// Earlier artifacts omit the control; callers must refuse a limit then.
const earlier = loadWindowsNativeAuthority(fixture().host);
if (!earlier.available) throw new Error(earlier.reason);
expect(earlier.authority.inspection.jobMemoryLimit).toBeUndefined();
expect(
loadWindowsNativeAuthority(fixture({ jobMemoryLimit: false }).host)
.available,
).toBe(false);
});
it("admits an affirmative transient-write capability alongside existing controls", () => {
expect(
loadWindowsNativeAuthority(
fixture({
runtimeWriteTransient: true,
protocolStdin: true,
jobMemoryLimit: true,
}).host,
),
).toMatchObject({
available: true,
authority: {
inspection: {
runtimeWriteTransient: true,
protocolStdin: true,
jobMemoryLimit: true,
atomicJobAssignment: true,
killOnOwnerClose: true,
},
},
});
});
it("keeps earlier artifacts admitted without claiming transient writes", () => {
const earlier = loadWindowsNativeAuthority(
fixture({ protocolStdin: true }).host,
);
if (!earlier.available) throw new Error(earlier.reason);
expect(earlier.authority.inspection.runtimeWriteTransient).toBeUndefined();
expect(earlier.authority.inspection).toMatchObject({
protocolStdin: true,
privateDacl: true,
atomicJobAssignment: true,
killOnOwnerClose: true,
});
});
it.each([false, "true", 1, null])(
"refuses a nonaffirmative transient-write inspection value %j",
(runtimeWriteTransient) => {
expect(
loadWindowsNativeAuthority(fixture({ runtimeWriteTransient }).host),
).toMatchObject({
available: false,
failureStage: "inspection",
reason: expect.stringContaining("runtimeWriteTransient"),
});
},
);
it("loads only after compatibility and digest checks, preserving producer identity", () => {
const value = fixture();
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
available: true,
authority: {
inspection: { filesystem: { requestedPath: "D:\\fixture\\runtime" } },
},
});
expect(value.loads()).toBe(1);
});
it.each([
[{ platform: "linux" as const }, "host"],
[{ architecture: "arm64" }, "host"],
[{ nodeApiVersion: "7" }, "host"],
[{ nodeApiVersion: undefined }, "host"],
[{ packageVersion: "2.0.0" }, "artifact"],
] as const)(
"rejects incompatible host metadata before loading: %j",
(override, failureStage) => {
const value = fixture();
expect(
loadWindowsNativeAuthority({ ...value.host, ...override }),
).toMatchObject({ available: false, failureStage });
expect(value.loads()).toBe(0);
},
);
it("names the absent packaged manifest and matching-version recovery", () => {
const value = fixture();
const missing = Object.assign(new Error("file absent"), {
code: "ENOENT",
});
expect(
loadWindowsNativeAuthority({
...value.host,
readManifest: () => {
throw missing;
},
}),
).toMatchObject({
available: false,
failureStage: "artifact",
reason: expect.stringMatching(
/Missing packaged Windows native file native\/windows\/build\/manifest\.json \(ENOENT\).*native\/windows\/build\/rea-windows-x64\.node.*native\/windows\/README\.md/u,
),
});
expect(value.loads()).toBe(0);
});
});
describe("Windows native artifact recovery", () => {
it("does not present matching package version as sufficient source recovery", () => {
const value = fixture();
const result = loadWindowsNativeAuthority({
...value.host,
readManifest: () => {
throw Object.assign(new Error("file absent"), { code: "ENOENT" });
},
});
expect(result).toMatchObject({
available: false,
failureStage: "artifact",
reason: expect.stringContaining(
"Matching package version alone is insufficient",
),
});
expect(result).toMatchObject({
reason: expect.stringContaining("from this exact source checkout"),
});
const readiness = inspectWindowsNativeReadiness("win32", result);
expect(readiness).toMatchObject({
failureStage: "artifact",
code: "runtime_missing",
remediation: expect.stringContaining(
"Matching package version alone is insufficient",
),
});
expect(readiness).toMatchObject({
remediation: expect.stringContaining("from this exact source checkout"),
});
expect(value.loads()).toBe(0);
});
});
describe("packaged Windows native artifact validation", () => {
it("rejects tampering even when the original file was a valid PE image", () => {
const value = fixture();
value.bytes[127] = 1;
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
available: false,
failureStage: "artifact",
reason: expect.stringContaining("SHA-256"),
});
expect(value.loads()).toBe(0);
});
it.each(["abiVersion", "nodeApiVersion", "architecture", "artifact"])(
"rejects invalid manifest %s",
(key) => {
const value = fixture();
expect(
loadWindowsNativeAuthority({
...value.host,
readManifest: () => ({ ...value.manifest, [key]: "invalid" }),
}).available,
).toBe(false);
expect(value.loads()).toBe(0);
},
);
it.each([
{ signature: "MZ", offset: 0 },
{ signature: "PE", offset: 64 },
])(
"rejects digest-valid high-bit $signature lookalikes before loading",
({ offset }) => {
const value = fixture();
value.bytes[offset] = (value.bytes[offset] ?? 0) | 0x80;
value.manifest.artifactSha256 = createHash("sha256")
.update(value.bytes)
.digest("hex");
expect(loadWindowsNativeAuthority(value.host).available).toBe(false);
expect(value.loads()).toBe(0);
},
);
it("rejects a digest-valid artifact for another machine", () => {
const value = fixture();
value.bytes.writeUInt16LE(0xaa64, 68);
value.manifest.artifactSha256 = createHash("sha256")
.update(value.bytes)
.digest("hex");
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
available: false,
reason: expect.stringContaining("PE32+"),
});
expect(value.loads()).toBe(0);
});
it("preserves host permission failure rather than promoting a partial probe", () => {
const value = fixture();
expect(
loadWindowsNativeAuthority({
...value.host,
loadArtifact: () => ({
call: () => {
throw new Error("private DACL: access denied");
},
}),
}),
).toMatchObject({
available: false,
failureStage: "self-test",
retryable: true,
reason: expect.stringContaining("private DACL: access denied"),
});
});
it("explains an OS policy refusal of the verified artifact without file-restore advice", () => {
const value = fixture();
const loaded = loadWindowsNativeAuthority({
...value.host,
loadArtifact: () => {
throw new Error(
"An Application Control policy has blocked this file.\r\n\\\\?\\C:\\rea\\rea-windows-x64.node",
);
},
});
expect(loaded).toMatchObject({
available: false,
failureStage: "load",
reason: expect.stringContaining(
"An Application Control policy has blocked this file.",
),
});
expect(loaded).not.toHaveProperty("retryable");
const readiness = inspectWindowsNativeReadiness("win32", loaded);
expect(readiness).toMatchObject({
code: "runtime_missing",
remediation: expect.stringContaining("Smart App Control"),
});
expect(readiness).not.toMatchObject({
remediation: expect.stringContaining("restore native/windows/build"),
});
});
it("describes NTFS and ReFS temporary volumes in self-test remediation", () => {
expect(windowsNativeFailureRemediation("self-test")).toContain(
"writable local NTFS/ReFS temporary directory",
);
});
it("requires every inspected native fact", () => {
const value = fixture();
const loaded = loadWindowsNativeAuthority({
...value.host,
loadArtifact: () => ({ call: () => ({ privateDacl: true }) }),
});
expect(loaded.available).toBe(false);
expect(loaded).toMatchObject({ failureStage: "inspection" });
expect(loaded).not.toHaveProperty("retryable");
});
it("rejects Windows controls without handle-bound Cutter descriptor access", () => {
const value = fixture({ privateBridgeDescriptorAccess: undefined });
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
available: false,
reason: expect.stringContaining("privateBridgeDescriptorAccess"),
});
});
});
describe("system authority cache", () => {
// Each case starts from an empty module-level cache.
beforeEach(() => {
vi.resetModules();
});
const systemAuthority = async () =>
(await import("./WindowsNativeLoader.js")).systemWindowsNativeAuthority;
const other = {
platform: HOST_PLATFORM.platform === "win32" ? "linux" : "win32",
architecture: "x64",
} as const;
it("keeps the host result after another host is injected", async () => {
const authority = await systemAuthority();
const host = authority();
const injected = authority(other);
expect(injected).not.toEqual(host);
expect(authority()).toEqual(host);
});
it("never caches an injected host's result for the host", async () => {
const authority = await systemAuthority();
const injected = authority(other);
const host = authority();
expect(host).not.toEqual(injected);
expect(authority()).toEqual(host);
});
});