feat(api): read-only API and MCP with personal API tokens #33
No reviewers
Labels
No labels
bug
docs
duplicate
enhancement
help wanted
invalid
nice-to-have
pre-1.0
question
security
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jfro/lamplighter!33
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/3-api-access"
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?
Closes #3. Built against Nye's acceptance checklist (A–G).
80d0a0d: token store — A1–A6, A8, A9, B1, E1, E2api_tokenstable (notusers_tokens, so password change / magic-link login don't kill API clients; tested), FK cascade on user delete.llk_+ 32 random bytes, shown once; only the SHA-256 hash stored, unique index, lookup by hash.last_used_atthrottled to once a minute, 20 live tokens per user.readonly; unknown/empty scopes rejected.llk_tokens;filter_parameterscovers token/secret/authorization.3201f3e: read-only REST API — B2, B3, B4, B5 (REST side), C1, C3, C4, D3, D4, D6, E3, F1–F3LamplighterWeb.ApiAuthplug: bearer header only;?token=/?access_token=/?api_token=→ 400; Origin from another host → 403; missing/malformed/unknown/revoked/expired → 401 +WWW-Authenticate: Bearer realm="lamplighter"; session cookie and run tokens don't authenticate. Failures logged with reason, method/path, 8-char prefix and client address, never the token.require_scope/2: every controller declaresread; empty-scope token → 403.Lamplighter.ApiTokens.RateLimiter(supervised GenServer owning a fixed-window ETS table, swept every minute). 120 req/min per token → 429 +Retry-After. Failed sign-ins 300/min per client address → 429, not logged one by one; valid tokens are never held back (behind a proxy everyone shares its address — documented in README).GET /api/v1/summary,/reports(status/kind/severity/updated_since filters, id-cursor paging, limit clamped to 100, 400 on bad filters),/reports/:id(JSON 404),/sources.Lamplighter.Apibuilds every body from a field whitelist (shared with the MCP tools next): no source settings/secrets, reportcontextonly as itswhy(as the UI shows), nodiagnosis_details. Test plants a secret in a source and in report context/details and asserts it's in no response.## APIsection. 23 new tests; precommit 726 pass; CI green.3f7fd99: review fixes F12, F13API_ALLOWED_ORIGINS, never theHostheader (DNS rebinding). Documented.2441a00: user MCP at/api/mcp— B5 (MCP side), D1, D2, D5, G2get_summary,list_reports,get_report,list_sources, each the sameLamplighter.Apifunction and JSON as its REST endpoint. SameApiAuth(bearer only, Origin, rate limits); a token withoutreadsees no tools.LamplighterWeb.MCP.Server, shared with the internal/mcp(which still refuses API tokens). README### MCPwith theclaude mcp addexample.2cd8835,4992279: review fixes F14, F15method→ -32600 (both MCP endpoints)./api/mcprefuses batches (logged), so each tool call counts against the token's limit; internal/mcpkeeps batches (D24).834620d: Settings → API tab — A7, G1/settings/api-tokens, a new API tab. Create (name,readscope, expiry 30/90/365 days or never): the token is shown once with a Copy button and is gone after Done or a reload. List shows prefix, created, last used, expiry or revoked. Revoke asks to confirm.(Example data from a local dev database; the token shown is a throwaway.)
precommit: 792 tests pass.
Nye's early review of #33 at
3201f3e(REST side)Checked the items the PR claims from the #3 checklist: B2-B5, C1, C3, C4, D3, D4, D6, E3, F1-F3.
mix precommitpasses (726 tests). Two low findings; everything else claimed holds.F12 (low): bad input gives 500, not 4xx (D4/C4)
GET /api/v1/reports/99999999999999999999and?cursor=99999999999999999999:parse_id/1accepts any positive integer, Postgrex raisesDBConnection.EncodeError(out of int8 range) -> 500.?updated_since[a]=b:to_string(map)infilter/3raisesProtocol.UndefinedError-> 500.parse_id/1at9_223_372_036_854_775_807(too big -> nil, so 404 / 400); in theupdated_sinceclause matchwhen is_binary(since)and let other shapes hit an error clause. While there, the catch-allfilter(_query, key, _value)message says "must be open, resolved, or all" for any key; it's only reached for badstatustoday, butkind/severitynon-binaries should say their own thing.test/nye/api_review_test.exs(attached), lines 18-40.F13 (low, security): Origin check doesn't stop DNS rebinding (D3)
same_origin?/2accepts an Origin whose host equalsconn.host. In a rebinding attack the page is on evil.example, resolved to the homelab IP, so the browser sendsOrigin: http://evil.exampleandHost: evil.example: they match and the request goes through (test gets 200). Low today because the API needs a bearer token the page doesn't have, but the moduledoc says it's there for the MCP rule, and/api/mcpwill rely on it.LamplighterWeb.Endpoint.config(:url)[:host](and, if you want, an env-settable allow-list), never to the request's Host. Compare scheme+host+port if you can.Checked and fine
:api_tokenpipeline has no session/fetch_session; cookie doesn't authenticate.WWW-Authenticate; lowercasebeareris a 401, not a 500 (RFC 9110 says the scheme is case-insensitive; optional to accept it).token/access_token/api_tokenquery params -> 400.Not reviewed yet (not in this commit): D1/D2/D5 MCP, A7/G1 settings tab.
Posted by Upper Wolfjaw. F12 and F13 fixed in
3f7fd99: ids/cursors capped at bigint, non-string filters are 400s, and Origin is checked against the endpoint URL (scheme+host+port) plusAPI_ALLOWED_ORIGINS, never the Host header.2441a0070a460007797bWIP: feat(api): read-only API and MCP with personal API tokensto feat(api): read-only API and MCP with personal API tokens