Skip to content

Commit 3c9e9d7

Browse files
wings1029claude
andcommitted
fix(tools): reject null and non-object entries in availability group shape guard
Plugin-authored availability data is untyped at the descriptor boundary. Null entries and non-object primitives (string, number, boolean) inside allOf/anyOf arrays passed the shape guard but crashed the evaluator on "kind" in expr (TypeError). Reject them so the whole expression is stripped with a warning instead of crashing tool registration. This fixes the P1 bug where a malformed descriptor with null entries could crash plugin registration instead of just producing a warning. Co-Authored-By: Claude <[email protected]>
1 parent 482a225 commit 3c9e9d7

2 files changed

Lines changed: 71 additions & 21 deletions

File tree

‎src/plugins/tool-descriptor-cache.test.ts‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe("capturePluginToolDescriptor availability validation", () => {
290290
});
291291
expect(warnSpy).toHaveBeenCalledTimes(1);
292292
expect(warnSpy).toHaveBeenCalledWith(
293-
expect.stringContaining("Non-array availability group"),
293+
expect.stringContaining("Malformed availability group"),
294294
);
295295
// Malformed availability must be stripped so the evaluator never sees it
296296
expect(cached.descriptor.availability).toBeUndefined();
@@ -309,7 +309,7 @@ describe("capturePluginToolDescriptor availability validation", () => {
309309
});
310310
expect(warnSpy).toHaveBeenCalledTimes(1);
311311
expect(warnSpy).toHaveBeenCalledWith(
312-
expect.stringContaining("Non-array availability group"),
312+
expect.stringContaining("Malformed availability group"),
313313
);
314314
expect(cached.descriptor.availability).toBeUndefined();
315315
} finally {
@@ -335,7 +335,7 @@ describe("capturePluginToolDescriptor availability validation", () => {
335335
});
336336
// All groups are valid arrays — no shape warning expected
337337
const shapeWarnings = warnSpy.mock.calls.filter((c) =>
338-
String(c[0]).includes("Non-array availability group"),
338+
String(c[0]).includes("Malformed availability group"),
339339
);
340340
expect(shapeWarnings).toHaveLength(0);
341341
// Descriptor should preserve the valid availability
@@ -359,7 +359,7 @@ describe("capturePluginToolDescriptor availability validation", () => {
359359
optional: false,
360360
});
361361
expect(warnSpy).toHaveBeenCalledWith(
362-
expect.stringContaining("Non-array availability group"),
362+
expect.stringContaining("Malformed availability group"),
363363
);
364364
// Descriptor should strip the malformed availability
365365
expect(cached.descriptor.availability).toBeUndefined();
@@ -368,10 +368,58 @@ describe("capturePluginToolDescriptor availability validation", () => {
368368
}
369369
});
370370

