Skip to content

fix(api): spend-gate section history with empty key segments; query-role TLS settings - #540

Merged
fazpu merged 3 commits into
mainfrom
fix/spend-gate-empty-segments
Nov 6, 2026
Merged

fazpu merged 3 commits into
mainfrom
fix/spend-gate-empty-segments

Conversation

@fazpu

@fazpu fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member

Two small engine fixes found while building the UMC shared API.

1. Spend-gating bypass. GET /documents/{doc_id}/sections/{section_key:path}/history takes the section key as a path parameter, so .../sections/a//b/history routes. I verified this: it returns 200 with key a//b. However, _spend_gated_route returned None for any path with an empty segment before matching it, so this memory read skipped the D46 spend lease. The section-history match now runs before the empty-segment refusal. Every other route's parameters are single non-empty segments (Starlette's default converter is [^/]+), so a path with an empty segment matches none of them, gets a 404 and spends nothing; that refusal stays. Tests: three empty-segment section-history paths are now gated (test_memory_reads_are_spend_gated).

2. TLS for the customer-SQL connection. New settings SelfHostSettings.query_role_sslmode (libpq modes disable … verify-full) and query_role_sslrootcert (path), set via env REMEMBERSTACK_SELFHOST_QUERY_ROLE_SSLMODE / _SSLROOTCERT. Both are passed to the query-role psycopg.connect only when set; unset keeps libpq's defaults. They are documented on the configuration reference page and in D147 item 5. Tests check that the values reach the connection and that unset values add nothing.

Checks: ruff, pyright (0), the unit lane (3106 passed) and the docs checks.

🤖 Generated with Claude Code

https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 1 (gpt-6.1-sol, max effort)

VERDICT: REQUEST_CHANGES

Major — Spend gating still classifies a different path from Starlette routing, at src/rememberstack/surfaces/http_api.py:2848.

Two bypasses reproduce with a valid bearer token and a lease configured to refuse:

  • root_path: Mounting the app at /api lets GET /api/documents return 200, perform an inventory read, and make zero reservations. Starlette routes /documents; the gate sees /api/documents and returns None.
  • Encoded delimiters: POST /query/saved/a%3Fb/x/run and its %23 equivalent reach the saved-query execution port without reserving. Routing preserves the decoded ?/# inside the namespace, while request.url.path truncates there.

Simplest fix: Pass get_route_path(request.scope) to _spend_gated_route. It uses Starlette’s router-relative ASGI path and fixes both cases. Add end-to-end tests asserting that a refused lease prevents these reads.

The empty-segment fix works. Checks across all 40 build_api route registrations found no additional bypass involving trailing, repeated, or encoded slashes outside these path-representation issues.

TLS plumbing is correct and default-preserving: settings reach the connection factory, certificate paths become strings, and unset values omit both kwargs, preserving libpq defaults. The updated docs are accurate.

Validation: 76 focused tests passed. No files changed.

Disposition (Claude): accepted, fixed in ba50ece

The spend-lease middleware now classifies get_route_path(request.scope), Starlette's own router-relative, decoded path, instead of request.url.path. New end-to-end tests: a refused lease stops GET /api/documents when the app is mounted at /api, and stops POST /query/saved/a%3Fb/x/run and a%23b (423). All three fail without the fix.

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 2 (gpt-6.1-sol, max effort)

VERDICT: REQUEST_CHANGES

Major — Encoded final newline bypasses spend gating, at src/rememberstack/surfaces/http_api.py:2853.

get_route_path() preserves decoded \n. Starlette’s route regex ends with $, which also matches before a final newline. Consequently, /documents%0A reaches the document-list handler while _spend_gated_route() returns None.

Reproduced over real Uvicorn HTTP with a valid bearer token and a refusing lease:

  • /documents: 423, zero inventory reads.
  • /documents%0A: 200, one inventory read, zero reservations.

Section history and SQL/saved-query execution also bypass the lease this way. This is a regression: the previous request.url.path discarded the newline.

Simplest fix: Reject decoded LF in the path before classification and dispatch, returning 400. Add an end-to-end regression asserting /documents%0A cannot perform a read.

The original empty-segment fix and both round-1 fixes work. Auditing all 40 route registrations found no other mismatch among the tested trailing/repeated/encoded slash, delimiter, method, and root_path variants.

TLS plumbing is correct and default-preserving: profile settings reach the factory, CA paths become strings, and unset values omit both kwargs. Docs are accurate.

Validation: 167 focused tests passed, plus both docs checks. No files changed.

Disposition (Claude): accepted, fixed in 036e70f

The perimeter gate, which is always installed and the outermost layer apart from CORS, now answers 400 invalid_path for any ASCII control character (\x00–\x1f, \x7f) in the decoded path, before resolution, authentication, the lease or routing. No route accepts such a path legitimately. Test: /documents%0A, %0D and %00 are refused with 400, with no inventory read and no reservation.

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 3 (gpt-6.1-sol, max effort)

VERDICT: APPROVE

No blocker, major, or minor findings.

  • Verified the latest control-character rejection closes the newline bypass. No remaining bypass found across all 40 route registrations, including trailing/repeated/encoded slashes, encoded delimiters, and root_path.
  • TLS settings reach the query-role connection correctly. Unset values omit both keywords, preserving existing behavior. Docs are accurate.
  • Validation: 185 focused tests, both docs checks, 148,656 route/path combinations, and 286 real Uvicorn requests passed.

No files changed.

fazpu and others added 3 commits November 6, 2026 22:59
…ole TLS

- A section key is a path parameter, so /documents/x/sections/a//b/history
  routes, but _spend_gated_route refused any path with an empty segment
  before matching it, so that read skipped the spend lease. The
  section-history match now runs first; every other route's parameters are
  single non-empty segments, so such paths 404 and spend nothing.
- SelfHostSettings.query_role_sslmode / query_role_sslrootcert reach libpq
  for the customer-SQL connection; unset keeps libpq's defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
…imiters)

Codex round 1: under a mount (root_path) and with an encoded ? or # inside
a parameter, request.url.path differs from the path Starlette routes, so
reads skipped the lease. The gate now classifies get_route_path(scope).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Codex round 2: a decoded final newline matches a route pattern's $, so
/documents%0A read the inventory while the spend classification saw a
different path. The perimeter gate answers 400 for any ASCII control
character in the decoded path, before resolution, auth or routing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the fix/spend-gate-empty-segments branch from 036e70f to d5a202e Compare November 6, 2026 22:00
@fazpu
fazpu merged commit 0de5d15 into main Nov 6, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant