Medlar/docs/tests/S023-port-server.md

48 lines
5.5 KiB
Markdown

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# S023 — The port and its protection: test file
Written by the worker that built it, run by the test phase. Nothing here has been tested until its Result says so.
## Built
- `crates/core/src/connector/mod.rs` · `Connector::start(on, port) -> (Connector, Receiver<Arrival>)`, `set(on, port)` live, `status()`, `log()`, `on_change`; `guard` (Host, Origin, preflight, type), `Gate` (16 connections, bucket 30 + 5/s).
- `connector/wire.rs` · strict HTTP/1.1 read (head 16 KiB, body 1 MiB, Content-Length only), `answer` (no CORS).
- `connector/link.rs` · `Body` (object or list), `Raw::check` -> `Incoming`, `Incoming::task(&Secrets, common)`.
- `connector/peer.rs` · sender / listener program name from `/proc/net/tcp` and `/proc/*/fd`.
- `crates/core/tests/connector_nacre.json` · Nacre's `custom_body` output, as a fixture. `docs/CONNECTOR.md` · format.
Nothing is wired into the app yet (S024). Reach it from a test: `Connector::start(true, 0)`, read the port from `status()`.
## Checks
| # | Risk | Do this | Expect | How | Result |
|---|---|---|---|---|---|
| 1 | HIGH | Start on port 0; `ss -ltn` / `status()` | `On(p)`, bound on 127.0.0.1 only, nothing on 0.0.0.0 or :: | integration | pass |
| 2 | HIGH | curl POST the fixture with `Content-Type: application/json` | 200 `{"accepted":1,"refused":[]}`; one `Arrival`: url, name `a.zip`, page `https://example.com/`, user agent `UA`, secret cookies `k=v`, `task_headers()` has User-Agent and Referer, no Cookie | integration | pass |
| 3 | HIGH | `Host: evil.example:p`, `Host: 127.0.0.1:1`, two Host headers, no Host | 403 `host`, no Arrival, log entry Refused(Host) | integration | pass |
| 4 | HIGH | Add `Origin: https://evil.example`, `Origin: null`, `Sec-Fetch-Site: cross-site`, `Sec-Fetch-Mode: cors` (each alone) | 403 `origin` | integration | pass |
| 5 | HIGH | POST with `application/x-www-form-urlencoded`, `multipart/form-data`, `text/plain`, `application/json; charset=latin1` | 415 `content-type` | integration | pass |
| 6 | HIGH | `OPTIONS /` with `Access-Control-Request-Method: POST`; also POST with that header | 405 `preflight`; no `Access-Control-*` header in any answer of checks 2–9 | integration | pass |
| 7 | HIGH | Link headers `{"X-A":"1\r\nX-B: 2","Connection":"close","Range":"bytes=5-","Accept-Encoding":"gzip","If-None-Match":"x","Authorization":"Bearer t"}` | X-A, Connection, Range, Accept-Encoding, If-None-Match dropped (in `dropped`); Authorization only in `secret.headers`; user agent with `\n` dropped | unit | pass |
| 8 | HIGH | Links `javascript:alert(1)`, `file:///etc/passwd`, `data:,x`, `http://`, a 9 KiB URL, `https://u:p@h/f` | first five refused (`scheme`/`invalid`), last accepted as `https://h/f` with the login in `secret` | unit | pass |
| 9 | HIGH | Filename `../../.bashrc`, `a\u0000b`, 400 chars | made safe by `http::name::safe` (no slashes, no leading dot, capped) | unit | pass |
| 10 | HIGH | `Incoming::task` with `Secrets::new(Bus::Off)` | task.secret is a `task:` name; `secrets.load` gives the cookies; task headers carry no secret (store accepts it) | unit | pass |
| 11 | HIGH | Log after checks 2–8 | entries with time, what, outcome; no cookie value, login or query string appears in any `what` | integration | pass |
| 12 | MED | GET `/` | 200 `{"name":"Medlar","version":...}`, not logged | integration | pass |
| 13 | MED | List of 3 links, one bad; then a list of 1001; then `{}`; then `[]`; then `not json` | 200 accepted 2 refused index 1; 413 `too-many`; 422 `no-links`; 422; 400 `body` | integration | pass |
| 14 | MED | Body 1 MiB + 1; `Transfer-Encoding: chunked`; head of 20 KiB; open and send nothing for 6 s | 413; 411; 413; 408 or closed | integration | pass |
| 15 | MED | 40 requests at once from one loop | the first ≈30 answered normally, later ones 429 `busy` | integration | pass (40 requests sent in a loop; parallel ones beyond 16 connections are dropped silently, see Doubts) |
| 16 | MED | Hold the port with another listener, start on it | `Taken { by: Some(test binary name) }`, `words()` names the port and the fix; free it: `On` within ~5 s | integration | pass |
| 17 | MED | `set(false, p)`, then `set(true, q)` | port closed (connect refused), then open on q; status follows | integration | pass |
| 18 | LOW | Sender of check 2 | `from.process` is `curl` (a process of this user) | integration | not run: the sender is the test binary, not curl |
## Needs
Loopback only, ports chosen free (port 0) — never 15151 or 38151 or 17616 while the user's Medlar runs. curl for check 2. No network.
## Doubts
- Never compiled beyond `cargo check`; never run. The hand-written HTTP reader is the main risk: partial reads, a request split over several packets, HTTP/1.0 clients.
- Nacre's request (ureq): check it sends `Host: 127.0.0.1:<port>`, no `Origin`, no chunked body — else check 2 passes with curl but Nacre fails.
- `Sec-Fetch-Site: none` is let through (typed in the address bar: GET only, harmless).
- Rate limit is per listener and global, so a flood also delays the real browser for a few seconds.
- `peer::process` scans every `/proc/*/fd`: may be slow with many processes; it runs beside the read.
- `Body` is untagged: a known field of the wrong JSON type (for example `"headers": []`) refuses the whole body with `body`.
- The UI is not wired: S024 must call `Connector::start`/`set` from settings and show `status().words()`.
## Doubts found in S041
- More than 16 connections open at once are dropped without any answer (no 429); the 429 comes only from the rate gate (check 15 run in a loop).