javascripthttpcookiestracking-pixel

Determining origin of cookie which javascript or tracking pixel


I need to be able to determine and identify the source of cookies. While many cookies will come to the browser in the HTTP response of the original page, others are added to the browser via javascript or via assets being loaded on the page using http (such as tracking pixels or AJAX calls).

What is a good way to determine/identify the source of each cookie?


Solution

  • Posting this as I was struggling with this question as well and finally found a solution.

    This works in the Firefox console only as far as I can tell...

    1. Set a breakpoint on the very first line of javascript that you know runs on your page after a refresh (before any cookie is set).
    2. Then clear your cache and cookies.
    3. Paste the below code snippet into your console in Firefox.
    4. Remove the breakpoint and resume script execution.

    You should see the stack trace for each cookie being created in the console!

    origDescriptor = Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie'); // add cookie property to HTMLDocument constructor
    
    Object.defineProperty(document, 'cookie', {
        get() {
            return origDescriptor.get.call(this);
        },
    
        set(value) {
            console.log("%c Cookie is :" + value, "background: #ffffff; color: #000000");
            console.trace();
            // debugger;
            return origDescriptor.set.call(this, value);
        },
    
        enumerable: true,
        configurable: true
    });
    

    I have to give credit to fflorent for this code he posted in another topic - thanks!