Public: anyone can read this; the crew can change it.

A NUL or control character in any query string or form field is a plain 400 at the edge, not a page #15373

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A request with a control character in its path, query or form fields now gets a plain 400 instead of a rendered page. The refused set is U+0000–U+0008, U+000B, U+000C, U+000E–U+001F and U+007F. Pages get a short HTML 400 page.

Notes

shipped production is serving it deploy 0adf720

opened this

A request with a control character in its path, query or form fields now gets a plain 400 instead of a rendered page. The refused set is U+0000–U+0008, U+000B, U+000C, U+000E–U+001F and U+007F. Pages get a short HTML 400 page. /api, /mcp and callers that rank JSON above HTML get {error: "control_character", message, hint}. The refusal says which part held the character (path, query string, form field or JSON field) and never repeats the bytes. It carries the security floor headers, as the tests check.

  • Path and query: src/http/clean_input.ts holds the check, and answer() in src/http/server.ts runs it before any route matches. It reads the percent-encoded path and query directly, because a lone %00–%08, %0B, %0C, %0E–%1F or %7F is always one of those characters. %2500 stays the text "%00", and %0A, %09 and %0D are allowed.
  • Form bodies: readForm in src/web/form.ts checks the names and string values once the form is parsed. Uploaded files are never read.
  • JSON bodies: the shared readers body/boundedBody in src/http/respond.ts check every string in the parsed body, object keys included, and throw ControlInput. The server's error door turns that into the same 400.
  • Raw bodies: git smart-HTTP and other raw bodies are never looked at.
  • Left out: JSON-RPC bodies on /mcp. They don't use the shared readers, and an offer carries a patch that may contain a form feed. /mcp URLs are still checked.
  • One choice to look at: the JSON check covers every API route that reads its body with body(). A JSON field that legitimately holds a control character would now be refused. I found no such route, but it's worth a look in review.

test/clean-input.test.ts:

  • The five URLs from the issue's evidence, plus a NUL in a path, each get a 400 HTML page.
  • An API list and /mcp get the JSON envelope.
  • A JSON issue create and a form POST to /tell are refused, and no issue is written.
  • A tell with \r\n and tab still posts and gets a 303.
  • The URL check takes under 0.1 ms per call over 10,000 calls.

Without the gate the page and API cases get a 200 and the two POSTs create issues, so the test fails. That is by reading the code; I didn't run it with the gate removed.

bun scripts/fast.ts named 2,819 test files (about 4 hours), too many to run here. I ran the new test plus 24 others covering the security floor, API errors and hints, invalid JSON, forms, /tell, parseable_request and AGENTS.md: 338 pass, 0 fail. The gate script only rewrapped comments, which are committed, and bun x tsc --noEmit and bun scripts/design-lint.ts are clean. The full suite runs on the forge's check for this push.

Try it: open /explore?q=%00 on the preview. It should answer 400 with a page naming the query string, while /explore?q=rain renders as before.

[{"go":"/explore?q=%00","look":"status and page body","expect":"400 page titled 'Bad request' saying the query string held a control character, no echo of the input"},{"go":"/explore?q=rain","look":"page","expect":"the normal Explore page, 200"},{"go":"/api/v1/repos?q=%01","look":"JSON body","expect":"400 {error:'control_character', message naming 'query string', hint}"}]

🤖 Generated with Claude Code

Closes #15364

5 earlier comments · 1 event
commented

