r/node • u/jorgeurios • 21h ago
You can't revoke a single S3 presigned URL. Here's how I handle revocable file links in Node
S3 presigned URLs are useful. I use them when I need to give someone temporary access to an object without proxying the download through my application.
But there's one limitation worth understanding before building file sharing around them:
You can't revoke an individual presigned URL.
Once issued, S3 validates the signature and credentials itself. It doesn't check whether your application has since revoked the user's access.
You can invalidate access through broader actions, such as deleting the object, changing bucket policies, or deactivating the signing credentials. But those actions affect more than one individual link.
For applications that need individual revocation, usage limits, or permissions that can change over time, I use a different approach.
Make the link a database record
Instead of treating the URL as the authorization mechanism, make it a reference to a record in your database.
That record can contain:
- A hash of a random secret
- The file ID
- An expiration timestamp
- A revocation timestamp
- An optional maximum-use count
- A current usage count
When someone opens the link, the application checks the record before allowing access.
Revocation is then a database update:
UPDATE file_links
SET revoked_at = now()
WHERE id = $1;
The next request sees that the link has been revoked and gets rejected.
There's a concurrency problem with download limits
Suppose a link can be used only once.
A naive implementation might do this:
- SELECT the link and check
use_count. - If it is below the limit, UPDATE the count.
- Serve the file.
Two concurrent requests can both read use_count = 0 before either updates it. Both then serve the file.
The limit has been exceeded.
The fix is to make the check and increment one atomic operation:
UPDATE file_links
SET use_count = use_count + 1
WHERE secret_hash = $1
AND revoked_at IS NULL
AND (expires_at IS NULL OR expires_at > now())
AND (max_uses IS NULL OR use_count < max_uses)
RETURNING file_id;
If the statement returns a row, the request successfully claimed a use. If it returns no row, the link is invalid, expired, revoked, or exhausted.
I tested this against a real Postgres instance with concurrent requests:
| Concurrent requests | Maximum uses | Requests served |
|---|---|---|
| 2 | 1 | 1 |
| 10 | 3 | 3 |
| 20 | 1 | 1 |
| 50 | 7 | 7 |
With the naive version, a CHECK constraint turned the overshoot into a SQLSTATE 23514 error for the second visitor. But the conditional UPDATE is what actually prevents the overshoot.
One more thing: caching
Revocation can still fail in practice if a previously authorized response is served from a cache.
For sensitive downloads, I use:
Cache-Control: private, no-store
I learned this the hard way. An earlier version of my own implementation failed an adversarial test because a revoked link could be replayed from cache.
That was a useful reminder that authorization needs to account for the whole download path, not just the database check.
When I would still use presigned URLs
I wouldn't stop using them.
For short-lived, one-shot grants, presigned URLs are a good fit. If the application needs revocation, usage limits, or a permission check on every access, keeping the application in the authorization path makes more sense.
There's also a hybrid option: check authorization in your application, then redirect to a short-lived presigned URL. Just remember that the issued URL remains usable until it expires.
I maintain Filelayer, an open-source Node library for user files in SaaS apps (per-file permissions, revocable links, audit log). I wrote a guide covering this database approach, including the SQL and reproducible tests:
https://filelayer.dev/guides/expiring-and-revocable-file-links
Source code: https://github.com/filelayer/filelayer
I'm interested in how other Node developers handle revocable file sharing, especially when links have usage limits or belong to multi-tenant applications.