API v1: write endpoints, expanded token permissions, docs #123

Open
opened 2026-12-03 04:31:06 +00:00 by jfro · 1 comment
Owner

Part of the Ink Lantern rename & API project. Follows the design in the API design issue.

  • Write endpoints (create notebook/folder, add page, upload PDF, move, review loop, and whatever else the design lists: todos, quick notes, suggestions accept/dismiss, sync trigger).
  • Token permission changes from the design: migration, defaults that give existing keys nothing new, key settings UI.
  • docs/api.md final, with curl examples; README pointer.
  • Tests that a read-only key gets 403 on every write; mix precommit passes.
Part of the Ink Lantern rename & API project. Follows the design in the API design issue. - Write endpoints (create notebook/folder, add page, upload PDF, move, review loop, and whatever else the design lists: todos, quick notes, suggestions accept/dismiss, sync trigger). - Token permission changes from the design: migration, defaults that give existing keys nothing new, key settings UI. - `docs/api.md` final, with curl examples; README pointer. - Tests that a read-only key gets 403 on every write; `mix precommit` passes.
Author
Owner

Attack checklist for this issue, from Nye (posted by Haystack; Nye's token can't comment).

#123 attack checklist: writes, grants, /events (Nye, from #125 @e42d1bd + docs/api.md + D7)

Key shapes to test each endpoint with: full+can_write; full, can_write off; folder-limited (item_ids) +can_write; tools-trimmed; grants [] vs grant set; revoked; no key.

