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
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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(mainf4f3f91) listens on all interfaces unlessadmin_ipis 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 itsContent-Type. Phoenix PR 17 is the same:pipeline :apihas onlyaccepts json,AdminApiController.read_params/2reads 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": "…"}'}). Atext/plainbody withAccept: */*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-Typeis notapplication/json(415). A cross-originapplication/jsonrequest 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-siteOriginas well.Done when:
Content-Type: text/plain(or none) is refused with 415, and nothing is dispatched. One test per app, red first.Content-Type: application/json.Refs macula-services/mcl-bookclub-phoenix#4
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 astext/plain. The other no-preflight types areapplication/x-www-form-urlencodedandmultipart/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 itsContent-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/jsononly after a successful CORS preflight. Neither app answersOPTIONSwith 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/jsonwithout a browser, as the operator's own tools do. NoOrigincheck is added. Reads (GET) change nothing in the store, and a no-cors page can't read their replies.Rule. For any
POSTto an admin API route: if theContent-Typemedia type is notapplication/json(compared case-insensitively, parameters such as; charset=utf-8allowed), or there is noContent-Typeat all, answer415 {"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
mcl_bookclub_admin): atext/plainPOST to a command route is answered 415 and dispatches nothing. Proved red first: the same test passes the write on mainf4f3f91cand 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, andapplication/json; charset=utf-8is accepted.mcl_bookclub_admin_testscases still pass, sendingapplication/json.AdminApiController, folded into PR 5b of mcl-bookclub-phoenix#4): the same rule and the same cases, red first on main.Correction to the Done-when above (Mercurius): the 415 check runs for every
POSTunder/api/, before the dispatch table. So a non-JSONPOSTto an unknown route now gets 415, not 404. A JSONPOSTto an unknown route still gets 404. This holds in both halves (Erlang PR #10; the Phoenix plug runs in the:apipipeline before the controller).Correction to the correction above (Mercurius): the Phoenix half differs. Phoenix runs the
:apipipeline only on a matched route, so a non-JSONPOSTto an unknown/apiroute 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.Done (Mercurius). Both admin APIs refuse a
POSTthat is notapplication/jsonwith 415, before the body is read.e63d74c968(PR #10, Fable PASS81754b14+ fold).MclBookclubPhoenixWeb.RequireJsonWrite; an unmatched/apiroute stays 404 there, see comment 7418). CI green on that sha: runs 6355, 6356.DNS rebinding, which this does not cover, is #12.