Skip to content

perf(artwork): keep revisioned artwork URLs stable for a UTC day - #1376

Merged
Quick104 merged 2 commits into
mainfrom
perf/artwork-stable-signed-urls
Sep 24, 2026
Merged

Quick104 merged 2 commits into
mainfrom
perf/artwork-stable-signed-urls

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: none changed. #1210 C1 and #1211 C1 check artwork after it is edited; new artwork still gets a new URL immediately because its key carries a new revision.

Artwork URLs changed much more often than the images behind them. The web client, the Apple and Android apps (Nuke and Coil), and many third-party clients cache images by full URL. A new URL for the same poster means a new download, and on the web the card fades back to its placeholder until the download finishes.

  • Local storage: signed URLs rolled every 15 minutes, even though the capability lasts 4 hours by default. Coming back to a page after 15 minutes downloaded every visible poster again.
  • S3 presigned: the URL carries its signing second (X-Amz-Date). Every resolve produced a new URL, and each replica produced a different one.
  • Cloudflare token: the token carries its issue second, so the CDN cache key changed every second and the CDN could not serve a cache hit.

This affects every deployment. S3 and multi-node deployments see the most churn.

Approach

A revisioned key, whose filename carries a content revision such as w500.<revision>.webp, names bytes that never change. The path identifies the image and the query only authorizes the request. At the default lifetime (s3.metadata_presign_expiry, 4 hours), such a URL now stays the same for its UTC day:

  • Local signer: the issuance bucket is 24 hours. The URL expires at the end of the day plus the TTL, so it stays valid for at least the TTL whenever it is handed out.
  • S3 presigned: the URL is signed at the start of the UTC day through a wrapper that fixes the SigV4 signing time, with X-Amz-Expires set to the day plus the TTL. If the TTL is close to the SigV4 seven-day limit, the window shrinks and the TTL stays.
  • Cloudflare token: the WAF rule (is_timed_hmac_valid_v0) makes a token valid for the token TTL from its timestamp, so a day-long window would hand out expired URLs. The timestamp is truncated to a quarter of the token TTL instead, capped at a day: 45 minutes for the default 3 hours. Each URL stays valid for at least three quarters of the token TTL.

Maintainer decision needed: the target was 1 or 2 token URLs a day. With the default 3-hour token TTL this change gives 33. Reaching the target needs a token TTL of at least 4 days (345600), set in Silo and in the WAF rule, which gives 2 URLs a day and lets a leaked token URL work for up to 4 days. Please confirm the quarter-TTL cap, or say whether the default token TTL should change. docs/s3-storage-setup.md now describes this trade-off next to the Token TTL setting.

Replicas compute the same URL for the same window because the signing inputs no longer include the current second. For S3 this also relies on every replica signing with the same key. The S3 client only uses the configured static access key (credentials.NewStaticCredentialsProvider with no session token), so that holds today. The resolver now reports each URL's real expiry instead of now + TTL.

These cases keep their current behavior:

  • Mutable keys, such as library posters, collection images, and avatars that are replaced in place, keep 15-minute buckets locally and fresh URLs on S3. A URL change is the only way some clients see replaced bytes: the Apple apps keep images in Nuke's data cache, which does not revalidate.
  • Capabilities issued for less than the default, such as 15-minute avatar and chapter-thumbnail URLs, keep their shorter lifetime.
  • Admin job artifact downloads keep short buckets.

Cache headers: local revisioned responses already send private, max-age=<remaining URL lifetime>, immutable, and mutable ones send private, no-cache with an ETag. A new test now covers the revisioned header. S3 uploads are unchanged. Browsers already apply heuristic freshness from Last-Modified once the URL is stable, and setting Cache-Control at upload time would only reach new objects.

docs/architecture/blob-storage.md now records the "path is identity, query is authorization" rule and the windows. docs/images-api.md tells clients what to expect and says they may key revisioned artwork by path.

Alternatives considered:

  • A shorter bucket such as min(1h, ttl/4) would limit the extra lifetime of a leaked URL to an hour. The maintainer chose one day.
  • Staggering each key's rollover would spread the misses after 00:00 UTC without reducing them, so every revisioned URL rolls at the same time.
  • The metadata layer's 5-minute cache of resolved URLs no longer affects URL identity, because a new resolve within the window returns the same URL. It is left alone.

Client and compatibility follow-up: nothing is required in silo-apple or silo-android. Their poster caches key on the full URL, so they benefit without changes. Keying revisioned artwork by path is an optional later improvement that would also survive the daily rollover. Jellyfin-compatible image redirects use the same resolvers, so their targets become stable too.

Before and after

