Linora

0105. Bounded queries: linear-time name regex, find and toSvg budgets, XML-safe text

Status: Accepted (2026-11-28, DC2; fixes security review SR-01, SR-08, SR-15 in packages/doc; S1 wires the budgets in the doc workers). Changes shared/src/doc-api.ts (FindBudget, FindResult.truncated, SvgOptions).

Context

Shared doc workers run find and toSvg for every tenant (docs/security/review-2026-11.md):

Decision

  1. nameRegex runs only on V8’s linear-time engine (flag l, enabled by --enable-experimental-regexp-engine), against the first 256 UTF-16 units of each name.
    • A pattern it cannot compile, or a runtime without the engine (browsers, MCP), throws QueryError (field: 'nameRegex').
    • checkNameRegex / compileNameRegex expose the same rule to validators, so they cannot drift.
  2. find takes a budget {timeMs?, maxNodes?}. Work is counted per node, plus string tests by length, plus 1,024 units per regex test; the clock is read every 1,024 units.
    • When the budget is spent, the result has truncated: true, a lower-bound total and a cursor.
    • The cursor anchors at the last decided node, and the next call starts the walk there, so no node is tested twice and repeated calls always progress.
    • Even linear regexes cost ~20 us per name at the cap, so the budget, not the engine, bounds a request.
  3. toSvg takes {maxChars?, timeMs?}. Output is counted while it is built and the build throws QueryError('region') before passing the cap (default 32 Mi chars), or once the deadline passes (checked every 256 elements). Text is cut at 4,096 chars per node plus an ellipsis.
  4. XML safety at render time: esc maps C0 controls (except tab, LF, CR), U+FFFE, U+FFFF and lone surrogates to U+FFFD. The doc rules do not reject these characters:
    • they are legal user content (pasted text);
    • rejecting them would bump DOC_RULES_VERSION and invalidate existing pages, for a problem of one export format.
  5. Queries never write to the doc (no lazy caches), so a worker can interrupt one without leaving the doc inconsistent.

Alternatives

Consequences