Skip to content

Commit b098789

Browse files
committed
Prevent altering the result when supplied user-agent is different from current user-agent
1 parent 30de983 commit b098789

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

dist/ua-parser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ua-parser.pack.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ http.createServer(function (req, res) {
477477
res.setHeader('Accept-CH', getHighEntropyValues);
478478
res.setHeader('Critical-CH', getHighEntropyValues);
479479
480-
var ua = uap(req.headers);
480+
var ua = uap(req.headers).withClientHints();
481481
482482
483483

src/ua-parser.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,26 +932,27 @@
932932
['data', createUAParserData(itemType, ua, rgxMap, uaCH)]
933933
]);
934934
this.parse();
935+
var isSelfNav = NAVIGATOR && NAVIGATOR.userAgent == ua;
935936
switch(this.itemType) {
936937
case UA_BROWSER:
937938
// Brave-specific detection
938-
if (NAVIGATOR && NAVIGATOR.userAgent == ua && NAVIGATOR.brave && typeof NAVIGATOR.brave.isBrave == FUNC_TYPE) {
939+
if (isSelfNav && NAVIGATOR.brave && typeof NAVIGATOR.brave.isBrave == FUNC_TYPE) {
939940
this.set(NAME, 'Brave');
940941
}
941942
this.set(MAJOR, majorize(this.get(VERSION)));
942943
break;
943944
case UA_DEVICE:
944-
if (!this.get(TYPE) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[MOBILE]) {
945+
if (isSelfNav && !this.get(TYPE) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[MOBILE]) {
945946
this.set(TYPE, MOBILE);
946947
}
947948
// iPadOS-specific detection: identified as Mac, but has some iOS-only properties
948-
if (this.get(MODEL) == 'Macintosh' && NAVIGATOR && typeof NAVIGATOR.standalone !== UNDEF_TYPE && NAVIGATOR.maxTouchPoints && NAVIGATOR.maxTouchPoints > 2) {
949+
if (isSelfNav && this.get(MODEL) == 'Macintosh' && NAVIGATOR && typeof NAVIGATOR.standalone !== UNDEF_TYPE && NAVIGATOR.maxTouchPoints && NAVIGATOR.maxTouchPoints > 2) {
949950
this.set(MODEL, 'iPad')
950951
.set(TYPE, TABLET);
951952
}
952953
break;
953954
case UA_OS:
954-
if (!this.get(NAME) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[PLATFORM] && NAVIGATOR_UADATA[PLATFORM] != 'Unknown') {
955+
if (isSelfNav && !this.get(NAME) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[PLATFORM]) {
955956
this.set(NAME, NAVIGATOR_UADATA[PLATFORM]);
956957
}
957958
break;

src/ua-parser.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,26 +936,27 @@ const window = undefined;
936936
['data', createUAParserData(itemType, ua, rgxMap, uaCH)]
937937
]);
938938
this.parse();
939+
var isSelfNav = NAVIGATOR && NAVIGATOR.userAgent == ua;
939940
switch(this.itemType) {
940941
case UA_BROWSER:
941942
// Brave-specific detection
942-
if (NAVIGATOR && NAVIGATOR.userAgent == ua && NAVIGATOR.brave && typeof NAVIGATOR.brave.isBrave == FUNC_TYPE) {
943+
if (isSelfNav && NAVIGATOR.brave && typeof NAVIGATOR.brave.isBrave == FUNC_TYPE) {
943944
this.set(NAME, 'Brave');
944945
}
945946
this.set(MAJOR, majorize(this.get(VERSION)));
946947
break;
947948
case UA_DEVICE:
948-
if (!this.get(TYPE) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[MOBILE]) {
949+
if (isSelfNav && !this.get(TYPE) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[MOBILE]) {
949950
this.set(TYPE, MOBILE);
950951
}
951952
// iPadOS-specific detection: identified as Mac, but has some iOS-only properties
952-
if (this.get(MODEL) == 'Macintosh' && NAVIGATOR && typeof NAVIGATOR.standalone !== UNDEF_TYPE && NAVIGATOR.maxTouchPoints && NAVIGATOR.maxTouchPoints > 2) {
953+
if (isSelfNav && this.get(MODEL) == 'Macintosh' && NAVIGATOR && typeof NAVIGATOR.standalone !== UNDEF_TYPE && NAVIGATOR.maxTouchPoints && NAVIGATOR.maxTouchPoints > 2) {
953954
this.set(MODEL, 'iPad')
954955
.set(TYPE, TABLET);
955956
}
956957
break;
957958
case UA_OS:
958-
if (!this.get(NAME) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[PLATFORM] && NAVIGATOR_UADATA[PLATFORM] != 'Unknown') {
959+
if (isSelfNav && !this.get(NAME) && NAVIGATOR_UADATA && NAVIGATOR_UADATA[PLATFORM]) {
959960
this.set(NAME, NAVIGATOR_UADATA[PLATFORM]);
960961
}
961962
break;

0 commit comments

Comments
 (0)