Cross-Domain Cookies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Little

    #1

    Cross-Domain Cookies

    If I load a JS file from a different domain, the browser requests it via
    HTTP, so I assume it sends any relevant cookie headers for that domain;
    why then can I not access them? E.g. suppose I have this in a file on
    domain1.com:

    <SCRIPT TYPE="text/javascript" src="http://www.domain2.com/cookie.js">
    </SCRIPT>

    and in cookie.js I have

    alert(document. cookie);

    It seems to me it should show me the cookies for domain2, no? But it
    shows me the cookies for domain1. Is there a way to get it to do what I
    want?

    --
    Alan Little
    Phorm PHP Form Processor

  • Hywel Jenkins

    #2
    Re: Cross-Domain Cookies

    In article <Xns96246ABA149 E2alanphormcom@ 216.196.97.131> , alan@n-o-s-p-
    a-m-phorm.com says...[color=blue]
    > If I load a JS file from a different domain, the browser requests it via
    > HTTP, so I assume it sends any relevant cookie headers for that domain;
    > why then can I not access them? E.g. suppose I have this in a file on
    > domain1.com:
    >
    > <SCRIPT TYPE="text/javascript" src="http://www.domain2.com/cookie.js">
    > </SCRIPT>
    >
    > and in cookie.js I have
    >
    > alert(document. cookie);
    >
    > It seems to me it should show me the cookies for domain2, no? But it
    > shows me the cookies for domain1. Is there a way to get it to do what I
    > want?[/color]

    One would hope not - what's to stop unscrupulous developers from trying
    to grab cookie data from other sites without your knowledge?

    --
    Hywel http://kibo.org.uk/
    I do not eat quiche.

    Comment

    • Matthew Hagston

      #3
      Re: Cross-Domain Cookies

      "Alan Little" <alan@n-o-s-p-a-m-phorm.com> wrote in message
      news:Xns96246AB A149E2alanphorm com@216.196.97. 131...[color=blue]
      > If I load a JS file from a different domain, the browser requests it via
      > HTTP, so I assume it sends any relevant cookie headers for that domain;
      > why then can I not access them? E.g. suppose I have this in a file on
      > domain1.com:
      >
      > <SCRIPT TYPE="text/javascript" src="http://www.domain2.com/cookie.js">
      > </SCRIPT>
      >
      > and in cookie.js I have
      >
      > alert(document. cookie);
      >
      > It seems to me it should show me the cookies for domain2, no? But it
      > shows me the cookies for domain1. Is there a way to get it to do what I
      > want?
      >
      > --
      > Alan Little
      > Phorm PHP Form Processor
      > http://www.phorm.com/[/color]

      I do not belive this is possible for just this reasons that Mr. Jenkins
      stated. If by chance you own both domains you would have to make a php
      script on the second domain to read it's own cookie then transfer the data
      that way. As far as directly reading it across another domain, you should
      not be able to do it.

      --
      Matthew Hagston


      Comment

      • Alan Little

        #4
        Re: Cross-Domain Cookies

        Carved in mystic runes upon the very living rock, the last words of
        Hywel Jenkins of comp.lang.javas cript make plain:
        [color=blue]
        > In article <Xns96246ABA149 E2alanphormcom@ 216.196.97.131> ,
        > alan@n-o-s-p- a-m-phorm.com says...[color=green]
        >> If I load a JS file from a different domain, the browser requests it
        >> via HTTP, so I assume it sends any relevant cookie headers for that
        >> domain; why then can I not access them? E.g. suppose I have this in a
        >> file on domain1.com:
        >>
        >> <SCRIPT TYPE="text/javascript"
        >> src="http://www.domain2.com/cookie.js"> </SCRIPT>
        >>
        >> and in cookie.js I have
        >>
        >> alert(document. cookie);
        >>
        >> It seems to me it should show me the cookies for domain2, no? But it
        >> shows me the cookies for domain1. Is there a way to get it to do what
        >> I want?[/color]
        >
        > One would hope not - what's to stop unscrupulous developers from
        > trying to grab cookie data from other sites without your knowledge?[/color]

        Because it would depend on the javascript on the other end to provide the
        data. The two sites would have to be in cooperation.

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Alan Little

          #5
          Re: Cross-Domain Cookies

          Carved in mystic runes upon the very living rock, the last words of
          Matthew Hagston of comp.lang.javas cript make plain:
          [color=blue]
          > "Alan Little" <alan@n-o-s-p-a-m-phorm.com> wrote in message
          > news:Xns96246AB A149E2alanphorm com@216.196.97. 131...
          >[color=green]
          >> If I load a JS file from a different domain, the browser requests it
          >> via HTTP, so I assume it sends any relevant cookie headers for that
          >> domain; why then can I not access them? E.g. suppose I have this in a
          >> file on domain1.com:[/color]
          >
          > I do not belive this is possible for just this reasons that Mr.
          > Jenkins stated. If by chance you own both domains you would have to
          > make a php script on the second domain to read it's own cookie then
          > transfer the data that way.[/color]

          I do control both domains. They are two sites that share a member base; I
          would like members to be able to log in to either one, then go to the
          other without having to log in again. I could just pass an identifier in
          the URL, but I'd rather not do that if I can figure out a way to do it
          transparently.

          Actually the dual membership is just one scenario; I have two clients at
          the moment with similar requirements. In the other case I don't have
          direct access to the server with the page that needs to receive the data;
          I can only provide an HTML/JS page that they upload, which is why I was
          mainly pursuing a JS solution.

          Anyway, I don't follow what you mean about a PHP page on the originating
          server. It's only going to have access to the cookie data if the browser
          makes the request -- i.e. I couldn't have a PHP script on the receiving
          end making the request. How would it return the data in a way that the
          receiving JS could make use of it? Sorry if I'm being dense; sometimes
          you stare at a problem so long you lose sight of the forest for the
          trees.

          --
          Alan Little
          Phorm PHP Form Processor

          Comment

          • kaeli

            #6
            Re: Cross-Domain Cookies

            In article <Xns962484062A8 8alanphormcom@2 16.196.97.131>, alan@n-o-s-p-a-m-
            phorm.com enlightened us with...[color=blue]
            >
            > Anyway, I don't follow what you mean about a PHP page on the originating
            > server. It's only going to have access to the cookie data if the browser
            > makes the request -- i.e. I couldn't have a PHP script on the receiving
            > end making the request. How would it return the data in a way that the
            > receiving JS could make use of it? Sorry if I'm being dense; sometimes
            > you stare at a problem so long you lose sight of the forest for the
            > trees.[/color]

            There is no "receiving" JS. JS is run on the client, and only the client.
            Unless you're talking .NET JScript which you aren't. :)

            As to sharing cookies across sites, lots and lots of people do this. It's a
            third party cookie.

            --
            --
            ~kaeli~
            Frisbeetarianis m (n.), The belief that, when you die, your
            soul goes up on the roof and gets stuck there.



            Comment

            • Alan Little

              #7
              Re: Cross-Domain Cookies

              Carved in mystic runes upon the very living rock, the last words of
              kaeli of comp.lang.javas cript make plain:
              [color=blue]
              > In article <Xns962484062A8 8alanphormcom@2 16.196.97.131>,
              > alan@n-o-s-p-a-m- phorm.com enlightened us with...[color=green]
              >>
              >> Anyway, I don't follow what you mean about a PHP page on the
              >> originating server. It's only going to have access to the cookie data
              >> if the browser makes the request -- i.e. I couldn't have a PHP script
              >> on the receiving end making the request. How would it return the data
              >> in a way that the receiving JS could make use of it? Sorry if I'm
              >> being dense; sometimes you stare at a problem so long you lose sight
              >> of the forest for the trees.[/color]
              >
              > There is no "receiving" JS. JS is run on the client, and only the
              > client. Unless you're talking .NET JScript which you aren't. :)[/color]

              I understand that; it was perhaps not the best term to use. I was
              attempting to refer to the page which would be "receiving" or reading the
              cookie set by the other site.
              [color=blue]
              > As to sharing cookies across sites, lots and lots of people do this.
              > It's a third party cookie.[/color]

              I understand how domain1 can set a cookie in the browser while the person
              is viewing domain2, by the domain2 page loading, for example, a web bug
              from domain1. That's my understanding of what a 3P cookie is, anyway. But
              in such a case, that cookie is still only readable by code served from
              domain2. That is, domain1 can track the person's activity across domain2,
              but I want domain1 to be able to hand the person off and say, "Here,
              domain2, this is Joe Surfer. He's all yours now."

              --
              Alan Little
              Phorm PHP Form Processor

              Comment

              • Jan Ehrhardt

                #8
                Re: Cross-Domain Cookies

                Alan Little in comp.lang.javas cript (Fri, 25 Mar 2005 11:58:42 -0600):
                [color=blue]
                >I do control both domains. They are two sites that share a member base; I
                >would like members to be able to log in to either one, then go to the
                >other without having to log in again. I could just pass an identifier in
                >the URL, but I'd rather not do that if I can figure out a way to do it
                >transparentl y.[/color]

                I have solved this by creating .js files that are actually php-files.
                Create an extra directory /jsphp/ on domain2 with the following in your
                ..htaccess (for Apache):

                AddType application/x-httpd-php .js

                This will make sure a *.js file is parsed by php. Then all you have to
                do is to put cookie.js in that directory with the following content:

                <?php
                header("Content-Type: application/x-javascript");
                $user = $_COOKIE['user'];
                ?>
                var user = "<?php echo $user; ?>";

                Suppose a user has set a cookie on domain2 with user = myname.
                Add this to any file on domain1:

                <script type="text/javascript" src="http://domain2.tld/jsphp/cookie.js>
                </script>

                cookie.js is parsed by php on domain2, gets the cookie value for 'user'
                on domain2 (!) and outputs this in the file on domain1:

                var user = "myname";

                Effect: any subsequent javascript within the file on domain1 can use the
                variable 'user'. You may duplicate the cookie or whatever. Works
                flawlessly in MSIE6, Firefox and Opera 7.

                Jan

                Comment

                • kaeli

                  #9
                  Re: Cross-Domain Cookies

                  In article <Xns9624A66E819 09alanphormcom@ 216.196.97.131> , alan@n-o-s-p-a-m-
                  phorm.com enlightened us with...[color=blue]
                  >
                  > I understand how domain1 can set a cookie in the browser while the person
                  > is viewing domain2, by the domain2 page loading, for example, a web bug
                  > from domain1. That's my understanding of what a 3P cookie is, anyway. But
                  > in such a case, that cookie is still only readable by code served from
                  > domain2. That is, domain1 can track the person's activity across domain2,
                  > but I want domain1 to be able to hand the person off and say, "Here,
                  > domain2, this is Joe Surfer. He's all yours now."[/color]

                  Oh, okay, I see the difference now.

                  Out of curiousity, is there a reason simply passing what you need via the URL
                  won't work for you?

                  Other than that, Jan seems to have posted a nice solution and there are an
                  absolute crapload of results from Google. ;)



                  --
                  --
                  ~kaeli~
                  Abdicate (v.), to give up all hope of ever having a flat
                  stomach.



                  Comment

                  • Jan Ehrhardt

                    #10
                    Re: Cross-Domain Cookies

                    Jan Ehrhardt in comp.lang.javas cript (Fri, 25 Mar 2005 22:22:38 +0100):
                    [color=blue]
                    ><?php
                    >header("Conten t-Type: application/x-javascript");
                    >$user = $_COOKIE['user'];
                    >?>
                    >var user = "<?php echo $user; ?>";[/color]

                    BTW: if you are passing really sensitive information you ought to be
                    carefull. Any other page that includes cookie.js gets hold of the same
                    information. You can always check HTTP_RERERER:

                    <?php
                    header("Content-Type: application/x-javascript");
                    $refr = $_SERVER['HTTP_REFERER'];
                    if (eregi("^http://www\.domain1\.t ld/"),$refr) $user = $_COOKIE['user'];
                    ?>
                    var user = "<?php echo $user; ?>";

                    Most browsers pass the location of the calling page to an included
                    js-file. But you can never be sure of that, so this scheme will fail in
                    a few cases.

                    Jan

                    Comment

                    • Jan Ehrhardt

                      #11
                      Re: Cross-Domain Cookies

                      kaeli in comp.lang.javas cript (Fri, 25 Mar 2005 15:45:35 -0600):
                      [color=blue]
                      >there are an absolute crapload of results from Google. ;)
                      >http://www.google.com/search?hl=en&q...=Google+Search[/color]

                      Which are all pointing to the same article at phpbuilder.com ;-)

                      Jan

                      Comment

                      • Alan Little

                        #12
                        Re: Cross-Domain Cookies

                        Carved in mystic runes upon the very living rock, the last words of Jan
                        Ehrhardt of comp.lang.javas cript make plain:
                        [color=blue]
                        > Jan Ehrhardt in comp.lang.javas cript (Fri, 25 Mar 2005 22:22:38 +0100):
                        >[color=green]
                        >><?php
                        >>header("Conte nt-Type: application/x-javascript");
                        >>$user = $_COOKIE['user'];
                        >>?>
                        >>var user = "<?php echo $user; ?>";[/color]
                        >
                        > BTW: if you are passing really sensitive information you ought to be
                        > carefull. Any other page that includes cookie.js gets hold of the same
                        > information.[/color]

                        All they would be getting would be the cookie value from their own
                        browser, which of course they can get anyway.

                        --
                        Alan Little
                        Phorm PHP Form Processor

                        Comment

                        • Alan Little

                          #13
                          Re: Cross-Domain Cookies

                          Carved in mystic runes upon the very living rock, the last words of Jan
                          Ehrhardt of comp.lang.javas cript make plain:
                          [color=blue]
                          > Alan Little in comp.lang.javas cript (Fri, 25 Mar 2005 11:58:42 -0600):
                          >[color=green]
                          >>I do control both domains. They are two sites that share a member
                          >>base; I would like members to be able to log in to either one, then go
                          >>to the other without having to log in again. I could just pass an
                          >>identifier in the URL, but I'd rather not do that if I can figure out
                          >>a way to do it transparently.[/color]
                          >
                          > I have solved this by creating .js files that are actually php-files.
                          >
                          > <?php
                          > header("Content-Type: application/x-javascript");
                          > $user = $_COOKIE['user'];
                          > ?>
                          > var user = "<?php echo $user; ?>";[/color]

                          Brilliant. I should have thought of that!

                          One thing: you don't have to do the .htaccess gimmick. You can just use:

                          <SCRIPT TYPE="text/javascript" src="http://www.domain1.com/cookie.php">

                          The TYPE attribute and the Content-Type header tell the browser what to
                          do with it. I learned this doing a similar thing with PHP files in IMG
                          tags. Of course, if you want to hide the fact that you're calling a PHP
                          file, you can do the other as well.

                          --
                          Alan Little
                          Phorm PHP Form Processor

                          Comment

                          • Alan Little

                            #14
                            Re: Cross-Domain Cookies

                            Carved in mystic runes upon the very living rock, the last words of Jan
                            Ehrhardt of comp.lang.javas cript make plain:
                            [color=blue]
                            > kaeli in comp.lang.javas cript (Fri, 25 Mar 2005 15:45:35 -0600):
                            >[color=green]
                            >>there are an absolute crapload of results from Google. ;)
                            >>http://www.google.com/search?hl=en&q...&btnG=Google+S
                            >>earch[/color]
                            >
                            > Which are all pointing to the same article at phpbuilder.com ;-)[/color]

                            Which requires server-side scripting on both sides, which I don't have in
                            one case, and requires passing an identifier in the URL, which I wanted
                            to avoid. A workable solution, but not a very clever one, unlike yours!

                            --
                            Alan Little
                            Phorm PHP Form Processor

                            Comment

                            • Alan Little

                              #15
                              Re: Cross-Domain Cookies

                              Carved in mystic runes upon the very living rock, the last words of
                              kaeli of comp.lang.javas cript make plain:
                              [color=blue]
                              > Out of curiousity, is there a reason simply passing what you need via
                              > the URL won't work for you?[/color]

                              Not that it wouldn't work, I just wanted to avoid it. URLs are more easily
                              tampered with than cookies, and cookies are just cleaner.

                              --
                              Alan Little
                              Phorm PHP Form Processor

                              Comment

                              Working...