Fix #797: Iterate over brands as an array

This commit is contained in:
@anulman 2025-07-17 16:28:03 -04:00
parent c9d008e97a
commit d78e8bc7d0
7 changed files with 39 additions and 7 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -188,6 +188,8 @@
}, },
setProps = function (arr) { setProps = function (arr) {
for (var i in arr) { for (var i in arr) {
if (!arr.hasOwnProperty(i)) continue;
var propName = arr[i]; var propName = arr[i];
if (typeof propName == OBJ_TYPE && propName.length == 2) { if (typeof propName == OBJ_TYPE && propName.length == 2) {
this[propName[0]] = propName[1]; this[propName[0]] = propName[1];
@ -1263,7 +1265,7 @@
case UA_ENGINE: case UA_ENGINE:
var brands = uaCH[FULLVERLIST] || uaCH[BRANDS], prevName; var brands = uaCH[FULLVERLIST] || uaCH[BRANDS], prevName;
if (brands) { if (brands) {
for (var i in brands) { for (var i=0; i<brands.length; i++) {
var brandName = brands[i].brand || brands[i], var brandName = brands[i].brand || brands[i],
brandVersion = brands[i].version; brandVersion = brands[i].version;
if (this.itemType == UA_BROWSER && if (this.itemType == UA_BROWSER &&

View File

@ -190,6 +190,8 @@
}, },
setProps = function (arr) { setProps = function (arr) {
for (var i in arr) { for (var i in arr) {
if (!arr.hasOwnProperty(i)) continue;
var propName = arr[i]; var propName = arr[i];
if (typeof propName == OBJ_TYPE && propName.length == 2) { if (typeof propName == OBJ_TYPE && propName.length == 2) {
this[propName[0]] = propName[1]; this[propName[0]] = propName[1];
@ -1265,7 +1267,7 @@
case UA_ENGINE: case UA_ENGINE:
var brands = uaCH[FULLVERLIST] || uaCH[BRANDS], prevName; var brands = uaCH[FULLVERLIST] || uaCH[BRANDS], prevName;
if (brands) { if (brands) {
for (var i in brands) { for (var i=0; i<brands.length; i++) {
var brandName = brands[i].brand || brands[i], var brandName = brands[i].brand || brands[i],
brandVersion = brands[i].version; brandVersion = brands[i].version;
if (this.itemType == UA_BROWSER && if (this.itemType == UA_BROWSER &&

View File

@ -91,6 +91,34 @@ describe('Returns', function () {
}); });
done(); done();
}); });
it('works even when Array.prototype has been mangled', function(done) {
const result = withMangledArrayProto(() => new UAParser('').getResult());
function withMangledArrayProto(fn, key = 'isEmpty', value = function() { return this.length === 0; }) {
const originalValue = Array.prototype[key];
const restore = Object.hasOwnProperty.call(Array.prototype, key)
? () => Array.prototype[key] = originalValue
: () => delete Array.prototype[key];
Array.prototype[key] = value;
const result = fn();
restore();
return result;
}
assert.deepEqual(result,
{
ua : '',
browser: { name: undefined, version: undefined, major: undefined, type: undefined },
cpu: { architecture: undefined },
device: { vendor: undefined, model: undefined, type: undefined },
engine: { name: undefined, version: undefined},
os: { name: undefined, version: undefined }
});
done();
});
}); });
describe('Extending Regex', function () { describe('Extending Regex', function () {
@ -353,4 +381,4 @@ describe('Read user-agent data from req.headers', function () {
const { browser } = UAParser(reqHeaders); const { browser } = UAParser(reqHeaders);
assert.strictEqual(browser.is('Midori'), true); assert.strictEqual(browser.is('Midori'), true);
}); });
}); });