S023: browser port on loopback with Host, Origin, type and preflight guards, link checks, sender log
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
6fa8330b4f
commit
fe012decc0
3 changed files with 928 additions and 10 deletions
63
docs/CONNECTOR.md
Normal file
63
docs/CONNECTOR.md
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# The browser port
|
||||
|
||||
Medlar listens on `127.0.0.1:17616` (D-006; changeable under Settings, Browser; on and off live). Only programs on this
|
||||
PC can reach it. Code: `crates/core/src/connector/`.
|
||||
|
||||
## Format
|
||||
|
||||
`GET /` answers `{"name": "Medlar", "version": "0.1.0"}`: a ping.
|
||||
|
||||
`POST /` with `Content-Type: application/json` and a body of one link or a list of them. This is Nacre's
|
||||
`custom_body` as it is (D-003, D-010); unknown fields are ignored.
|
||||
|
||||
| Field | | |
|
||||
|---|---|---|
|
||||
| `url` | required | http, https, ftp, ftps, ftpes, sftp, rtsp, mms, mmsh, mmst |
|
||||
| `filename` | | made safe: path parts, control characters and leading dots go |
|
||||
| `referrer` | | the page; sent as `Referer`, kept to find a new link later |
|
||||
| `cookies` | | as in a `Cookie` header; kept in the keyring only |
|
||||
| `user_agent` | | sent with the download |
|
||||
| `headers` | | object of name to value; `Cookie`, `User-Agent`, `Referer` fill the fields above when those are empty |
|
||||
| `size`, `mime` | | the sender's guess, shown only |
|
||||
|
||||
Answer `200 {"accepted": n, "refused": [{"index": i, "why": "scheme"}]}` when one or more links came in, else an error
|
||||
with `{"error": "<code>"}`:
|
||||
|
||||
| Status | `error` | Why |
|
||||
|---|---|---|
|
||||
| 403 | `host` | `Host` is not `127.0.0.1:<port>` or `localhost:<port>` (DNS rebinding) |
|
||||
| 403 | `origin` | an `Origin` header, `Sec-Fetch-Site` other than `none`, or `Sec-Fetch-Mode: cors` |
|
||||
| 405 | `preflight`, `method` | `OPTIONS`, `Access-Control-Request-Method`, or anything but GET and POST |
|
||||
| 415 | `content-type` | not `application/json` (form types, `text/plain`) |
|
||||
| 404 | `path` | anything but `/` |
|
||||
| 400 | `malformed`, `body` | broken HTTP, or JSON that is no link or list |
|
||||
| 411 | `length` | chunked body |
|
||||
| 413 | `too-large`, `too-many` | head over 16 KiB, body over 1 MiB, list over 1000 links |
|
||||
| 408 | `slow` | request not in within 5 s |
|
||||
| 429 | `busy` | over 30 at once or 5 a second |
|
||||
| 422 | `no-links` | no link in it could be taken (`why`: `missing`, `invalid`, `scheme`) |
|
||||
|
||||
## Examples
|
||||
|
||||
```sh
|
||||
curl -s http://127.0.0.1:17616/
|
||||
curl -s -H 'Content-Type: application/json' -d '{"url": "https://example.com/a.zip"}' http://127.0.0.1:17616/
|
||||
curl -s -H 'Content-Type: application/json' \
|
||||
-d '[{"url": "https://example.com/a.zip", "filename": "a.zip", "referrer": "https://example.com/",
|
||||
"cookies": "k=v", "user_agent": "UA", "headers": {"X-Kind": "one"}}, {"url": "ftp://example.com/b.iso"}]' \
|
||||
http://127.0.0.1:17616/
|
||||
```
|
||||
|
||||
## Security
|
||||
|
||||
- Bound to 127.0.0.1 only, never 0.0.0.0. Port 15151 belongs to AB Download Manager and is not used.
|
||||
- No CORS headers in any answer, so a web page can't read one; JSON only, so a page can't post without a preflight,
|
||||
and preflights are refused. `Host` is checked against loopback and the port, so a rebound name is refused.
|
||||
- One request per connection, 5 s to send it, 16 connections at once, a token bucket against floods.
|
||||
- Headers in values with line breaks or control characters are dropped, and so are hop-by-hop and controlled ones
|
||||
(`Connection`, `Host`, `Range`, `Accept-Encoding`, `Transfer-Encoding`, `Content-*`, `If-*`, `Proxy-*`, `Sec-*` …),
|
||||
at most 32 kept. Cookies, a login in the link and secret headers go to the keyring (`secret::Secrets`), never to
|
||||
the store or the log.
|
||||
- Sender log (`Connector::log`): when, what (link without login or query string), accepted or why not, and the
|
||||
sending program's name from `/proc/net/tcp` when it is the user's own.
|
||||
- Links arrive on the channel from `Connector::start` as `Arrival`; `Incoming::task` makes a task and keeps its secret.
|
||||
|
|
@ -208,3 +208,8 @@ Seen: boxes were ticked together after the tests, against rule 3.
|
|||
## S022 — Control screens (compiled, not tested, no screenshots)
|
||||
- Toolbar mode switch (`views/modes.rs`, snail included) opens one sheet (`views/control.rs`): Speed dial (log scale, KB/s|MB/s, typing), Queues (`queues.rs` cards, live totals, reorder by handle or buttons, "move selected here", editor with week grid), Week (`Settings.mode_grid` → `limits_at`, applied by `bridge.apply`, rechecked each hour), When done (`schedule.rs`: Watcher countdown dialog, Logind, `run::run`, Inhibitor while running). Priority share text in details.
|
||||
- Not done: dragging tasks onto queue cards (used a button), start-time grid, priority share preview in list rows, core `Grid` unused (own grid instead). Limit changes save through the settings debounce, not on release.
|
||||
|
||||
## S023 — Browser port (compiled, not tested)
|
||||
- `connector::Connector::start(on, port) -> (Connector, UnboundedReceiver<Arrival>)`: the incoming-link event for the UI. `set(on, port)` live, `status()` (`Taken{by}` names the fix via `words()`), `log()` sender log, `on_change`. `Incoming::task(&Secrets, common)` makes the task and keeps cookies/login in the keyring.
|
||||
- Guards: loopback only, Host = 127.0.0.1|localhost:port, no Origin / cross-site Sec-Fetch, JSON only, preflights refused, no CORS; header and link checks. Format: `docs/CONNECTOR.md`. Tests: `docs/tests/S023-port-server.md`.
|
||||
- Not done: no app wiring (S024), no automatic tests yet; own small HTTP/1.1 reader instead of hyper's server.
|
||||
|
|
|
|||
45
docs/tests/S023-port-server.md
Normal file
45
docs/tests/S023-port-server.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# 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 | — |
|
||||
| 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 | — |
|
||||
| 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 | — |
|
||||
| 4 | HIGH | Add `Origin: https://evil.example`, `Origin: null`, `Sec-Fetch-Site: cross-site`, `Sec-Fetch-Mode: cors` (each alone) | 403 `origin` | integration | — |
|
||||
| 5 | HIGH | POST with `application/x-www-form-urlencoded`, `multipart/form-data`, `text/plain`, `application/json; charset=latin1` | 415 `content-type` | integration | — |
|
||||
| 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 | — |
|
||||
| 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 | — |
|
||||
| 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 | — |
|
||||
| 9 | HIGH | Filename `../../.bashrc`, `a\u0000b`, 400 chars | made safe by `http::name::safe` (no slashes, no leading dot, capped) | unit | — |
|
||||
| 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 | — |
|
||||
| 11 | HIGH | Log after checks 2–8 | entries with time, what, outcome; no cookie value, login or query string appears in any `what` | integration | — |
|
||||
| 12 | MED | GET `/` | 200 `{"name":"Medlar","version":...}`, not logged | integration | — |
|
||||
| 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 | — |
|
||||
| 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 | — |
|
||||
| 15 | MED | 40 requests at once from one loop | the first ≈30 answered normally, later ones 429 `busy` | integration | — |
|
||||
| 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 | — |
|
||||
| 17 | MED | `set(false, p)`, then `set(true, q)` | port closed (connect refused), then open on q; status follows | integration | — |
|
||||
| 18 | LOW | Sender of check 2 | `from.process` is `curl` (a process of this user) | integration | — |
|
||||
|
||||
## 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()`.
|
||||
Loading…
Reference in a new issue