Sub topic 14
Controlling who can fetch media: signed URLs and signed cookies
This chapter assumes you know how a CDN caches content and how presigned upload URLs work. Public media can be cached by anyone. Private media, such as invoices, paid videos, or personal photos, needs a check before the CDN serves it.
The problem with checking on every request
You could route every download through your API server and check permissions there. That puts your server back in the data path and defeats the CDN. The usual answer is to check permission once, then hand the client a short-lived proof the CDN can verify by itself.
The CDN holds a public key. Your server holds the private key. The server signs a statement such as "this path is allowed until 10:30". The CDN checks the signature and the expiry without calling your server.
Signed URLs
A signed URL carries its proof in the query string. It suits a single file, such as a download link or one video.
import datetime
from botocore.signers import CloudFrontSigner
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
def rsa_signer(message):
with open("private_key.pem", "rb") as f:
key = serialization.load_pem_private_key(f.read(), password=None)
return key.sign(message, padding.PKCS1v15(), hashes.SHA1())
signer = CloudFrontSigner("K2EXAMPLEKEYID", rsa_signer)
expires = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=10)
url = signer.generate_presigned_url(
"https://cdn.example.com/private/42/report.pdf",
date_less_than=expires,
)
print(url)The key ID names the public key registered with the CDN. The SHA-1 signature is what CloudFront's canned policy requires. Anyone who holds the URL can use it until it expires, so keep the lifetime short.
Signed cookies
A signed cookie carries the same proof in cookies. It suits many files under one path, such as an HLS stream. A video player requests a playlist and then hundreds of segments. Signing each segment URL would force you to rewrite the playlist for every viewer. With cookies, the browser attaches the proof to every request automatically.
| Row | Signed URL | Signed cookie |
|---|---|---|
| Scope | One file, or a wildcard in a custom policy | A path pattern, such as `/videos/42/*` |
| Where the proof lives | Query string | `CloudFront-Policy`, `CloudFront-Signature`, `CloudFront-Key-Pair-Id` cookies |
| Leaks through | Shared links, logs, referrers | Rarely shared by accident |
| Best for | Downloads, single images | Streaming, galleries |
Signed cookies only work when the media is served from a domain your cookies can reach. A common layout is app.example.com for the app and cdn.example.com for media, with the cookie set on .example.com.
Common errors
- Long expiry times. A 30-day URL is a public URL. Use minutes for downloads. For streaming, use the length of the video plus a margin.
- Caching the signed URL. The cache key should ignore the signature parameters. Otherwise every viewer gets a separate cache entry and your hit rate falls to zero.
- Leaving the origin open. If the bucket is public, people can skip the CDN and its checks. Restrict the bucket so that only the CDN can read it.
- Embedding the private key in client code. Signing happens only on the server.
Exercise
A paid course serves each lesson as an HLS stream: one playlist and about 400 segment files. Would you use signed URLs or signed cookies, and why?
Show answer
Signed cookies. One cookie with a path policy such as /lessons/7/* covers the playlist and every segment. With signed URLs you would have to sign all 400 segment URLs and rewrite the playlist for each viewer.