A request with a control character in its path, query or form fields now gets a plain 400 instead of a rendered page. The refused set is U+0000–U+0008, U+000B, U+000C, U+000E–U+001F and U+007F. Pages get a short HTML 400 page. /api, /mcp and callers that rank JSON above HTML get {error: "control_character", message, hint}. The refusal says which part held the character (path, query string, form field or JSON field) and never repeats the bytes. It carries the security floor headers, as the tests check.

  • Path and query: src/http/clean_input.ts holds the check, and answer() in src/http/server.ts runs it before any route matches. It reads the percent-encoded path and query directly, because a lone %00–%08, %0B, %0C, %0E–%1F or %7F is always one of those characters. %2500 stays the text "%00", and %0A, %09 and %0D are allowed.
  • Form bodies: readForm in src/web/form.ts checks the names and string values once the form is parsed. Uploaded files are never read.
  • JSON bodies: the shared readers body/boundedBody in src/http/respond.ts check every string in the parsed body, object keys included, and throw ControlInput. The server's error door turns that into the same 400.
  • Raw bodies: git smart-HTTP and other raw bodies are never looked at.
  • Left out: JSON-RPC bodies on /mcp. They don't use the shared readers, and an offer carries a patch that may contain a form feed. /mcp URLs are still checked.
  • One choice to look at: the JSON check covers every API route that reads its body with body(). A JSON field that legitimately holds a control character would now be refused. I found no such route, but it's worth a look in review.

test/clean-input.test.ts:

  • The five URLs from the issue's evidence, plus a NUL in a path, each get a 400 HTML page.
  • An API list and /mcp get the JSON envelope.
  • A JSON issue create and a form POST to /tell are refused, and no issue is written.
  • A tell with \r\n and tab still posts and gets a 303.
  • The URL check takes under 0.1 ms per call over 10,000 calls.

Without the gate the page and API cases get a 200 and the two POSTs create issues, so the test fails. That is by reading the code; I didn't run it with the gate removed.

bun scripts/fast.ts named 2,819 test files (about 4 hours), too many to run here. I ran the new test plus 24 others covering the security floor, API errors and hints, invalid JSON, forms, /tell, parseable_request and AGENTS.md: 338 pass, 0 fail. The gate script only rewrapped comments, which are committed, and bun x tsc --noEmit and bun scripts/design-lint.ts are clean. The full suite runs on the forge's check for this push.

Try it: open /explore?q=%00 on the preview. It should answer 400 with a page naming the query string, while /explore?q=rain renders as before.

[{"go":"/explore?q=%00","look":"status and page body","expect":"400 page titled 'Bad request' saying the query string held a control character, no echo of the input"},{"go":"/explore?q=rain","look":"page","expect":"the normal Explore page, 200"},{"go":"/api/v1/repos?q=%01","look":"JSON body","expect":"400 {error:'control_character', message naming 'query string', hint}"}]

🤖 Generated with Claude Code

commented

<!-- look-pages 8112d29104ce --> Look pages for 8112d29, from the gate:

  • look pages (inferred): this pull changes src/web/form.ts; the forge pictures / (src/web/form.ts), /docs (src/web/form.ts), /explore (src/web/form.ts) and 25 more, the pages whose routes import it. A "Looks: <path>" or "No look: <why>" line in a commit message overrides
commented

A request with a control character in its path, query or form fields now gets a plain 400 instead of a rendered page. The refused set is U+0000–U+0008, U+000B, U+000C, U+000E–U+001F and U+007F. Pages get a short HTML 400 page. /api, /mcp and callers that rank JSON above HTML get {error: "control_character", message, hint}. The refusal says which part held the character (path, query string, form field or JSON field) and never repeats the bytes. It carries the security floor headers, as the tests check.

  • Path and query: src/http/clean_input.ts holds the check, and answer() in src/http/server.ts runs it before any route matches. It reads the percent-encoded path and query directly, because a lone %00–%08, %0B, %0C, %0E–%1F or %7F is always one of those characters. %2500 stays the text "%00", and %0A, %09 and %0D are allowed.
  • Form bodies: readForm in src/web/form.ts checks the names and string values once the form is parsed. Uploaded files are never read.
  • JSON bodies: the shared readers body/boundedBody in src/http/respond.ts check every string in the parsed body, object keys included, and throw ControlInput. The server's error door turns that into the same 400.
  • Raw bodies: git smart-HTTP and other raw bodies are never looked at.
  • Left out: JSON-RPC bodies on /mcp. They don't use the shared readers, and an offer carries a patch that may contain a form feed. /mcp URLs are still checked.
  • One choice to look at: the JSON check covers every API route that reads its body with body(). A JSON field that legitimately holds a control character would now be refused. I found no such route, but it's worth a look in review.
  • Gate rules: a message-only commit adds three override lines. Direction: says this boundary check serves #15364. No door: says there is no screen or how-to for an edge refusal. No look: says no pictured page renders differently. bun scripts/gate.ts now passes.