Metric Before After How measured
Distinct URLs for one revisioned key over 1,440 per-minute resolves, local storage 97 2 TestRevisionedArtworkURLChurnOverADay: fake clock (testing/synctest), production resolvers, simulated day from 03:07 UTC; same test run on the base commit and on this branch
Same, S3 presigned 1,440 2 same test
Same, Cloudflare token (3-hour token TTL) 1,440 33 same test
Resolves where a second replica, 20 s later, returned a different URL (local / S3 / token) 0 / 1,440 / 1,440 0 / 0 / 0 same test
Guaranteed remaining lifetime when a URL is handed out (local / S3 / token) 4 h / 4 h / 3 h 4 h / 4 h / 2 h 15 min after: asserted by the same test (4-hour artwork TTL, 3-hour token TTL); before: read from the base resolver code, which issued now + TTL

The count of 2 includes one rollover at midnight UTC. A day that starts at midnight gets 1. These counts are taken at the resolver. Before this change, the metadata layer's 5-minute cache of resolved URLs meant clients saw a new S3 URL from each replica about every 5 minutes. That figure is an estimate and was not measured.

Validation

All checks below ran after rebasing onto current main, which applied cleanly. The branch changes no generated artifacts, migrations, workflows, settings, or /api/v2 schema. Every check passed, so none needed a comparison run on main.

  • make embed-stub, go build ./..., go vet ./...: pass. gofmt -l .: no output.
  • make lint-changed: 0 issues.
  • GOFLAGS=-p=6 make test-go: pass, no failing packages.
  • make verify-settings-bindings-all verify-playback-fixtures verify-route-inventory verify-migration-ledger verify-scenario-catalogs verify-offline-routes verify-apiv2-openapi verify-apiv2-contract verify-apiv2-fixtures verify-local-paths: all pass.
  • go test -count=1 -run '^TestCommittedArtifactMatchesRouter$' ./internal/apiv2/: pass.
  • TestRevisionedArtworkURLChurnOverADay re-run: 2 distinct URLs with local storage, 2 with S3 presigning, and 33 with Cloudflare tokens, and no replica disagreements in any mode. These match the table above, and the test's minimum-lifetime assertions pass.
  • The branch's tests in internal/artworkurl, internal/s3client, internal/blobstore, and internal/apiv2 pass under -race.
  • One existing test changed: TestArtworkRejectsInvalidSignaturesBeforeStorage built its "expired" revisioned URL by signing 2 hours in the past. That URL is now still valid, so the test signs 26 hours back.
  • Live check against a throwaway MinIO container, using a temporary test that is not part of this change:
    • A URL signed at 00:00 UTC with X-Amz-Expires of 28 hours returned 200 with the object bytes.
    • A second client resolving 30 seconds later produced the same URL.
    • With a 156-hour TTL the window shrank to 12 hours, giving X-Amz-Expires of 168 hours, the SigV4 limit. That URL returned 200.
    • A URL signed 30 hours earlier with a 4-hour lifetime returned 403.
  • Not run:
    • DB-backed tests. No SQL, repository, or migration code changed, and the tests that cover DirectURL and avatar resolution do not use a database.
    • Web lint, build, and tests, because web/ is unchanged.
    • Live checks against AWS S3, R2, Garage, or a real Cloudflare WAF rule. The token test follows the WAF rule's documented timestamp arithmetic.

