Cache Poisoning via Service Workers
What you'll learn: How attackers exploit service worker caching to inject and persist malicious content across sessions in web applications.
The Core Threat
Service workers sit between your browser and the network, caching resources for offline use and performance. But if an attacker can inject malicious content into a service worker's cache, that poisoned content persists across browser sessions—even after the user closes all tabs. Every time the app requests a "clean" resource, the service worker serves the attacker's version instead.
How the Attack Works
Imagine a library that stores copies of books in a vault. If someone sneaks in and replaces a trusted book with a fake containing dangerous instructions, everyone who borrows that book gets the poisoned version—until someone manually checks and removes it.
An attacker might:
- Exploit a service worker registration vulnerability (covered in your previous lesson) to register a malicious service worker
- Inject malicious responses into the Cache API storage during the service worker's fetch handling
- Poison legitimate resource URLs (like
main.jsorindex.html) with malicious content - Persist across sessions because cached content remains until explicitly cleared
The poisoned cache survives:
- Page refreshes
- Browser restarts
- Multiple user sessions
Why It's Dangerous
Unlike traditional XSS that might disappear on reload, cache poisoning creates persistent compromise. The malicious content becomes the "source of truth" for that origin, potentially stealing credentials, injecting backdoors, or redirecting users indefinitely.
Key Takeaway: Cache poisoning via service workers turns temporary injection into persistent compromise by hijacking the service worker's caching layer, requiring explicit cache clearing or service worker unregistration to remediate.