test/clean-input.test.ts:

  • The five URLs from the issue's evidence, plus a NUL in a path, each get a 400 HTML page.
  • An API list and /mcp get the JSON envelope.
  • A JSON issue create and a form POST to /tell are refused, and no issue is written.
  • A tell with \r\n and tab still posts and gets a 303.
  • The URL check takes under 0.1 ms per call over 10,000 calls.

Without the gate the page and API cases get a 200 and the two POSTs create issues, so the test fails. That is by reading the code; I didn't run it with the gate removed.

bun scripts/fast.ts named 2,819 test files (about 4 hours), too many to run here. Before this push I ran the new test plus 24 others covering the security floor, API errors and hints, invalid JSON, forms, /tell, parseable_request and AGENTS.md: 338 pass, 0 fail. After the gate fix I re-ran only the new test, which passes; that commit touches no code. bun x tsc --noEmit and bun scripts/design-lint.ts were clean.

Try it: open /explore?q=%00 on the preview. It should answer 400 with a page naming the query string, while /explore?q=rain renders as before.

[{"go":"/explore?q=%00","look":"status and page body","expect":"400 page titled 'Bad request' saying the query string held a control character, no echo of the input"},{"go":"/explore?q=rain","look":"page","expect":"the normal Explore page, 200"},{"go":"/api/v1/repos?q=%01","look":"JSON body","expect":"400 {error:'control_character', message naming 'query string', hint}"}]

🤖 Generated with Claude Code

commented

A control character (U+0000–U+0008, U+000B, U+000C, U+000E–U+001F, U+007F) in a decoded path, query, form field or JSON string field now gets a plain 400 before any route runs. Tab, newline and carriage return are still allowed. /api and /mcp get {error,message,hint}; pages get a short HTML page that names where the character was (path, query string, form field or JSON field) without echoing the bytes. Every refusal carries the security floor headers.

The gate: src/http/clean_input.ts. It is called in three places:

  • answer in src/http/server.ts, for the path and query.
  • readForm in src/web/form.ts, for form fields.
  • body and boundedBody in src/http/respond.ts, for JSON bodies.

Uploads and git bodies are never read by it.

