Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 253
DMCS Avatar asked Jun 19 '15 20:06

DMCS


2 Answers

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!

like image 132
Matt Ciaravino Avatar answered Nov 08 '22 19:11

Matt Ciaravino


Code to paste on Chrome console, based on this: Breaking JavaScript execution always when cookie is set

function debugAccess(obj, prop, debugGet){
        var origValue = obj[prop];
        Object.defineProperty(obj, prop, {
            get: function () {
                if ( debugGet )
                    debugger;
                return origValue;
            },
            set: function(val) {
                debugger;
                return origValue = val;
            }
        });
    };
    debugAccess(document, 'cookie');
like image 1
Asaf M Avatar answered Nov 08 '22 18:11

Asaf M