mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 19:41:20 -05:00
5f718ce30e
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer * fix(kernel): keep the regex worker's own error text inside its isolated source * refactor(kernel): exclude copies that run in another realm from the error-message guard by name The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them. * refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552) Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560) byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out. Refs #11548 * refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562) sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted. Refs #11548 * refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563) All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567) All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
139 lines
4.6 KiB
TypeScript
139 lines
4.6 KiB
TypeScript
import { stat } from "node:fs/promises";
|
|
import { basename } from "node:path";
|
|
|
|
import {
|
|
AnalysisCleanupLease,
|
|
ArtifactFileCleanupFailure,
|
|
} from "../resources/index.js";
|
|
import {
|
|
PACKAGE_FILE_FIELDS,
|
|
packageFileMismatch,
|
|
parseDpkgDebFields,
|
|
} from "../domain/desktop/debianPackageFile.js";
|
|
import type { DesktopPackage } from "../domain/desktop/desktopCapture.js";
|
|
import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
|
|
import {
|
|
sameRegularFileState,
|
|
type RegularFileState,
|
|
} from "../filesystem/RegularFile.js";
|
|
import {
|
|
StableFileHashFailure,
|
|
observeStableFile,
|
|
stableFileHashCause,
|
|
} from "../filesystem/StableFileHash.js";
|
|
import {
|
|
hostReadFailure,
|
|
hostFileKind,
|
|
isRefusedRead,
|
|
} from "./HostFileRead.js";
|
|
import type { QueryRunner } from "./HostQuery.js";
|
|
import { errorMessage } from "../kernel/errorMessage.js";
|
|
|
|
type PackageFileFacts = Pick<
|
|
DesktopPackage,
|
|
"package_file" | "package_file_reason"
|
|
>;
|
|
|
|
/**
|
|
* The file's SHA-256, size and identity, streamed through one admitted
|
|
* descriptor that must hold still while it is read, or why it could not be
|
|
* read. A close that fails is thrown with the handle's cleanup owner, which
|
|
* fails the capture, even when the capture was also cancelled.
|
|
*/
|
|
const hashPackageFile = async (
|
|
path: string,
|
|
signal: AbortSignal | undefined,
|
|
): Promise<
|
|
| {
|
|
readonly sha256: string;
|
|
readonly bytes: number;
|
|
readonly state: RegularFileState;
|
|
}
|
|
| string
|
|
> => {
|
|
let lease: AnalysisCleanupLease | undefined;
|
|
try {
|
|
return await observeStableFile(path, {
|
|
...(signal === undefined ? {} : { signal }),
|
|
retainFailedClose: (owner) => {
|
|
lease = new AnalysisCleanupLease(() => owner.close(), [path]);
|
|
},
|
|
});
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof RegularFileCleanupFailure && lease !== undefined)
|
|
throw new ArtifactFileCleanupFailure(cause, lease);
|
|
const failure = hostReadFailure(cause);
|
|
if (failure !== cause) throw failure;
|
|
const read = stableFileHashCause(cause);
|
|
if (!(cause instanceof StableFileHashFailure) || !isRefusedRead(read))
|
|
throw cause;
|
|
return `${path} could not be read: ${errorMessage(read)}`;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* The `.deb` file the caller says installed the application: its control
|
|
* fields from `dpkg-deb --field`, then its SHA-256 and size, recorded only
|
|
* when it holds the installed package's name, version and architecture;
|
|
* otherwise why not. The file itself is never copied.
|
|
*/
|
|
export const inspectPackageFile = async (
|
|
path: string,
|
|
installed: DesktopPackage,
|
|
runQuery: QueryRunner,
|
|
signal: AbortSignal | undefined,
|
|
): Promise<PackageFileFacts> => {
|
|
const refused = (reason: string): PackageFileFacts => ({
|
|
package_file_reason: reason,
|
|
});
|
|
if (
|
|
installed.kind !== "deb" ||
|
|
installed.version === null ||
|
|
installed.architecture === null
|
|
)
|
|
return refused(
|
|
`dpkg reports no installed version of the application (${installed.kind}${installed.name === null ? "" : ` ${installed.name}`}), so ${path} was not matched against it`,
|
|
);
|
|
const regularFile = await hostFileKind(path);
|
|
if (regularFile === null)
|
|
return refused(
|
|
`${path} could not be checked, so it was not read as a file`,
|
|
);
|
|
if (!regularFile) return refused(`${path} is not a regular file`);
|
|
// dpkg-deb and the hash read the file apart; its identity must hold across both.
|
|
const before = await stat(path).catch(() => undefined);
|
|
if (before === undefined) return refused(`${path} could not be read`);
|
|
const outcome = await runQuery(
|
|
"dpkg-deb",
|
|
["--field", path, ...PACKAGE_FILE_FIELDS],
|
|
signal,
|
|
);
|
|
if (outcome === "missing")
|
|
return refused(
|
|
`dpkg-deb is not installed, so the control fields of ${path} were not read`,
|
|
);
|
|
if (outcome === "failed" || outcome.exitCode !== 0)
|
|
return refused(`dpkg-deb could not read ${path} as a Debian package`);
|
|
const fields = parseDpkgDebFields(outcome.stdout);
|
|
if (fields === undefined)
|
|
return refused(
|
|
`dpkg-deb did not report the Package, Version and Architecture of ${path}`,
|
|
);
|
|
const mismatch = packageFileMismatch(fields, installed);
|
|
if (mismatch !== undefined) return refused(`${path} ${mismatch}`);
|
|
const hashed = await hashPackageFile(path, signal);
|
|
if (typeof hashed === "string") return refused(hashed);
|
|
if (!sameRegularFileState(hashed.state, before))
|
|
return refused(
|
|
`${path} changed while it was read, so its control fields and its hash could describe different files`,
|
|
);
|
|
return {
|
|
package_file: {
|
|
name: basename(path),
|
|
sha256: hashed.sha256,
|
|
size: hashed.bytes,
|
|
...fields,
|
|
},
|
|
};
|
|
};
|