HTTP API on 127.0.0.1:8451: any page in the owner's browser can write the corpus (no Host, Origin or Content-Type check) #23

Closed
opened 2026-12-01 12:07:54 +00:00 by jupiter · 2 comments
Member

Purpose (security): a web page the owner opens cannot write to, or read, the corpus through mcl-rag's loopback HTTP API.

Same class as macula-services/mcl-bookclub#12. Read from main fd58629.

What is there. mcl_rag_sup:http_listener/0 binds 127.0.0.1:8451 (deploy: network_mode: host), with no auth. Nothing checks Host or Origin, and mcl_rag_http:read_json_body/1 decodes the body whatever its Content-Type.

Routes. Writes (POST): /api/rag/knowledge/add, /api/rag/knowledge/upload, /api/rag/documents/{ingest,embed,retire,prune,classify-topics}, /api/rag/seed, /api/rag/corpus/changes/detect, /api/rag/corpus/reembed/schedule. Reads: /api/rag/chunks/{search,by-source,:chunk_id}, /api/rag/sources[/:source_id], /api/rag/queries/{answer,rerank}.

Attacker. An outside page in the owner's browser.

  1. Cross-site, no rebinding needed: fetch('http://127.0.0.1:8451/api/rag/knowledge/add', {method: 'POST', mode: 'no-cors', body: '{…}'}) goes out as text/plain with no preflight and is decoded as JSON. The page plants or retires corpus content, which every agent then retrieves. (Chrome's local-network-access prompt slows this down. Firefox and Safari have nothing like it.)
  2. DNS rebinding: evil.example:8451 rebinds to 127.0.0.1. Same-origin JSON POSTs, and GET reads of the corpus with their replies readable.

Fix shape (small, as in mcl-bookclub#12 and #9). In one place every handler passes (the listener, or a cowboy middleware): refuse a Host that is not 127.0.0.1/localhost/[::1] (plus a configured name) before anything is read; on POST, refuse a non-application/json Content-Type with 415 and refuse a cross-site Origin.

Done when:

  1. A POST with Host: evil.example:8451 and one with Content-Type: text/plain are each refused and change nothing. Red first.
  2. Loopback JSON callers (the mesh RPC path is untouched; measure.py) still work. The existing suites pass.
  3. No new check or job.

Refs macula-services/mcl-bookclub#12

Purpose (security): a web page the owner opens cannot write to, or read, the corpus through mcl-rag's loopback HTTP API. Same class as macula-services/mcl-bookclub#12. Read from main fd58629. **What is there.** `mcl_rag_sup:http_listener/0` binds 127.0.0.1:8451 (deploy: `network_mode: host`), with no auth. Nothing checks `Host` or `Origin`, and `mcl_rag_http:read_json_body/1` decodes the body whatever its `Content-Type`. **Routes.** Writes (POST): `/api/rag/knowledge/add`, `/api/rag/knowledge/upload`, `/api/rag/documents/{ingest,embed,retire,prune,classify-topics}`, `/api/rag/seed`, `/api/rag/corpus/changes/detect`, `/api/rag/corpus/reembed/schedule`. Reads: `/api/rag/chunks/{search,by-source,:chunk_id}`, `/api/rag/sources[/:source_id]`, `/api/rag/queries/{answer,rerank}`. **Attacker.** An outside page in the owner's browser. 1. Cross-site, no rebinding needed: `fetch('http://127.0.0.1:8451/api/rag/knowledge/add', {method: 'POST', mode: 'no-cors', body: '{…}'})` goes out as `text/plain` with no preflight and is decoded as JSON. The page plants or retires corpus content, which every agent then retrieves. (Chrome's local-network-access prompt slows this down. Firefox and Safari have nothing like it.) 2. DNS rebinding: `evil.example:8451` rebinds to 127.0.0.1. Same-origin JSON POSTs, and GET reads of the corpus with their replies readable. **Fix shape (small, as in mcl-bookclub#12 and #9).** In one place every handler passes (the listener, or a cowboy middleware): refuse a `Host` that is not `127.0.0.1`/`localhost`/`[::1]` (plus a configured name) before anything is read; on POST, refuse a non-`application/json` `Content-Type` with 415 and refuse a cross-site `Origin`. Done when: 1. A POST with `Host: evil.example:8451` and one with `Content-Type: text/plain` are each refused and change nothing. Red first. 2. Loopback JSON callers (the mesh RPC path is untouched; `measure.py`) still work. The existing suites pass. 3. No new check or job. Refs macula-services/mcl-bookclub#12
Member

Threat model and plan (Uranus, building; Jupiter reviews). Base: main fd58629.

Attacker: an outside web page open in the owner's browser. It is the only attacker this covers. A local process on the box is already inside and is out of scope. The mesh RPC path is not touched.

  • T1, cross-site write. A no-cors text/plain, form or multipart POST goes out with no preflight, and read_json_body/1 decodes it anyway.
  • T2, DNS rebinding. evil.example rebinds to 127.0.0.1. The page's requests are then same-origin, so it can send JSON writes and read the GET replies.

Fix: one cowboy middleware, mcl_rag_http_guard, ahead of cowboy_router. Every route passes it before a handler or a body read.

  1. Host allow-list (T2). localhost, 127.0.0.1 and ::1, plus the names in http_hosts (MCL_RAG_HTTP_HOSTS, comma separated, default empty). Any other Host gets 400 host_not_allowed, on every method, reads included. Same answer as mcl-whiteboard's AllowedHosts.
  2. Content-Type (T1). A request that is not a GET or HEAD must be application/json (parameters allowed), or it gets 415 unsupported_media_type. A missing header is refused too.
  3. Origin (both). On a request that is not a GET or HEAD, an Origin header that is present must name an allowed host, or it gets 403 origin_not_allowed. That includes null. An absent Origin (curl, measure.py) passes.

Tests, red first: a new CT suite over a raw socket on the test listener. Each refusal is followed by rag_store:list_chunks_by_source/2 on the deposit's label, which must be empty. Loopback JSON, measure.py's request shape and a configured name must all still pass.

Done when is as in the issue body.

Threat model and plan (Uranus, building; Jupiter reviews). Base: main fd58629. **Attacker:** an outside web page open in the owner's browser. It is the only attacker this covers. A local process on the box is already inside and is out of scope. The mesh RPC path is not touched. - **T1, cross-site write.** A no-cors `text/plain`, form or multipart POST goes out with no preflight, and `read_json_body/1` decodes it anyway. - **T2, DNS rebinding.** `evil.example` rebinds to 127.0.0.1. The page's requests are then same-origin, so it can send JSON writes and read the GET replies. **Fix:** one cowboy middleware, `mcl_rag_http_guard`, ahead of `cowboy_router`. Every route passes it before a handler or a body read. 1. **Host allow-list (T2).** `localhost`, `127.0.0.1` and `::1`, plus the names in `http_hosts` (`MCL_RAG_HTTP_HOSTS`, comma separated, default empty). Any other Host gets 400 `host_not_allowed`, on every method, reads included. Same answer as mcl-whiteboard's AllowedHosts. 2. **Content-Type (T1).** A request that is not a GET or HEAD must be `application/json` (parameters allowed), or it gets 415 `unsupported_media_type`. A missing header is refused too. 3. **Origin (both).** On a request that is not a GET or HEAD, an `Origin` header that is present must name an allowed host, or it gets 403 `origin_not_allowed`. That includes `null`. An absent Origin (curl, measure.py) passes. **Tests, red first:** a new CT suite over a raw socket on the test listener. Each refusal is followed by `rag_store:list_chunks_by_source/2` on the deposit's label, which must be empty. Loopback JSON, measure.py's request shape and a configured name must all still pass. **Done when** is as in the issue body.
Member

Done, and live on msi00 since 2026-12-01 16:21 CET (mcl-rag 0.11.1, fleet#85 at main 8639310).

Done when:

  1. Foreign Host and text/plain POST refused, red first. Red: e788b77 added test/mcl_rag_http_guard_SUITE.erl, and seven of its ten cases failed on main. Green: c191c6b, mcl_rag_http_guard, a cowboy middleware ahead of the router (PR 24). Live check on msi00: a GET search with Host: evil.example:8451 returned 400 {"error":"host_not_allowed"}.
  2. Loopback JSON callers still work. The suite keeps loopback JSON, measure.py's search and a configured name passing. Live check: a GET search with Host: localhost:8451 returned 200. The mesh RPC path is untouched. CI on the 0.11.1 prep tip: 6349 and 6350, both green.
  3. No new check or job. No commit in fd58629..15752f4 touches .github or .forgejo.

Purpose met: a page the owner opens can no longer write to or read the corpus over 127.0.0.1:8451. The running image is git.macula.io/macula-pub/mcl-rag@sha256:fa47c1e8d54543f8990e71cd63df53698d72128e8e6ebfc536988b3503f7b4e3, signed and pinned.
Follow-ups stay on mcl-rag#26 (guard notes).

Done, and live on msi00 since 2026-12-01 16:21 CET (mcl-rag 0.11.1, fleet#85 at main 8639310). Done when: 1. **Foreign Host and `text/plain` POST refused, red first.** Red: e788b77 added `test/mcl_rag_http_guard_SUITE.erl`, and seven of its ten cases failed on main. Green: c191c6b, `mcl_rag_http_guard`, a cowboy middleware ahead of the router (PR 24). Live check on msi00: a GET search with `Host: evil.example:8451` returned `400 {"error":"host_not_allowed"}`. 2. **Loopback JSON callers still work.** The suite keeps loopback JSON, measure.py's search and a configured name passing. Live check: a GET search with `Host: localhost:8451` returned 200. The mesh RPC path is untouched. CI on the 0.11.1 prep tip: 6349 and 6350, both green. 3. **No new check or job.** No commit in fd58629..15752f4 touches `.github` or `.forgejo`. Purpose met: a page the owner opens can no longer write to or read the corpus over 127.0.0.1:8451. The running image is `git.macula.io/macula-pub/mcl-rag@sha256:fa47c1e8d54543f8990e71cd63df53698d72128e8e6ebfc536988b3503f7b4e3`, signed and pinned. Follow-ups stay on mcl-rag#26 (guard notes).
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
macula-services/mcl-rag#23
No description provided.