保护 Module Federation 远程应用
学习如何保护远程加载机制本身,防止攻击者在运行时向联邦代码注入内容或篡改代码。
保护 Module Federation 远程应用 是 CoddyKit 上的免费 Micro Frontends Architecture with Module Federation 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Micro Frontends Architecture with Module Federation 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Remotes Are Live Code
Module Federation fetches and executes remote JavaScript at run time. That power is also a risk: if an attacker controls a remote URL, they can run code inside your app.
The Threat: Remote Tampering
Key threats to the federation layer include:
- A compromised remote host serving malicious code
- Man-in-the-middle modification of
remoteEntry.js - Loading a remote from an unexpected origin
Always Serve Over HTTPS
Loading any remote over plain HTTP allows in-transit tampering. Every remoteEntry.js and chunk must be served over HTTPS, with HSTS enforced.
Allowlist Remote Origins
Do not load remotes from arbitrary URLs. Restrict allowed origins with a Content Security Policy so only trusted hosts can supply scripts.
Content-Security-Policy: script-src 'self' https://cdn.trusted.comSubresource Integrity (SRI)
SRI lets the browser verify a fetched script matches a known hash, rejecting it if it was altered. Pairing SRI with federation guards against tampered remotes.
<script src="/cart/remoteEntry.js"
integrity="sha384-..." crossorigin="anonymous">Validate the Remote Manifest
If you load remote URLs from a manifest, that manifest is a high-value target. Serve it from a trusted origin and validate its contents before using any URL.
Avoid Dynamic Untrusted URLs
Never build a remote URL from user input or untrusted config. An attacker who influences the URL can point your app at malicious code.
// dangerous:
import(userProvidedUrl);
// safe: import from a fixed allowlisted nameIsolate Remotes Where Possible
Because remotes share the same page context, a malicious remote can read the DOM and globals. For untrusted third-party MFEs, consider iframe or sandbox isolation.
Protect Shared State and Tokens
A compromised remote can read shared stores and globals. Never place raw auth tokens on window or in shared state where any remote could harvest them.
Verify Integrity in CI/CD
Generate and pin SRI hashes during the build, and check that deployed remoteEntry files match expected hashes, so a tampered artifact fails verification before users hit it.
Defense in Depth
No single control is enough. Combine HTTPS, CSP allowlists, SRI, manifest validation, and isolation so that bypassing one layer still leaves others protecting the app.
Quick Check
Test your federation-security knowledge.
Recap
You learned to secure federation remotes:
- Remotes execute live code, so the loader is an attack surface
- Always use HTTPS and a CSP script-src allowlist
- Verify integrity with SRI and hash checks in CI
- Never load remotes from untrusted URLs
- Isolate untrusted MFEs and protect tokens
Defense in depth keeps federated code trustworthy.
常见问题解答
「保护 Module Federation 远程应用」课时是免费的吗?
是的 — 「保护 Module Federation 远程应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Micro Frontends Architecture with Module Federation 课程的其余内容,请升级到 CoddyKit PRO。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
「保护 Module Federation 远程应用」这节课中我会学到什么?
学习如何保护远程加载机制本身,防止攻击者在运行时向联邦代码注入内容或篡改代码。 你通过在浏览器中直接运行的动手代码来练习 Micro Frontends Architecture with Module Federation,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Micro Frontends Architecture with Module Federation 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Micro Frontends Architecture with Module Federation 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「保护 Module Federation 远程应用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Micro Frontends Architecture with Module Federation 课中编写并运行代码吗?
能。每节 Micro Frontends Architecture with Module Federation 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 身份验证与授权
- 跨应用安全风险
- 安全联邦化的最佳实践
- 保护 Module Federation 远程应用