fix: bind identity and coverage to the producing rule (#1513)
* fix: bind identity and coverage to the producing rule Logical paths, container families, examined bytes, path-segment conventions, destination collisions, file-id leases, Mach-O CPU selection, and firmware cleanup now follow the observation that owns each rule. * fix: fail unmatched Mach-O slices and keep firmware roots independent A fat target whose architecture lipo did not list now stops before slice tools run. Chained fixups still count unread LC_DYLD_INFO rebase bytes, and a firmware cleanup failure no longer blocks the next workspace. * test: cover path identity and runtime families at the projection boundary NFC joins and segment conventions are now asserted by the path resolver, the Apple projection, and a real APK inventory instead of helper unit tests. * test: expect unread dyld rebase bytes in the Apple dispatch fixture The legacy LC_DYLD_INFO fixture keeps a rebase range this reader does not decode, so the public CLI and MCP check now requires that partial coverage beside the bind opcodes. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
@@ -6,7 +6,10 @@ import {
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import type { Stats } from "node:fs";
|
||||
|
||||
import { classifyArtifactContent } from "./ArtifactGraphConstruction.js";
|
||||
import {
|
||||
classifyArtifactBytes,
|
||||
classifyArtifactContent,
|
||||
} from "./ArtifactGraphConstruction.js";
|
||||
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../ArtifactHash.js";
|
||||
import type { ArtifactOccurrence } from "../../domain/artifactGraph.js";
|
||||
import {
|
||||
@@ -17,17 +20,6 @@ import { ArtifactReaderFailure } from "../ArtifactReader.js";
|
||||
import type { HashResult } from "../ArtifactHash.js";
|
||||
import { hashStableRootArtifactHandle } from "./hashStableRootArtifact.js";
|
||||
|
||||
const classifyContainerExtension = (
|
||||
path: string,
|
||||
): ArtifactOccurrence["artifact_format"] | undefined => {
|
||||
const lower = path.toLowerCase();
|
||||
const zipPackage = zipPackageFormatForPath(lower);
|
||||
if (zipPackage !== undefined) return zipPackage;
|
||||
for (const format of ["asar", "dmg", "pkg"] as const)
|
||||
if (lower.endsWith(`.${format}`)) return format;
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/** Classify and hash one file root through the same stable open descriptor. */
|
||||
export const classifyAndHashRoot = async (
|
||||
path: string,
|
||||
@@ -47,9 +39,7 @@ export const classifyAndHashRoot = async (
|
||||
"integrity",
|
||||
`Root artifact changed before inventory: ${path}`,
|
||||
);
|
||||
const format =
|
||||
classifyContainerExtension(path) ??
|
||||
(await classifyOpenedRoot(path, handle));
|
||||
const format = await classifyRootFormat(path, handle);
|
||||
const digest = await hashStableRootArtifactHandle(
|
||||
path,
|
||||
handle,
|
||||
@@ -79,14 +69,50 @@ const openRootFile = async (
|
||||
}
|
||||
};
|
||||
|
||||
const classifyOpenedRoot = async (
|
||||
const classifyRootFormat = async (
|
||||
path: string,
|
||||
handle: FileHandle,
|
||||
): Promise<ArtifactOccurrence["artifact_format"]> => {
|
||||
const magic = Buffer.alloc(ARTIFACT_CLASSIFICATION_PREFIX_BYTES);
|
||||
const observed = await handle.read(magic, 0, magic.length, 0);
|
||||
const prefix = magic.subarray(0, observed.bytesRead);
|
||||
if (hasZipSignature(prefix)) return "zip";
|
||||
return classifyArtifactContent(path, prefix, (await handle.stat()).size)
|
||||
.format;
|
||||
const lower = path.toLowerCase();
|
||||
if (hasZipSignature(prefix)) return zipPackageFormatForPath(lower) ?? "zip";
|
||||
if (lower.endsWith(".asar") && hasAsarHeader(prefix)) return "asar";
|
||||
if (
|
||||
lower.endsWith(".pkg") &&
|
||||
prefix.subarray(0, 4).toString("ascii") === "xar!"
|
||||
)
|
||||
return "pkg";
|
||||
if (lower.endsWith(".dmg") && (await hasKolyTrailer(handle))) return "dmg";
|
||||
const size = (await handle.stat()).size;
|
||||
const format = classifyArtifactContent(path, prefix, size).format;
|
||||
// Path suffixes stay role hints for nested members. A root whose container
|
||||
// magic did not match is classified from the bytes that were read.
|
||||
if (format === "asar" || format === "pkg" || format === "dmg")
|
||||
return classifyArtifactBytes(prefix, size);
|
||||
return format;
|
||||
};
|
||||
|
||||
/**
|
||||
* Electron ASAR starts with a size pickle whose payload is one uint32, then
|
||||
* a header pickle whose payload begins with a JSON object.
|
||||
*/
|
||||
const hasAsarHeader = (prefix: Buffer): boolean => {
|
||||
if (prefix.length < 16 || prefix.readUInt32LE(0) !== 4) return false;
|
||||
const headerSize = prefix.readUInt32LE(4);
|
||||
const payloadSize = prefix.readUInt32LE(8);
|
||||
if (headerSize < 16 || payloadSize !== headerSize - 4) return false;
|
||||
const stringLength = prefix.readUInt32LE(12);
|
||||
return (
|
||||
stringLength >= 2 && stringLength <= payloadSize - 4 && prefix[16] === 0x7b
|
||||
);
|
||||
};
|
||||
|
||||
const hasKolyTrailer = async (handle: FileHandle): Promise<boolean> => {
|
||||
const size = (await handle.stat()).size;
|
||||
if (size < 512) return false;
|
||||
const trailer = Buffer.alloc(4);
|
||||
const read = await handle.read(trailer, 0, trailer.length, size - 512);
|
||||
return read.bytesRead === 4 && trailer.toString("ascii") === "koly";
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user