Skip to content

嵌入内容 ​

定义 ​

嵌入内容指将外部文档、插件或应用载入当前页面:最常用的是 <iframe>(嵌套浏览上下文);<embed>、<object> 多用于插件或备用 HTML 回退。嵌入会引入独立的安全边界,必须控制来源、权限与沙箱策略。

核心概念 ​

概念说明
浏览上下文iframe 内页面有独立 window、Cookie、存储(同源策略下)
同源策略父页面 JS 无法读跨域 iframe DOM;postMessage 可安全通信
sandbox限制 iframe 能力:脚本、表单、弹窗、同源等
Permissions Policy通过 allow 声明可使用的 powerful features(相机、全屏等)
点击劫持透明 iframe 盖在诱导按钮上;用 X-Frame-Options / CSP frame-ancestors 防护

<iframe> 属性速查 ​

属性说明推荐
src嵌入文档 URL可信 HTTPS 源
title无障碍名称必填,描述 iframe 用途
width / height尺寸或用 CSS;防 CLS
loadinglazy | eager非首屏地图/广告用 lazy
name浏览上下文名target 指向、脚本引用
sandbox空格分隔限制令牌见下表;最小权限原则
allowPermissions Policy如 fullscreen; payment
referrerpolicy请求 iframe 资源时的 Referer第三方常设 strict-origin-when-cross-origin
allowfullscreen允许全屏(旧)优先用 allow="fullscreen"
srcdoc内联 HTML 字符串简单沙盒内容,注意转义
credentialless无凭据加载(实验/部分浏览器)降低跨站 Cookie 风险

sandbox 令牌 ​

令牌效果
(空)最严:禁止脚本、表单、弹窗、同源、top 导航等
allow-scripts允许 JS(仍禁止同源访问)
allow-same-origin与 allow-scripts 同用才恢复同源;慎用
allow-forms允许提交表单
allow-popups允许 window.open
allow-top-navigation-by-user-activation用户激活时可改 top URL
allow-downloads允许下载

<embed> 与 <object> ​

元素典型用途主要属性
<embed>插件内容(PDF 查看器等,已式微)src, type, width, height
<object>外部资源 + 回退 HTMLdata, type, width, height;子节点为 fallback

现代 Web 优先 <iframe> 嵌同源/可信 SPA;PDF 等多用浏览器内置查看或 <iframe src="file.pdf">。

iframe 安全要点 ​

风险缓解
恶意站点嵌你的页(点击劫持)响应头 Content-Security-Policy: frame-ancestors 'self' 或 X-Frame-Options: DENY
嵌不可信第三方sandbox 默认全开限制;仅按需加令牌
sandbox + allow-same-origin + allow-scripts等同给予完整脚本能力,仅防 top 导航等
钓鱼 / 假冒 UI明确 title;视觉上区分 iframe 边界
敏感数据经 Referer 泄露referrerpolicy="no-referrer" 或严格策略
过度 allow只列需要的 feature,如 allow="clipboard-write"

示例 ​

安全的第三方 iframe(地图) ​

html
<!-- title 供读屏;sandbox 按需放开;lazy 推迟加载 -->
<iframe
  title="公司总部位置 - 交互式地图"
  src="https://maps.example.com/embed?id=123"
  width="600"
  height="450"
  loading="lazy"
  referrerpolicy="strict-origin-when-cross-origin"
  allow="fullscreen"
  sandbox="allow-scripts allow-same-origin allow-popups"
></iframe>

沙盒广告 / 不可信 HTML ​

html
<!-- 无 allow-same-origin:脚本无法访问父页面 Cookie -->
<iframe
  title="赞助商内容"
  src="https://ads.example.com/slot/1"
  width="300"
  height="250"
  loading="lazy"
  sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox"
  referrerpolicy="no-referrer"
></iframe>

object 回退 ​

html
<object data="/report.pdf" type="application/pdf" width="100%" height="600">
  <!-- PDF 无法显示时的回退 -->
  <p>无法内嵌 PDF,请 <a href="/report.pdf">下载报告</a>。</p>
</object>

父页与 iframe 通信(postMessage) ​

javascript
// 父页面:仅接受预期 origin
window.addEventListener("message", (event) => {
  if (event.origin !== "https://trusted.example.com") return;
  // 处理 event.data
});

const frame = document.querySelector("iframe");
frame.contentWindow.postMessage({ type: "init" }, "https://trusted.example.com");

常见陷阱 ​

问题建议
缺少 title无障碍审计必失败;写清 iframe 作用
全宽 iframe 高度为 0用固定高度、aspect-ratio 或 JS 同源 resize
嵌登录页在 iframe许多站点 CSP 禁止被嵌;OAuth 常用 top-level 重定向
忘记 CSP frame-src限制页面可嵌哪些源,防 XSS 后嵌恶意 iframe
sandbox="" 但内容需表单加 allow-forms,仍避免 allow-same-origin 除非必要
混用 HTTP iframe 于 HTTPS 页混合内容被 block

延伸阅读(MDN) ​