HTTP face hardening and log discipline (#17) #28

Merged
svc-agent-writer merged 1 commit from issue-17-http-hardening into main 2026-11-13 02:45:43 +00:00

Closes #17.

What this adds

The controls that a request passes before the framework reads it, in src/messages_mind_mcp/face.py, and a log that holds counts, roles, codes and fault classes only.

Control Limit Answer
Host allow-list An exact entry of server.allowed_hosts, which is required and has no default (ADR 0011) 421
Origin No request may carry an Origin header 403
Rate, for each remote address A burst of server.rate_burst (20), then server.rate_per_second (5) 429 with Retry-After
Request body 65,536 bytes, and 5 seconds to arrive 413, 408
Store read of one tool call 10 seconds Error: code=deadline-exceeded
Open connections 128 503
Request header 16,384 bytes 400
  • GET /healthz answers ok. GET /healthz/sync answers ok when a sync ended in the last 15 minutes, else 503 with unavailable. Neither needs a token. No sync runs yet, so the second answers 503 today.
  • Every reply with a status outside 2xx is one fixed word. The MCP library writes request values into its error text, and the router's redirect for a trailing slash repeats the query.
  • The tool log line gains role=. A refusal is a count of its kind: refused=host count=3. A record of the HTTP server reaches the log only from a closed list of start and stop lines.
  • Every log line passes a scrub of the held tokens and of the word after Bearer.

A change that breaks an existing config

[server] now needs allowed_hosts. A config without it is refused at startup. config.example.toml shows the key.

What the review found

The two-axis review found seven defects. All are closed in this commit.

  1. A WebSocket handshake needed no token and made the HTTP server log the caller's address and query. The server now holds no WebSocket code.
  2. POST /healthz with a slow body skipped the rate limit. Only a GET /healthz with no body is outside the limit.
  3. GET /mcp/?x=… returned a 307 whose Location repeated the query. It is now 404.
  4. A sync record dated in the future read as fresh.
  5. rate_per_second accepted a string and a boolean.
  6. The server had no cap on open connections.
  7. The word "peer" named the socket address. In this repo "peer" means the People Mind peer, so the socket address is now the "remote address".

Tests

  • One test for each control: Host, Origin, burst, declared body, streamed body, slow body, deadline.
  • A log scan runs every tool over every fixture row. It sends fixture text in each field that a caller controls, a WebSocket handshake, bytes that are no request, and each refusal. It checks every line against a closed list of forms.
  • A test that an error reply names no file path and no secret.
  • A test that compares each limit in README.md and SECURITY.md with the number in the code.
  • make check passes on macOS with 270 tests. 37 mutation checks each fail a test.

Choices for the reviewer

  • The api refuses every Origin, its own included. The issue says "a foreign Origin". No MCP client sends the header.
  • The stale limit of /healthz/sync is 15 minutes. The sync worker (#12) must agree with it.
  • /healthz/sync is inside the rate limit, so a flood from one address makes a monitor on that address read 429.

Limits that the documents state

  • Behind a proxy, all callers share the proxy's one rate bucket, because the api reads no forwarding header.
  • The HTTP server has no time limit for a slow request header. The proxy of a published deployment must set one.
  • The deadline stops a query. It does not cover a wait for a lock (5 seconds) or a block in the operating system.

🤖 Generated with Claude Code

Closes #17. ## What this adds The controls that a request passes before the framework reads it, in `src/messages_mind_mcp/face.py`, and a log that holds counts, roles, codes and fault classes only. | Control | Limit | Answer | |---|---|---| | Host allow-list | An exact entry of `server.allowed_hosts`, which is required and has no default (ADR 0011) | `421` | | Origin | No request may carry an `Origin` header | `403` | | Rate, for each remote address | A burst of `server.rate_burst` (20), then `server.rate_per_second` (5) | `429` with `Retry-After` | | Request body | 65,536 bytes, and 5 seconds to arrive | `413`, `408` | | Store read of one tool call | 10 seconds | `Error: code=deadline-exceeded` | | Open connections | 128 | `503` | | Request header | 16,384 bytes | `400` | - `GET /healthz` answers `ok`. `GET /healthz/sync` answers `ok` when a sync ended in the last 15 minutes, else `503` with `unavailable`. Neither needs a token. No sync runs yet, so the second answers `503` today. - Every reply with a status outside `2xx` is one fixed word. The MCP library writes request values into its error text, and the router's redirect for a trailing slash repeats the query. - The tool log line gains `role=`. A refusal is a count of its kind: `refused=host count=3`. A record of the HTTP server reaches the log only from a closed list of start and stop lines. - Every log line passes a scrub of the held tokens and of the word after `Bearer`. ## A change that breaks an existing config `[server]` now needs `allowed_hosts`. A config without it is refused at startup. `config.example.toml` shows the key. ## What the review found The two-axis review found seven defects. All are closed in this commit. 1. A WebSocket handshake needed no token and made the HTTP server log the caller's address and query. The server now holds no WebSocket code. 2. `POST /healthz` with a slow body skipped the rate limit. Only a `GET /healthz` with no body is outside the limit. 3. `GET /mcp/?x=…` returned a `307` whose `Location` repeated the query. It is now `404`. 4. A sync record dated in the future read as fresh. 5. `rate_per_second` accepted a string and a boolean. 6. The server had no cap on open connections. 7. The word "peer" named the socket address. In this repo "peer" means the People Mind peer, so the socket address is now the "remote address". ## Tests - One test for each control: Host, Origin, burst, declared body, streamed body, slow body, deadline. - A log scan runs every tool over every fixture row. It sends fixture text in each field that a caller controls, a WebSocket handshake, bytes that are no request, and each refusal. It checks every line against a closed list of forms. - A test that an error reply names no file path and no secret. - A test that compares each limit in `README.md` and `SECURITY.md` with the number in the code. - `make check` passes on macOS with 270 tests. 37 mutation checks each fail a test. ## Choices for the reviewer - The api refuses every `Origin`, its own included. The issue says "a foreign Origin". No MCP client sends the header. - The stale limit of `/healthz/sync` is 15 minutes. The sync worker (#12) must agree with it. - `/healthz/sync` is inside the rate limit, so a flood from one address makes a monitor on that address read `429`. ## Limits that the documents state - Behind a proxy, all callers share the proxy's one rate bucket, because the api reads no forwarding header. - The HTTP server has no time limit for a slow request header. The proxy of a published deployment must set one. - The deadline stops a query. It does not cover a wait for a lock (5 seconds) or a block in the operating system. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
HTTP face hardening and log discipline (#17)
All checks were successful
ci/woodpecker/pr/validation Pipeline was successful
ci/woodpecker/push/validation Pipeline was successful
b4fae83740
The controls that a request passes before the framework reads it, and
a log that holds counts, roles, codes and fault classes only.

The face (`face.py`, the outermost layer of the app)
- Host: `server.allowed_hosts` is required and has no default (ADR
  0011). An entry is an exact DNS name or an IP address. A request with
  another Host, with no Host or with two gets 421.
- Origin: every request that carries an Origin header gets 403.
- Rate: a token bucket for each remote address, `server.rate_burst`
  (20) then `server.rate_per_second` (5). Over the rate is 429 with
  Retry-After. The table holds at most 1,024 addresses. Only a GET of
  /healthz with no body is outside the limit.
- Body: over 65,536 bytes is 413, declared or streamed. A body that
  does not arrive in 5 seconds is 408.
- Every reply with a status outside 2xx is one fixed word, with no
  header of the inner reply but Allow. The MCP library writes request
  values into its error text, and the router's redirect for a trailing
  slash repeats the query, so a redirect becomes 404. A fault that no
  layer caught is 500 with the word `error`.

The server
- A store read has a deadline of 10 seconds. A SQLite progress handler
  stops the query, and the call answers `deadline-exceeded`. A wait for
  a lock (5 seconds) and a block in the operating system are outside it.
- `/healthz` answers `ok`. `/healthz/sync` answers `ok` when a sync
  ended in the last 15 minutes, else 503 `unavailable`. Neither needs a
  token. No sync runs yet, so the second answers 503 today.
- uvicorn reads no forwarding header, holds no WebSocket code, serves
  at most 128 connections and takes a header of at most 16,384 bytes.
  Every option is stated and none is left to a default.

The log
- The tool line gains the role of the token: `role=read`.
- A refusal is a count of its kind, at most one line in 10 seconds:
  `refused=host count=3`. No line holds an address.
- A record of uvicorn reaches the log only when it is in a closed list
  of start and stop lines. Any other becomes `server=<kind> count=<n>`
  or the class of its fault. With WebSocket support, uvicorn logged
  the address, the path and the query of a handshake.
- A fault in a thread, an unraisable fault and a fault that nothing
  caught are one line with the class. A Python warning is dropped.
- Every line passes a scrub of the held tokens and of the word after
  `Bearer`.

Tests
- One test for each control: Host, Origin, burst, declared body,
  streamed body, slow body, deadline.
- A log scan runs every tool over every fixture row, sends fixture text
  in each field a caller controls, a WebSocket handshake, bytes that
  are no request and each refusal. It checks every line against a
  closed list of forms and finds no Handle, name, message word or
  token.
- An error reply names no file path and no secret.
- The documents state the limits that the code holds: a test compares
  each number.

Limits that the documents state
- Behind a proxy, all callers share the proxy's one rate bucket.
- uvicorn has no time limit for a slow request header. The proxy of a
  published deployment must set one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.