---
title: "Iframes in XSS, CSP and SOP"
section: "Web Pentesting"
sectionSlug: "pentesting-web"
sourcePath: "src/pentesting-web/xss-cross-site-scripting/iframes-in-xss-and-csp.md"
sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/pentesting-web/xss-cross-site-scripting/iframes-in-xss-and-csp.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---
# Iframes in XSS, CSP and SOP
## Iframes in XSS
There are 3 ways to indicate the content of an iframed page:
- Via `src` indicating an URL (the URL may be cross origin or same origin)
- Via `src` indicating the content using the `data:` protocol
- Via `srcdoc` indicating the content
**Accessing parent and child variables**
```html
```
```html
```
If you access the previous html via a http server (like `python3 -m http.server`) you will notice that all the scripts will be executed (as there is no CSP preventing it)., **the parent won’t be able to access the `secret` var inside any iframe** and **only the iframes if2 & if3 (which are considered to be same-site) can access the secret** in the original window.\
Note how if4 is considered to have `null` origin.
### `srcdoc` quirks that matter in real exploits
Two details around `srcdoc` are easy to miss during exploitation:[[8]](#references)
- Unless the frame is sandboxed without `allow-same-origin`, a `srcdoc` document is **same-origin with the parent**. Therefore, injecting attacker-controlled HTML into `srcdoc` is usually equivalent to giving it direct DOM access to the top document.
- Even though the document URL is `about:srcdoc`, **relative URLs are resolved using the embedding page URL as the base URL**. This means payloads such as `` or `` will target the parent origin, not `about:srcdoc`.
Practical payload:
```html
```
This is specially useful when you only control markup but know a same-origin path that returns attacker-controlled JavaScript, JSONP, or HTML without a restrictive CSP.
### Iframes with CSP
> [!TIP]
> Please, note how in the following bypasses the response to the iframed page doesn't contain any CSP header that prevents JS execution.
The `self` value of `script-src` won’t allow the execution of the JS code using the `data:` protocol or the `srcdoc` attribute.\
However, even the `none` value of the CSP will allow the execution of the iframes that put a URL (complete or just the path) in the `src` attribute.\
Therefore it’s possible to bypass the CSP of a page with:
```html