Enforce maximum limit to user-agent input

This commit is contained in:
Faisal Salman 2021-03-20 07:53:30 +07:00
parent fe5ca1de96
commit 8c2b84fc31
3 changed files with 65 additions and 48 deletions

View File

@ -56,7 +56,7 @@ console, mobile, tablet, smarttv, wearable, embedded
Acer, Alcatel, Amazon, Apple, Archos, Asus, BenQ, BlackBerry, Dell, Essential, Acer, Alcatel, Amazon, Apple, Archos, Asus, BenQ, BlackBerry, Dell, Essential,
GeeksPhone, Google, HP, HTC, Huawei, Jolla, Lenovo, LG, Meizu, Microsoft, Motorola, GeeksPhone, Google, HP, HTC, Huawei, Jolla, Lenovo, LG, Meizu, Microsoft, Motorola,
Nexian, Nintendo, Nokia, Nvidia, OnePlus, Ouya, Palm, Panasonic, Pebble, Polytron, Nexian, Nintendo, Nokia, Nvidia, OnePlus, Ouya, Palm, Panasonic, Pebble, Polytron,
RIM, Samsung, Sharp, Siemens, Sony[Ericsson], Sprint, Xbox, Xiaomi, ZTE, ... RIM, Samsung, Sharp, Siemens, Sony[Ericsson], Sprint, Xbox, Xiaomi, Zebra, ZTE, ...
# 'device.model' determined dynamically # 'device.model' determined dynamically
``` ```

View File

@ -35,7 +35,8 @@
TABLET = 'tablet', TABLET = 'tablet',
SMARTTV = 'smarttv', SMARTTV = 'smarttv',
WEARABLE = 'wearable', WEARABLE = 'wearable',
EMBEDDED = 'embedded'; EMBEDDED = 'embedded',
UA_MAX_LENGTH = 255;
/////////// ///////////
@ -56,11 +57,7 @@
return mergedRegexes; return mergedRegexes;
}, },
has : function (str1, str2) { has : function (str1, str2) {
if (typeof str1 === "string") { return typeof str1 === STR_TYPE ? str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1 : false;
return str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1;
} else {
return false;
}
}, },
lowerize : function (str) { lowerize : function (str) {
return str.toLowerCase(); return str.toLowerCase();
@ -68,8 +65,9 @@
major : function (version) { major : function (version) {
return typeof(version) === STR_TYPE ? version.replace(/[^\d\.]/g,'').split(".")[0] : undefined; return typeof(version) === STR_TYPE ? version.replace(/[^\d\.]/g,'').split(".")[0] : undefined;
}, },
trim : function (str) { trim : function (str, len) {
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); str = str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
return typeof(len) === UNDEF_TYPE ? str : str.substring(0, UA_MAX_LENGTH);
} }
}; };
@ -563,6 +561,13 @@
/(kin\.[onetw]{3})/i // Microsoft Kin /(kin\.[onetw]{3})/i // Microsoft Kin
], [[MODEL, /\./g, ' '], [VENDOR, 'Microsoft'], [TYPE, MOBILE]], [ ], [[MODEL, /\./g, ' '], [VENDOR, 'Microsoft'], [TYPE, MOBILE]], [
/droid\s[\d\.]+;\s(cc6{3,4}|et5[16]|mc[239][23]x?|vc8[03]x?)\)/i // Zebra
], [MODEL, [VENDOR, 'Zebra'], [TYPE, TABLET]], [
/droid\s[\d\.]+;\s(ec30|ps20|tc[2-8]\d[kx])\)/i
], [MODEL, [VENDOR, 'Zebra'], [TYPE, MOBILE]], [
/droid\s[\d\.]+;\s(wt63?0{2,3})\)/i
], [MODEL, [VENDOR, 'Zebra'], [TYPE, WEARABLE]], [
// CONSOLES // CONSOLES
/\s(ouya)\s/i, // Ouya /\s(ouya)\s/i, // Ouya
/(nintendo)\s([wids3utch]+)/i // Nintendo /(nintendo)\s([wids3utch]+)/i // Nintendo
@ -715,45 +720,45 @@
///////////////// /////////////////
// Constructor // Constructor
//////////////// ////////////////
var UAParser = function (uastring, extensions) { var UAParser = function (ua, extensions) {
if (typeof uastring === 'object') { if (typeof ua === 'object') {
extensions = uastring; extensions = ua;
uastring = undefined; ua = undefined;
} }
if (!(this instanceof UAParser)) { if (!(this instanceof UAParser)) {
return new UAParser(uastring, extensions).getResult(); return new UAParser(ua, extensions).getResult();
} }
var ua = uastring || ((typeof window !== 'undefined' && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY); var _ua;
var rgxmap = extensions ? util.extend(regexes, extensions) : regexes; var _rgxmap = extensions ? util.extend(regexes, extensions) : regexes;
this.getBrowser = function () { this.getBrowser = function () {
var browser = { name: undefined, version: undefined }; var _browser = { name: undefined, version: undefined };
mapper.rgx.call(browser, ua, rgxmap.browser); mapper.rgx.call(_browser, _ua, _rgxmap.browser);
browser.major = util.major(browser.version); // deprecated _browser.major = util.major(_browser.version); // deprecated
return browser; return _browser;
}; };
this.getCPU = function () { this.getCPU = function () {
var cpu = { architecture: undefined }; var _cpu = { architecture: undefined };
mapper.rgx.call(cpu, ua, rgxmap.cpu); mapper.rgx.call(_cpu, _ua, _rgxmap.cpu);
return cpu; return _cpu;
}; };
this.getDevice = function () { this.getDevice = function () {
var device = { vendor: undefined, model: undefined, type: undefined }; var _device = { vendor: undefined, model: undefined, type: undefined };
mapper.rgx.call(device, ua, rgxmap.device); mapper.rgx.call(_device, _ua, _rgxmap.device);
return device; return _device;
}; };
this.getEngine = function () { this.getEngine = function () {
var engine = { name: undefined, version: undefined }; var _engine = { name: undefined, version: undefined };
mapper.rgx.call(engine, ua, rgxmap.engine); mapper.rgx.call(_engine, _ua, _rgxmap.engine);
return engine; return _engine;
}; };
this.getOS = function () { this.getOS = function () {
var os = { name: undefined, version: undefined }; var _os = { name: undefined, version: undefined };
mapper.rgx.call(os, ua, rgxmap.os); mapper.rgx.call(_os, _ua, _rgxmap.os);
return os; return _os;
}; };
this.getResult = function () { this.getResult = function () {
return { return {
@ -766,12 +771,13 @@
}; };
}; };
this.getUA = function () { this.getUA = function () {
return ua; return _ua;
}; };
this.setUA = function (uastring) { this.setUA = function (ua) {
ua = uastring; _ua = ua.length > UA_MAX_LENGTH ? util.trim(ua, UA_MAX_LENGTH) : ua;
return this; return this;
}; };
this.setUA(ua || ((typeof window !== 'undefined' && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY));
return this; return this;
}; };

File diff suppressed because one or more lines are too long