# Protecting HLS video with CloudFront signed cookies

> Why signed cookies fit HLS better than signed URLs, and how StreamVault issues them after a Cognito PKCE sign-in.

Canonical: https://pshah.fun/notes/protecting-hls-with-cloudfront-signed-cookies
Last updated: 2026-10-21

Published 28 September 2026, updated 21 October 2026. Tags: Cloud, AWS, Security.

## In short

Use CloudFront signed cookies, not signed URLs, to protect HLS video: one cookie scoped to a path covers every playlist and segment the player requests, so nothing has to be rewritten. StreamVault sets the cookies from a Lambda after a Cognito PKCE sign-in, and CloudFront checks them at the edge before any request reaches S3.

An HLS player doesn’t request one video file. It requests a master playlist, then a media playlist, then a segment every few seconds. Signing each URL means rewriting playlists on the fly. A signed cookie scoped to the distribution path covers every request the player makes, and CloudFront validates it at the edge before anything reaches S3.

## How does a viewer get the signed cookies?

### Sign-in

1. /auth/login (`PKCE verifier + HMAC-signed state`)
2. Cognito Hosted UI (`authorization code`)
3. /auth/callback (`verify state, redeem code`)
4. Verify ID token (`aws-jwt-verify`)
5. Set cookies (`CloudFront-Policy, -Signature, -Key-Pair-Id`)

The Lambda keeps the PKCE verifier in a short-lived HttpOnly cookie scoped to /auth and signs it with a secret from Secrets Manager, so a tampered or replayed state is rejected before the code is redeemed. After the ID token verifies, it writes a custom policy that allows the viewer domain for four hours:

```ts
const policy = JSON.stringify({
  Statement: [{
    Resource: `https://${config.viewerDomain}/*`,
    Condition: { DateLessThan: { "AWS:EpochTime": Math.floor(expiresAt.getTime() / 1000) } },
  }],
});
```

## Which cookie settings matter?

- Every cookie is HttpOnly and Secure, so page scripts can’t read the signature.
- The auth-state cookie uses SameSite=Lax so it survives the redirect back from Cognito.
- Sign-out clears all three CloudFront cookies and the state cookie explicitly.
- The public key, key group, user pool, secrets and API are declared in one CDK stack, so the setup is reproducible.

Takeaway: for segmented media, the unit of access control is a path prefix, not a file.

Based on: StreamVault repository: services/auth/src/handler.ts, infra/lib/video-auth-stack.ts.

Project: [StreamVault case study](https://pshah.fun/projects/streamvault), [source on GitHub](https://github.com/pshah-lab/StreamVault)

## Related

- [All engineering notes](https://pshah.fun/notes)
