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

Web Workers and Shared Workers Security

Security considerations for Web Workers and Shared Workers, including data isolation and cross-context communication risks.

Web Workers and Shared Workers Security

What you'll learn: How Web Workers create isolated execution contexts and what security risks emerge from cross-context communication.

The Core Concept

Web Workers run JavaScript in background threads, separate from the main browser thread. Web Workers are dedicated to a single page, while Shared Workers can be accessed by multiple pages from the same origin. While this isolation improves performance, it introduces security considerations around data handling and message passing.

Think of workers like office employees in secure rooms: they can't directly access the main building's files (the DOM), but they can receive and send packages through a controlled mail slot (the postMessage API). The security question becomes: what goes in those packages, and who can send them?

Security Considerations

Data Isolation
Workers cannot access the DOM, window, or document objects. This sandboxing is good—it limits damage if a worker is compromised. However, workers can access XMLHttpRequest, fetch, and IndexedDB, meaning they can still make network requests or read stored data.

Cross-Context Communication Risks
Workers communicate via postMessage, which you've learned about. The same validation rules apply: always verify message origins and sanitize data. Passing sensitive data to workers means trusting that worker script not to leak it.

Shared Worker Risks
Shared Workers are accessible by multiple tabs/windows from the same origin. If one page is compromised (e.g., via XSS), it could connect to a Shared Worker and poison data or spy on messages from other tabs. This creates a cross-tab attack surface.

Script Injection
Worker scripts must be loaded from the same origin (SOP applies). Never construct worker URLs from user input or you risk executing attacker-controlled code in an isolated context with storage and network access.

Key Takeaway:
Web Workers provide execution isolation but not security boundaries—validate all messages, avoid passing sensitive data unnecessarily, and be especially cautious with Shared Workers that bridge multiple browsing contexts.