fix(application): budget bridge candidate projection by bytes

This commit is contained in:
morluto
2026-10-09 05:15:23 +08:00
parent 73b09f4c5c
commit 3d450a29fc
7 changed files with 302 additions and 34 deletions
+60
View File
@@ -494,4 +494,64 @@ describe("Apple application bridge candidates", () => {
],
]);
});
it("reports candidate omission when one root expands beyond the bridge safety budget", () => {
const escapedDirectory = `${"é".repeat(100)}${'"'.repeat(120)}`;
const scripts = Array.from({ length: 129 }, (_, index) => ({
path: `Payload/Fixture.app/${escapedDirectory}/script-${String(index).padStart(3, "0")}.js`,
format: "javascript-bundle",
}));
const native = Array.from({ length: 128 }, (_, index) =>
macho(
`Payload/Fixture.app/${escapedDirectory}/lib${String(index).padStart(3, "0")}.dylib`,
),
);
const result = project(
inventoryEvidence("zip", "Fixture.zip", [...scripts, ...native]),
);
expect(result.components.javascript).toHaveLength(129);
expect(result.components.native_libraries).toHaveLength(128);
expect(result.bridge_candidate_coverage).toMatchObject({
status: "partial",
total_candidates: 129 * 128,
emitted_candidates: result.bridge_candidates.length,
omitted_candidates: 129 * 128 - result.bridge_candidates.length,
});
expect(result.bridge_candidates.length).toBeGreaterThan(0);
expect(result.bridge_candidates.length).toBeLessThan(129 * 128);
expect(
Buffer.byteLength(JSON.stringify(result.bridge_candidates)),
).toBeLessThanOrEqual(2 * 1024 * 1024);
expect(result.coverage).toMatchObject({
status: "partial",
inventory_complete: true,
});
expect(result.limitations).toContain(
`Bridge candidate pairs exceeded the projection safety budget; ${result.bridge_candidate_coverage.omitted_candidates} hypotheses are omitted. Component arrays still include every component from the supplied inventory pages.`,
);
});
it("preserves candidate pairing for components without an application root", () => {
const result = project(
inventoryEvidence("ipa", "Fixture.ipa", [
{ path: "Payload/Fixture.app/Info.plist" },
{
path: "Payload/Fixture.app/Resources/app.js",
format: "javascript-bundle",
},
{ path: "SwiftSupport/outside.js", format: "javascript-bundle" },
{ path: "SwiftSupport/liboutside.dylib", format: "mach-o" },
]),
);
expect(result.bridge_candidates).toContainEqual({
source_path: "SwiftSupport/outside.js",
native_path: "SwiftSupport/liboutside.dylib",
basis: "javascript-and-native-content",
});
expect(result.bridge_candidate_coverage).toMatchObject({
status: "complete",
});
});
});
+43 -15
View File
@@ -18,6 +18,10 @@ import { parseArtifactInventoryEvidence } from "../artifactInventoryEvidence.js"
import { evidenceSchema } from "../evidence.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
import {
bridgeCandidateCoverageSchema,
projectCartesianCandidates,
} from "../bridgeCandidateProjection.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const pathSchema = z.string().min(1);
@@ -74,6 +78,7 @@ export const appleApplicationProjectionResultSchema = z.strictObject({
]),
}),
),
bridge_candidate_coverage: bridgeCandidateCoverageSchema,
coverage: z.strictObject({
status: z.enum(["complete-within-inventory", "partial"]),
inventory_complete: z.boolean(),
@@ -162,6 +167,7 @@ export const projectAppleApplication = (
sidecars: entries.some(({ path }) => isSidecar(path)),
symlinks: entries.some(({ kind }) => kind === "symlink"),
unattributed,
bridgeCandidateCoverage: bridgeProjection.coverage,
});
const withoutId = {
root_sha256: inventory.manifest.root_sha256,
@@ -179,10 +185,12 @@ export const projectAppleApplication = (
.sort(compare),
runtime_families: runtimeFamilies,
bridge_candidates: bridgeProjection.candidates,
bridge_candidate_coverage: bridgeProjection.coverage,
coverage: {
status: complete
? ("complete-within-inventory" as const)
: ("partial" as const),
status:
complete && bridgeProjection.coverage.status === "complete"
? ("complete-within-inventory" as const)
: ("partial" as const),
inventory_complete: inventory.complete,
},
limitations,
@@ -310,7 +318,7 @@ const identifyRuntimeFamilies = (all: readonly Component[]) => {
return [...families].sort(compare);
};
/** Pair scripts and native code only within the same application root. */
/** Pair scripts and native code within matching application-root groups. */
const identifyBridgeCandidates = (
scripts: readonly Component[],
native: readonly Component[],
@@ -318,18 +326,32 @@ const identifyBridgeCandidates = (
) => {
const owner = (path: string): string | null =>
roots.find((root) => isWithin(path, root)) ?? null;
return {
candidates: scripts.flatMap((script) => {
const root = owner(script.path);
return native
.filter(({ path }) => owner(path) === root)
.map((item) => ({
source_path: script.path,
native_path: item.path,
basis: bridgeBasis(item.path),
}));
const scriptGroups: { root: string | null; left: Component[] }[] = [];
for (const script of scripts) {
const root = owner(script.path);
const last = scriptGroups.at(-1);
if (last?.root === root) last.left.push(script);
else scriptGroups.push({ root, left: [script] });
}
const nativeByRoot = new Map<string | null, Component[]>();
for (const item of native) {
const root = owner(item.path);
const scoped = nativeByRoot.get(root) ?? [];
scoped.push(item);
nativeByRoot.set(root, scoped);
}
const groups = scriptGroups.map(({ root, left }) => ({
left,
right: nativeByRoot.get(root) ?? [],
}));
return projectCartesianCandidates({
groups,
createCandidate: (source_path, item) => ({
source_path,
native_path: item.path,
basis: bridgeBasis(item.path),
}),
};
});
};
const bridgeBasis = (
@@ -355,6 +377,7 @@ const projectionLimitations = (facts: {
readonly sidecars: boolean;
readonly symlinks: boolean;
readonly unattributed: number;
readonly bridgeCandidateCoverage: AppleApplicationProjectionResult["bridge_candidate_coverage"];
}): string[] => [
...(!facts.complete
? ["Source inventory pages are incomplete; absence is unknown."]
@@ -392,6 +415,11 @@ const projectionLimitations = (facts: {
"AppleDouble sidecar entries (._* files and __MACOSX/) describe neighbouring files and are excluded from bundle roles.",
]
: []),
...(facts.bridgeCandidateCoverage.status === "partial"
? [
`Bridge candidate pairs exceeded the projection safety budget; ${facts.bridgeCandidateCoverage.omitted_candidates} hypotheses are omitted. Component arrays still include every component from the supplied inventory pages.`,
]
: []),
"Bundle roles follow path conventions. Read each info_plist_path with inspect_plist for CFBundleExecutable, identifiers, and declared services.",
"Bundle identifiers and signing claims require dedicated plist and CMS parsing; this projection reports only exact paths and hashes.",
"Bridge candidates are path-based hypotheses, not observed runtime calls.",