防范远程内容风险
使用 webSecurity、CSP 和导航控制,保护 Electron 应用免受远程或不受信任网页内容带来的威胁,并建立在隔离与沙箱机制的基础之上。
防范远程内容风险 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Danger of Remote Content
Loading remote URLs or untrusted HTML can expose your app to cross-site scripting and code execution. Hardening is essential.
Prefer Local Content
The safest app loads only local files you control. Treat any remote content as hostile until proven otherwise.
Keep webSecurity On
Never disable webSecurity. It enforces the same-origin policy inside your renderer.
new BrowserWindow({
webPreferences: {
webSecurity: true
}
});Content Security Policy
A CSP restricts what scripts and resources can load, blocking injected code.
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">Validating a CSP
A strict CSP avoids unsafe-inline and unsafe-eval. You can lint your policy programmatically.
function isStrict(csp) {
return !csp.includes('unsafe-inline') && !csp.includes('unsafe-eval');
}
console.log(isStrict("default-src 'self'"));Controlling Navigation
Block unexpected navigation with the will-navigate event, allowing only your trusted origins.
function allowed(url) {
return url.startsWith('https://myapp.example.com');
}
console.log(allowed('https://evil.com'));Blocking New Windows
Intercept setWindowOpenHandler to deny or vet any attempt to open new windows from content.
contents.setWindowOpenHandler(({ url }) => {
return { action: allowed(url) ? 'allow' : 'deny' };
});Opening Links Safely
Send external links to the OS browser with shell.openExternal instead of loading them inside your app.
Disable Unused Permissions
Use a setPermissionRequestHandler to deny camera, geolocation, and other requests your app does not need.
session.setPermissionRequestHandler((wc, perm, cb) => {
cb(perm === 'notifications');
});Avoid Disabling Protections
Flags like allowRunningInsecureContent and nodeIntegration: true on remote content are dangerous. Keep defaults.
Audit Regularly
Run Electron's security checklist and keep Electron updated to inherit Chromium's latest patches.
Quick Check
Test your remote-content hardening knowledge.
Recap
You learned to harden against remote content: prefer local files, keep webSecurity on, enforce a strict CSP, control navigation and window opening, deny unneeded permissions, and audit regularly.
常见问题解答
「防范远程内容风险」课时是免费的吗?
是的 — 「防范远程内容风险」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。
「防范远程内容风险」这节课中我会学到什么?
使用 webSecurity、CSP 和导航控制,保护 Electron 应用免受远程或不受信任网页内容带来的威胁,并建立在隔离与沙箱机制的基础之上。 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Electron Desktop App Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「防范远程内容风险」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Electron Desktop App Development 课中编写并运行代码吗?
能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 安全的 IPC 模式
- 上下文隔离与预加载脚本
- 渲染进程沙箱
- 防范远程内容风险