Make the tracking snippet cache safe#8
Open
loevgaard wants to merge 2 commits into
Open
Conversation
The injected snippet was built server side at render time with per-visitor data baked into the HTML (client id, referrer, matched source), and the injection decision itself depended on per-visitor state (bot check, session cookie). Under a full page cache, whichever visitor populated the cache froze their identity — or the absence of the snippet — into the page for everyone. The snippet is now byte-identical for every visitor: only the track route URL and the session timeout are interpolated. Everything per-visitor moved to where per-visitor state is actually available: - The session throttle runs in the browser against a rolling localStorage timestamp; a query string or cross-host referrer bypasses it. - The client id is resolved in TrackAction from the setono_client_id cookie riding the same-origin fetch() POST. - Source matching runs in TrackAction against a synthetic request built from the posted page and referrer, reusing ClientInformationResolver unchanged. - Bot filtering happens solely in TrackAction. Posted payload values always win over resolved ones, so pages cached with the pre-1.2 snippet (which POST full payloads) keep behaving exactly as before until they expire. AddJavascriptSubscriber keeps its now-unused constructor arguments for signature compatibility; TrackAction gains a nullable-last resolver argument with the usual deprecation shim. Fixes #7
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #8 +/- ##
============================================
+ Coverage 63.98% 68.99% +5.01%
- Complexity 137 142 +5
============================================
Files 23 23
Lines 472 487 +15
============================================
+ Hits 302 336 +34
+ Misses 170 151 -19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Automated browsers (Selenium, Puppeteer, Playwright, headless Chrome) execute JavaScript, so they pass the "bots don't run JS" filter, and many use a real browser User-Agent that the server side bot detection on /track cannot flag. Guard on the standardized navigator.webdriver flag instead — real browsers leave it false or undefined. This also keeps host apps' own end-to-end test runs out of the attribution data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7
The problem
The injected tracking snippet was built server side at render time, with per-visitor data baked into the HTML: the client id, referrer and matched source were embedded as a literal JSON payload, and the injection decision itself depended on per-visitor state (bot check, session-timeout cookie check). Under a full page cache (Varnish, CDN, HttpCache), whichever visitor populated the cache froze their identity into the shared page — every visitor served from that cache entry tracked as the cached visitor. Worse, a mid-session or bot visitor populating the cache stripped the snippet for everyone after them.
The fix
The snippet is now byte-identical for every visitor — only the track route URL and the
session_timeoutvalue are interpolated, both cache-safe constants. Everything per-visitor moved to where per-visitor state actually exists:localStorageand skips the POST withinsession_timeout, unless the URL has a query string or the referrer is cross-host (campaign hints — the server-side matcher decides definitively). Storage failures fail open to tracking.{page, referrer}. The same-originfetch()carries thesetono_client_idcookie (deliberatelyHttpOnly=false), andTrackActionresolves the id via the existingClientContextchain. First-ever visits work too: the id is minted server side and the cookie is set on the POST response.TrackActionbuilds a syntheticRequestfrom the posted page/referrer (seeded with the real IP/UA) and reusesClientInformationResolverunchanged — matcher chain, default source and client context all work as-is.Backwards compatibility
clientIdandsource) skip resolution entirely — byte-for-byte legacy behaviour. The README recommends purging the page cache after upgrading.AddJavascriptSubscriber's constructor signature is unchanged; the four now-unused arguments are kept and marked@deprecated unused, to be removed in 2.0. Its previous$sourceMatcherdeprecation (unreleased) was dropped since the argument is no longer used.TrackActiongains a nullable-lastClientInformationResolverInterfaceargument with the documentedtrigger_deprecation()shim (version1.2; the stale unreleased1.1strings from Fix security, correctness, and robustness issues found in audit #6 were bumped too), plus a?Requestparameter appended to__invokethat the framework injects.Tests
assertSame-identical snippets, with all per-visitor services mocked as never-consulted at render time.TrackAction: minimal-payload resolution (asserting the synthetic request carries the posted page/query/referrer and the real IP/UA), legacy-payload passthrough, per-field overlay, malformed-URL tolerance (version-adaptive for lowest deps), bot ordering, and both deprecation shims.ClientInformationResolvergot its first direct test file (it is now load-bearing at POST time).Verification
lint:container/lint:yaml/lint:twigpasscomposer normalizeclean; dependency-analyser output identical to master