Skip to content

fix(runtime): a query with a NUL byte is refused as invalid_request and still logged - #683

Merged
allisonmahmood merged 1 commit into
mainfrom
fix/666-nul-safe-call-row
Oct 15, 2026
Merged

allisonmahmood merged 1 commit into
mainfrom
fix/666-nul-safe-call-row

Conversation

@allisonmahmood

Copy link
Copy Markdown
Owner

Problem

RuntimeLog.begin writes the call row before the operation checks its arguments, and Postgres text can't hold NUL. So a published Postgres query, or a PostHog query or name, carrying a NUL failed the INSERT: the patch got source_unavailable, with no call row and no correlation id. The dev door (/api/connections/dev-read) avoided this with its own cleaning.

Fix

  • RuntimeLog.begin stores resource and sql as audit text: whatever the existing PostgresText rule refuses (NUL, unpaired surrogates) becomes U+FFFD. The query is cleaned before the 8 KiB cut, because U+FFFD takes 3 bytes where NUL takes 1 and the column checks octet_length(sql) <= 8192.
  • The cleaner, toPostgresText, sits next to PostgresText in packages/api/src/schemas.ts and reuses its regex, so the two definitions can't drift apart.
  • The arguments themselves are untouched, so the operation still refuses them with invalid_request, now carrying the row's correlation id.
  • The dev door's own audit cleaning is removed.

Tests, one per path. Published: postgres/Runtime.test.ts sends a NUL in sql and gets 400 invalid_request, with the row logged holding the stand-in. Dev: DevReads.test.ts now asserts invalid_request plus the row's outcome and cleaned resource. Run against main's RuntimeLog.ts (with the dev door's cleaning removed), both fail with 503.

Tested by hand

I used pnpm dev up team with a Postgres and a PostHog connection inserted directly into the local database. The connector refuses loopback sources and PostHog needs a real key, but neither NUL call reaches a source.

  • Published tier 2. A patch action calls ctx.connections.sales.query("SELECT 1\u0000 AS id", …); I published it as Ada and opened it as Ben with pnpm dev shot. The action received { code: "invalid_request", correlationId: "call_…" }. The row shows postgres.query, session, Ben, failure / invalid_request, and sql = SELECT 1� AS id.
  • Dev door. I POSTed /api/connections/dev-read with the environment CLI's machine key, once with a NUL in query and once with one in name. Both got 400 invalid_request with a correlation id. Both rows exist as machine calls on the PostHog connection, with U+FFFD in sql and in resource respectively.
  • Admin view. Both connection pages list all three refused calls, with the stand-in visible.

patchy dev itself never sends either query to the instance. Postgres reads PGlite fixtures, and the local PostHog binding decodes PostHogQuery, which refuses NUL, before forwarding.

Surfaces

I walked CLAUDE.md's "Hit every surface" list and none of it needed changes. The wire contract is the same (docs/API.md already says every dev-door call is recorded, refusals included). Nothing changes in the CLI, the dev loop, vocabulary, migrations or company resources. On served pages, the connection pages render the stored text, and I checked them by hand.

Closes #666

Made by Claude Opus 5.5 in Claude Code, running in T3 Code.

@github-actions github-actions Bot added area:server Touches the server area area:integrations Company connections and integration implementations size:s Under 100 changed lines labels Oct 15, 2026
…nd still logged

RuntimeLog.begin writes the call row before the operation checks its
arguments, and Postgres text can't hold NUL. A published query or
PostHog name carrying one failed the INSERT: the patch got
source_unavailable, no row and no correlation id.

begin now stores resource and sql as audit text, replacing what the
PostgresText rule refuses (NUL, unpaired surrogates) with U+FFFD before
the 8 KiB cut. The arguments are untouched, so the operation still
refuses them with invalid_request. The dev door's own NUL cleaning goes.

Closes #666

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the fix/666-nul-safe-call-row branch from dc5e855 to b2c1f7a Compare October 15, 2026 17:26
@allisonmahmood
allisonmahmood merged commit 3768a54 into main Oct 15, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the fix/666-nul-safe-call-row branch October 15, 2026 17:36
allisonmahmood added a commit that referenced this pull request Oct 16, 2026
…u when they fail (#720)

Release 13's What's new entry, written from `c9b8fc4`, ahead of
deploying it. `patchy` and `CURRENT_RELEASE` move to `0.13.0`, so CLIs
and patch repos installed before this deploy are told to upgrade.

| Change | PRs |
| --- | --- |
| New: Home puts the patches you use first | #713, #716, #719 |
| New: Every patch in one list | #715 |
| Improved: Each patch's page leads with Open | #717 |
| New: See when your tools are failing | #718 |
| Fixed: Everyone has a name | #687 |
| Improved: Your company's look is easier to get right | #697, #696,
#703, #688 |
| Improved: Forms follow your look's error states | #691, #692 |
| Improved: Clearer answers when a publish goes wrong | #686, #685, #689
|
| Fixed: Your agent's Patchy commands are steadier | #695, #704, #680,
#681 |
| Behind the scenes: stricter checks on static pages and tools' database
requests; malformed requests are logged | #682, #693, #683 |
| Behind the scenes: usage numbers count every publish, sharing change
and restore | #698, #699, #700, #690 |
| Test and tooling only, no line | #705, #702, #701, #694, #684, #607 |

Made by Claude Opus 5.5 in Claude Code, running in T3 Code.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:integrations Company connections and integration implementations area:server Touches the server area size:s Under 100 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(runtime): a query containing a NUL byte fails as source_unavailable and leaves no call row

1 participant