Grants / migration

  1. Migration: grants {:array,:string}, default [], null: false. Existing key after migrate -> grants == [] (test with a key made before the migration). Can't be nil.
  2. ApiKeys.allowed?/2 must take grants as a separate path: a key with tools: nil must NOT pass suggestions/quick_notes/todos/sync (today allowed? = permission in tools(key); if grants are just appended to tools/1, check nil-tools doesn't widen).
  3. Unknown grant string in update_access -> rejected (changeset), not stored.
  4. Write grants (suggestions, quick_notes add, todos sync) need can_write too: grant set + can_write off -> 403. sync doesn't need can_write.
  5. A key can't change its own grants / make keys / list keys via the API (no such endpoint; 404 on any /keys path).
  6. MCP: grants give MCP nothing new (tools/list unchanged for a key with grants).

Library writes (existing MCP write tools)

  1. Each of POST /documents/:id/pages, /notebooks, /folders, PATCH /documents/:id, /reports, /pdfs, /reviews: can_write off -> 403; folder-limited key targeting a doc/folder outside reach -> 404 (same body as unknown id).
  2. PATCH /documents/:id move: doc inside reach, folder_id OUTSIDE reach -> refused (not moved). And doc outside, folder inside.
  3. POST /notebooks, /folders with folder_id/parent_id outside reach (and "" root for a limited key) -> refused.
  4. POST /reports base_document_id outside reach -> 404; replace of a doc outside reach -> refused.
  5. POST /documents/:id/pages on a PDF -> 422; style other than typed|drawn -> 400; draw_page trimmed while add_page allowed -> "drawn" 403.
  6. POST /pdfs: multipart with a non-PDF / oversize file -> 4xx not 500; url form -> SSRF check same as the MCP tool (no localhost/private IPs); base64 junk -> 400.
  7. Body params: wrong types (list for text, number for name, nested maps) -> 400 not 500 (same class as F2; QueryParams plug only covers query string).
  8. Idempotency-Key: same key+same body twice -> one page, same response; same key from ANOTHER api key or user must not return the first key's result (scope the store by key id); different body with same key -> 409 or 422; >24h -> new.

App features (new grants)

  1. CONFIRMED TRAP: Sorting.approve/3 with target_id checks notebook_choices(scope) (sorting.ex:39), which uses Remarkable.list_documents_outside_trash -> list_under (remarkable.ex:454-466): no scope.library filter. So a folder-limited key with suggestions could file a page into ANY of the user's notebooks. The API must check Notes.get(scope, target_id) (or reaches_target?) before calling approve. Test: limited key, override target outside reach -> 404/422, page not moved.
  2. Moves: suggestion visible only if key reaches Quick sheets AND target (reaches_target?/2); approve/reject/undo/retry on one it can't see -> 404, not 409 (no oracle). Undo moves back into Quick sheets: needs reach.
  3. Tag approve: {tags} only on docs reached; tags list shape validated.
  4. Already handled -> 409 only when visible to the key.
  5. GET/POST /quick-notes: limited key sees only notebooks it reaches; document_id outside reach -> 404; no document_id -> default notebook only if reached, else refuse (don't fall back to an unreached default).
  6. GET /todos/pages: only pages of reached docs; POST sync on an unreached page id -> 404.
  7. POST /sync: needs sync grant (not can_write); second call while running -> 202 no-op.

/events (SSE)

  1. Needs a key; events filtered by reach and permission: limited key gets no changes hint for unreached docs, no suggestions hint without the grant, no todos hint without it, no changes without list_changes (match F1 fix).
  2. Revoked key: stream closes (or at least next reconnect 401). Keepalive every 25s; no data payloads (hints only).

transcribed change kind + docs

  1. transcribed changes respect reach (pages of unreached docs never appear in /changes, MCP list_changes, webhooks). mcp.md + api.md say "ignore kinds you don't know".
  2. openapi.json (Choice E) lists every route in the router: route test fails if one is missing. No new deps (stdlib/Jason only).
  3. Settings page renamed "API keys", grants checkboxes all off by default, QR code shows the token only once (at creation), not on later visits.
  4. D1 holds: rma_ prefix, rm-analyzer:// URI, webhook header unchanged.
Attack checklist for this issue, from Nye (posted by Haystack; Nye's token can't comment). # #123 attack checklist: writes, grants, /events (Nye, from #125 @e42d1bd + docs/api.md + D7) Key shapes to test each endpoint with: full+can_write; full, can_write off; folder-limited (item_ids) +can_write; tools-trimmed; grants [] vs grant set; revoked; no key. ## Grants / migration 1. Migration: `grants` {:array,:string}, default [], null: false. Existing key after migrate -> grants == [] (test with a key made before the migration). Can't be nil. 2. ApiKeys.allowed?/2 must take grants as a separate path: a key with tools: nil must NOT pass `suggestions`/`quick_notes`/`todos`/`sync` (today allowed? = permission in tools(key); if grants are just appended to tools/1, check nil-tools doesn't widen). 3. Unknown grant string in update_access -> rejected (changeset), not stored. 4. Write grants (suggestions, quick_notes add, todos sync) need can_write too: grant set + can_write off -> 403. `sync` doesn't need can_write. 5. A key can't change its own grants / make keys / list keys via the API (no such endpoint; 404 on any /keys path). 6. MCP: grants give MCP nothing new (tools/list unchanged for a key with grants). ## Library writes (existing MCP write tools) 7. Each of POST /documents/:id/pages, /notebooks, /folders, PATCH /documents/:id, /reports, /pdfs, /reviews: can_write off -> 403; folder-limited key targeting a doc/folder outside reach -> 404 (same body as unknown id). 8. PATCH /documents/:id move: doc inside reach, folder_id OUTSIDE reach -> refused (not moved). And doc outside, folder inside. 9. POST /notebooks, /folders with folder_id/parent_id outside reach (and "" root for a limited key) -> refused. 10. POST /reports base_document_id outside reach -> 404; replace of a doc outside reach -> refused. 11. POST /documents/:id/pages on a PDF -> 422; style other than typed|drawn -> 400; draw_page trimmed while add_page allowed -> "drawn" 403. 12. POST /pdfs: multipart with a non-PDF / oversize file -> 4xx not 500; `url` form -> SSRF check same as the MCP tool (no localhost/private IPs); base64 junk -> 400. 13. Body params: wrong types (list for text, number for name, nested maps) -> 400 not 500 (same class as F2; QueryParams plug only covers query string). 14. Idempotency-Key: same key+same body twice -> one page, same response; same key from ANOTHER api key or user must not return the first key's result (scope the store by key id); different body with same key -> 409 or 422; >24h -> new. ## App features (new grants) 15. CONFIRMED TRAP: Sorting.approve/3 with target_id checks notebook_choices(scope) (sorting.ex:39), which uses Remarkable.list_documents_outside_trash -> list_under (remarkable.ex:454-466): no scope.library filter. So a folder-limited key with `suggestions` could file a page into ANY of the user's notebooks. The API must check Notes.get(scope, target_id) (or reaches_target?) before calling approve. Test: limited key, override target outside reach -> 404/422, page not moved. 16. Moves: suggestion visible only if key reaches Quick sheets AND target (reaches_target?/2); approve/reject/undo/retry on one it can't see -> 404, not 409 (no oracle). Undo moves back into Quick sheets: needs reach. 17. Tag approve: {tags} only on docs reached; tags list shape validated. 18. Already handled -> 409 only when visible to the key. 19. GET/POST /quick-notes: limited key sees only notebooks it reaches; document_id outside reach -> 404; no document_id -> default notebook only if reached, else refuse (don't fall back to an unreached default). 20. GET /todos/pages: only pages of reached docs; POST sync on an unreached page id -> 404. 21. POST /sync: needs `sync` grant (not can_write); second call while running -> 202 no-op. ## /events (SSE) 22. Needs a key; events filtered by reach and permission: limited key gets no `changes` hint for unreached docs, no `suggestions` hint without the grant, no `todos` hint without it, no `changes` without list_changes (match F1 fix). 23. Revoked key: stream closes (or at least next reconnect 401). Keepalive every 25s; no data payloads (hints only). ## transcribed change kind + docs 24. `transcribed` changes respect reach (pages of unreached docs never appear in /changes, MCP list_changes, webhooks). mcp.md + api.md say "ignore kinds you don't know". 25. openapi.json (Choice E) lists every route in the router: route test fails if one is missing. No new deps (stdlib/Jason only). 26. Settings page renamed "API keys", grants checkboxes all off by default, QR code shows the token only once (at creation), not on later visits. 27. D1 holds: rma_ prefix, rm-analyzer:// URI, webhook header unchanged.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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
jfro/rm-analyzer#123
No description provided.