> For the complete documentation index, see [llms.txt](https://gitbook.cdxiaodong.life/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.cdxiaodong.life/web-ying-yong-an-quan/html-zhu-ru/iframe.md). Agents can also search and read this documentation through its [MCP server](https://gitbook.cdxiaodong.life/~gitbook/mcp).

# iframe注入

## 概述

[`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) 标签用于将HTML文档嵌入到另一个HTML文档中。如果插入文档的源位于另一个源，同源策略将阻止它们双方访问另一个文档的内容。

## 开放重定向

子文档可以查看和设置父文档的 location 属性，即使是跨域的 `top.window.location`。

例如，如果 `vulnerable-website.com` 包含以下 `iframe`：

```html
<iframe src=//malicious-website.com/toplevel.html></iframe>
```

其中 `https://malicious-website.com/toplevel.html` 是：

```html
<html><head></head><body><script>top.window.location = "https://malicious-website.com/pwned.html"</script></body></html>
```

当 `iframe` 加载时，父文档将被重定向到 `https://malware-website.com/pwned.html` 页面，即使子文档是从不同的源加载的。在这种情况下，同源策略将被绕过，因为 `iframe` 没有被"沙盒化"，请查看 [sandbox](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) iframe 属性。

参考资料：

* [`<meta>` 和 `<iframe>` 标签链接到SSRF](https://medium.com/@know.0nix/hunting-good-bugs-with-only-html-d8fd40d17b38)
