fix(javascript): advertise the exact empty seed rule and match empty API keys

Express the empty-seed rule as a JSON-Schema-representable union, so
the advertised trace_application_feature schema rejects what the
server rejects, and match context-bridge seeds on the exact api_key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Iwakurakaoru0822
2026-10-06 22:00:14 -07:00
co-authored by Claude Opus 5.5
parent 6cc45e8dc9
commit 35e5508fd0
3 changed files with 64 additions and 42 deletions
@@ -1,7 +1,9 @@
import { writeFile } from "node:fs/promises"; import { writeFile } from "node:fs/promises";
import { join } from "node:path"; import { join } from "node:path";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect, it } from "vitest"; import { expect, it } from "vitest";
import { z } from "zod";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { javascriptApplicationAnalysisResultSchema } from "../domain/javascriptApplicationAnalysis.js"; import { javascriptApplicationAnalysisResultSchema } from "../domain/javascriptApplicationAnalysis.js";
@@ -181,23 +183,27 @@ it("selects an empty IPC channel with an exact empty seed", async () => {
]); ]);
}); });
it.each([ const seedSchema = traceApplicationFeatureInputSchema.shape.seed;
{ kind: "channel", value: "" }, const validateAdvertisedSeed = new Ajv2020({
{ kind: "string", value: "", match: "exact" }, strict: false,
])("accepts an exact empty seed: %j", (seed) => { validateFormats: false,
expect( }).compile(z.toJSONSchema(seedSchema, { io: "input" }));
traceApplicationFeatureInputSchema.shape.seed.safeParse(seed).success,
).toBe(true);
});
it.each([ it.each([
{ kind: "string", value: "" }, [{ kind: "channel", value: "", match: "exact" }, true],
{ kind: "channel", value: "", match: "contains" }, [{ kind: "string", value: "", match: "exact" }, true],
{ kind: "node-id", value: "", match: "exact" }, [{ kind: "channel", value: "/x" }, true],
])("rejects an empty seed that is not an exact literal: %j", (seed) => { [{ kind: "string", value: "" }, false],
const parsed = traceApplicationFeatureInputSchema.shape.seed.safeParse(seed); [{ kind: "channel", value: "" }, false],
expect(parsed.success).toBe(false); [{ kind: "channel", value: "", match: "contains" }, false],
}); [{ kind: "node-id", value: "", match: "exact" }, false],
] as const)(
"validates empty seed %j at runtime and in the advertised schema",
(seed, accepted) => {
expect(seedSchema.safeParse(seed).success).toBe(accepted);
expect(validateAdvertisedSeed(seed)).toBe(accepted);
},
);
it("pairs the same empty IPC channel across application versions", async () => { it("pairs the same empty IPC channel across application versions", async () => {
const left = await analyzeSource(IPC_SOURCE); const left = await analyzeSource(IPC_SOURCE);
@@ -225,3 +231,21 @@ it("pairs the same empty IPC channel across application versions", async () => {
"ipc-channel", "ipc-channel",
); );
}); });
it("selects an empty context-bridge API with an exact empty seed", async () => {
const output = await analyzeSource(
"const { contextBridge } = require('electron'); contextBridge.exposeInMainWorld('', {}); contextBridge.exposeInMainWorld('\"\"', {});",
);
const apis = new Map(
output.graph.nodes
.filter(({ kind }) => kind === "context-bridge-api")
.map((node) => [node.node_id, node.observations[0]?.properties.api_key]),
);
const matches = findApplicationFeatureSeeds(output.graph.nodes, {
kind: "api",
value: "",
match: "exact",
case_sensitive: true,
});
expect(matches.map(({ node_id: nodeId }) => apis.get(nodeId))).toEqual([""]);
});
+1 -1
View File
@@ -132,7 +132,7 @@ const memberFieldSuffixes = new Set([
const relevantFields = { const relevantFields = {
route: [".label", ".value", ".path"], route: [".label", ".value", ".path"],
api: [".label", ".api_name", ".key", ".methods", ".members"], api: [".label", ".api_name", ".api_key", ".key", ".methods", ".members"],
channel: [".label", ".channel", ".key"], channel: [".label", ".channel", ".key"],
module: [ module: [
".label", ".label",
+24 -26
View File
@@ -17,36 +17,34 @@ export const featureSeedMatchMode = (seed: {
}): "exact" | "contains" => }): "exact" | "contains" =>
seed.match ?? (seed.kind === "string" ? "contains" : "exact"); seed.match ?? (seed.kind === "string" ? "contains" : "exact");
const literalSeedKinds = [
"route",
"string",
"api",
"channel",
"module",
"native-export",
] as const;
/** /**
* Literal starting point for one application feature trace. An exact literal * Literal starting point for one application feature trace. An empty value,
* seed may be empty, such as the channel in `ipcMain.handle("")`; an empty * such as the channel in `ipcMain.handle("")`, must be an explicit exact
* containment seed would select every node, so it is rejected. * literal seed: empty containment would select every node.
*/ */
const applicationFeatureSeedSchema = z const applicationFeatureSeedSchema = z.union([
.strictObject({ z.strictObject({
kind: z.enum([ kind: z.enum(["node-id", ...literalSeedKinds]),
"node-id", value: boundedTextSchema,
"route",
"string",
"api",
"channel",
"module",
"native-export",
]),
value: z.string(),
match: z.enum(["exact", "contains"]).nullable().default(null), match: z.enum(["exact", "contains"]).nullable().default(null),
case_sensitive: z.boolean().default(false), case_sensitive: z.boolean().default(false),
}) }),
.superRefine((seed, context) => { z.strictObject({
if (seed.value !== "") return; kind: z.enum(literalSeedKinds),
if (seed.kind === "node-id" || featureSeedMatchMode(seed) === "contains") value: z.literal(""),
context.addIssue({ match: z.literal("exact"),
code: "custom", case_sensitive: z.boolean().default(false),
path: ["value"], }),
message: ]);
'An empty seed value requires an exact literal match: use match "exact" with a non-node-id kind',
});
});
/** Evidence-backed application graph and feature seed. */ /** Evidence-backed application graph and feature seed. */
export const traceApplicationFeatureInputSchema = z export const traceApplicationFeatureInputSchema = z