Programing

iframe에서 상위 URL에 액세스

crosscheck 2020. 5. 26. 19:42
반응형

iframe에서 상위 URL에 액세스


좋아, 나는 페이지를 가지고 있고이 페이지에는 iframe이있다. 내가해야 할 일은 iframe 페이지에서 메인 페이지의 URL이 무엇인지 확인하십시오.

검색 한 결과 크로스 사이트 스크립팅이므로 iframe 페이지가 다른 도메인에 있으면 이것이 불가능하다는 것을 알고 있습니다. 그러나 내가 읽은 곳마다 iframe 페이지가 부모 페이지와 동일한 도메인에 있으면 예를 들어 그렇게하면 작동해야한다고 말합니다.

parent.document.location
parent.window.document.location
parent.window.location
parent.document.location.href

같은 정보를 얻는 여러 가지 방법이있는 것처럼 ... 또는 다른 유사한 콤보.

어쨌든 여기에 문제가 있습니다. 내 iframe이 기본 페이지와 동일한 도메인에 있지만 동일한 SUB 도메인에 없습니다. 예를 들어

http : // www.mysite.com/pageA.html

내 iframe URL은

http : // qa-www.mysite.com/pageB.html

pageB.html(iframe 페이지) 에서 URL을 가져 오려고 할 때 동일한 액세스 거부 오류가 계속 발생합니다. 따라서 하위 도메인조차도 크로스 사이트 스크립팅으로 간주되거나 정확합니까, 아니면 내가 잘못하고 있습니까?


당신은 맞습니다. iframe을 사용할 때 하위 도메인은 여전히 ​​별도의 도메인으로 간주됩니다. 를 사용하여 메시지를 전달할 수 postMessage(...)있지만 다른 JS API는 의도적으로 액세스 할 수 없습니다.

상황에 따라 URL을 가져올 수도 있습니다. 자세한 내용은 다른 답변을 참조하십시오.


예, iframe과 기본 페이지가 동일한 (하위) 도메인에 있지 않으면 상위 페이지의 URL에 액세스 할 수 없습니다. 그러나 기본 페이지의 URL (예 : 브라우저 URL) 만 필요한 경우 다음을 시도해보십시오.

var url = (window.location != window.parent.location)
            ? document.referrer
            : document.location.href;

노트 :

window.parent.location허용된다; href속성 에 액세스하여 발생하는 OP의 보안 오류를 방지합니다 window.parent.location.href. "원점을 가진 프레임이 차단되었습니다 ..."

document.referrer"이 페이지에 링크 된 페이지의 URI"를 나타냅니다. 다른 소스가 위치를 결정한 경우 포함 문서를 반환 하지 않을 수 있습니다 . 예를 들면 다음과 같습니다.iframe

  • 컨테이너 iframe @ 도메인 1
  • 자식 iframe을 도메인 2로 보냅니다.
  • 그러나 자식 iframe에서 ... 도메인 2는 도메인 3으로 리디렉션 (예 : 인증, 아마도 SAML)하고 도메인 3은 다시 도메인 2로 다시 전송합니다 (예 : 표준 SAML 기술인 양식 제출 ()).
  • 자식 iframe의 경우 포함 도메인 1이 아닌 document.referrer도메인 3됩니다.

document.location"문서의 URL에 대한 정보를 포함하는 Location 객체"를 나타냅니다. 아마도 현재 문서, 즉 현재 열려있는 iframe 일 것입니다. window.location === window.parent.location다음은 iframe의이 href는 IS 같은 포함하는 부모의로 href.


방금이 문제에 대한 해결 방법을 발견했지만 너무 언급이있는 곳에서는 토론을 찾지 못했습니다. 부모 프레임의 제어가 필요합니다.

iFrame에서 다음 iframe을 원한다고 가정하십시오. src = "http://www.example.com/mypage.php"

글쎄, iframe을 지정하기 위해 HTML 대신 자바 스크립트를 사용하여 iframe에 대한 HTML을 작성하고 "빌드 타임에"자바 스크립트를 통해 부모 URL을 가져 와서 src 대상의 쿼리 문자열에 url GET 매개 변수로 보냅니다. 그래서:

