Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript, track iframes redirecting top window

Since there is no way to prevent an iframe from redirecting the top frame besides sandboxing which prevents other features required for viewability tracking I would like to track redirects. Since one site can have more than one iframe, it could be any of these.

Is there any way to track/find out which one (specific iframe) caused the top frame redirect?

Here is a sandbox (use browser console and enable preserve log):

Edit 3v50wqxz66

Note the iframe content is usually cross domain. For ease of use its within the sandox.

like image 971
user2693017 Avatar asked May 15 '18 14:05

user2693017


People also ask

How do I stop iframe from redirecting top level windows?

You can set sandbox="" , which prevents the iframe from redirecting. That being said it won't redirect the iframe either.

Does iframe support redirect?

Using a simple HTML trick, you can use an iFrames to redirect users to other Web pages without changing the URL that appears in their browsers.

What is redirect using iframe?

URL redirect in iframe embedded surveys will be "trapped" inside the iframe. In other words, the new website will only show up inside the iframe window, which is usually not what users desire.

How do I prevent an iframe?

The SAMEORIGIN option allows the page to be embedded in an iframe only if the parent page is from the same domain, which presumably is also your code. SAMEORIGIN option can be replaced with DENY , which prevents browsers from loading the page in an iframe regardless of the domain name of the parent page.


1 Answers

We can access to the iframe content with somethig like iframe.contentWindow.document but this is possible if we observe Same-origin policy.

Another approach could be setting a Content-Security-Policy header like:

<meta http-equiv="Content-Security-Policy" content="frame-src http://example.com">

This header in the parent page prevents to load sites different to http://example.com in frames, There is also a way to report the refuse behavior sending a post but unfortunately can't be setting with <meta> tag (it's only server side). With this approach we have to perform a white list, so I think maybe it's not useful in this case. But, if the white list is given the first time, is possible to set all sites available, so when the iframe redirect, browser will refuse to load it.

If it's not the case of same-origin and the possibility of performing a white list, then I think the better we can do is calling iframe onunload event, unfortunately this event are going to be fired also when iframe page reloads not only on redirection. I think it's the closest approach. To achieve that, this code works.

var srcs = ["iframe2.html","iframe.html","iframe2.html"];


        for (let i = 0; i < srcs.length; i++) {
            var iframe = document.createElement('iframe'); 
                iframe.src = srcs[i]; 
                iframe.name = "i"+i; 
                document.body.appendChild(iframe);
                window["i"+i].onunload = function(){console.log("change "+i)}
        }

Of course onunload is fired the first time, when all iframes load, so redirections are 2th 3th and so on. But we could exclude that first case.

Here a full example https://codesandbox.io/s/o16yk7mqy , I've created iframe3.html that doesn't refresh neither reload to show clearly the point. Also I've created a simple List of redirect or reload iframes.

UPDATE As I understand now, what you want is to set iframes with sandbox property and whitelist all what you want but without allow-top-navigation, something like:

<iframe src="iframe.html" sandbox="allow-script allow-forms allow-popups allow-pointer-lock allow-same-origin"></iframe>
  • This Example doesn't allow allow-top-navigation https://codesandbox.io/s/lpmv6wr6y9
  • This Example here https://codesandbox.io/s/4x8v1mojq7 allow allow-top-navigation but codesandbox prevents the frame to redirect so if we try https://4x8v1mojq7.codesandbox.io/ that is the url created by codesandbox, we could see the top frame reload.

As I said in comments, at least Chrome 64.0.3282.167, when we delegate all but allow-top-navigation when the iframe attempt to redirect top frame, it throw an exception. The behavior is different in Firefox (at least 58.0.2). Firefox deny top navigation but continues with the code.

So, as conclusion the best approach in my opinion is or a combination of sanbox and onunload or just onunload. Of course, if it could be possible, Content-Security-Policy is the safest and more flexible way. It depends of the implementation. It's almost impossible I think not to involve server side code to perform a perfect solution. There are white list to check, like this API https://developers.google.com/safe-browsing/v4/ and there are black list to check, look at this post https://security.stackexchange.com/questions/32058/looking-for-url-blacklists-of-malicious-websites .

like image 166
Emeeus Avatar answered Oct 19 '22 08:10

Emeeus