skip to Main Content

I would like to know if it is possible to obtain information about the host address of the domain from which a cookie was created on my webpage?

I have this sittuation:

  1. I’am on "domain A", where I have a script linked from "domain B".
  2. On "domain B" a method is execuded that sets a cookie on my "domain A".

Is there any way to obtain information that the cookie was created from "domain B"?

I need precise information that the cookie originates from "domain B".

2

Answers


  1. Chosen as BEST ANSWER

    I've modified the script above and it seems like I've achieved the desired effect:

    <script type="text/javascript">
        let originalSet = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').set;
        function debugAccess(obj, prop, debugGet){
           Object.defineProperty(obj, prop, {
              set: function(val) {
                 let debugObj = {};
                 try { Error.captureStackTrace(debugObj, val); }
                 catch(e) { debugObj = e; }
                 console.log(`cookie: "${val}"nstack trace:n${debugObj.stack.replace('Error', '')}`);
                 originalSet.call(this, val);
              }
           });
        }
        debugAccess(Document.prototype, 'cookie');
    </script>
    

    And now I'm getting something like this:

    cookie: "test_cookie3=hello world po 30 sekundach; expires=Tue, 15 Aug 2023 12:39:31 GMT; path=/"
    stack trace:
    
        at HTMLDocument.set [as cookie] (http://creator.domain.local/cart/:61:19)
        at setCookie (http://fedev.pl/cookie.js:9:21)
        at http://fedev.pl/cookie.js:20:3
    

  2. Modified answer from here https://stackoverflow.com/a/48185552/2213309

    <script type="text/javascript">
        function debugAccess(obj, prop, debugGet){
            Object.defineProperty(document, prop, {
                set: function(val) {
                    let obj = {};
                    try { Error.captureStackTrace(obj, val); }
                    catch(e) { obj = e; } // this is needed to make it work in FF
                    console.log (`cookie: "${val}"nstack trace:n${obj.stack.replace('Error', '')}`);
                }
            });
        debugAccess(document, 'cookie');
    </script>
    

    This should print something like the following to the console every time a piece of js code is writing a cookie.

    cookie: "cookie=chocolate"
    stack trace:
    set@http://domain.a/test.html:16:17
    @http://domain.b/script.js:1:1
    

    The stack trace can be much longer but the last line should always refer to the initiator script.

    The exact output depends on the browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search