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?
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...
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!
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With