Rebuilding Cookie AutoDelete for Manifest V3: Ephemeral Service Workers, Cookie Jars, and CHIPS
When Google completed the Manifest V2 shutdown in Chrome, one of the hardest-hit privacy tools was Cookie AutoDelete (200,000+ Chrome users before removal; 160,000+ Firefox users still running a 2022 build). Classic MV2
When Google completed the Manifest V2 shutdown in Chrome, one of the hardest-hit privacy tools was Cookie AutoDelete (200,000+ Chrome users before removal; 160,000+ Firefox users still running a 2022 build).
Classic MV2 cookie cleaners relied on a persistent background page holding Redux state in memory, open-tab maps, and long-lived setTimeout queues. None of that survives a Manifest V3 Service Worker that Chrome can terminate 30 seconds after the last event.
Here are the five concrete engineering problems we had to solve while building Lark Cookie Sweep β an MIT-licensed, zero-dependency MV3 replacement for Chrome, Edge, and Firefox (source on GitHub).
1. Surviving Service Worker Suspension Without Losing Delayed Sweeps
Users expect a configurable grace period after closing a tab (say 15 seconds) so accidental tab closes followed by Ctrl+Shift+T don't wipe their session.
In Manifest V3, you face two conflicting constraints:
-
setTimeout(..., 15000)is precise, but disappears if Chrome terminates the Service Worker early. -
chrome.alarms.create()survives Service Worker restarts, but Chrome enforces a minimum ~30-second floor for unpacked/packed alarms.
The clean solution is a hybrid schedule where setTimeout fires fast when the worker stays warm and chrome.alarms acts as a durable safety net:
async function scheduleSweep(trigger) {
const s = await getSettings();
if (!s.autoClean) return;
const delayMs = s.delaySec * 1000;
clearTimeout(timer);
timer = setTimeout(() => {
chrome.alarms.clear("sweep");
sweep("tab", { trigger });
}, delayMs);
// Backup in case the service worker suspends before setTimeout fires
chrome.alarms.create("sweep", {
when: Date.now() + Math.max(delayMs, 30000) + 1000,
});
}
And because we never trust in-memory tab caches across worker restarts, every sweep queries live ground truth directly from chrome.tabs.query({}) and chrome.cookies.getAll(...) inside a serialized running lock.
2. Don't Mix Cookie-Jar Isolation With Rule Scoping
A subtle bug shows up quickly when supporting both incognito windows and Firefox Multi-Account Containers:
-
Open-tab protection must isolate each raw cookie jar (
"0"normal vs"1"incognito vs"firefox-container-2"). Havingexample.comopen in a normal tab must not protectexample.comcookies sitting in a closed incognito store. -
Keep-list rules, however, should share
"default"between normal and incognito stores (matching Cookie AutoDelete behavior), while keeping Firefox Containers scoped to their ownfirefox-container-*IDs.
Keeping both concepts separate inside a pure planner module (src/lib/planner.js) makes the behavior trivial to unit test in Node.js without mocking Chrome:
// Raw cookie store for open-tab protection
const jar = (id) => (id ? String(id) : "0");
// Normalized rule scope: default & private share "default"; containers stay separate
export function storeKey(storeId) {
if (!storeId || ["0", "1", "firefox-default", "firefox-private"].includes(storeId)) {
return "default";
}
return String(storeId);
}
3. Handling Partitioned (CHIPS) Cookies
Modern browsers increasingly store third-party cookies with a partitionKey ({ topLevelSite: "https://host.example" }). Two pitfalls bite naive MV3 cleaners:
- Calling
chrome.cookies.getAll({})withoutpartitionKey: {}can miss partitioned cookies depending on browser version. Querying{ ...q, partitionKey: {} }with a fallback catches both partitioned and unpartitioned entries. - When deleting a partitioned cookie,
chrome.cookies.remove()requires forwarding the exactpartitionKeyobject β and whiletopLevelSiteis still open in a tab, that partitioned cookie must be protected just like first-party cookies of the open tab.
const topSite = c.partitionKey?.topLevelSite
? baseDomain(hostFromUrl(c.partitionKey.topLevelSite))
: "";
if (mode === "tab" && keepOpenTabs && (isOpen(j, base) || (topSite && isOpen(j, topSite)))) {
kept++;
continue;
}
4. One-Click Compatibility With Legacy Expressions.json Backups
Nobody wants to manually re-type 80 whitelisted domains when switching extensions. Cookie AutoDelete exports a straightforward map keyed by storeId:
{
"default": [
{ "expression": "*.github.com", "listType": "WHITE", "storeId": "default" },
{ "expression": "youtube.com", "listType": "GREY", "cookieNames": ["PREF"] }
]
}
Three details keep imported lists behaving identically:
- Strip leading
www.(andwww2.) on both rule and hostname before matching. -
*.example.commatches bothexample.comand every subdomain ((^|\.)example\.com$), whileexample.commatches only the exact host. - On conflict, exact rules beat wildcards/regex, and
WHITEbeatsGREYso we never accidentally delete cookies the user explicitly asked to keep.
And when rendering imported expressions in options.js, avoiding innerHTML completely (using a 10-line document.createElement helper instead) both eliminates XSS from malformed imports and gets web-ext lint down to 0 errors, 0 warnings.
5. Testing Against Real Chrome & Firefox Before Shipping
Unit tests cover the pure planner and parser (24/24 tests in node --test), while real-browser E2E scripts verify actual cookie deletion end-to-end:
-
Chrome E2E: launches Chrome via Puppeteer (
enableExtensions: true+browser.installExtension()), spins up a local HTTP server setting cookies on127.0.0.1andlocalhost, verifies open-tab protection, tab-close auto-cleanup, greylist survival + browser-start sweep,Expressions.jsonfile upload in Options, and toolbar badge updates (15/15checks). -
Firefox E2E: launches Firefox via WebDriver BiDi, installs the signed-ready
dist/firefoxpackage temporarily, and asserts directly on the HTTPCookieheader received by the local server (7/7checks).
If you're looking for a clean, local-only MV3 replacement for Cookie AutoDelete β or want to reuse the MV3 Service Worker + E2E setup for your own extension β the full MIT source and v1.0.0 ZIPs are live at github.com/larkandlever/cookie-sweep and larkandlever.github.io/cookie-sweep.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.