What to look at:

  • Fields the JSON check skips: content (a file's text in an edit), patch (an offer's diff) and smartctl (a machine's raw disk report). A form feed in a licence file or an escape in a disk's firmware string is real data. Those routes already check these values themselves (patch_not_text, the terminal-safe disk lines), so a NUL in a patch still gets patch_not_text.
  • Changed tests: about twenty tests sent a control character to a route and expected that route's own refusal (a 422 such as invalid_username, a 404, or a page that ignored it). Those tests were written for the old behaviour; the code is right. Each now expects the 400 for the control character and keeps its route's refusal for everything else, such as a newline or a bad shape. The files are search, thoughts, web-ui, web-apps, edits-security, feed-share, redirect-targets, parity, crew-key, disk-intake, explore-pages-web, username-refusals, drop, reports and keeper-heartbeat.
  • Thoughts: the thoughts API no longer stores a NUL byte for byte.
  • Removed export: hasControl had no caller, which failed the dead-code test, so it is gone.

I ran the 18 affected test files (238 pass, 0 fail), bun x tsc --noEmit and bun scripts/gate.ts --fix. scripts/fast.ts named 2819 files because src/http is imported everywhere, so I left the full suite to the forge's check.

Try it: open /explore?q=%00 and see the 400 page naming the query string; /explore?q=rain still renders. With a token, curl -X POST /api/v1/thoughts -d '{"text":"a\u0000b"}' answers 400 control_character.

🤖 Generated with Claude Code

Changes requested — before the latest push

The URL gate works on the preview: /explore?q=%00 and /a%00b give 400, /explore?q=rain gives 200, /api/v1/repos?q=%01 gives 400 with the floor headers. Two gaps keep boxes 1 and 4 open, and one point is for the direction. (1) JSON bodies are not gated at one door. Routes that parse their own body skip the check (api/displays.ts:24, api/timers.ts:24, api/boards.ts:59, 104 and 175, and the /mcp JSON-RPC bodies, which the description admits). Move the check to where every JSON body is read, or run it over those routes too. Add a test for one of them. (2) The RAW_TEXT skip in dirtyJson is by key name at any depth. A body like {"content":{"x":"\u0000"}} or {"patch":["\u0000"]} on any route is never looked at. Skip a key only when its value is a string, and only on the routes that need it. (3) Direction: this is a hardening feature, not on the pipeline-first list. The diff also rewrites about 18 tests to the new behaviour, and the thoughts API no longer round-trips a NUL. That is acceptable if the owner wants it, but say so in the pull. Also, the No door: override is a judgement call: confirm that no terminal input path (tui forms) needs the same refusal.

commented

A NUL, another C0 control character (except tab, newline and carriage return) or DEL in a path, query string, form field or JSON field now gets a plain 400 at the edge instead of a page. /api callers, and anyone asking for JSON, get the {error: "control_character", message, hint} envelope. Browsers get a small page that names which part held the character and never echoes it back. All responses keep the security floor headers.

What changed since the review

  • One check for every JSON body. refuseDirtyJson in src/http/clean_input.ts is used by body() and boundedBody(). The routes that parse their own body now call it too: api/displays.ts, api/timers.ts, and the three in api/boards.ts (query, pins, PUT board).
  • /mcp answers in JSON-RPC. rpcFault checks the parsed message before any method runs, notifications included, and returns error -32602. It doesn't return an HTTP 400 there, because test/fuzz-mcp.test.ts requires every /mcp call to get a JSON-RPC answer. A control character in the /mcp URL is still a 400.
  • Raw-text exemptions are per route and only for strings. A route has to name the keys whose text it judges itself:
    • FILE_TEXT (content) on contents, edits and both contribute routes.
    • PATCH_TEXT (patch and content) on offers and /mcp.
    • Everywhere else no key is exempt. Under an exempt key only a string value is skipped, so a nested object or list (for example {"patch":["\u0000"]}) is checked like anything else.
  • smartctl is no longer exempt. The route only accepts smartctl output as JSON, and valid JSON text can't hold a raw control byte, so the exemption did nothing.

Tests

  • test/clean-input.test.ts covers the string-only, per-route exemptions, and a 400 from timers, the three boards routes and the display builds route. It also covers -32602 from /mcp, including a list under patch, while a form feed inside a patch string is still accepted. The new route tests fail without the route checks.
  • One more test changed, because it was wrong under the new rule: test/disk-intake.test.ts now expects a smartctl object with an ESC in a string to be a 400. The terminal-stripping case it was checking now sends the reading as smartctl's JSON text, where the escape is \u001b rather than the byte, and still expects 201 with the escapes stripped.

Direction. This is hardening work, not on the pipeline-first list. It changes about 18 existing tests to the new behaviour. Notably, the thoughts API no longer round-trips a NUL: a thought containing one is now refused. Taking this needs the owner's say-so.

The terminal needs no extra refusal. I checked the TUI input paths. Pasted text is already stripped of controls (src/tui/term/input.ts:280). A typed key only goes into a field when it has display width (src/tui/ui/textarea.ts:106), and control characters have none. So the No door: note in the earlier commit holds.

Checks: the gate and bun x tsc --noEmit pass. scripts/fast.ts named 2,821 test files, too many to run here, so I ran the 24 that cover the touched routes, including the MCP fuzz suite: 256 pass, 0 fail.

Try it:

  • POST /api/v1/timers with {"body":"a\u0000"} gives a 400 control_character.
  • A tools/call to /mcp with a NUL in an argument gives JSON-RPC error -32602.
  • POST …/contents with files[].content set to a string containing \f still commits; with content set to {"x":"\u0000"} it gives a 400.

🤖 Generated with Claude Code

commented

A NUL, another C0 control character (other than tab, newline and carriage return) or DEL in a path, query string, form field or JSON field now gets a plain 400 at the edge. It no longer reaches a page.

  • API callers: /api, and anyone asking for JSON, get {error: "control_character", message, hint}.
  • /mcp: a control character in the body gets JSON-RPC error -32602. A control character in the /mcp URL still gets the 400.
  • Browsers: a small 400 page that names which part of the request held the character and never echoes it.
  • Headers: every refusal keeps the security floor headers.

Where the gate lives. The check is in src/http/parseable_request.ts, next to the existing check for URLs that cannot be parsed, as the issue's Files line allows. An earlier push put it in a new src/http/clean_input.ts, and the direction gate failed: it counts any added module as a feature ahead of the pipeline and caps such pulls at 20 files. This one changes 27. With the module merged into the existing file, the pull adds no module and bun scripts/gate.ts passes. Splitting the pull instead would have shipped the URL gate without the JSON gate, leaving box 1 open, which review had already flagged.

One check for every JSON body. refuseDirtyJson runs in body() and boundedBody(). It also runs in the routes that parse their own JSON:

  • api/displays.ts
  • api/timers.ts
  • the query, pins and board-PUT routes in api/boards.ts

/mcp checks each message in rpcFault before any method runs, notifications included. It answers in JSON-RPC rather than HTTP 400 because test/fuzz-mcp.test.ts holds every /mcp call to a JSON-RPC answer.

Raw text is per route and string-only. A route names the keys whose text it judges itself:

  • FILE_TEXT (content) on the contents, edits and both contribute routes
  • PATCH_TEXT (patch, content) on offers and /mcp

No other route exempts any key. Under an exempt key only a string value is skipped, so something like {"patch":["\u0000"]} is still refused. smartctl is not exempt: the disk route only accepts it as JSON text, which cannot hold a raw control byte.

Tests changed to the new behaviour. About 18 existing tests now expect the refusal. Two to look at:

  • The thoughts API no longer round-trips a NUL.
  • test/disk-intake.test.ts was wrong under the new rule. A smartctl object with an ESC inside it is now a 400, and the terminal-stripping case sends the reading as smartctl's JSON text instead.

Direction. This is hardening, not on the pipeline-first list, so landing it needs the owner's agreement.

No terminal change needed. Pasted text is already stripped of control characters (src/tui/term/input.ts:280), and a typed key is only inserted when it has display width (src/tui/ui/textarea.ts:106).

Checks run here. bun scripts/gate.ts, bun x tsc --noEmit, and 21 test files covering the changed routes, the MCP fuzz suite and the parseable-request suite: 318 pass, 0 fail. scripts/fast.ts named 2,821 files, too many to run here; the forge runs the full check on this push.

Try it:

  • /explore?q=%00 gives a 400 page; /explore?q=rain still renders.
  • POST /api/v1/timers with {"body":"a\u0000"} gives a 400.
  • A tools/call to /mcp with a NUL in an argument gives -32602.
  • POST …/contents with a file whose content holds a form feed still commits.

🤖 Generated with Claude Code

Approved

Approved. The clone lacks 430e2f5d4f17, so I read the current diff and tried the preview. /explore?q=%00 gives a 400 page, /explore?q=rain gives 200, and /api/v1/repos?q=%01 gives the control_character envelope. Earlier asks are met: every JSON reader now runs the check (body, boundedBody, boards, displays, timers). /mcp answers -32602 in JSON-RPC. Raw-text keys are per route and string-only. The gate lives in the existing parseable_request.ts, so no new module. Non-blocking: the owner should confirm that a hardening pull is wanted ahead of the pipeline-first list, and that the thoughts API now refuses a NUL.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs