Skip to content

Commit

Permalink
check if clientHints is undefined on parser>>device>device-trusted.js (
Browse files Browse the repository at this point in the history
  • Loading branch information
gilsongabriel authored Oct 7, 2024
1 parent 0d0b41f commit 9697caf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 80 additions & 35 deletions parser/device/device-trusted.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const helper = require('../helper');
const {attr} = helper;
const { attr } = helper;

/**
* check screen size
Expand All @@ -9,21 +9,43 @@ const {attr} = helper;
* @param {string} metaHeight
* @return {boolean}
*/
const isDeviceSizeValid = (deviceWidth, deviceHeight, metaWidth, metaHeight) => {

const isDeviceSizeValid = (
deviceWidth,
deviceHeight,
metaWidth,
metaHeight
) => {
if (deviceWidth && deviceHeight && metaWidth && metaHeight) {
const minMetaWidth = parseInt(metaWidth) - 1;
const maxMetaWidth = parseInt(metaWidth) + 1;
const minMetaHeight = parseInt(metaHeight) - 1;
const maxMetaHeight = parseInt(metaHeight) + 1;

const isWidthValid = helper.fuzzyBetweenNumber(parseInt(deviceWidth), minMetaWidth, maxMetaWidth)
&& helper.fuzzyBetweenNumber(parseInt(deviceHeight), minMetaHeight, maxMetaHeight);

const isHeightValid = helper.fuzzyBetweenNumber(parseInt(deviceWidth), minMetaHeight, maxMetaHeight)
&& helper.fuzzyBetweenNumber(parseInt(deviceHeight), minMetaWidth, maxMetaWidth);

return (isWidthValid || isHeightValid);
const isWidthValid =
helper.fuzzyBetweenNumber(
parseInt(deviceWidth),
minMetaWidth,
maxMetaWidth
) &&
helper.fuzzyBetweenNumber(
parseInt(deviceHeight),
minMetaHeight,
maxMetaHeight
);

const isHeightValid =
helper.fuzzyBetweenNumber(
parseInt(deviceWidth),
minMetaHeight,
maxMetaHeight
) &&
helper.fuzzyBetweenNumber(
parseInt(deviceHeight),
minMetaWidth,
maxMetaWidth
);

return isWidthValid || isHeightValid;
}

return true;
Expand All @@ -50,7 +72,7 @@ const extractGpuName = (metaGPU) => {
const gpuMap = [
{ regex: /Adreno \(TM\) ([^$,]+)/i, name: 'Qualcomm Adreno $1' },
{ regex: /Mali-([^$,]+)/i, name: 'ARM Mali-$1' },
{ regex: /PowerVR Rogue ([^$,]+)/i, name: 'PowerVR $1' }
{ regex: /PowerVR Rogue ([^$,]+)/i, name: 'PowerVR $1' },
];

for (let i = 0, l = gpuMap.length; i < l; i++) {
Expand All @@ -63,7 +85,6 @@ const extractGpuName = (metaGPU) => {
return metaGPU;
};


/**
* @param {ResultDevice} deviceData
* @param {Object} clientHints
Expand All @@ -75,17 +96,26 @@ const checkDisplaySize = (deviceData, clientHints) => {
let deviceInfo = deviceData.info;

if (deviceInfo) {
if (deviceInfo.display && typeof deviceInfo.display.resolution === 'string') {
if (
deviceInfo.display &&
typeof deviceInfo.display.resolution === 'string'
) {
let resolution = deviceInfo.display.resolution.split('x');
deviceWidth = resolution[0];
deviceHeight = resolution[1];
} else {
deviceWidth = deviceInfo.display && deviceInfo.display.resolution && deviceInfo.display.resolution.width
? deviceInfo.display.resolution.width
: null;
deviceHeight = deviceInfo.display && deviceInfo.display.resolution && deviceInfo.display.resolution.height
? deviceInfo.display.resolution.height
: null;
deviceWidth =
deviceInfo.display &&
deviceInfo.display.resolution &&
deviceInfo.display.resolution.width
? deviceInfo.display.resolution.width
: null;
deviceHeight =
deviceInfo.display &&
deviceInfo.display.resolution &&
deviceInfo.display.resolution.height
? deviceInfo.display.resolution.height
: null;
}

let metaWidth = attr(clientHints.meta, 'width', null);
Expand All @@ -99,12 +129,16 @@ const checkDisplaySize = (deviceData, clientHints) => {
};

const isGpuExistForClientHints = (clientHints) => {
return clientHints.meta && clientHints.meta.gpu && clientHints.meta.gpu.length > 0;
}
return (
clientHints.meta && clientHints.meta.gpu && clientHints.meta.gpu.length > 0
);
};

const isAppleBrandForDeviceData = (deviceData) => {
return deviceData.brand === 'Apple'
|| (deviceData.brand === '' && /ip(?:hone|[ao]d)/i.test(deviceData.code))
return (
deviceData.brand === 'Apple' ||
(deviceData.brand === '' && /ip(?:hone|[ao]d)/i.test(deviceData.code))
);
};

/**
Expand All @@ -114,9 +148,10 @@ const isAppleBrandForDeviceData = (deviceData) => {
*/
const checkGpu = (deviceData, clientHints) => {
let deviceInfo = deviceData.info;
let deviceGPU = deviceInfo && deviceInfo.hardware && deviceInfo.hardware.gpu
? deviceInfo.hardware.gpu.name
: null;
let deviceGPU =
deviceInfo && deviceInfo.hardware && deviceInfo.hardware.gpu
? deviceInfo.hardware.gpu.name
: null;

let metaGPU = attr(clientHints.meta, 'gpu', null);

Expand All @@ -125,15 +160,13 @@ const checkGpu = (deviceData, clientHints) => {
}

if (metaGPU !== null && deviceGPU !== null) {
return isDeviceGpuValid(deviceGPU, metaGPU)
return isDeviceGpuValid(deviceGPU, metaGPU);
}

return true;
};


class DeviceTrusted {

/**
* @param {ResultOs} osData
* @param {ResultClient} clientData
Expand All @@ -142,19 +175,31 @@ class DeviceTrusted {
* @return {boolean|null}
*/
static check(osData, clientData, deviceData, clientHints) {

const regexAppleGpu = /GPU Apple/i;
const isGpuExist = isGpuExistForClientHints(clientHints);
const isAppleBrand = isAppleBrandForDeviceData(deviceData);

// is Apple and lack of client-hints
if (isAppleBrand && clientHints.client.brands.length > 0) {
if (
isAppleBrand &&
clientHints &&
clientHints.client &&
clientHints.client.brands &&
clientHints.client.brands.length > 0
) {
return false;
}
// is Apple and check correct gpu name
if (isAppleBrand && isGpuExist && !regexAppleGpu.test(clientHints.meta.gpu)) {
if (
isAppleBrand &&
isGpuExist &&
!regexAppleGpu.test(clientHints.meta.gpu)
) {
return false;
} else if (isAppleBrand && isGpuExist && regexAppleGpu.test(clientHints.meta.gpu)) {
} else if (
isAppleBrand &&
isGpuExist &&
regexAppleGpu.test(clientHints.meta.gpu)
) {
return true;
}

Expand All @@ -175,4 +220,4 @@ class DeviceTrusted {
}
}

module.exports = DeviceTrusted;
module.exports = DeviceTrusted;
8 changes: 8 additions & 0 deletions tests/fixtures/oss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5198,6 +5198,14 @@
version: 10.15.7
platform: ""
family: Mac
-
user_agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
os:
name: Mac
short_name: MAC
version: 10.15.7
platform: ""
family: Mac
-
user_agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) (Ecosia [email protected])
os:
Expand Down

0 comments on commit 9697caf

Please sign in to comment.