Sub topic 14
Protecting content: signed URLs and DRM
This chapter assumes you know HTTP caching, the HLS segment layout from the packaging chapter, and how a CDN fronts an origin. It covers two layers of protection. Access control decides who can fetch bytes. DRM (digital rights management) decides who can decrypt and play them.
Two different problems
A signed URL stops people from fetching files they were not given access to. It does not stop an authorized viewer from saving the file. DRM encrypts the media so that a saved copy is useless without a license from your server.
| Layer | Protects against | Mechanism | Cost |
|---|---|---|---|
| Signed URL or cookie | Hotlinking, link sharing, scraping | Expiring HMAC token checked at the CDN edge | Low |
| Encryption with DRM | Saved or redistributed files | Encrypted segments, license server, device keys | High |
Most platforms start with signed access and add DRM for premium or licensed titles. Studios often require DRM in their contracts.
Signed URLs
The API checks that the user may watch the title. It then returns a URL containing an expiry time and a signature. The CDN recomputes the signature with a shared secret and rejects expired or tampered requests, so the origin is never contacted for invalid ones.
import hashlib
import hmac
import time
from urllib.parse import urlencode
SECRET = b"change-me" # load from a secrets manager in real use
def _mac(path, exp):
return hmac.new(SECRET, f"{path}:{exp}".encode(), hashlib.sha256).hexdigest()
def sign(path, ttl=300, now=None):
exp = int((now if now is not None else time.time()) + ttl)
return f"{path}?{urlencode({'exp': exp, 'sig': _mac(path, exp)})}"
def verify(path, exp, sig, now=None):
if int(exp) < (now if now is not None else time.time()):
return False
return hmac.compare_digest(_mac(path, exp), sig)
url = sign("/720p/index.m3u8", ttl=300, now=1_000)
print(url)
print(verify("/720p/index.m3u8", 1300, _mac("/720p/index.m3u8", 1300), now=1_100)) # True
print(verify("/720p/index.m3u8", 1300, _mac("/720p/index.m3u8", 1300), now=1_400)) # FalseThe segment problem
A player resolves relative segment URLs such as seg_0001.ts against the playlist URL, and the query string is not carried over. Signing only the playlist therefore leaves every segment request unsigned. There are three common fixes:
- Sign a path prefix, such as
/titles/42/*, and send the token in a signed cookie. - Put the token in the path (
/t/<token>/720p/seg_0001.ts) so relative URLs inherit it. - Rewrite the playlist so each segment URL carries its own signature.
Exclude the signature from the CDN cache key. If you do not, every viewer gets a different key and the cache hit rate drops to near zero.
DRM
With DRM, segments are encrypted with a content key, usually AES-128 in CBC or CTR mode. The player gets the key through a license exchange, not from the CDN.
Three DRM systems cover most devices: Widevine (Chrome, Android), FairPlay (Apple devices) and PlayReady (Windows, many TVs). Common Encryption (CENC) lets you encrypt once and serve all three. Use CMAF segments with cbcs encryption so a single set of files works everywhere. Only the license servers differ per system.
Trade-offs
- Short token lifetimes limit sharing, but long videos need tokens that outlast playback or can be refreshed.
- DRM adds startup latency from the license round trip and rules out some older devices.
- Hardware-backed DRM levels limit HD and 4K playback to trusted devices. Lower levels get lower resolutions.
Exercise
Your playlist URL is signed with a query string, but segment requests return 403 at the CDN. Why does this happen, and what is one fix?
Show answer
Relative segment URLs are resolved against the playlist path without its query string, so segments carry no signature. Fix it by sending a signed cookie that covers the title's path prefix, putting the token in the URL path, or signing each segment URL in the playlist.