Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for an iframe to redirect the parent page? [duplicate]

It is possible for a iframe to redirect the page it is on?

Example:

You go to www.fuu.com

On fuu.com there is a iframe

In that iframe is a website the redirects to another website.

Is it possible for fuu.com to be redirected? instead on just the iframe going to another page?

like image 598
john martin Avatar asked Aug 06 '14 23:08

john martin


1 Answers

No. An iframe is treated as a separate document with its own DOM. A redirect within the iframe is treated as a redirect only within that iframe.

In other words, The main page can not be redirected by an iframe.

EDIT: I was wrong. Consider the following situation

Top Page

<html>
<body>
<iframe src="redirect.html"></iframe>
</body>
</html>

redirect.html

<html>
    <head>
        <script>
            window.top.location = "http://www.w3schools.com";
        </script>
    </head>
</html>

That does redirect the top page to w3schools.com

To prevent this type of thing, you can remove that by using the following

<html>
<body>
<iframe src="redirect.html" sandbox="allow-scripts"></iframe>
</body>
</html>

In chrome, this would give you the following error:

Unsafe JavaScript attempt to initiate navigation for frame with URL 'http://127.0.0.1/scg/?search=&submit=Search' from frame with URL 'http://127.0.0.1/scg/iframeRedirect.html'. The frame attempting navigation of the top-level window is sandboxed, but the 'allow-top-navigation' flag is not set.

The allow-scripts allows Javascript to still be executed in the iframe but removes window.top from allowing to execute. Check this out

like image 93
Mic1780 Avatar answered Oct 12 '22 07:10

Mic1780