fix: refuse a stray brace, bound value length, and drop a quadratic scan

Addresses Copilot's round-14 review on #2035. Three suppressed comments,
all real; each measured on 1c595e96 before acting.

1. A brace surviving in a literal is an unmatched one, and not a legal
   literal character. The SDK's constructor rejects an unclosed `{` but
   reads a stray `}` as text, and this parser did the same:

     expandUriTemplateStrict("x://a}", {})  =>  "x://a}"

   -- a "URI" carrying a brace, with the panel enabling the read on it.
   Both halves are now refused the same way, so the withhold behavior
   covers the whole class rather than the half the SDK happened to catch.

2. Replacing `UriTemplate.expand` also removed its 1,000,000-character
   per-value ceiling. Nothing else bounded what reached the
   allocation-heavy encoders, so a pasted or programmatically supplied
   megabyte-plus value went straight through. Restored at the SDK's own
   limit -- the guard it was, not a new policy -- and the error names the
   offending variable.

3. `templateVariables` re-scanned an expression's whole varspec list for
   every one of its names to compute `conforming`, which is quadratic in a
   multi-name expression. Templates are server-controlled and the SDK
   admits one up to 1 MB, so a single expression can carry thousands of
   short comma-separated names, and this runs on the render thread.
   Iterating the varspecs supplies the name and its conformance in one
   pass.

npm run ci passes.

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-17 00:47:19 -04:00
commit c6b17c5241

View file

@ -270,10 +270,13 @@ export function templateVariables(uriTemplate: string): TemplateVariable[] {
for (const part of parseUriTemplate(uriTemplate)) {
if (part.kind !== "expression") continue;
const required = !OMITTABLE_OPERATORS.has(part.operator);
for (const name of part.names) {
const conforming = !part.varspecs.some(
(spec) => spec.name === name && spec.conforming === false,
);
// Iterating the varspecs (rather than the names, re-scanning the varspecs
// for each) keeps discovery linear. A template is server-controlled and the
// SDK admits one up to 1 MB, so a single expression can carry thousands of
// short comma-separated names -- and this runs on the render thread.
for (const spec of part.varspecs) {
const { name } = spec;
const conforming = spec.conforming !== false;
const existing = byName.get(name);
if (existing) {
existing.required = existing.required || required;
@ -668,6 +671,33 @@ export function expandUriTemplateStrict(
): string {
new UriTemplate(uriTemplate);
const parts = parseUriTemplate(uriTemplate);
// A brace left in a literal is an unmatched one, and it is not a legal
// literal character. The SDK's constructor rejects an unclosed `{` but reads
// a stray `}` as text, and this parser does the same, so without this check
// `x://a}` expanded to itself -- a "URI" carrying a brace, with the panel
// happily enabling the read. Both halves are now refused the same way.
const stray = parts.find(
(part) => part.kind === "literal" && /[{}]/.test(part.text),
);
if (stray) {
throw new Error(
`Unmatched brace in "${uriTemplate}" — an expression must be a complete {…}.`,
);
}
// Replacing `UriTemplate.expand` also dropped its per-value ceiling, and
// nothing else bounded what reaches the allocation-heavy encoders below: a
// pasted or programmatically supplied megabyte-plus value hit them unguarded.
// Kept at the SDK's own limit so this is the guard it was, not a new policy.
for (const [name, value] of Object.entries(values)) {
if (value.length > MAX_VALUE_LENGTH) {
throw new Error(
`The value for "${name}" exceeds the ${MAX_VALUE_LENGTH}-character limit.`,
);
}
}
const bad = parts.find((part) => part.kind === "expression" && part.invalid);
if (bad) {
throw new Error(
@ -679,6 +709,12 @@ export function expandUriTemplateStrict(
return expandParts(parts, values);
}
/**
* The pinned SDK's per-value ceiling, enforced here because this module no
* longer calls `UriTemplate.expand` — which is where that check lived.
*/
const MAX_VALUE_LENGTH = 1_000_000;
/**
* The outcome of an expansion: the URI, or the reason there isn't one.
*