Skip to content

[Bug]: store SQL lists (#986): any creator can force every owner's sorted lists onto the in-memory path with an ordinary string #990

Closed

Description

@jimhoyd

Found in an adversarial review of PR #986 (#951, open, branch claude/store-sql-order-951-v2 at cb0509a). It is filed as an issue so it is not lost if the PR merges first.

Runtime revision and environment

PR #986 head cb0509a (includes main 348920f), Node 26 with node:sqlite 3.53.4, macOS (Apple silicon), store package test harness (test/direct.ts).

Summary

listInSql falls back to the in-memory path for the whole collection whenever the partial "anomaly" index holds any row. For a string property, anomaly() flags a row when instr(data -> '$.<field>', '\ud') > 0 (or '\uD').

That detection is meant to catch lone-surrogate escapes. It also matches an ordinary string with a backslash before ud, such as C:\udo\stuff, whose JSON text is C:\\udo\\stuff. So one record, written by any principal who may create, with that text in any sortable or filterable string property, moves every owner's sorted and filtered lists back to the pre-#951 path. They stay there until that record changes. The check is not scoped to the caller: it reads collection = '<name>' and all its rows.

Minimal reproduction

notes:
  mount: /api/notes
  ownership: owner
  maxRecords: 20000
  pageSize: 100
  schema: {type: object, additionalProperties: false, required: [title], properties: {title: {type: string, maxLength: 64}}}
  sortable: [title]
  filterable: [title]
  1. Principal alice holds 10,000 notes. GET /api/notes?sort=title&limit=100 as alice, 50 times: p50 0.49 ms.
  2. Principal mallory sends POST /api/notes {"title":"C:\\udo\\stuff"} and gets 201.
  3. alice's same list, 50 times: p50 9.17 ms, p99 16.4 ms, about 19× slower. The answers are still correct.

These are single-process, in-harness timings on one laptop run, not a benchmark. The PR's own table puts the in-memory path at 169/386 ms p50/p99 under concurrency 16 at 10k records, against 7.7/18.1 ms for SQL.

What I observed vs. assume

  • Observed: the slowdown above, triggered by a string with no lone surrogate at all, and an unchanged correct page.
  • Observed: the fallback is no slower than main's current path, which reads every record in scope. The worst case is the pre-[Request]: Store: order sorted and filtered lists in SQL near the 10,000-record maximum #951 behavior, not something new.
  • Assumed: a readers mount's scope is every owner's records, so it bears the most cost. I did not measure this.
  • Assumed, not measured: the anomaly check covers every sortable or filterable property, including ones a projected readers mount hides. In principle the latency of a projected list reveals one bit: whether some record, anywhere, holds \ud in a hidden property. I consider this negligible and did not demonstrate it.

Suggested direction (not prescriptive)

Flag only real lone surrogates. One option is to test the decoded bytes, for example the WTF-8 lead ED followed by A0–BF in CAST(data ->> '$.f' AS BLOB), instead of the escaped JSON text. A better one is to refuse lone surrogates at write time (#988), so the anomaly class becomes "rows written under another declaration" only. A per-scope fallback would also bound the blast radius.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions