fix: reject non-finite numbers before they reach _meta

`StrictJsonValue` removed `undefined`, but the type cannot express the rest of
the gap: TypeScript has one `number`, and JSON has no encoding for `NaN` or
`±Infinity`, so `JSON.stringify` rewrites them to `null`.

This is reachable from ordinary input, not a careless caller.
`JSON.parse('{"a":1e400}')` returns `Infinity` on overflow — so text a user
types parses cleanly, satisfies the type, and reaches the server as `null`
while the editor still shows `1e400`.

`isSerializableJson` in `core/json` is the runtime half of `StrictJsonValue`,
applied at both boundaries that turn text into metadata:

- the editor's draft parser, which now reports "Numbers must be finite" inline
  and withholds the value rather than emitting one it cannot send;
- the CLI's `--metadata` / `--tool-metadata` parser, which throws. Silently
  sending `null` would be wrong, and so would falling back to the literal
  string — neither is what was asked for.

Tests cover the validator directly (including that `JSON.stringify` really does
write `null`, which is the reason it matters), the editor parser, and the CLI
flag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177mPHAdECD18nLLCTwR5rh
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-23 19:43:39 -04:00
commit 7f5992081e
3 changed files with 156 additions and 0 deletions

View file

@ -0,0 +1,47 @@
import { describe, it, expect } from "vitest";
import { isSerializableJson } from "@inspector/core/json/jsonUtils.js";
describe("isSerializableJson", () => {
it.each([
["a string", "a"],
["a finite number", 1.5],
["zero", 0],
["a negative number", -42],
["a boolean", true],
["null", null],
["a nested object", { a: { b: [1, 2, { c: "d" }] } }],
["an empty array", []],
])("accepts %s", (_label, value) => {
expect(isSerializableJson(value)).toBe(true);
});
it.each([
["NaN", NaN],
["Infinity", Infinity],
["-Infinity", -Infinity],
])("rejects %s, which JSON.stringify rewrites to null", (_label, value) => {
expect(isSerializableJson(value)).toBe(false);
// The reason it matters, asserted rather than asserted-about.
expect(JSON.stringify({ v: value })).toBe('{"v":null}');
});
it.each([
["undefined", undefined],
["a function", () => {}],
["a symbol", Symbol("s")],
["a bigint", 10n],
])("rejects %s, which is not JSON at all", (_label, value) => {
expect(isSerializableJson(value)).toBe(false);
});
it("descends into arrays and objects", () => {
expect(isSerializableJson({ a: [1, { b: Infinity }] })).toBe(false);
expect(isSerializableJson([[[NaN]]])).toBe(false);
expect(isSerializableJson({ a: [1, { b: 2 }] })).toBe(true);
});
it("rejects what `JSON.parse` produces for an overflowing literal", () => {
// The reachable path: valid JSON text that parses to an unsendable value.
expect(isSerializableJson(JSON.parse('{"n":1e400}'))).toBe(false);
});
});

View file

@ -24,6 +24,25 @@ describe("parseJsonObjectDraft", () => {
});
});
it.each([
["an overflowing literal", '{"n":1e400}'],
["a negative overflow", '{"n":-1e400}'],
["one nested in an array", '{"a":[1,1e400]}'],
])("rejects %s, which would be sent as null", (_label, text) => {
// `JSON.parse` accepts these; `JSON.stringify` then writes `null`, so the
// editor would show one value and the wire would carry another.
const result = parseJsonObjectDraft(text);
expect(result.ok).toBe(false);
expect(result.ok === false && result.error).toMatch(/finite/);
});
it("still accepts large-but-finite numbers", () => {
expect(parseJsonObjectDraft('{"n":1e308}')).toEqual({
ok: true,
value: { n: 1e308 },
});
});
it("rejects text that is not JSON", () => {
expect(parseJsonObjectDraft('{"a":')).toEqual({
ok: false,

View file

@ -1,4 +1,5 @@
import type { StrictJsonObject } from "@inspector/core/json/jsonUtils.js";
import { isSerializableJson } from "@inspector/core/json/jsonUtils.js";
/**
* What a JSON-object editor's draft text currently means: the object to emit,
@ -49,5 +50,15 @@ export function parseJsonObjectDraft(text: string): JsonObjectDraft {
error: "Must be a JSON object (`{ … }`) — changes are not applied",
};
}
// `JSON.parse` accepts numeric literals it cannot represent: `1e400` parses
// to `Infinity`, which `JSON.stringify` then writes as `null`. Accepting it
// here would show the user one value and send another.
if (!isSerializableJson(parsed)) {
return {
ok: false,
error:
"Numbers must be finite — a value like `1e400` overflows and would be sent as null",
};
}
return { ok: true, value: parsed };
}