<script type="text/javascript">
  url = parent.document.URL;
  document.write('<iframe src="http://example.com/mydata/page.php?url=' + url + '"></iframe>');
</script>

그런 다음 URL 문자열을 구문 분석하여 사용자가 url 변수를 가져 오는 Javascript URL 구문 분석 함수를 찾으십시오 (이 경우 "url").

나는 여기에 훌륭한 URL 문자열 파서를 발견했다 : http://www.netlobo.com/url_query_string_javascript.html


iframe이 다른 도메인 (크로스 도메인)에서 온 경우 다음을 사용하면됩니다.

var currentUrl = document.referrer;

그리고-여기에 주요 URL이 있습니다!


동일한 도메인과 다른 하위 도메인에있는 페이지의 경우 document.domainjavascript를 통해 속성을 설정할 수 있습니다 .

부모 프레임과 iframe은 모두 document.domain을 공통적 인 것으로 설정해야합니다.

www.foo.mydomain.com, api.foo.mydomain.com각각 foo.mydomain.com또는 하나만 사용 mydomain.com하고 호환 가능합니다 ( com보안상의 이유로 둘 다로 설정할 수는 없습니다 ...)

also, note that document.domain is a one way street. Consider running the following three statements in order:

// assume we're starting at www.foo.mydomain.com
document.domain = "foo.mydomain.com" // works
document.domain = "mydomain.com" // works
document.domain = "foo.mydomain.com" // throws a security exception

Modern browsers can also use window.postMessage to talk across origins, but it won't work in IE6. https://developer.mozilla.org/en/DOM/window.postMessage


The following line will work: document.location.ancestorOrigins[0] this one returns the ancestor domain name.


Try it:

document.referrer

When you change you are in a iframe your host is "referrer".


I've had issues with this. If using a language like php when your page first loads in the iframe grab $_SERVER['HTTP_REFFERER'] and set it to a session variable.

This way when the page loads in the iframe you know the full parent url and query string of the page that loaded it. With cross browser security it's a bit of a headache counting on window.parent anything if you you different domains.


var url = (window.location != window.parent.location) ? document.referrer: document.location;

I found that the above example suggested previously worked when the script was being executed in an iframe however it did not retrieve the url when the script was executed outside of an iframe, a slight adjustment was required:

var url = (window.location != window.parent.location) ? document.referrer: document.location.href;

I couldnt get previous solution to work but I found out that if I set the iframe scr with for example http:otherdomain.com/page.htm?from=thisdomain.com/thisfolder then I could, in the iframe extract thisdomain.com/thisfolder by using following javascript:

var myString = document.location.toString();
var mySplitResult = myString.split("=");
fromString = mySplitResult[1];

The problem with the PHP $_SERVER['HTTP_REFFERER'] is that it gives the fully qualified page url of the page that brought you to the parent page. That's not the same as the parent page, itself. Worse, sometimes there is no http_referer, because the person typed in the url of the parent page. So, if I get to your parent page from yahoo.com, then yahoo.com becomes the http_referer, not your page.


I've found in the cases where $_SERVER['HTTP_REFERER'] doesn't work (I'm looking at you, Safari), $_SERVER['REDIRECT_SCRIPT_URI'] has been a useful backup.


In chrome it is possible to use location.ancestorOrigins It will return all parent urls


I know his is super old but it blows my mind no one recommended just passing cookies from one domain to the other. As you are using subdomains you can share cookies from a base domain to all subdomains just by setting cookies to the url .basedomain.com

Then you can share whatever data you need through the cookies.


This worked for me to access the iframe src url.

window.document.URL

Get All Parent Iframe functions and HTML

var parent = $(window.frameElement).parent();
        //alert(parent+"TESTING");
        var parentElement=window.frameElement.parentElement.parentElement.parentElement.parentElement;
        var Ifram=parentElement.children;      
        var GetUframClass=Ifram[9].ownerDocument.activeElement.className;
        var Decision_URLLl=parentElement.ownerDocument.activeElement.contentDocument.URL;

참고URL : https://stackoverflow.com/questions/3420004/access-parent-url-from-iframe

반응형