Risks

  • Security: a leaked revisioned artwork URL now works for up to a day plus the TTL (28 hours at the default), up from the TTL plus 15 minutes locally and the TTL on S3. Artwork is not secret media, but this is a real change. Removing a user's library access also takes up to that long to stop artwork URLs they already hold. Lowering s3.metadata_presign_expiry no longer brings a revisioned URL's lifetime below about a day; it shortens only the TTL part. Mutable keys, short-lived capabilities, and job artifacts are unchanged.
  • Every revisioned URL rolls at 00:00 UTC, so the first browse after midnight UTC downloads visible artwork once.
  • Cloudflare token deployments: a URL is now valid for at least 2 hours 15 minutes after it is handed out with the default token TTL, down from 3 hours.
  • S3-compatible backends must accept a presigned URL whose X-Amz-Date is up to a day old. That is standard SigV4 behavior, and MinIO accepted it in the live check above.
  • Replica-identical S3 URLs depend on a shared static access key. If Silo later supports rotating credentials (instance roles, IRSA, STS), each pod would sign differently, URLs would change at every rotation, and a URL would stop working when its session credential expires, whatever X-Amz-Expires says. That path does not exist today and was not tested.
  • Replicas with skewed clocks can briefly produce two URLs at a window boundary. The only cost is one extra download.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code, running in T3 Code
  • Tool(s): Claude Code built-in tools (shell, file read/edit, Workflow subagents), GitHub CLI (gh)
  • Model(s): claude-opus-5-5[1m] for the main session and all subagents
  • Involvement: Fully AI-generated; maintainer review pending
  • Adversarial review: a separate reviewer subagent (claude-opus-5-5) reviewed the full diff, re-ran the measurements, and checked for regressions. It built a scratch worktree at main as it stood then, ran the churn test there (97 / 1,440 / 1,440 distinct URLs, 0 / 1,440 / 1,440 replica disagreements), merged this branch onto main (clean merge, build and focused tests pass, new tests pass under -race -count=3), read the vendored AWS SDK presign middleware to confirm the fixed signing time applies, and ran its own MinIO container: a day-old X-Amz-Date returned 200, an expired URL returned 403, and a URL over seven days returned 400. It approved with no blockers or majors. Its findings and how each was resolved:
    • Minor: replica-identical, day-stable S3 URLs need shared static credentials; temporary credentials would differ per pod and expire early. The S3 client only uses static keys today, so this cannot happen yet. blob-storage.md and the Risks section now state the dependency.
    • Minor: the token path gives 33 URLs a day, not the requested 1 or 2, and the body did not flag that as a departure. Added the "Maintainer decision needed" note above, including the 4-day token TTL route, and a trade-off note in docs/s3-storage-setup.md.
    • Nit: lowering s3.metadata_presign_expiry no longer shortens a revisioned URL below about a day. Added to the Security risk and to blob-storage.md.
    • Nit: the "before" guaranteed-lifetime values came from the code, not a test assertion. Reworded the method cell.
    • Nit: TestArtworkRevisionedCachePolicy could fail if a run crossed midnight UTC between signing and serving. The assertion now allows 5 seconds of slack.

🤖 Generated with Claude Code

Quick104 and others added 2 commits September 24, 2026 02:56
Clients and CDNs cache artwork by full URL, so every new URL for unchanged
bytes costs a download the client already has. Local signed URLs rolled every
15 minutes. S3 presigned URLs carried the signing second and Cloudflare token
URLs the issue second, so they changed on every resolve and differed between
replicas.

A revisioned key names immutable bytes: its path is the identity and its
query only authorizes. At the default lifetime such a URL now holds for its
UTC day. The local signer uses a day-long issuance bucket, and S3 presigns at
the start of the day with X-Amz-Expires set to the day plus the TTL,
shortened to fit the SigV4 seven-day limit. The WAF rule fixes a token's
lifetime from its timestamp, so token timestamps are truncated to a quarter
of the token TTL instead. Every replica mints the same URL for the same
window. Mutable keys, short-lived capabilities such as avatars and chapter
thumbnails, and job artifact downloads keep their current buckets.

Over a simulated day of per-minute resolves through the production
resolvers, distinct URLs for one revisioned key drop from 97 to 2 (local),
1440 to 2 (S3 presigned), and 1440 to 33 (three-hour Cloudflare token), and
a second replica resolving 20 seconds later now always agrees.

A leaked revisioned URL now works for up to a day plus the TTL instead of
the TTL plus 15 minutes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Record that replica-identical presigned URLs rely on the shared static
access key, that lowering s3.metadata_presign_expiry shortens the TTL but
not the day window, and how the Cloudflare Token TTL sets how often token
URLs change. Give the revisioned cache-policy test a few seconds of slack
so a run that crosses midnight UTC cannot fail it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 6795c125-b564-431a-8b1b-4e043cc6159d

📥 Commits

Reviewing files that changed from the base of the PR and between d4e35ba and 09d51c3.

📒 Files selected for processing (13)
  • docs/architecture/blob-storage.md
  • docs/images-api.md
  • docs/s3-storage-setup.md
  • internal/api/handlers/profile_avatars.go
  • internal/apiv2/artwork_test.go
  • internal/artworkurl/artworkurl.go
  • internal/artworkurl/artworkurl_test.go
  • internal/artworkurl/churn_test.go
  • internal/blobstore/fence.go
  • internal/blobstore/s3.go
  • internal/blobstore/store.go
  • internal/s3client/client.go
  • internal/s3client/presign_window_test.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T04:00:13.725184Z 09d51c3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Quick104

Copy link
Copy Markdown
Contributor Author

Client follow-ups filed: Silo-Server/silo-apple#406 and Silo-Server/silo-android#373 key the native image caches by the revisioned artwork path, so a re-signed URL stops re-downloading bytes the device already holds.

@Quick104
Quick104 merged commit 26c5a54 into main Sep 24, 2026
15 of 16 checks passed
@Quick104
Quick104 deleted the perf/artwork-stable-signed-urls branch September 24, 2026 14:17
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