Blocking cookie setting/retrieving by modifying headers

可紊 提交于 2019-12-24 15:33:32

问题


I'm trying to make a Google Chrome extension that analyzes cookies before they're set/retrieved by websites and, if they don't meet certain requirements, stops the operation.

To start, I'm trying to just block cookie setting entirely. After trying (and failing) to override their getter and setter, I was recommended to try and modify headers and their Set-Cookie elements instead. If anyone is interested in the previous question, it's here.

The extension I came up with only has the following two files:

manifest.json

{
  "manifest_version": 2,

  "name": "CookieStop",
  "description": "Extension to filter cookies",
  "version": "1.0",

  "permissions": [
    "<all_urls>",
    "tabs",
    "webRequest",
    "webRequestBlocking"
  ],

  "background": {
    "scripts": ["background.js"]
  }
}

background.js

chrome.webRequest.onHeadersReceived.addListener(
  function(details) {
    for (var i = 0; i < details.responseHeaders.length; i++) {
      if (details.responseHeaders[i].name === 'Set-Cookie') {
        details.responseHeaders.splice(i, 1);
        i--;
      }
    }
    return {
      responseHeaders: details.responseHeaders
    };
  }, {
    urls: ["<all_urls>"]
  }, ["blocking", "responseHeaders"]);

I made a version of background.js that logs in the console every header before and after the splicing and it seems to correctly remove every Set-Cookie part.

Testing the result by visiting en.wikipedia.org, which sets some cookies as soon as you visit it, I observe the following behavior. Without the extension, cookies are set for upload.wikimedia.org, wikipedia.org, en.wikipedia.org. With the extension, no cookies are set for wikipedia.org, but there are still cookies set for the other two.

I know only one extension is allowed to modify a request, I've also tested this with only my extension enabled, but the result is the same.

"<all_urls>" should stop cookies from any domain, so can anyone tell what exactly is wrong in my extension which is letting cookies being set and how to fix it?

来源:https://stackoverflow.com/questions/35395105/blocking-cookie-setting-retrieving-by-modifying-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!