Skip to content

Capture inbox: fast dump page and token API - #9

Merged
rooty-bot merged 2 commits into
mainfrom
claude/2-capture-inbox-fast-dump-page-and-token-a
Nov 30, 2026
Merged

rooty-bot merged 2 commits into
mainfrom
claude/2-capture-inbox-fast-dump-page-and-token-a

Conversation

@rooty-bot

Copy link
Copy Markdown
Collaborator

Summary

  • Dump status: migration 002_dump_status.sql adds dumps.status (new | triaged | archived, CHECK-constrained, existing processed dumps become triaged). src/lib/server/dumps.ts has typed create, get, list (newest first, optional status filter), set-status and delete functions.
  • Inbox page: a large autofocused textarea where Ctrl/Cmd+Enter saves (progressive enhancement via use:enhance; it also works without JS). Below it, recent dumps newest first with relative time ("just now", "3 hours ago"), status and delete.
  • POST /api/dumps: accepts text/plain or JSON {"text": "..."} with Authorization: Bearer $DUMPDOWN_API_TOKEN. Returns 201 with the dump, 401 for a missing or wrong token (constant-time compare), 400 for empty text or bad JSON, 415 for other content types, and 404 while the token is unset (API disabled).
  • README: a config table, curl examples for both content types, and an iOS Shortcuts hint. docker-compose.yml passes through ORIGIN and DUMPDOWN_API_TOKEN.

Design call: CSRF check moved into handle

SvelteKit's built-in origin check rejects any text/plain POST without a matching Origin header. That blocks curl and iOS Shortcuts before the route runs, and it can't be scoped per route. So vite.config.ts sets csrf.trustedOrigins: ['*'], and src/lib/server/csrf.ts and hooks.server.ts re-implement the check more strictly: every non-GET request outside /api/* must carry an Origin equal to the app origin. /api/* relies on its bearer token, which a browser can't forge cross-site. As before, adapter-node needs ORIGIN set when serving over plain HTTP; this is now documented.

Testing

  • npm run lint, npm run check, npm test (37 tests) and npm run build all pass locally.
  • Unit tests cover the API (auth ok, denied, disabled; text/plain and JSON; bad input), the route wiring with DUMPDOWN_API_TOKEN and DATABASE_PATH stubbed, the DB functions and status CHECK, the CSRF predicate, the inbox form actions and relative time.
  • Against the production build (node build) with curl: text/plain 201, JSON 201, wrong token 401, same-origin form POST 200, foreign or missing Origin 403.
  • In headless Chrome against the production build: the textarea is focused on load, Ctrl+Enter and Cmd+Enter save, the new dump appears at the top as "just now", the textarea clears and refocuses, and delete removes the row without a reload.
  • Docker was not available locally; CI builds and smoke-tests the image.

Closes #2

🤖 Generated with Claude Code

rooty-bot and others added 2 commits November 30, 2026 11:35
- Migration 002 adds dumps.status (new | triaged | archived).
- DB functions to create, list, update status and delete dumps.
- Inbox page: autofocused textarea, Ctrl/Cmd+Enter saves, recent dumps
  newest first with relative time and delete.
- POST /api/dumps accepts text/plain or JSON {text}, guarded by a bearer
  token from DUMPDOWN_API_TOKEN (disabled with 404 when unset).
- SvelteKit's global origin check rejects text/plain posts without an
  Origin header (curl, iOS Shortcuts) and cannot be scoped per route, so
  it now runs in the handle hook for everything except /api/*.
- README documents ORIGIN, the API, curl examples and an iOS Shortcuts hint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rooty-bot

Copy link
Copy Markdown
Collaborator Author

Hi! I reviewed this against #2. It's a clean, well-scoped PR. Approving 🎉

Scope vs. #2: all covered

  • Inbox page: big autofocused textarea, Ctrl/⌘+Enter calls requestSubmit() (and it still works without JS), recent dumps newest first with relative time, status and delete.
  • POST /api/dumps: takes text/plain and JSON {text}, uses a bearer token from DUMPDOWN_API_TOKEN, and returns 404 while the token is unset. The README has curl examples for both content types and an iOS Shortcuts hint.
  • Status new | triaged | archived: enforced by a DB CHECK, with typed helpers in dumps.ts.
  • Tests: the API has auth ok/denied/disabled, both content types, plus 400/415 cases. The route-level test reads the real env var. The DB functions and form actions are tested too. CI is green.

What I ran locally: lint, check, test (37 passing) and build are all green. I also started the production build with ORIGIN and a token set and tried it with curl:

  • API with the right token: 201 for both text/plain and JSON. With a wrong token: 401.
  • Inbox form POST with no Origin: 403. With a foreign Origin: 403, including a text/plain body. With the same origin: 200.
  • Path tricks against the /api/ exemption with a foreign Origin (--path-as-is with /api/../inbox?/create and /api/%2e%2e/inbox?/create): all 403. The URL gets normalized before pathname.startsWith('/api/') runs, so the exemption holds.
  • Dump content containing HTML renders escaped (&lt;b>).

About disabling SvelteKit's CSRF check (trustedOrigins: ['*']): normally I'd push back on turning off a framework check. Here it's replaced by a stricter one: every mutating method outside /api/ needs an exact Origin match, and the check isn't limited to form content types. The only exempt route is the token-authenticated one, and src/routes/api/ contains just that today. The comments in vite.config.ts and csrf.ts explain why. I'm fine with it.

Security also looks good: auth runs before the body is read, the compare is constant-time over SHA-256 digests (so it doesn't leak the token length either), queries are parameterized, a blank token is treated as unset, and no secrets are committed.

Non-blocking nits / follow-ups:

  1. /api/ exemption, future risk: if a cookie- or session-authenticated page or action ever goes under /api/, it will silently lose CSRF protection. A one-line warning in csrf.ts, or an explicit allowlist (/api/dumps) instead of a prefix, would guard against that.
  2. handle wiring is only tested indirectly through isCrossSiteWrite. A tiny test that calls handle and expects a 403 would catch a regression if someone drops the hook.
  3. Migration backfill: UPDATE ... SET status = 'triaged' WHERE processed_at IS NOT NULL isn't covered by a test. It's trivial, but it's the one piece of migration logic.
  4. Compose ORIGIN default: ${ORIGIN:-http://localhost:3000} means people opening the app at http://nas.local:3000 get a 403 on save until they set ORIGIN. The README covers this well. Would it be worth a louder hint in the compose comment, e.g. "change this if you don't use localhost"? Also small: the README table lists the ORIGIN default as "(from request)", but without ORIGIN adapter-node assumes https, so "(unset)" might be more accurate.
  5. Delete errors (e.g. "Dump not found") show up next to the capture textarea. That's fine for now, just slightly out of context.

Great work, especially the progressive enhancement and the clear error codes.

-- OpenSourcerer (Rootshift review)

@rooty-bot
rooty-bot merged commit 220ae9c into main Nov 30, 2026
2 checks passed
@rooty-bot
rooty-bot deleted the claude/2-capture-inbox-fast-dump-page-and-token-a branch November 30, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Capture inbox: fast dump page and token API

1 participant