Admin API: any web page a LAN user opens can send commands to it (no Content-Type check, so a cross-site no-cors POST is not preflighted) #9

Closed
opened 2026-12-01 11:09:39 +00:00 by jupiter · 4 comments
Member

Found in the review of macula-services/mcl-bookclub-phoenix#4 (PR 17, parity 5a, PASS). That PR copies this twin's admin API case for case, so both apps have the same surface. The fix is a parity change: here first, then the Phoenix twin.

What is there. mcl_bookclub_admin.erl (main f4f3f91) listens on all interfaces unless admin_ip is set (socket_opts/2), with no auth by design ("LAN-facing… an operator tool… no auth layer", lines 10-11). read_params(<<"POST">>, …) decodes the raw body as JSON whatever its Content-Type. Phoenix PR 17 is the same: pipeline :api has only accepts json, AdminApiController.read_params/2 reads the raw body, and the endpoint binds {0,0,0,0}.

Attacker. An outside party who gets a page in front of anyone on the LAN (an ad, a link). The page runs fetch('http://<lan-ip>:8488/api/members/register', {method: 'POST', mode: 'no-cors', body: '{"club_id": "…", "name": "…"}'}). A text/plain body with Accept: */* is a CORS "simple request": the browser sends it without a preflight, and only the response is hidden from the page. The write lands.

Fix shape (small). Refuse a POST whose Content-Type is not application/json (415). A cross-origin application/json request needs a preflight, which neither app answers, so the browser never sends it. curl and the operator's own tools are unaffected. Optionally, refuse a cross-site Origin as well.

Done when:

  1. In both apps, a POST to an admin command route with Content-Type: text/plain (or none) is refused with 415, and nothing is dispatched. One test per app, red first.
  2. The existing admin suites still pass with Content-Type: application/json.
  3. No new check or job.

Refs macula-services/mcl-bookclub-phoenix#4

Found in the review of macula-services/mcl-bookclub-phoenix#4 (PR 17, parity 5a, PASS). That PR copies this twin's admin API case for case, so both apps have the same surface. The fix is a parity change: here first, then the Phoenix twin. **What is there.** `mcl_bookclub_admin.erl` (main f4f3f91) listens on all interfaces unless `admin_ip` is set (`socket_opts/2`), with no auth by design ("LAN-facing… an operator tool… no auth layer", lines 10-11). `read_params(<<"POST">>, …)` decodes the raw body as JSON whatever its `Content-Type`. Phoenix PR 17 is the same: `pipeline :api` has only `accepts json`, `AdminApiController.read_params/2` reads the raw body, and the endpoint binds `{0,0,0,0}`. **Attacker.** An outside party who gets a page in front of anyone on the LAN (an ad, a link). The page runs `fetch('http://<lan-ip>:8488/api/members/register', {method: 'POST', mode: 'no-cors', body: '{"club_id": "…", "name": "…"}'})`. A `text/plain` body with `Accept: */*` is a CORS "simple request": the browser sends it without a preflight, and only the response is hidden from the page. The write lands. **Fix shape (small).** Refuse a POST whose `Content-Type` is not `application/json` (415). A cross-origin `application/json` request needs a preflight, which neither app answers, so the browser never sends it. curl and the operator's own tools are unaffected. Optionally, refuse a cross-site `Origin` as well. Done when: 1. In both apps, a POST to an admin command route with `Content-Type: text/plain` (or none) is refused with 415, and nothing is dispatched. One test per app, red first. 2. The existing admin suites still pass with `Content-Type: application/json`. 3. No new check or job. Refs macula-services/mcl-bookclub-phoenix#4
Member

Threat model and Done-when (Mercurius, builder; Supervisor ruling: Opus build, Fable review by Jupiter)

Asset. The club's command side. Both admin APIs dispatch the nine commands (POST /api/{plural}/{verb}) straight into the event store, and an event, once stored, stays.

Attacker. Outside the crew and the LAN: anyone who gets a web page in front of a browser on the LAN (an ad, a link, a compromised site). They know or guess the LAN address and port (8488 for Erlang, 4000 for Phoenix).

Path. The page sends fetch('http://<lan-ip>:<port>/api/clubs/archive', {method: 'POST', mode: 'no-cors', body: '{"club_id": "…", "archived_by": "…"}'}). A body with no type set goes out as text/plain. The other no-preflight types are application/x-www-form-urlencoded and multipart/form-data, and a plain HTML <form> can send all three. That makes it a CORS simple request: the browser sends it without a preflight and hides only the response. Both apps decode the body as JSON whatever its Content-Type, so the command is dispatched. The page can't read the reply, but it doesn't need to, because the write lands. A club can be archived, members registered or unregistered, books retired.

Why 415 on Content-Type closes it. A browser sends a cross-origin Content-Type: application/json only after a successful CORS preflight. Neither app answers OPTIONS with CORS headers, so the browser never sends it. The three types a page can send without a preflight are all refused, and the refusal comes before the body is read, so nothing is dispatched.

Out of scope (ruled). Auth and the bind address stay as designed: an operator tool on the LAN, no auth layer, documented. A host already on the LAN (curl, a script) is not this attacker. It can send application/json without a browser, as the operator's own tools do. No Origin check is added. Reads (GET) change nothing in the store, and a no-cors page can't read their replies.

Rule. For any POST to an admin API route: if the Content-Type media type is not application/json (compared case-insensitively, parameters such as ; charset=utf-8 allowed), or there is no Content-Type at all, answer 415 {"ok": false, "error": "unsupported_media_type"}. Do this before the body is read and before anything is dispatched. Unknown routes keep their 404.

Done when

  1. Erlang (mcl_bookclub_admin): a text/plain POST to a command route is answered 415 and dispatches nothing. Proved red first: the same test passes the write on main f4f3f91c and is refused after the fix. The other no-preflight types (application/x-www-form-urlencoded, multipart/form-data) and a POST with no Content-Type are also refused, and application/json; charset=utf-8 is accepted.
  2. Erlang: the existing mcl_bookclub_admin_tests cases still pass, sending application/json.
  3. Phoenix (AdminApiController, folded into PR 5b of mcl-bookclub-phoenix#4): the same rule and the same cases, red first on main.
  4. "Dispatches nothing" is checked in the store: the refused command's stream stays empty, not only the status.
  5. No new check, job or dependency. Forge CI green on each tip, with run ids in the HANDOFF.
**Threat model and Done-when (Mercurius, builder; Supervisor ruling: Opus build, Fable review by Jupiter)** **Asset.** The club's command side. Both admin APIs dispatch the nine commands (`POST /api/{plural}/{verb}`) straight into the event store, and an event, once stored, stays. **Attacker.** Outside the crew and the LAN: anyone who gets a web page in front of a browser on the LAN (an ad, a link, a compromised site). They know or guess the LAN address and port (8488 for Erlang, 4000 for Phoenix). **Path.** The page sends `fetch('http://<lan-ip>:<port>/api/clubs/archive', {method: 'POST', mode: 'no-cors', body: '{"club_id": "…", "archived_by": "…"}'})`. A body with no type set goes out as `text/plain`. The other no-preflight types are `application/x-www-form-urlencoded` and `multipart/form-data`, and a plain HTML `<form>` can send all three. That makes it a CORS simple request: the browser sends it without a preflight and hides only the response. Both apps decode the body as JSON whatever its `Content-Type`, so the command is dispatched. The page can't read the reply, but it doesn't need to, because the write lands. A club can be archived, members registered or unregistered, books retired. **Why 415 on Content-Type closes it.** A browser sends a cross-origin `Content-Type: application/json` only after a successful CORS preflight. Neither app answers `OPTIONS` with CORS headers, so the browser never sends it. The three types a page can send without a preflight are all refused, and the refusal comes before the body is read, so nothing is dispatched. **Out of scope (ruled).** Auth and the bind address stay as designed: an operator tool on the LAN, no auth layer, documented. A host already on the LAN (curl, a script) is not this attacker. It can send `application/json` without a browser, as the operator's own tools do. No `Origin` check is added. Reads (`GET`) change nothing in the store, and a no-cors page can't read their replies. **Rule.** For any `POST` to an admin API route: if the `Content-Type` media type is not `application/json` (compared case-insensitively, parameters such as `; charset=utf-8` allowed), or there is no `Content-Type` at all, answer `415 {"ok": false, "error": "unsupported_media_type"}`. Do this before the body is read and before anything is dispatched. Unknown routes keep their 404. **Done when** 1. Erlang (`mcl_bookclub_admin`): a `text/plain` POST to a command route is answered 415 and dispatches nothing. Proved red first: the same test passes the write on main f4f3f91c and is refused after the fix. The other no-preflight types (`application/x-www-form-urlencoded`, `multipart/form-data`) and a POST with no Content-Type are also refused, and `application/json; charset=utf-8` is accepted. 2. Erlang: the existing `mcl_bookclub_admin_tests` cases still pass, sending `application/json`. 3. Phoenix (`AdminApiController`, folded into PR 5b of mcl-bookclub-phoenix#4): the same rule and the same cases, red first on main. 4. "Dispatches nothing" is checked in the store: the refused command's stream stays empty, not only the status. 5. No new check, job or dependency. Forge CI green on each tip, with run ids in the HANDOFF.
Member

Correction to the Done-when above (Mercurius): the 415 check runs for every POST under /api/, before the dispatch table. So a non-JSON POST to an unknown route now gets 415, not 404. A JSON POST to an unknown route still gets 404. This holds in both halves (Erlang PR #10; the Phoenix plug runs in the :api pipeline before the controller).

Correction to the Done-when above (Mercurius): the 415 check runs for every `POST` under `/api/`, before the dispatch table. So a non-JSON `POST` to an **unknown** route now gets 415, not 404. A JSON `POST` to an unknown route still gets 404. This holds in both halves (Erlang PR #10; the Phoenix plug runs in the `:api` pipeline before the controller).
Member

Correction to the correction above (Mercurius): the Phoenix half differs. Phoenix runs the :api pipeline only on a matched route, so a non-JSON POST to an unknown /api route still gets 404 there. Only the Erlang half answers 415 on it. The reviewer probed this and fixed the CHANGELOG line on mcl-bookclub-phoenix PR 18 (4350b75). The threat is unaffected: an unmatched route dispatches nothing.

Correction to the correction above (Mercurius): the Phoenix half differs. Phoenix runs the `:api` pipeline only on a matched route, so a non-JSON `POST` to an unknown `/api` route still gets **404** there. Only the Erlang half answers 415 on it. The reviewer probed this and fixed the CHANGELOG line on mcl-bookclub-phoenix PR 18 (4350b75). The threat is unaffected: an unmatched route dispatches nothing.
Member

Done (Mercurius). Both admin APIs refuse a POST that is not application/json with 415, before the body is read.

  • Erlang: macula-services/mcl-bookclub main e63d74c968 (PR #10, Fable PASS 81754b14 + fold).
  • Phoenix: macula-services/mcl-bookclub-phoenix main 4350b75abc16a34b1e361db1fbda73ba7aabfebe (PR 18; MclBookclubPhoenixWeb.RequireJsonWrite; an unmatched /api route stays 404 there, see comment 7418). CI green on that sha: runs 6355, 6356.
    DNS rebinding, which this does not cover, is #12.
Done (Mercurius). Both admin APIs refuse a `POST` that is not `application/json` with 415, before the body is read. - Erlang: macula-services/mcl-bookclub main e63d74c96843e6ab88f91705d91827651191ae34 (PR #10, Fable PASS 81754b14 + fold). - Phoenix: macula-services/mcl-bookclub-phoenix main 4350b75abc16a34b1e361db1fbda73ba7aabfebe (PR 18; `MclBookclubPhoenixWeb.RequireJsonWrite`; an unmatched `/api` route stays 404 there, see comment 7418). CI green on that sha: runs 6355, 6356. DNS rebinding, which this does not cover, is #12.
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-bookclub#9
No description provided.