371-
it("does not crash on non-object entries inside availability groups", () => {
371+
it("warns and strips availability when allOf contains null entries", () => {
372+
// Null entries crash the evaluator (TypeError on "kind" in null).
373+
// The shape guard must reject them so the whole expression is stripped
374+
// with a warning instead of crashing tool registration.
375+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
376+
try {
377+
const result = capturePluginToolDescriptor({
378+
pluginId: "test-plugin",
379+
tool: makeTool({
380+
availability: {
381+
allOf: [{ kind: "always" }, null as never],
382+
},
383+
}),
384+
optional: false,
385+
});
386+
expect(warnSpy).toHaveBeenCalledWith(
387+
expect.stringContaining("Malformed availability group"),
388+
);
389+
// Malformed availability must be stripped so the evaluator never sees it
390+
expect(result.descriptor.availability).toBeUndefined();
391+
} finally {
392+
warnSpy.mockRestore();
393+
}
394+
});
395+
396+
it("warns and strips availability when anyOf contains non-object entries", () => {
397+
// Non-object primitives (string, number, boolean) also crash the
398+
// evaluator. The shape guard must reject them.
399+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
400+
try {
401+
const result = capturePluginToolDescriptor({
402+
pluginId: "test-plugin",
403+
tool: makeTool({
404+
availability: {
405+
anyOf: ["not-an-object" as never, 42 as never],
406+
},
407+
}),
408+
optional: false,
409+
});
410+
expect(warnSpy).toHaveBeenCalledWith(
411+
expect.stringContaining("Malformed availability group"),
412+
);
413+
expect(result.descriptor.availability).toBeUndefined();
414+
} finally {
415+
warnSpy.mockRestore();
416+
}
417+
});
418+
419+
it("does not crash when availability group contains non-object entries", () => {
372420
// Plugin-authored data is arbitrary JS — null, primitives, and arrays
373-
// inside allOf/anyOf should not throw during the shape check. The
374-
// evaluator will diagnose them as malformed expressions later.
421+
// inside allOf/anyOf should never throw. They are rejected by the
422+
// shape guard and the availability is stripped with a warning.
375423
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
376424
try {
377425
const result = capturePluginToolDescriptor({
@@ -390,13 +438,13 @@ describe("capturePluginToolDescriptor availability validation", () => {
390438
}),
391439
optional: false,
392440
});
393-
// Should not throw — non-object entries are silently skipped
441+
// Should not throw — malformed entries are detected and stripped
394442
const shapeWarnings = warnSpy.mock.calls.filter((c) =>
395-
String(c[0]).includes("Non-array availability group"),
443+
String(c[0]).includes("Malformed availability group"),
396444
);
397-
expect(shapeWarnings).toHaveLength(0);
398-
// Valid availability preserved (non-object entries are evaluator's problem)
399-
expect(result.descriptor.availability).toBeDefined();
445+
expect(shapeWarnings.length).toBeGreaterThanOrEqual(1);
446+
// Availability is stripped because the group contained null/primitives
447+
expect(result.descriptor.availability).toBeUndefined();
400448
} finally {
401449
warnSpy.mockRestore();
402450
}

‎src/plugins/tool-descriptor-cache.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ function hasValidAvailabilityGroupShape(
164164
return false;
165165
}
166166
return expr.allOf.every((entry) => {
167-
// Plugin-authored data is untyped — primitives, null, and arrays
168-
// are not group expressions; skip them. The evaluator will
169-
// diagnose them as malformed expressions later.
167+
// Plugin-authored data is untyped — null and non-object primitives
168+
// (string, number, boolean) crash the evaluator (TypeError on
169+
// "kind" in expr). Reject them so the whole expression is stripped
170+
// with a warning instead of crashing tool registration.
170171
if (entry === null || typeof entry !== "object") {
171-
return true;
172+
return false;
172173
}
173174
return hasValidAvailabilityGroupShape(entry as ToolAvailabilityExpression);
174175
});
@@ -179,7 +180,7 @@ function hasValidAvailabilityGroupShape(
179180
}
180181
return expr.anyOf.every((entry) => {
181182
if (entry === null || typeof entry !== "object") {
182-
return true;
183+
return false;
183184
}
184185
return hasValidAvailabilityGroupShape(entry as ToolAvailabilityExpression);
185186
});
@@ -213,13 +214,14 @@ export function capturePluginToolDescriptor(params: {
213214
: undefined;
214215

215216
// Defensive shape guard: plugin-authored availability is untyped at this
216-
// boundary. Non-array allOf / anyOf values would reach the evaluator and
217-
// throw on .flatMap / .map, crashing tool registration. Diagnose and
218-
// strip the malformed expression so the evaluator stays pure.
217+
// boundary. Non-array allOf / anyOf values and null/non-object entries
218+
// would reach the evaluator and throw (TypeError on .flatMap / .map or
219+
// "kind" in expr), crashing tool registration. Diagnose and strip the
220+
// malformed expression so the evaluator stays pure.
219221
if (availability && !hasValidAvailabilityGroupShape(availability)) {
220222
console.warn(
221223
`[plugins] tool descriptor authoring error (${params.pluginId}/${params.tool.name}): ` +
222-
`Non-array availability group — allOf/anyOf values must be arrays`,
224+
`Malformed availability group — allOf/anyOf must be arrays of valid expression objects`,
223225
);
224226
availability = undefined;
225227
}

0 commit comments

Comments
 (0)