Why do frame breakers work cross-domain, and can you conditionally use frame breakers?

倖福魔咒の 提交于 2019-11-29 06:29:32

FOr your answer to number 1: In terms of security, there is a big difference between read access and write access. Being able to read top.location.href is a security problem. Being able to write to top.location.href is not.

As for the answer to your question, I don't know javascript well enough to be sure, but one idea would be to assumine that if reading top.location fails (check for exceptions), it is on a different domain.

The answer to question 1 is that the equality operator can be used against top.location.href for legacy reasons. Breaker.html cannot read top.location.href but it can compare it with another value.

The answer to question 2 then becomes no, you must use the !== to part because you won't be able to do a substring on top.location.href from a cross domain breaker.html.

I could be wrong but that's my understand of the current iframe world.

This is for question number 2: If you want to take HREF of parent.location (not top.location), you can do this:

if ((window.top === window.parent) && (history.length==1)) parentHREF=document.referrer;

Basically what this code does is:
[1] Checking if parent frame is the top one because you can take only parent's HREF even if it is not the top frame.
[2] Checking if iframe's history was blank before loading its source, because if not... document.referrer will return the last HREF in this frame history.

After that, you have a new problem: in case history.length's value is more than one, you can use a whitelist of hostnames to check if it has to be opened or not:

if ([location.hostname, 'stackoverflow.com'].indexOf(location.hostname)>=0) hasToBeOpened=true;

Note that you another option: can use a landing page to check if the "first" page has to open or not, use this code:

<head>
<script>
var parentHREF;
if ((window.top === window.parent) && (history.length==1)) parentHREF=document.referrer;
if (/*conditions mentiones above*/) document.write("<META http-equiv='refresh' content='0;URL=http://example.com/go-here.html'>");
</script>
</head>

Doing it this way, the "first" page will replace history's first (in this case it is first) value. That code is asuming "example.com" is your domain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!