A client opens a proposal link four seconds early and sees "This link has expired". Another forwards a link from last year's project and it still resolves to a document that mentions a price you no longer charge.

Both are the same bug: the link had no opinion about time, or its opinion was checked against the wrong clock.

A share link makes three claims — which document, what the holder may do, until when — and only the third one gets tested by reality, usually in front of a client. Here is a design that survives forwarding, revocation and a server whose clock drifts.

Start with what the token has to say

Keep the payload small and boring. A version, the subject, the scope, the issuer's timestamps, and a nonce:

{ "v": 1, "doc": "9f1c2a4e-...", "scope": "view", "iss": "revercue.co.uk", "iat": 1795948800, "exp": 1797158400, "jti": "7d2f9c1e" } 

Six fields do the work:

  • scope — view, comment or sign. Never derive permission from who happens to be logged in when the link was made.
  • iat — needed for skew checks and for "links created before X are dead".
  • exp — absolute, in Unix seconds, from the issuing server's clock.
  • jti — the only thing that makes revocation possible without a database read on every request... and you will still want a read. See below.

Do not put the client's email or name in the token. It gets forwarded, it ends up in a Slack channel, and now you are leaking personal data through a URL.

Sign the bytes, not the object

This is the mistake that produces tokens that verify locally and fail in production: signing JSON.stringify(payload) means the signature covers one particular serialisation, and a different runtime may order the keys differently when it re-serialises.

Sign the exact encoded string you will send.

const enc = new TextEncoder(); async function sign(payload: object, key: CryptoKey) { const body = btoa(JSON.stringify(payload)).replace(/=+$/, ""); const data = enc.encode(body); const sig = new Uint8Array(await crypto.subtle.sign("HMAC", key, data)); return `${body}.${btoa(String.fromCharCode(...sig)).replace(/=+$/, "")}`; } async function verify(token: string, key: CryptoKey) { const [body, sigB64] = token.split("."); if (!body || !sigB64) return null; const expected = new Uint8Array(await crypto.subtle.sign("HMAC", key, enc.encode(body))); const given = Uint8Array.from(atob(sigB64), (c) => c.charCodeAt(0)); if (!timingSafeEqual(expected, given)) return null; // constant time, byte by byte return JSON.parse(atob(body)); } 

timingSafeEqual is a five-line loop that always compares the full length:

function timingSafeEqual(a: Uint8Array, b: Uint8Array) { if (a.length !== b.length) return false; let diff = 0; for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i]; return diff === 0; } 

A string === on signatures leaks length and prefix timing. It is not theoretical, and the fixed version costs nothing.

Expiry is a server decision

Validate exp against the server's clock, never the client's. The client's clock is attacker-controlled, and on a laptop that has been asleep for a month it is also just wrong.

The subtle part is which server clock. If you mint on one host and verify on another — or in a different region — the two clocks differ by milliseconds to seconds. Allow a small leeway, and be explicit about it:

const LEEWAY_SECONDS = 60; function isExpired(p: { exp: number }, now = Math.floor(Date.now() / 1000)) { return now > p.exp + LEEWAY_SECONDS; } 

Sixty seconds is enough for NTP-synced hosts and small enough that nobody exploits it. If you find yourself needing five minutes of leeway, fix the clocks, because the same drift is silently breaking signed webhooks and session cookies too.

Two more rules that prevent most support tickets:

  • Round exp down to the minute so the countdown you show in the UI matches the server exactly.
  • Check expiry before rendering anything. A page that flashes the document and then shows "expired" leaks the title.

Revocation, because a signature cannot be un-signed

If the token is valid until Friday and the deal dies on Tuesday, you cannot recall it. You need one of two things, and usually both:

  1. A short TTL — the blunt instrument. Default to days, not months.
  2. A version claim — put doc_version in the payload, store it on the document, and bump it when the owner revokes. Every outstanding link dies at once, with one update and no cleanup job.
-- one bump invalidates every outstanding link for this document update documents set share_version = share_version + 1 where id = $1; 

If you need to kill a single link ("the link I sent to the wrong person"), keep a small table:

create table revoked_links ( jti text primary key, document_id uuid not null references documents(id) on delete cascade, revoked_at timestamptz not null default now() ); 

One indexed lookup per request. That is cheap, and it is the difference between "we revoked it" and "we revoked it for anyone who did not already have the URL cached".

What the client sees when the link is dead

Status codes are part of the design, because they decide what tools and people do next.

Situation Status Body
Valid 200 The document
Expired 410 Gone "This link expired on 14 November 2026" + a request-new-link action
Revoked 410 Gone "The sender turned this link off"
Unknown token 404 Generic not-found
Bad signature 404 Generic not-found — do not explain the crypto to a stranger

A redirect to the homepage is the wrong answer for all of them: the client cannot tell whether the document vanished, the link is wrong, or your site is broken. A 410 with the date and a one-click "ask for a new link" button turns a support email into a solved problem.

Pick a TTL per document type

There is no universal number, but there is a defensible default per artefact:

Document Default TTL Why
Proposal 14 days, then a reminder Long enough to circulate internally, short enough to force a decision
Contract to sign 30 days Signing often waits on a third party
Invoice (view only) 90 days Accounts payable is slow and the link is harmless read-only
Internal draft 1 hour Nothing outside the team should hold it
One-off download Single use, 10 minutes The token is consumed on first success

Single use is the one case where you need a write on read: mark the jti as consumed inside the same transaction that serves the file.

Opens are not analytics

Recording that a link was opened is useful — "they looked at the pricing table twice" is a real signal — but a link that phones home is also a tracking pixel in a URL, and a link that logs IPs is personal data you now have to retain and delete.

What I would keep: timestamp, document id, which section was in view, and whether it was the same session as last time. What I would not keep: IP address, user agent string, or anything that turns "someone opened this" into "this specific person opened this on this device". A salted hash of the token is enough to count opens without storing the credential itself.

Checklist

  • Payload has doc, scope, iat, exp, jti, and nothing personal.
  • Signed over the encoded bytes with HMAC-SHA256, verified in constant time.
  • Expiry checked server-side with the server clock, 60-second leeway, rounded to the minute.
  • Revocation via a document-level share_version bump, plus a jti table for single links.
  • 410 with the expiry date and a "request a new link" action, never a redirect home.
  • A deliberate TTL per document type, written down.
  • No IPs, no user agents, no personal data in the URL.

Elsewhere in this series

The Client Thread is about the artefacts a project runs on — proposals, contracts, change requests and invoices — and the engineering behind keeping them in one place:

  1. The data model behind a freelancer's client thread
  2. Your proposal link should expire — plan for the day it does
  3. Signed share links: expiry, revocation and the clock skew that breaks both

Revercue sends proposals and contracts as links that expire, can be revoked, and tell the client exactly when they stopped working.