You need to enable JavaScript to run this app. 为何chrome-extension协议IFrame无法在homedepot.com页面正常加载
优惠活动
大模型
产品
解决方案
定价
更多

为何chrome-extension协议IFrame无法在homedepot.com页面正常加载

问题翻译

我正尝试向页面中插入IFrame,目标页面为:https://www.homedepot.com/order/view/tracking
插入IFrame使用的代码如下:

function createElementFromHTML(htmlString) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();
  // Change this to div.childNodes to support multiple top-level nodes
  return div.firstChild; 
}
element = createElementFromHTML(`<div id='testing-iframe'>
  <iframe src='chrome-extension://zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz/index.html'></iframe>
<div>`)
document.body.appendChild(element)

注意上述IFrame的src以chrome-extension://开头,和页面的HTTPS协议属于不同协议。
在homedepot.com页面执行上述代码后,检查#testing-iframe元素时看不到内部的IFrame,就像浏览器直接拒绝了该元素。
在互联网上绝大多数其他站点,比如https://example.com/,执行同样代码后可以在检查工具中看到IFrame元素(因为是测试URL所以没有内容加载)。
测试对比截图如下:
测试对比截图
可以看到example.com上正常生成IFrame,homedepot.com上没有生成。
另一个需要说明的点:该问题仅出现在使用chrome-extension协议的场景下,如果使用HTTPS协议的URL比如https://www.texasauto.com/,在两个页面都可以正常插入IFrame。
以上是问题的全部背景。
我的问题是:是什么导致了该问题?
我最初猜测是某类HTTP头的限制,首先怀疑是Strict-Transport-Security头,因此尝试通过其他扩展禁用该头,但是没有解决问题。之后我尝试禁用更多头,同时添加Access-Control-Allow-Origin: *头,仍然无法解决问题。
补充:homedepot.com返回的响应头如下:

access-control-allow-credentials: true
access-control-allow-headers: Authorization, Content-Type
access-control-allow-methods: POST, GET, OPTIONS, DELETE
access-control-allow-origin: http://localhost:8081
access-control-max-age: 0
cache-control: max-age=0, no-cache, no-store
content-encoding: gzip
content-language: he
content-type: text/html;charset=UTF-8
date: Sat, 29 May 2021 22:16:06 GMT
expires: Sat, 29 May 2021 22:16:06 GMT
pragma: no-cache
server-timing: cdn-cache; desc=MISS
server-timing: edge; dur=153
server-timing: origin; dur=75
set-cookie: AKA_A2=A; expires=Sat, 29-May-2021 23:16:06 GMT; path=/; domain=homedepot.com; secure; HttpOnly
strict-transport-security: max-age=86400; includeSubDomains; preload
vary: Accept-Encoding
x-akamai-transformed: 9 153137 0 pmb=mTOE,1mRUM,2
x-application-context: orders-purchases-ui:prod:8080
x-content-type-options: nosniff
x-device-type: desktop
x-frame-options: SAMEORIGIN
x-proto: secure
x-tm-zone: us-east4-a
x-xss-protection: 1; mode=block
问题解答

该问题由homedepot站点的前端安全防护策略导致,具体有两类可能:

  1. 内容安全策略(CSP)拦截
    你提供的响应头中没有包含CSP字段,但站点大概率通过页面内嵌的<meta>标签配置了CSP规则,其中frame-src配置项未放开chrome-extension:协议的加载权限,浏览器会直接拦截符合该特征的iframe插入操作,甚至将其直接从DOM中移除。
  2. DOM监听脚本主动删除
    大型电商站点普遍会部署反注入安全脚本,通过MutationObserver监听全页面的DOM变动,一旦发现插入的iframe使用了非HTTP/HTTPS的特殊协议(包括chrome-extension:、file:等),会主动将该iframe元素从DOM中删除,避免恶意脚本通过iframe窃取用户敏感信息。

你可以通过插入iframe后立即打断点的方式验证:如果DOM中短暂出现目标iframe后立刻消失,即可确认是站点的主动监听脚本触发了删除逻辑。


内容的提问来源于stack exchange,提问作者Poni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 06:36:00