Course contentsShow
← Security
Lesson 1084 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

Service Worker Message Interception

Risks of service workers intercepting fetch requests and messages, including man-in-the-middle scenarios within the browser.

Service Worker Message Interception

What you'll learn: How service workers can intercept network requests and messages to create man-in-the-middle attacks inside your own browser.

The In-Browser Proxy Problem

Service workers act like a programmable proxy layer between your web page and the network. Once registered, they can intercept every fetch request your page makes—including API calls, resource loads, and even messages sent via postMessage. This creates a unique security risk: an attacker who successfully registers a malicious service worker effectively controls all communication for that origin.

Think of it like having a suspicious mail carrier who can open, read, modify, or block every letter you send or receive—except this carrier works inside your browser.

Attack Scenarios

Fetch Interception: A compromised service worker can modify API responses before they reach your application. For example, changing account balances, injecting XSS payloads into HTML responses, or stealing authentication tokens from requests.

Message Tampering: Service workers can intercept postMessage communications between your page and other windows or workers, reading sensitive data or injecting malicious commands.

Data Exfiltration: Since service workers can make their own fetch requests, they can silently send intercepted credentials, personal data, or session tokens to attacker-controlled servers.

Why This Matters

Unlike traditional MITM attacks that require network access, service worker attacks happen entirely within the browser's security boundary. Once installed (often through XSS or a compromised CDN), they persist across page reloads and can operate even when the original attack vector is patched.

Key Takeaway: Service workers have powerful interception capabilities that can create persistent, in-browser man-in-the-middle attacks, making strict CSP policies, integrity checks (SRI), and careful registration validation critical defensive measures.