I went looking for the cookies on a WooCommerce site so I could write an accurate privacy policy. I found seven I had not put there, on a site that had never taken an order.
They come from WooCommerce core's Order Attribution feature, which is on by default and loads sourcebuster.js:
sbjs_migrations sbjs_current_add sbjs_first_add sbjs_current
sbjs_first sbjs_udata sbjs_sessionCode language: plaintext (plaintext)
Between them they record landing page, referrer, search engine and search term, any utm_* tags, user agent, device class and visit timestamps, and they are kept for six months by default. They were set on every page I visited — home, shop, a product, the privacy policy, the contact page — before any consent had been asked for, because there was no consent mechanism at all.
That is the whole audit. It is not a subtle finding. The interesting part is what happened when I tried to fix it.
Audit at the right layer
The first thing to get right is how you look. document.cookie cannot see an HttpOnly cookie — that is what HttpOnly means — and a scanner built on injected JavaScript therefore undercounts by exactly the cookies most worth knowing about. Every free scanner I surveyed does it that way, because it is the cheap way to build one.
Playwright's BrowserContext.cookies() reads the browser's own jar, populated by Chromium's network stack from every Set-Cookie header it received. It sees HttpOnly cookies like any other:
import { chromium } from 'playwright';
const browser = await chromium.launch();
const context = await browser.newContext( { ignoreHTTPSErrors: true } );
const page = await context.newPage();
for ( const path of paths ) {
await page.goto( new URL( path, base ).toString(), { waitUntil: 'networkidle' } );
}
// The browser's jar, not document.cookie.
const cookies = await context.cookies();Code language: JavaScript (javascript)
One discipline worth adopting: record that a cookie exists and how it is flagged (httpOnly, secure, sameSite, expires), and never its value. A cookie audit tool has no business keeping a copy of a session token in its own database.
The consent banner that gated nothing
The obvious next step was a consent banner, so we shipped one — Klaro, configured with a service per purpose, each service listing the cookies it is responsible for.
It gated nothing.
Klaro's services[].cookies is a clean-up list, swept after a script has already run. It is not a gate on the script. Klaro only prevents execution for a <script> tag it has been given in its own blocking form:
<script type="text/plain" data-name="analytics" src="..."></script>Code language: HTML, XML (xml)
Nothing on the site emitted that form, so the banner faithfully recorded a choice and then let every tracking request fire regardless of it. Worse than no banner, arguably: it told visitors something untrue.
This is the failure mode I would look for first in anyone else's consent implementation. The cookie disappearing from the jar after you decline is not evidence the script was blocked. It is evidence something deleted a cookie.
Gate at script_loader_tag
The right hook is script_loader_tag, and the reason is in core. WP_Scripts::do_item() assembles the whole thing — translations, the "before" inline script, the tag itself and the "after" inline script — into one string, and only then filters it:
$tag = $translations . $before_script;
$tag .= wp_get_script_tag( $attr );
$tag .= $after_script;
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );Code language: PHP (php)
That matters here more than it usually does, because the sbjs_* cookies are not set by a bare <script src>. They are set by wc-order-attribution's inline call into sourcebuster. Filtering at script_loader_tag means one rewrite covers a handle's src and its inline configuration together.
The gate itself is a map from handle to purpose, and a rewrite:
private const GATED_HANDLES = array(
'sourcebuster-js' => 'analytics',
'wc-order-attribution' => 'analytics',
'google-tag-manager' => 'analytics',
'wc-blocks-google-analytics' => 'analytics',
);
public static function gate( string $tag, string $handle, string $src ): string {
unset( $src );
if ( ! isset( self::GATED_HANDLES[ $handle ] ) ) {
return $tag;
}
$purpose = self::GATED_HANDLES[ $handle ];
$rewritten = preg_replace_callback(
'/<scriptb([^>]*)>/i',
static function ( array $matches ) use ( $purpose ): string {
// HTML allows exactly one `type` attribute -- drop whatever
// is there (typically none; WordPress only prints `type` for
// a non-default module/importmap script) before adding
// Klaro's own, so the two never collide.
$attrs = (string) preg_replace( '/s+type=(["']).*?1/i', '', $matches[1] );
return '<script type="text/plain" data-name="' . esc_attr( $purpose ) . '"' . $attrs . '>';
},
$tag
);
return null !== $rewritten ? $rewritten : $tag;
}Code language: PHP (php)
Three details in there are deliberate:
preg_replace_callback, notstr_replace. The filtered string can contain several<script>tags; all of them need rewriting or the inline one runs anyway.- Strip any existing
typefirst. HTML allows exactly one, and WordPress does print one for module and importmap scripts. Adding a second produces a tag whose behaviour depends on which the parser takes. - Fall back to the original
$tagonnull. Apreg_replace_callbackfailure must not blank a script tag out of the page.
The two Google handles are dormant on this install — the extension is not present — and are gated anyway, so activating it later cannot silently reopen the hole.
The gap I did not close, and said so
google-listings-and-ads loads gtag.js from a raw <script> echoed directly at wp_head, never through wp_enqueue_script(). script_loader_tag never sees it. Gating it needs output buffering around wp_head, which is a materially different and untested mechanism.
I left it as a named gap in the class docblock rather than claim the feature was complete. That is worth doing explicitly: a consent implementation that is 95% complete and honest about the 5% is a usable thing; one that is 95% complete and describes itself as finished is the same defect as the banner that gated nothing.
The discovery deadlock
Gating created a problem that took a while to see. The scanner exists to discover cookies. Klaro is configured default: false, so nothing runs until it is granted. And a purpose only appears in the consent modal if the scanner has already matched a cookie under it.
So on a fresh install: the gated script never runs, so its cookie is never set, so the scanner never sees it, so no service is registered for that purpose, so the modal never offers it, so the script can never be granted. A closed loop with no way in — and it looks like everything is working, because the site is quiet and the banner is up.
Two things break it. The gate publishes the purposes it enforces, so a purpose always exists as a Klaro service even before any cookie has been catalogued under it:
public static function gated_purposes(): array {
return array_values( array_unique( self::GATED_HANDLES ) );
}Code language: PHP (php)
And the scanner drives Klaro's own public API to accept everything before it starts, the same way a visitor who accepted would:
await page.evaluate( () => {
window.klaro.getManager().changeAll( true );
window.klaro.getManager().saveAndApplyConsents();
} );Code language: JavaScript (javascript)
A missing window.klaro is logged and the scan continues without consent, so a broken consent step degrades to the old behaviour rather than failing the scan outright.
Proving it — and why the cookie jar is not enough
Here is the part I would push back on if someone showed me this work.
The natural test is: decline, diff the jar, assert zero sbjs_*. It passes. It also passes with the gate removed entirely — because Klaro's own cookie sweep deletes sbjs_* on decline whether or not the script ever ran. The cookie jar cannot distinguish "never executed" from "executed, then tidied up after", and that distinction is the entire point. Under PECR the question is whether the script ran, not whether a cookie survived.
So assert on script execution state instead:
// Declined: the script must never have executed.
await expect( page.evaluate( () => typeof window.sbjs ) ).resolves.toBe( 'undefined' );
// Accepted (via the real banner button, not by writing the consent cookie):
await page.getByRole( 'button', { name: "That's ok" } ).click();
await expect( page.evaluate( () => typeof window.sbjs ) ).resolves.toBe( 'object' );Code language: JavaScript (javascript)
Measured that way: declined, window.sbjs undefined and 0 of 7 cookies; accepted, window.sbjs an object and 7 of 7, matching the original audit exactly; gate disabled, the script executes on first load with no consent asked. Three runs, and only the third combination proves the second one was the gate's doing.
Watching network requests for the sourcebuster src is a good additional check where the script has one. It would not have caught this case on its own, because the cookies come from the inline call rather than a separate request — which is the same reason the filter had to be at script_loader_tag.
One last trap, and it cost an hour: the first verification run was a false negative because a full-page cache was serving a render from before the plugin was activated. Flush the page cache before you measure anything about <head>.
What to take from it
- Audit with the browser's cookie jar, not
document.cookie. - A consent tool's cookie list is usually cleanup, not blocking. Check which yours is.
- Gate at
script_loader_tagso inline configuration is covered along with thesrc. - Assert on whether the script ran, not on whether a cookie survived.
- Name the thing you did not gate, in the code, where the next person will read it.
This is the kind of work I mean by technical audit — measured, reproducible, and honest about the edges. The stack is on my CV, and if you have a WooCommerce store whose cookie banner you have never actually tested, that is worth a conversation.