Signed share links: expiry, revocation and the clock skew that breaks both
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 ,…
When a client opens a proposal link a few seconds too early, they encounter the message "This link has expired". Strangely, a link from last year's project still resolves to a document that mentions a price no longer charged. Both occurrences stem from the same underlying issue: the link has no concept of time or checks it against the wrong clock. A share link makes three key claims - which document, what the holder may do, and until when - but only the third claim gets tested by reality, typically in front of a client.
A well-designed system should survive forwarding, revocation, and a server whose clock drifts. Begin by determining what the token must communicate. Keep the payload minimal and unassuming. Include 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 suffice to accomplish all necessary tasks. The scope field specifies whether the token allows viewing, commenting, or signing. Never infer permission based on who is logged in when the link was generated.
The 'iat' field ensures skew checks work correctly and serves as a reminder for "links created before X are dead". The 'exp' field represents the absolute expiration time, expressed in Unix seconds from the issuing server's clock. The 'jti' field allows revocation without needing a database read on each request, but a read is still recommended for security reasons.
Do not include the client's email or name within the token. Once forwarded, the token may end up in a Slack channel or other shared locations, potentially leaking personal data through the URL. Instead, focus on signing the bytes themselves, not the object. This common mistake results in tokens that verify locally but fail in production. To avoid this, sign the exact encoded string you will send.
Here's how to sign and verify tokens securely:
1. Encode the payload using TextEncoder.
2. Sign the encoded bytes using a CryptoKey with HMAC.
3. Verify the token by splitting it into body and signature, and compare the signature against an expected value generated from the body and CryptoKey.
TimingSafeEqual is a simple function that ensures the signature comparison is constant-time and byte-by-byte, preventing length and prefix timing leaks. Implement a small buffer for leeway (e.g., 60 seconds) to account for clock drift between servers, ensuring reasonable security without being overly strict.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to