security: reject download tokens without an expiry - #30
Open
amal66 wants to merge 1 commit into
Open
Conversation
Download tokens were HMAC-signed but never expired: any token ever issued (e.g. one lingering in old chat history or a shared link) remained valid forever. Ported from the fork's hardening: token payloads now carry an expiry (30-day default TTL), verification rejects expired tokens, and — fail-closed — tokens without an expiry field are rejected rather than treated as eternal. Mechanical port of apps/api/src/core/downloadTokens.ts and apps/api/src/lib/downloadTokens.ts from b3166dd into the backend/ layout, plus the fork's token-expiry test suite. tsconfig excludes test files from the build (same exclusion the test-harness PR adds) so `npm run build` stays green before vitest lands. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CEguyEgXa9JjCciXCcVemC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Download links are HMAC-signed tokens, but a token never expired: anyone who ever obtained a link (old chat exports, forwarded messages, logs) could fetch the file forever, and there was no way to invalidate it short of rotating the signing secret. This PR ports the fork's hardening: every token now carries an expiry (30-day default TTL), verification rejects expired tokens, and — fail-closed — a well-signed token without an expiry field is rejected rather than treated as eternal.
Changes
backend/src/core/downloadTokens.ts(new): pure sign/verify functions. Payload gains ane(expiry, unix seconds) field, default TTL 30 days;verifyDownloadPayloadreturnsnullwheneis missing, non-numeric, or in the past. Also hardens the timing-safe compare against length differences.backend/src/lib/downloadTokens.ts: becomes a thin env-secret wrapper (signDownload/verifyDownload/buildDownloadUrl) delegating to the core module. Public API unchanged — no caller changes needed.backend/src/lib/__tests__/downloadTokens.expiry.test.ts(new): the fork's "token expiry" suite — future-exp accepted, past-exp rejected, well-signed legacy token without an exp field rejected.backend/tsconfig.json: excludes test files fromtscoutput (byte-identical to the exclusion in the test-harness PR, so both merge cleanly), keepingnpm run buildgreen before vitest lands.Note on rollout: previously issued (non-expiring) tokens become invalid — deliberate, that is the vulnerability being closed. Fresh tokens are re-issued wherever download URLs are built.
Why
Fail-open token validation is the wrong default for a legal product: a leaked link is a permanent, unauthenticated-looking capability to the underlying document path. Expiring tokens bound that exposure, and rejecting exp-less tokens closes the legacy loophole instead of grandfathering it in.
Testing
cd backend && npm install && npm run build— tsc green as committed (no vitest required).git merge upstream-pr/test-harness, clean, no conflicts):cd backend && npm test— 2 files, 15/15 passed (12 from the harness'sdownloadTokens.test.ts+ 3 expiry tests from this PR).Provenance
All changes are mechanical ports of code in amal66/mike@origin/main (commit b3166dd); exceptions:
apps/api/src/lib/__tests__/downloadTokens.test.ts; the harness PR already ships the non-expiry subset of that file at the same backend path, and git's add/add merge conflicts even on pure one-sided insertions (verified empirically), so the remaining suite lands as a sibling filedownloadTokens.expiry.test.ts. Its contents are line-for-line from the fork's test file (only the unusedbeforeAll/afterAllimports trimmed).Credits & prior art
expfield, 30-day default TTL) to the HMAC-signed download tokens. The fork's original expiry commit records fix: add expiration to download tokens (30-day TTL) Open-Legal-Products/mike#77 as its precedent, and this PR carries the same payload shape and TTL. The one deliberate difference: fix: add expiration to download tokens (30-day TTL) Open-Legal-Products/mike#77 accepted legacy tokens without an expiry for backward compatibility; this PR rejects them (fail-closed).🤖 Generated with Claude Code
https://claude.ai/code/session_01CEguyEgXa9JjCciXCcVemC