Skip to content

News: review follow-ups for 0.10.10 - #1559

Merged
neuromechanist merged 6 commits into
devfrom
fix/news-review-followups
Sep 29, 2026
Merged

neuromechanist merged 6 commits into
devfrom
fix/news-review-followups

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Follow-ups from the prod-safety review of #1553 (news posts, squash 53f3a24), to land before the 0.10.10 release (#1557).

Changes

  • Image Content-Type comes from the file name (backend/src/routes/news.ts). GET /news/media/:file always sends the type its regex-locked extension implies (<sha256>.(png|jpg|webp|gif)) and no longer reads the R2 object's httpMetadata.contentType, so a bad metadata write can never make the API origin serve text/html.
  • Audit rows for post writes (backend/src/services/news.ts, backend/src/routes/admin/news.ts). Create, update and delete each write news_post_created / news_post_updated / news_post_deleted with resource_type news_post, resource_id the post id, user_id the actor, and details {id, slug, status, published_at, actor} (the actor's username). The audit insert rides in the same db.batch() as the write, the pattern users.ts uses for the upload-access repair, so a failed audit insert rolls the write back and the request is a 500 with nothing written.
    • It is an INSERT ... SELECT from the post rather than auditLogStatement: a create's id only exists after its INSERT runs, so the audit statement, queued after it, reads the row back by slug (unique). Update (queued after the UPDATE) and delete (queued before the DELETE) select by id, so a 404 writes no audit row. The columns match AUDIT_LOG_INSERT_SQL, and details stays far under AUDIT_DETAILS_MAX_BYTES because each field is capped by migration 0087's CHECKs or is fixed-width.
    • An update's row records the post as the update left it. The previous slug is in that post's earlier audit row; no post exists in production yet, so every production post will have a complete chain.
  • The image upload is audited too. An upload publishes: the object is readable by anyone with the URL from that moment, and the API can neither list nor delete it. POST /admin/news/media writes news_media_uploaded (resource_type news_media, resource_id the file name, details {url, content_type, bytes, already_stored, actor}) BEFORE the R2 put, through a hook in storeNewsImage. An R2 put cannot share a transaction with D1, and this order fails safe: an audit insert that fails leaves nothing stored. The cost is an extra row for a put that fails after it. A re-upload of bytes already stored is audited too, with already_stored: true.
  • published_at is bounded to the years 2000 through 2100 (backend/src/routes/admin/news.ts). The check is on the year as written. 9999-12-31T23:59:59-12:00 (a 500 from the NOT NULL insert, since datetime() returned NULL) and 0000-01-01T00:00:00+14:00 (stored as year -0001) are now 400s naming published_at: "published_at must be in the years 2000 through 2100". An offset can still move an edge date across a year boundary in UTC (2100-12-31T23:59:59-12:00 is stored as 2101-01-01 11:59:59). That stays readable.
  • ADR 0076, new Operations section:
    • (a) Recalling a mistaken image: the R2 object delete, the API cache purges for /news/media/<file> and the /nemar/news/media/<file> alias, and the website's copy (purge, or a redeploy for a fresh edge-cache namespace).
    • (b) Visibility after unpublish or delete: the 404 is no-store. The list and detail send public, max-age=30, s-maxage=300, stale-while-revalidate=600, so a cache can hold the old copy for at most 15 minutes. The website's own page caching comes on top.
    • (c) The deploy token needs R2. The ADR records run 36610376353: attempt 1 hit Authentication error [code: 10000] on /r2/buckets/nemar-news-media-dev at 18:22Z, and attempt 2 (18:29Z to 18:32Z) succeeded with the same token.
    • The recall command adds --remote: Wrangler 4 runs r2 object commands against local storage without it, so the command as first drafted would have "deleted" nothing.

Not done

  • The admin list still returns full bodies (review item 4). The website's admin client breaks without them: parseAdminNewsList in nemarOrg/website src/lib/news.ts (Updata command not working #372, merged to staging) parses each row with parseNewsPost, which drops any row without a string body. Returning NEWS_SUMMARY_COLUMNS would silently empty /admin/news on the website. The list page renders no body, so the fix is website-side first: parse the admin list as summaries, then switch this route to the summary columns (tracked in Admin news list: return summaries once the website parses them #1560). listAllNews now says so, and the admin-list test pins body on every row so the switch cannot land alone.
  • Pagination for the admin list: also not done. The website's admin page expects every post in one response, and a default page size would hide posts from it.
  • Notices' expires_at has the same unbounded-year gap: a year-10000 value becomes NULL. createNotice's round-trip guard turns that into a 500, not a silent never-expiring notice. It is outside News posts: storage, public read, admin CRUD, image upload #1553 and changes a different API, so it is left for its own change (Notice expires_at accepts years SQLite cannot read #1561).

Verification

  • bun run typecheck (root and backend), bun run lint, and biome check on every changed backend file: clean.

  • backend/test/news-routes.test.ts: 75 pass. It has 10 new tests: 5 for the year bounds, 5 for audit rows. The audit tests cover rows for all three writes, none for 400/403/404/409, and a trigger-failed audit insert rolling back each write. On the old code, the year tests fail (500 and 201) and the audit tests fail.

  • backend/test/news-media-routes.test.ts: 24 pass. It has 4 new tests:

    • 200 body and headers;
    • Content-Type over bad metadata (text/html, text/html; charset=utf-8, application/xhtml+xml, none);
    • upload audit rows;
    • a failed audit insert stores nothing.

    Every refusal now also asserts no audit row. The Miniflare instance now runs the public news routes in workerd (bundled from backend/test/helpers/news-routes-worker.ts), which reaches the 200 branch that bun cannot (DataCloneError). That closes the path the file header used to list as uncovered. The Content-Type test fails on the old code (text/html).

  • test/admin-route-inventory.unit.test.ts: 4 pass (no route changes). test/adr-index.unit.test.ts: 8 pass.

  • The whole offline unit-pure tier, with test.yml's file classification, run locally: 419 files, 7411 pass, 26 skip, 0 fail.

  • No mocks. Everything runs on real SQLite with every migration applied, and on Miniflare's R2 in workerd. Audit failures are produced by a real RAISE(ABORT) trigger, as test/backfill-names-route.test.ts does.

GET /news/media/:file now always sends the Content-Type its regex-locked
file extension implies, so a bad metadata write on the R2 object can never
make the API origin serve text/html.

Tested: the media route tests now run the public news routes in workerd
through Miniflare, which reaches the 200 branch bun cannot; the new test
fails on the old code (text/html) and passes (22 pass).
zod accepted 9999-12-31T23:59:59-12:00, which SQLite datetime() turns
into NULL (a 500 from the NOT NULL insert), and 0000-01-01T00:00:00+14:00,
stored as year -0001. The year as written must now be 2000 through 2100,
so both are a 400 naming published_at.

Tested: new cases for both extremes on POST and PUT, the years just
outside, and the accepted edges; they fail on the old code (500, 201) and
pass (70 pass).
Each admin write now inserts an audit_log row (news_post_created,
news_post_updated, news_post_deleted; resource_type news_post, resource_id
the post id; details with id, slug, status, published_at and the actor's
username) in the same D1 batch as the write, so a failed audit insert
fails the request with nothing written. The row is read from the post
itself (INSERT ... SELECT) because a create's id does not exist until its
INSERT runs; for update and delete this also means a 404 writes no row.

Tested on real SQLite migrations: rows for all three writes, none for
refused or missed writes, and a trigger-failed audit insert rolls each
write back; the 5 new tests fail on the old code (75 pass).
An upload publishes: the object is readable by anyone with its URL, and
the API can neither list nor delete it. POST /admin/news/media now writes
a news_media_uploaded row (resource_id the file name; details with url,
type, size, already_stored, actor) before the R2 put, so an audit insert
that fails leaves nothing stored. Re-uploads of existing bytes are
audited too.

Tested: rows for a new and a repeated upload, none for every refusal,
and a trigger-failed audit insert stores nothing; the 2 new tests fail on
the old code (24 pass).
Trimming GET /admin/news to the summary columns would empty the website's
admin list: its parseAdminNewsList (nemarOrg/website#372) drops any row
without a string body. The list keeps full posts until the website parses
it as summaries; the test now pins the body on every row.

Tested: news-routes.test.ts (75 pass).
Adds an Operations section: how to recall a mistaken image (R2 object
delete with --remote, the API and website cache purges, the website
redeploy), how long a post stays visible after unpublish or delete from
the public routes' Cache-Control, and why the deploy token needs R2 (run
36610376353 attempt 1). Also records the new audit rows.

Tested: test/adr-index.unit.test.ts.
@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1559

Version: 0.10.10-PR1559.a0263ad
Commit: a0263ad


🤖 Updated on each push.

neuromechanist added a commit that referenced this pull request Sep 29, 2026
Added (#1553, #1559), Migrations (0087), and Deploy coupling (the R2
binding, biosigio 1.2.10 on Hallu, the re-enrichment sweep). Changelog
format test passes.
@neuromechanist
neuromechanist merged commit 01eb954 into dev Sep 29, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the fix/news-review-followups branch September 29, 2026 19:13
neuromechanist added a commit that referenced this pull request Sep 29, 2026
Added (#1553, #1559), Migrations (0087), and Deploy coupling (the R2
binding, biosigio 1.2.10 on Hallu, the re-enrichment sweep). Changelog
format test passes.
@neuromechanist neuromechanist mentioned this pull request Sep 29, 2026

This branch was previously deployed

1 inactive deployment
npm — a0263add Deployed Sep 29, 2026 by neuromechanist via publish #2986
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.

1 participant