The sandbox attribute: This is crucial. allow-scripts, allow-same-origin, allow-forms. Don't just use sandbox="" (too restrictive) or no sandbox (insecure).
Embedding: Use the src attribute pointing to a specific domain/subdomain.
Clickjacking protection: The MFE should send Content-Security-Policy: frame-ancestors 'self' https://shell.com;.
2. CSP (Content Security Policy):
The Shell: Needs to trust the MFE domains.
The MFE: Needs to be strict.
Strategy: Strict-dynamic? Or strict whitelisting?
Specific directives:script-src (hashed or domains), style-src, connect-src (API calls), frame-src (if nesting MFEs).
Reporting: Use report-uri or report-to to catch violations without breaking the app initially (Report-Only mode).
3. Integrity Checks:
SRI (Subresource Integrity):integrity="sha384-...".
Problem: Module Federation (Webpack 5) loads chunks dynamically. SRI is hard there.
Solution: If using Iframes, the HTML document controls the loading. If using Module Federation, strict CSP is better than SRI. I should mention both scenarios.
Version Pinning: Ensure the shell requests specific versions to prevent supply chain attacks (though SRI is better).
4. Cross-App Communication:
The risk: XSS in one app spreading to another. Malicious iframe stealing data.
Iframe approach (postMessage):
Always specify targetOrigin. Never use '*'.
Receiver: Check event.origin.
Payload: Structure the data (interface), sanitize inputs.
Non-Iframe (Window/Bus):
Since it's the same window context, XSS in MFE A compromises MFE B. This is the biggest weakness of Module Federation.
Use code blocks for configuration examples (Nginx headers, HTML attributes, JS snippets).
Use bolding for emphasis.
Add a "Risk Level" or "Priority" tag to sections.
Self-Correction during drafting:
Initial thought: Focus heavily on Webpack Module Federation.
Correction: The prompt explicitly mentions "iframe isolation" first. I should treat Iframes as the "gold standard" for security, but acknowledge that many use "soft" isolation (Module Federation). I need to address the security gap in soft isolation.
Integrity Checks: SRI is great for static script tags. It breaks with dynamic imports usually. I need to explain how to handle dynamic imports (e.g., calculating hashes at build time and passing them to the loader).
Final Polish:
Make sure to mention * wildcard usage is bad.
Emphasize frame-ancestors to prevent hijacking the MFE.
Let's generate the response following this structure.
This guide provides a comprehensive blueprint for securing a micro-frontend architecture. It focuses on defense-in-depth, ensuring that a compromise in one micro-app does not cascade into a full system breach.
Micro-Frontend Security Hardening Guide
1. Iframe Isolation (Process Isolation)
Using <iframe> is the strongest form of isolation because it leverages the browser's same-origin policy (SOP) to create separate process boundaries. However, misconfiguration can lead to Clickjacking or UI Redressing.
The Strategy
Treat every micro-frontend (MFE) as a distinct origin whenever possible. If they must share a domain, use distinct subdomains (e.g., app.shell.com vs. mfe1.shell.com).
Hardening Implementation
A. The sandbox Attribute
The sandbox attribute restricts the capabilities of the iframe. Apply the principle of least privilege.
Avoid allow-top-navigation: Prevents the iframe from redirecting the main window (anti-phishing).
Avoid allow-popups: Unless specifically required, prevents the MFE from opening new windows that might spoof the user.
Use allow-scripts cautiously: Only enable if the app is interactive.
B. Restricting Embedding (Clickjacking Defense)
The micro-frontend backend must explicitly state who is allowed to embed it. Configure your web server (Nginx/Apache) or CDN to send the Content-Security-Policy header.
Header Configuration (Nginx example):
nginx
# Only allow this MFE to be embedded by the main shell
add_header Content-Security-Policy "frame-ancestors https://shell.example.com;";
Note: frame-ancestors is the modern replacement for the deprecated X-Frame-Options header.
2. Content Security Policy (CSP)
CSP is the most critical layer for preventing Cross-Site Scripting (XSS) and Data Excretion. In an MFE architecture, you must distinguish between the Shell Policy and the Remote Policy.
The Shell Policy (The Container)
The Shell loads the MFEs. Its CSP must allow the framing and connection to remote domains.
Challenge & Solution for Dynamic Loading:
If you are using a dynamic importer (like Module Federation), SRI can be difficult because the filename might change (e.g., main.[hash].js).
Solution: Use a "Manifest Server." The Shell fetches a trusted manifest.json (which contains the current filenames and their pre-calculated hashes) before injecting the script tags.
B. Version Pinning
Never load micro-frontends using a "latest" tag (e.g., .../dist/bundle.js).
Always load specific versions or content-hashed filenames (bundle.abc123.js).
This prevents a malicious actor who compromises the CDN from immediately affecting all users; they would need to update the specific version file, which is harder to hide.
4. Cross-App Communication
Communication between the Shell and an iframe is done via postMessage. This channel is a primary target for "Message Sniffing" or "Message Injection" attacks.
The Secure Communication Protocol
A. Strict Origin Checking
Never accept messages blindly. Always verify the sender.
Inside the Micro-Frontend (Receiver):
javascript
window.addEventListener('message', (event) => {
// 1. Check Origin: Is this message coming from our trusted Shell?
if (event.origin !== 'https://shell.example.com') {
return; // Discard silently
}
// 2. Check Structure: Does the payload look valid?
if (event.data && typeof event.data.type === 'string') {
handleEvent(event.data);
}
});
B. Specific Targeting
When the MFE sends data back to the Shell, do not use '*' as the target origin.
Inside the Micro-Frontend (Sender):
javascript
// BAD: Allows any listener to intercept
// window.parent.postMessage(data, '*');
// GOOD: Restricts delivery to the specific parent
window.parent.postMessage(data, 'https://shell.example.com');
C. Channel Messaging API
For complex, ongoing conversations, use the MessageChannel API. This creates a direct, private pipe between the two windows, preventing other iframes on the page from intercepting the traffic.
javascript
// Shell setup
const channel = new MessageChannel();
const iframe = document.getElementById('mfe');
// Send one port to the iframe
iframe.contentWindow.postMessage({ type: 'INIT_PORT' }, 'https://mfe.example.com', [channel.port2]);
// Keep port1 for local use
channel.port1.onmessage = (e) => { /* handle secure reply */ };
Summary Checklist
Security Domain
Action Item
Priority
Isolation
Use sandbox="allow-scripts allow-same-origin" on iframes.
High
Isolation
Set frame-ancestors in CSP headers to whitelist the Shell.
High
CSP
Use 'strict-dynamic' for scripts to support bundlers safely.
Medium
Integrity
Implement Subresource Integrity (SRI) hashes for all loaded bundles.
High
Communication
Enforce strict event.origin checks on all postMessage listeners.
Critical
Communication
Never use targetOrigin: '*' when sending messages.