Search code examples
firefoxcookiesfirefox-addonfirefox-addon-sdkcross-site

How do I delete cross site cookies with Firefox 22+ extension?


I am attempting to add a function to my Firefox extension to trigger an event to delete cookies from site B when a button on site A is clicked. Site A and B do not share a domain but site B is running in an iframe injected into site A. I need the click event in the Firefox content script to trigger an event either in the content script or the Firefox extension main to delete all of the cookies from site B.

I have the click listener assigned to the button and firing. I have already achieved this same effect in Google Chrome with an extension. I get an error about using components, but I could not find a solution to use instead of components. It only needs to work on Firefox 22+. I am using addon-sdk-1.14 to develop the extension.

ContentScript.js

function DeleteCookies() {
    var payload="Delete";
    self.port.emit("Delete", payload);
}

Main.js

var {Cc, Ci} = require("chrome");
pageMod.PageMod({
    include: "*",
    contentScriptFile: [ self.data.url("jquery-1.9.1.js")
                        ,self.data.url("script.js")],
    onAttach: function(worker) {
                  worker.port.on('Delete',function (){ DeleteCookies();});
              }
});

function DeleteCookies() {
    var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    var domain= "siteB.com";
    var iter = cookieManager.enumerator;
    var cookie_count = 0;
    while (iter.hasMoreElements()) {
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            if (domain.indexOf(cookie.host.toUpperCase()) != -1) {
                cookieManager.remove(cookie.host, cookie.name, cookie.path, cookie.blocked);
                cookie_count++;
            }
        }
    }
};

Solution

  • You can't access XPCOM from a content script. Use the port mechanism for communication between the content script and main.js, and do the cookie deletion from the latter.