Skip to content

Commit

Permalink
Look for headless browser in clientHint, #PG-3304
Browse files Browse the repository at this point in the history
  • Loading branch information
AltamashShaikh committed Oct 23, 2024
1 parent 5e7915c commit 7f260f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions TrackingSpamPrevention.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ public function isExcludedVisit(&$excluded, Request $request)
$browserLang = $request->getBrowserLanguage();

$browserDetection = new BrowserDetection();
$clientHints = json_encode($request->getClientHints());
if (
$settings->blockHeadless->getValue()
&& $browserDetection->isHeadlessBrowser($request->getUserAgent())
$settings->blockHeadless->getValue() &&
(
$browserDetection->isHeadlessBrowser($request->getUserAgent()) ||
$browserDetection->isHeadlessBrowser($clientHints)
)
) {
// note above user agent could have been overwritten with UA parameter but that's fine since it's easy to change useragent anyway
Common::printDebug("Excluding visit as headless browser detected");
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/TrackingSpamPreventionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public function test_isExcludedVisit_whenBlockedUserAgent()
$this->assertTrue($isExcluded);
}

public function test_isExcludedVisit_whenHeadlessClientHint()
{
StaticContainer::get(SystemSettings::class)->blockHeadless->setValue(1);
Cache::clearCacheGeneral();

// set normal browser not a headless browser
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.0 Safari/537.36';
$excluded = $this->makeExcluded('22.22.22.22');
$excluded->request->setParam('uadata', '{"fullVersionList":[{"brand":"Not A(Brand","version":"99.0.0.0"},{"brand":"HeadlessChrome","version":"121.0.6167.57"},{"brand":"Chromium","version":"121.0.6167.57"}],"mobile":false,"model":"","platform":"Linux","platformVersion":"5.15.0"}');
$isExcluded = $excluded->isExcluded();
unset($_SERVER['HTTP_USER_AGENT']);
$this->assertTrue($isExcluded);
}

public function test_isExcludedVisit_whenBlockedUserAgentDisabled()
{
StaticContainer::get(SystemSettings::class)->blockHeadless->setValue(0);
Expand Down

0 comments on commit 7f260f6

Please sign in to comment.