Skip to content

[Bug]: store unique and intervals checks miss values with a lone surrogate; the duplicate then blocks the next activation #988

Closed

Description

@jimhoyd

Security-sensitive (integrity and availability). Found in an adversarial review of #984; the same comparison is in the intervals check from #902.

Runtime revision and environment

main at 348920f (#984 merged), Node 26 with node:sqlite (SQLite 3.53.4), macOS. Store package tests run with node --conditions=development --test.

Summary

The write-time checks for unique (#953) and intervals.within (#902) compare data ->> '$.<field>' against a bound JS string. For a string holding a lone UTF-16 surrogate (JSON escape \uD800), the two sides are different bytes:

  • ->> decodes the stored JSON escape as WTF-8 (ED A0 80);
  • node:sqlite binds the JS string as UTF-8 and replaces the lone surrogate with U+FFFD (EF BF BD).

So the check never matches. Activation (Collection.open for unique, overlapping() for intervals) compares the values consistently and does see the duplicate. The effect is that the constraint is bypassed at write time, and the next activation then refuses the database.

Minimal reproduction

tags:
  mount: /api/tags
  ownership: owner
  schema: {type: object, additionalProperties: false, required: [handle], properties: {handle: {type: string, maxLength: 20}}}
  unique: [handle]
  1. As principal A: POST /api/tags with raw body {"handle":"\ud800bob"}. Result: 201.
  2. As principal B: the same body. Result: 201 (expected 409 value_taken).
  3. Restart the server. Activation fails with Collection tags: two records hold the same handle, which unique refuses; change one of them first.

The same happens with intervals: {start, end, within: [desk]} on an owned collection when two owners each book {"desk":"\udc00","start":0,"end":10}. Both get 201, and the restart fails with records … hold overlapping intervals. The control case, desk: "d1", correctly gets 409.

Direct SQL check: SELECT ('{"h":"\ud800"}' ->> '$.h') = ? with the JS string '\ud800' bound gives 0.

A related, lower-impact effect on key (shared collections): the key column stores the bound value, so the distinct keys "\ud800a", "\ud801a" and the valid "�a" all collide. After the first create, each later one gets 409 key_exists.

What I observed vs. assume

  • Observed: the three results above, through the store's own handle() and the test harness (test/direct.ts), with no pattern on the property.
  • Observed: a pattern such as the docs' ^[a-z0-9_]{3,20}$ refuses the lone surrogate (it cannot match), so the documented wallet example is not affected. The schema only requires type: string and maxLength.
  • Assumed, not tested against a real HTTP server: that core's JSON body path hands the \uD800 escape through unchanged. The harness encodes the raw request text exactly as a client would send it.
  • Impact: any principal who may create can bypass unique/intervals for these values. One such pair makes the next deploy or restart fail activation until the operator edits the database by hand.

Suggested direction (not prescriptive)

Refuse strings that hold lone surrogates (/\p{Cs}/u) in the record validator for every stored string. Well-formed UTF-8 is then an invariant, and every SQL comparison, including #986's list keys, can rely on it. Alternatively, compare in SQL on both sides, for example ->> against json_extract(json_quote(?), '$'), though that fixes only this one site.

A related documentation point for "Exact values" in docs/STORE.md: only case is named. Unicode normalization (é as U+00E9 versus e + U+0301) and leading or trailing whitespace are also distinct values, which I checked in SQL. The advice to use a pattern covers them, but the docs could say so.

Activity

  1. added
    bug
    important
    on Sep 29, 2026
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

    bugimportant

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions