Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a child redirect a parent frame?

I have a look at these two questions and i don't understand.

Redirect parent window from an iframe action

How to prevent IFRAME from redirecting top-level window

On one hand it appears that you can redirect the parent iframe and on the other you cannot? When i try it, I have no problem redirecting the parent frame so i'm curious as in why everyone say you cannot redirect parent frame unless you are on the same domain. But I can redirect without having the frame on the same domain.

As stated previously, will redirect the parent iframe. One thing to bear in mind is that both the website, and the site contained in the iframe need to be on the same domain for this to work, or you'll get an access denied exception.

Is it browser related?

Edit

I have two pages and this works but shouldn't :

On domain 1

<html>
  <body>
    <iframe src="http://domain2.fr"></iframe>
  </body>
</html>

On domain 2

<html>
  <body>
    <script type="text/javascript">
      window.top.location.href = "http://google.fr";
    </script>
  </body>
</html>
like image 256
yokoloko Avatar asked Jul 31 '13 10:07

yokoloko


People also ask

How do I stop redirecting iframe?

Use sandbox attribute. <iframe src=”Site_URL” sandbox=””> – Full Protection. <iframe src=”Site_URL” sandbox=”allow-forms allow-scripts”> – Allow form submission and scripts to run.

Can you redirect from an iframe?

Redirect the page containing your iframe embed Modern browsers will prevent an iframe from changing the location of its parent page for security reasons. Your iframe embed code and JavaScript code will need to be modified to allow this behavior.

What is parent iframe?

A parent iframe is an internal frame in a web page that holds a secondary web page or script. They are often called iframes, since the "parent" part of the name is from the viewpoint of its content. Anything in an iframe is considered its child, while it is considered the parent of its content.

Which will allow the document to break out of the frame by navigating the top level window?

allow-top-navigation allows the document to break out of the frame by navigating the top-level window.


3 Answers

The answer to Why it is possible is perfectly simple. window.location is part of the Web API, which is not exactly the same as the JavaScript core. It's part of the DOM interface, hence it's gouverned by W3C, not ECMA. That's why it allows you to manipulate the top-window's properties.

Strictly speaking, JS isn't capable of doing this, because it lacks IO capabilities, which makes the language extremely portable. That's why browser implementations require the DOM API, to query the DOM, and request repaints or interact with the client. The DOM, though, does need IO, because it renders, and reads from the actual UI. Some people in the ECMAScript committee would rather have seen the access to the window.top heavily restricted, if not removed all together, for XSS vulnerability reasons. Sadly W3C agreed to disagree, and implemented the window.top reference anyway.
Who's right or wrong in this case? I don't know, it's easy to redirect a client to a malicious site from within an iFrame, which is unsafe. But it would be frustrating to have an iFrame, and then not having access to the top window, which would mean not being able to interact with the client as easily. But that's not the point here. Bottom line is, you can change some top window properties, and it can be useful. Just think about mashups. They pose a lot of challenges in terms of XSS safety, but open up a lot of new and exciting possibilities for webaps. To plug some of the most dangerous XSS vulnerabilities, take a look at ADSafe, which was created by Douglas Crockford. Google has a similar lib, but I forgot its name ATM...

the Same origin policy doesn't apply here, either. By changing the url in the address bar in your browser window, you're changing the window.top.location.href property, too. If there were same-origin restrictions there, the internet would be dead. You're not sending a request to another location, you're not getting data from a third-party resource and loading it in your page, you're redirecting the browser to another location, which closes and clears the DOM.

like image 123
Elias Van Ootegem Avatar answered Oct 12 '22 21:10

Elias Van Ootegem


My guess is that it is the same reason you can do the following:

<a href="http://google.com" target="_top">Redirect top to Google</a>

I found the rules for this behavior here: http://www.w3.org/TR/html5/browsers.html#valid-browsing-context-name-or-keyword

I couldn't find a "why", but personally I have found it useful to redirect the parent after someone has clicked on something within an iframe. You may want to first perform an async operation and validate something before redirecting the entire page. Since this is already possible using the <a> tag perhaps it was found appropriate in JS as well. Not sure why the <a> tag allows the functionality though.

That being said you can always prevent this behavior by adding sandbox="" attribute, example: http://jsfiddle.net/ppkzS/1/

like image 37
Parris Avatar answered Oct 12 '22 22:10

Parris


Whenever you use iframes, frames, or objects, you set up a hierarchy of windows, with these items acting as "window"s in this hierarchy.

You can traverse this hierarchy with properties such as .parent, .frameElement and the like. The property .top is the window at the highest point in the hierarchy and usually corresponds to the outermost frame.

Some actions are prohibited between windows in the hierarchy, others are not. Changing the location of a window is not prohibited.

Ultimately, people who say you cannot do this are incorrect. What you can't do is access the contents of one window from a different window if their domains differ. However, you can modify their location properties.

like image 42
Dancrumb Avatar answered Oct 12 '22 22:10

Dancrumb