Iframes in XSS, CSP and SOP
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
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 thedata:
protocolVia
srcdoc
indicating the content
Accesing Parent & Child vars
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.
Iframes with CSP
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:
Note how the previous CSP only permits the execution of the inline script.
However, only if1
and if2
scripts are going to be executed but only if1
will be able to access the parent secret.
Therefore, it’s possible to bypass a CSP if you can upload a JS file to the server and load it via iframe even with script-src 'none'
. This can potentially be also done abusing a same-site JSONP endpoint.
You can test this with the following scenario were a cookie is stolen even with script-src 'none'
. Just run the application and access it with your browser:
Other Payloads found on the wild
Iframe sandbox
The content within an iframe can be subjected to additional restrictions through the use of the sandbox
attribute. By default, this attribute is not applied, meaning no restrictions are in place.
When utilized, the sandbox
attribute imposes several limitations:
The content is treated as if it originates from a unique source.
Any attempt to submit forms is blocked.
Execution of scripts is prohibited.
Access to certain APIs is disabled.
It prevents links from interacting with other browsing contexts.
Use of plugins via
<embed>
,<object>
,<applet>
, or similar tags is disallowed.Navigation of the content's top-level browsing context by the content itself is prevented.
Features that are triggered automatically, like video playback or auto-focusing of form controls, are blocked.
The attribute's value can be left empty (sandbox=""
) to apply all the aforementioned restrictions. Alternatively, it can be set to a space-separated list of specific values that exempt the iframe from certain restrictions.
Iframes in SOP
Check the following pages:
Bypassing SOP with Iframes - 1Bypassing SOP with Iframes - 2Blocking main page to steal postmessageSteal postmessage modifying iframe locationLearn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Last updated