EDIT: After being recommended to work with headers, I asked a similar question here.
I'm trying to develop a Chrome extension that analyzes cookies being set/retrieved by websites and, if a cookie doesn't meet certain requirements, stops it from being set/retrieved.
I figured I would have to override the cookie setter and getter.
I have found similar questions:
Cookie Law, Cookie setter & getter
Safely overriding document.cookie in Google Chrome extension
Defining a getter for document.cookie
Retrieving document.cookie getter and setter
Reading the answers I have to say I am quite confused and am no longer sure what I'm trying to do is even possible.
Nevertheless, referring to question #3 in particular, I tried coming up with a simple test extension that overrides cookie getter and setter with functions that don't do anything (so no cookie should ever be set while this extension is active). These are the 3 files the extension is made of.
manifest.json
{
"manifest_version": 2,
"name": "Test",
"description": "Test extension",
"version": "1.0",
"permissions": [
"<all_urls>",
"tabs",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
}
}
background.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
chrome.tabs.executeScript(null, {
file: "cookiescript.js"
});
}, {
urls: [
"<all_urls>"
]
}, ["blocking"]);
cookiescript.js
var old_cookie = document.cookie;
Object.defineProperty(document, 'cookie', {
get: function() {
console.log('Getting cookie...');
return '';
},
set: function(val) {
console.log('Setting cookie...');
}
});
document.cookie = old_cookie;
I usually test this on en.wikipedia.org as it sets various cookies the moment you visit it. However, with the above extension, the console log becomes filled with Getting cookie... alternated with Uncaught TypeError: Cannot redefine property: cookie, and when I check cookies stored, I can see that Wikipedia created all the cookies it wanted regardless of my extension.
The error seems to speak clear: you can't override cookie setter and getter, but the fact that other questions (#3 in particular) were answered saying to do just that, I am not sure.
Alternatively, I tried using this version of cookiescript.js, which adopts the (deprecated) defineSetter and defineGetter
var old_cookie = document.cookie;
document.__defineSetter__('cookie', function() {
console.log('Setting cookie...');
});
document.__defineGetter__('cookie', function() {
console.log('Getting cookie...');
return ('');
});
document.cookie = old_cookie;
Console log results in an alternation of Setting cookie... and Getting cookie..., but again, looking at cookies saved, it looks like Wikipedia successfully set all of its cookies.
Would anyone be able to point out what I'd have to do to successfully be in-between cookie setting/getting so that I could stop it, or explain if what I'm trying to do is just impossible?
My wild guess would be that the overriding of getter/setter actually happens, but only after the website set/got its cookies, but I'd have no idea how to make sure my overriding happens first instead (I figured the use of blocking onBeforeRequest would've achieved that, but I seem to be wrong), or if it's simply impossible to do so.
EDIT: I previously mentioned how a different extension to list cookies would not be able to retrieve them, so I thought I successfully overrode the getter. I was mistaken, this extension was broken, and when fixed it was displaying all the cookies.