Files
rea/src/artifacts/readStableArtifact.ts
T
ysopwnableandGitHub 01be082e9e feat(browser): trace captured website module imports (#879)
* feat(browser): trace captured website module imports

Compose verified exported scripts, inert native import syntax and a narrow
Chromium resolution port. Preserve query/fragment identity, import-map context,
all reported capture candidates and explicit unknown execution. Share named
CLI/MCP contracts and retain native reports, diagnostics and cleanup failures.

Add focused boundary regressions and a real module-loading oracle to the
existing conditional Chrome lane. No captured application code is executed
by module tracing, and no selected website asset is refetched.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve module contexts and source bindings

Keep capture completeness inline, validate actual source text against its
digest, and separate unsupported URL contexts from malformed input.
Use the importer origin for native module resolution and the selected map
base independently, including mixed HTTP/HTTPS and escaped HTML attributes.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
2026-10-07 14:59:52 +08:00

85 lines
2.6 KiB
TypeScript

import { createHash } from "node:crypto";
import { constants } from "node:fs";
import { lstat, open } from "node:fs/promises";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
/** Read one bounded regular file and reject replacement or in-place changes. */
export const readStableArtifact = async (
path: string,
maximumBytes: number,
signal?: AbortSignal,
): Promise<{ readonly bytes: Buffer; readonly sha256: string }> => {
signal?.throwIfAborted();
const before = await lstat(path, { bigint: true });
if (!before.isFile() || before.isSymbolicLink())
throw new ArtifactReaderFailure(
"path",
`Expected a regular file without a symlink: ${path}`,
);
if (before.size > BigInt(maximumBytes))
throw new ArtifactReaderFailure(
"limit",
`Selected artifact exceeds ${String(maximumBytes)} bytes: ${path}`,
);
const file = await open(
path,
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
);
try {
const initial = await file.stat({ bigint: true });
if (
!initial.isFile() ||
initial.dev !== before.dev ||
initial.ino !== before.ino
)
throw new ArtifactReaderFailure(
"integrity",
`Artifact changed before open: ${path}`,
);
const chunks: Buffer[] = [];
let count = 0;
for await (const chunk of file.createReadStream({
autoClose: false,
highWaterMark: 65536,
})) {
signal?.throwIfAborted();
const value: unknown = chunk;
if (!Buffer.isBuffer(value))
throw new ArtifactReaderFailure(
"format",
`Artifact stream did not return bytes: ${path}`,
);
const bytes = value;
count += bytes.length;
if (count > maximumBytes)
throw new ArtifactReaderFailure(
"limit",
`Selected artifact grew beyond its byte limit: ${path}`,
);
chunks.push(bytes);
}
const after = await file.stat({ bigint: true });
const current = await lstat(path, { bigint: true });
if (
[after, current].some(
(stat) =>
!stat.isFile() ||
stat.dev !== initial.dev ||
stat.ino !== initial.ino ||
stat.size !== initial.size ||
stat.mtimeNs !== initial.mtimeNs ||
stat.ctimeNs !== initial.ctimeNs,
) ||
BigInt(count) !== initial.size
)
throw new ArtifactReaderFailure(
"integrity",
`Artifact changed during read: ${path}`,
);
const bytes = Buffer.concat(chunks, count);
return { bytes, sha256: createHash("sha256").update(bytes).digest("hex") };
} finally {
await file.close();
}
};