Skip to content

Commit

Permalink
Detect not real user agent for apple brand (matomo-org#7580)
Browse files Browse the repository at this point in the history
* feat: added check your Apple device for correct operating system
* feat: added test UA Apple ClientHints Windows result unknown device.
  • Loading branch information
sanchezzzhak authored Feb 13, 2024
1 parent 267bdc2 commit f6727c0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
15 changes: 8 additions & 7 deletions DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,16 @@ protected function parseDevice(): void
$this->brand = $vendorParser->parse()['brand'] ?? '';
}

$osName = $this->getOsAttribute('name');
$osFamily = $this->getOsAttribute('family');
$osVersion = $this->getOsAttribute('version');
$clientName = $this->getClientAttribute('name');
$osName = $this->getOsAttribute('name');
$osFamily = $this->getOsAttribute('family');
$osVersion = $this->getOsAttribute('version');
$clientName = $this->getClientAttribute('name');
$appleOsNames = ['iPadOS', 'tvOS', 'watchOS', 'iOS', 'Mac'];

/**
* if it's fake UA then it's best not to identify it as Apple running Android OS
* if it's fake UA then it's best not to identify it as Apple running Android OS or GNU/Linux
*/
if ('Android' === $osName && 'Apple' === $this->brand) {
if ('Apple' === $this->brand && !\in_array($osName, $appleOsNames)) {
$this->device = null;
$this->brand = '';
$this->model = '';
Expand All @@ -927,7 +928,7 @@ protected function parseDevice(): void
/**
* Assume all devices running iOS / Mac OS are from Apple
*/
if (empty($this->brand) && \in_array($osName, ['iPadOS', 'tvOS', 'watchOS', 'iOS', 'Mac'])) {
if (empty($this->brand) && \in_array($osName, $appleOsNames)) {
$this->brand = 'Apple';
}

Expand Down
64 changes: 64 additions & 0 deletions Tests/fixtures/unknown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3372,3 +3372,67 @@
model: ""
os_family: Android
browser_family: Chrome
-
user_agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21B101 [FBAN/FBIOS;FBAV/445.0.0.35.117;FBBV/548375166;FBDV/iPhone12,1;FBMD/iPhone;FBSN/iOS;FBSV/17.1.2;FBSS/2;FBID/phone;FBLC/es_LA;FBOP/5;FBRV/550068703]'
headers:
brands:
- brand: Not_A Brand
version: '8'
- brand: Chromium
version: '120'
fullVersionList:
- brand: Not_A Brand
version: 8.0.0.0
- brand: Chromium
version: 120.0.6099.71
- brand: Google Chrome
version: 120.0.6099.71
mobile: '0'
platform: Linux
platformVersion: 5.19.0
uaFullVersion: 120.0.6099.71
wow64: '0'
architecture: 'x86'
os:
name: 'GNU/Linux'
version: '5.19.0'
platform: 'x86'
client:
name: 'Facebook'
type: 'mobile app'
version: '445.0.0.35.117'
device:
type: desktop
brand:
model:
os_family: GNU/Linux
browser_family: Unknown
-
user_agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/79.0.3945.73 Mobile/15E148 Safari/604.1
headers:
brands:
- brand: Not_A Brand
version: '99'
- brand: Chromium
version: '92'
mobile: '0'
platform: Windows
platformVersion: "10.0"
uaFullVersion: 92.0.4515.131
architecture: 'x86'
os:
name: Windows
version: "10"
platform: x86
client:
type: browser
name: Chrome Mobile iOS
version: 79.0.3945.73
engine: WebKit
engine_version: 605.1.15
device:
type: desktop
brand: ""
model: ""
os_family: Windows
browser_family: Chrome

0 comments on commit f6727c0

Please sign in to comment.