From bf3b97f4f3385093408dc48b44c04e8ac9bfc857 Mon Sep 17 00:00:00 2001 From: hasan-deriv Date: Wed, 17 Apr 2024 15:52:25 +0800 Subject: [PATCH] fix: updated regex to Set --- packages/shared/src/utils/os/os_detect.ts | 132 +++++++++++++++++++++- 1 file changed, 127 insertions(+), 5 deletions(-) diff --git a/packages/shared/src/utils/os/os_detect.ts b/packages/shared/src/utils/os/os_detect.ts index 58b636d21dfa..beee77945159 100644 --- a/packages/shared/src/utils/os/os_detect.ts +++ b/packages/shared/src/utils/os/os_detect.ts @@ -106,11 +106,133 @@ export const mobileOSDetect = () => { return 'unknown'; }; +// Simple regular expression to match potential Huawei device codes +const huaweiDevicesRegex = /\b([A-Z]{3}-)\b/gi; + +// Set of valid Huawei device codes +const validCodes = new Set([ + 'ALP-', + 'AMN-', + 'ANA-', + 'ANE-', + 'ANG-', + 'AQM-', + 'ARS-', + 'ART-', + 'ATU-', + 'BAC-', + 'BLA-', + 'BRQ-', + 'CAG-', + 'CAM-', + 'CAN-', + 'CAZ-', + 'CDL-', + 'CDY-', + 'CLT-', + 'CRO-', + 'CUN-', + 'DIG-', + 'DRA-', + 'DUA-', + 'DUB-', + 'DVC-', + 'ELE-', + 'ELS-', + 'EML-', + 'EVA-', + 'EVR-', + 'FIG-', + 'FLA-', + 'FRL-', + 'GLK-', + 'HMA-', + 'HW-', + 'HWI-', + 'INE-', + 'JAT-', + 'JEF-', + 'JER-', + 'JKM-', + 'JNY-', + 'JSC-', + 'LDN-', + 'LIO-', + 'LON-', + 'LUA-', + 'LYA-', + 'LYO-', + 'MAR-', + 'MED-', + 'MHA-', + 'MLA-', + 'MRD-', + 'MYA-', + 'NCE-', + 'NEO-', + 'NOH-', + 'NOP-', + 'OCE-', + 'PAR-', + 'PIC-', + 'POT-', + 'PPA-', + 'PRA-', + 'RNE-', + 'SEA-', + 'SLA-', + 'SNE-', + 'SPN-', + 'STK-', + 'TAH-', + 'TAS-', + 'TET-', + 'TRT-', + 'VCE-', + 'VIE-', + 'VKY-', + 'VNS-', + 'VOG-', + 'VTR-', + 'WAS-', + 'WKG-', + 'WLZ-', + 'JAD-', + 'MLD-', + 'RTE-', + 'NAM-', + 'NEN-', + 'BAL-', + 'JLN-', + 'YAL-', + 'MGA-', + 'FGD-', + 'XYAO-', + 'BON-', + 'ALN-', + 'ALT-', + 'BRA-', + 'DBY2-', + 'STG-', + 'MAO-', + 'LEM-', + 'GOA-', + 'FOA-', + 'MNA-', + 'LNA-', +]); + +// Function to validate Huawei device codes from a string +function validateHuaweiCodes(inputString: string) { + const matches = inputString.match(huaweiDevicesRegex); + if (matches) { + return matches.filter(code => validCodes.has(code.toUpperCase())).length > 0; + } + return false; +} + export const mobileOSDetectAsync = async () => { const userAgent = navigator.userAgent ?? window.opera ?? ''; - const huaweiDevicesRegex = - /\b(AL[PN]|AMN|AN[AE]|A(N[EG]|QM|RS|RT|TU)|B([AL]|AC|LA)|BRQ|C(A[GMN]|AZ|DL|DY|LT|RO|UN)|D(IG|RA|UA|UB|VC)|E(L[ES]|ML|VA|VR)|F(IG|LA|RL)|G(LK|OA)|H(MA|WI?)|I(N[EY]|NE)|J(AT|EF|ER|KM|NY|SC)|L(DN|IO|ON|UA)|LY[AO]|M(A[ORD]|ED|HA|LA)|M(YA|R[DE]|NA)|N(AM|CE|EN|EO|OH|OP)|OCE|P([AIR]|AR|IC|OT|PA|P[AE]|RA)|R(NE|TE)|S(EA|LA|NE|PN)|STK|T(A[HST]|ET|RT)|V(CE|IE|KY|NS|OG|TR)|W(A[SZ]|KG|LZ)|XYAO|F(GD|OA)|DBY2|STG|LEM|MNA|LNA)\b/gi; - // Windows Phone must come first because its UA also contains "Android" if (/windows phone/i.test(userAgent)) { return 'Windows Phone'; @@ -120,10 +242,10 @@ export const mobileOSDetectAsync = async () => { // Check if navigator.userAgentData is available for modern browsers if (navigator?.userAgentData) { const ua = await navigator.userAgentData.getHighEntropyValues(['model']); - if (huaweiDevicesRegex.test(ua?.model || '')) { + if (validateHuaweiCodes(ua?.model || '')) { return 'huawei'; } - } else if (huaweiDevicesRegex.test(userAgent) || /huawei/i.test(userAgent)) { + } else if (validateHuaweiCodes(userAgent) || /huawei/i.test(userAgent)) { return 'huawei'; } return 'Android';