soajs.controller 4.3.23 → 4.3.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mw/mt/utils.js +87 -2
- package/package.json +2 -2
- package/test/unit/mw/mt/utils.js +121 -2
package/mw/mt/utils.js
CHANGED
|
@@ -15,7 +15,7 @@ const coreLibs = require("soajs.core.libs");
|
|
|
15
15
|
|
|
16
16
|
const async = require("async");
|
|
17
17
|
const Netmask = require('netmask').Netmask;
|
|
18
|
-
const
|
|
18
|
+
const UAParser = require('ua-parser-js');
|
|
19
19
|
const merge = require('merge');
|
|
20
20
|
|
|
21
21
|
const UracDriver = require("./urac.js");
|
|
@@ -49,6 +49,91 @@ function getCachedNetmask(addr) {
|
|
|
49
49
|
return netmaskCache.get(addr);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
// Device rules were written against the useragent package names; map ua-parser-js names back to them
|
|
53
|
+
const UA_MAX_LENGTH = 512;
|
|
54
|
+
const UA_OS_FAMILIES = {"Mac OS": "Mac OS X", "Chromium OS": "Chrome OS"};
|
|
55
|
+
const UA_ANDROID_FAMILIES = {"Firefox": "Firefox Mobile", "Opera": "Opera Mobile", "Chrome WebView": "Chrome Mobile WebView"};
|
|
56
|
+
const UA_IOS_FAMILIES = {"Chrome": "Chrome Mobile iOS", "Firefox": "Firefox iOS", "WebKit": "Mobile Safari UI/WKWebView"};
|
|
57
|
+
const UA_FAMILIES = {"Yandex": "Yandex Browser"};
|
|
58
|
+
|
|
59
|
+
// Own keys only: the family comes from the caller and must not resolve to Object.prototype members
|
|
60
|
+
function mapName(map, name) {
|
|
61
|
+
return Object.prototype.hasOwnProperty.call(map, name) ? map[name] : name;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function splitVersion(version) {
|
|
65
|
+
let parts = (version || "").split(".");
|
|
66
|
+
return {
|
|
67
|
+
"major": parts[0] || "0",
|
|
68
|
+
"minor": parts[1] || "0",
|
|
69
|
+
"patch": parts[2] || "0"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parses a User-Agent into {family, major, minor, patch, os: {family, major, minor, patch}}
|
|
75
|
+
*
|
|
76
|
+
* @param {String} ua
|
|
77
|
+
* @returns {Object}
|
|
78
|
+
*/
|
|
79
|
+
function parseUserAgent(ua) {
|
|
80
|
+
ua = String(ua).substring(0, UA_MAX_LENGTH);
|
|
81
|
+
let result = new UAParser(ua).getResult();
|
|
82
|
+
let family = result.browser.name;
|
|
83
|
+
let version = result.browser.version;
|
|
84
|
+
let osFamily = result.os.name;
|
|
85
|
+
let osVersion = result.os.version;
|
|
86
|
+
let match;
|
|
87
|
+
|
|
88
|
+
if (!family) {
|
|
89
|
+
// ua-parser-js guesses iOS from Darwin/CFNetwork without a version
|
|
90
|
+
if (osFamily === "iOS" && !osVersion) {
|
|
91
|
+
osFamily = null;
|
|
92
|
+
}
|
|
93
|
+
// non browser clients: curl/8.4.0, MyApp/142 CFNetwork/...
|
|
94
|
+
match = /^Mozilla\//.test(ua) ? null : /^([^\/\s]+)\/([\d.]+)/.exec(ua);
|
|
95
|
+
family = match ? match[1] : "Other";
|
|
96
|
+
version = match ? match[2] : null;
|
|
97
|
+
}
|
|
98
|
+
if (family === "Edge") {
|
|
99
|
+
if (/ Edg(A)?\//.test(ua)) {
|
|
100
|
+
match = /Chrome\/([\d.]+)/.exec(ua);
|
|
101
|
+
family = "Chrome";
|
|
102
|
+
version = match ? match[1] : null;
|
|
103
|
+
} else if (/ EdgiOS\//.test(ua)) {
|
|
104
|
+
match = /Version\/([\d.]+)/.exec(ua);
|
|
105
|
+
family = "Mobile Safari";
|
|
106
|
+
version = match ? match[1] : null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (osFamily === "Android") {
|
|
110
|
+
// useragent reported every Android version as 0.0.0, keep that so os version rules do not change
|
|
111
|
+
osVersion = null;
|
|
112
|
+
if (family === "Chrome" && / Mobile /.test(ua)) {
|
|
113
|
+
family = "Chrome Mobile";
|
|
114
|
+
} else {
|
|
115
|
+
family = mapName(UA_ANDROID_FAMILIES, family);
|
|
116
|
+
}
|
|
117
|
+
} else if (osFamily === "iOS") {
|
|
118
|
+
if (family === "WebKit") {
|
|
119
|
+
version = osVersion;
|
|
120
|
+
}
|
|
121
|
+
family = mapName(UA_IOS_FAMILIES, family);
|
|
122
|
+
}
|
|
123
|
+
family = mapName(UA_FAMILIES, family);
|
|
124
|
+
|
|
125
|
+
let uaObj = splitVersion(version);
|
|
126
|
+
uaObj.family = String(family);
|
|
127
|
+
if (osFamily === "Windows") {
|
|
128
|
+
// Windows versions are names: 10, 8.1, XP, Vista
|
|
129
|
+
uaObj.os = {"family": osFamily, "major": osVersion || "0", "minor": "0", "patch": "0"};
|
|
130
|
+
} else {
|
|
131
|
+
uaObj.os = splitVersion(osVersion);
|
|
132
|
+
uaObj.os.family = osFamily ? String(mapName(UA_OS_FAMILIES, osFamily)) : "Other";
|
|
133
|
+
}
|
|
134
|
+
return uaObj;
|
|
135
|
+
}
|
|
136
|
+
|
|
52
137
|
let sensitiveEnvCodes = ["dashboard"];
|
|
53
138
|
if (process.env.SOAJS_SENSITIVE_ENVS) {
|
|
54
139
|
let temp_sensitiveEnvCodes = null;
|
|
@@ -381,7 +466,7 @@ let utils = {
|
|
|
381
466
|
};
|
|
382
467
|
|
|
383
468
|
let checkAccess = (deviceAccessArr, ua) => {
|
|
384
|
-
let uaObj =
|
|
469
|
+
let uaObj = parseUserAgent(ua);
|
|
385
470
|
//if (uaObj && uaObj.family && uaObj.os && uaObj.os.family) {
|
|
386
471
|
if (uaObj && uaObj.family) {
|
|
387
472
|
return (deviceAccessArr.some(function (da) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soajs.controller",
|
|
3
3
|
"description": "soajs multi tenant API gateway",
|
|
4
|
-
"version": "4.3.
|
|
4
|
+
"version": "4.3.24",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "soajs team",
|
|
7
7
|
"email": "team@soajs.org"
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"soajs.core.libs": "1.2.3",
|
|
33
33
|
"soajs.core.modules": "5.2.22",
|
|
34
34
|
"soajs.urac.driver": "3.2.20",
|
|
35
|
-
"
|
|
35
|
+
"ua-parser-js": "1.0.41"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@babel/core": "7.29.7",
|
package/test/unit/mw/mt/utils.js
CHANGED
|
@@ -309,8 +309,127 @@ describe("Unit test for: mw - mt utils", () => {
|
|
|
309
309
|
done();
|
|
310
310
|
});
|
|
311
311
|
});
|
|
312
|
-
|
|
313
|
-
|
|
312
|
+
|
|
313
|
+
// [name, User-Agent, [family, major, minor, patch], [os family, major, minor, patch]] as useragent 2.3.0 parsed them
|
|
314
|
+
let userAgents = [
|
|
315
|
+
["chrome-win", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.130 Safari/537.36", ["Chrome", "120", "0", "6099"], ["Windows", "10", "0", "0"]],
|
|
316
|
+
["chrome-mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.129 Safari/537.36", ["Chrome", "120", "0", "6099"], ["Mac OS X", "10", "15", "7"]],
|
|
317
|
+
["chrome-mac-old", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", ["Chrome", "58", "0", "3029"], ["Mac OS X", "10", "12", "4"]],
|
|
318
|
+
["chrome-linux", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", ["Chrome", "120", "0", "0"], ["Linux", "0", "0", "0"]],
|
|
319
|
+
["chrome-cros", "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", ["Chrome", "120", "0", "0"], ["Chrome OS", "14541", "0", "0"]],
|
|
320
|
+
["safari-mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15", ["Safari", "17", "2", "0"], ["Mac OS X", "10", "15", "7"]],
|
|
321
|
+
["firefox-win", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0", ["Firefox", "121", "0", "0"], ["Windows", "10", "0", "0"]],
|
|
322
|
+
["firefox-mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0", ["Firefox", "121", "0", "0"], ["Mac OS X", "10", "15", "0"]],
|
|
323
|
+
["firefox-linux", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0", ["Firefox", "121", "0", "0"], ["Ubuntu", "0", "0", "0"]],
|
|
324
|
+
["edge-win", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.91", ["Chrome", "120", "0", "0"], ["Windows", "10", "0", "0"]],
|
|
325
|
+
["edge-mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.91", ["Chrome", "120", "0", "0"], ["Mac OS X", "10", "15", "7"]],
|
|
326
|
+
["edge-legacy", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582", ["Edge", "18", "19582", "0"], ["Windows", "10", "0", "0"]],
|
|
327
|
+
["ie11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", ["IE", "11", "0", "0"], ["Windows", "10", "0", "0"]],
|
|
328
|
+
["opera", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0", ["Opera", "106", "0", "0"], ["Windows", "10", "0", "0"]],
|
|
329
|
+
["ios-safari", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1", ["Mobile Safari", "17", "2", "0"], ["iOS", "17", "2", "1"]],
|
|
330
|
+
["ipad-safari", "Mozilla/5.0 (iPad; CPU OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1", ["Mobile Safari", "16", "6", "0"], ["iOS", "16", "6", "0"]],
|
|
331
|
+
["ios-chrome", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1", ["Chrome Mobile iOS", "120", "0", "6099"], ["iOS", "17", "2", "0"]],
|
|
332
|
+
["ios-firefox", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/121.0 Mobile/15E148 Safari/605.1.15", ["Firefox iOS", "121", "0", "0"], ["iOS", "17", "2", "0"]],
|
|
333
|
+
["ios-webview", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", ["Mobile Safari UI/WKWebView", "17", "2", "0"], ["iOS", "17", "2", "0"]],
|
|
334
|
+
["edge-ios", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 EdgiOS/120.2210.79 Mobile/15E148 Safari/605.1.15", ["Mobile Safari", "17", "0", "0"], ["iOS", "17", "2", "0"]],
|
|
335
|
+
["android-chrome", "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.144 Mobile Safari/537.36", ["Chrome Mobile", "120", "0", "6099"], ["Android", "0", "0", "0"]],
|
|
336
|
+
["android-chrome-tablet", "Mozilla/5.0 (Linux; Android 13; SM-X700) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", ["Chrome", "120", "0", "0"], ["Android", "0", "0", "0"]],
|
|
337
|
+
["android-webview", "Mozilla/5.0 (Linux; Android 13; Pixel 7 Build/TQ3A.230805.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36", ["Chrome Mobile WebView", "116", "0", "0"], ["Android", "0", "0", "0"]],
|
|
338
|
+
["edge-android", "Mozilla/5.0 (Linux; Android 10; HD1913) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.144 Mobile Safari/537.36 EdgA/120.0.2210.84", ["Chrome Mobile", "120", "0", "6099"], ["Android", "0", "0", "0"]],
|
|
339
|
+
["samsung", "Mozilla/5.0 (Linux; Android 13; SAMSUNG SM-S911B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/23.0 Chrome/115.0.0.0 Mobile Safari/537.36", ["Samsung Internet", "23", "0", "0"], ["Android", "0", "0", "0"]],
|
|
340
|
+
["android-firefox", "Mozilla/5.0 (Android 13; Mobile; rv:121.0) Gecko/121.0 Firefox/121.0", ["Firefox Mobile", "121", "0", "0"], ["Android", "0", "0", "0"]],
|
|
341
|
+
["opera-android", "Mozilla/5.0 (Linux; Android 10; VOG-L29) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 OPR/79.0.4195.76523", ["Opera Mobile", "79", "0", "4195"], ["Android", "0", "0", "0"]],
|
|
342
|
+
["yandex", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 YaBrowser/23.11.0.0 Safari/537.36", ["Yandex Browser", "23", "11", "0"], ["Windows", "10", "0", "0"]],
|
|
343
|
+
["ios-app", "democav/142 CFNetwork/3826.400.120 Darwin/24.3.0", ["democav", "142", "0", "0"], ["Other", "0", "0", "0"]],
|
|
344
|
+
["curl", "curl/8.4.0", ["curl", "8", "4", "0"], ["Other", "0", "0", "0"]]
|
|
345
|
+
];
|
|
346
|
+
|
|
347
|
+
let deviceObj = (ua, device) => {
|
|
348
|
+
return {
|
|
349
|
+
req: {
|
|
350
|
+
getClientUserAgent: function () {
|
|
351
|
+
return ua;
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
keyObj: {
|
|
355
|
+
device: device
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
let exactRule = (b, os) => {
|
|
361
|
+
return {
|
|
362
|
+
"family": b[0], "major": b[1], "minor": b[2], "patch": b[3],
|
|
363
|
+
"os": {"family": os[0], "major": os[1], "minor": os[2], "patch": os[3]}
|
|
364
|
+
};
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
userAgents.forEach((row) => {
|
|
368
|
+
let name = row[0], ua = row[1], b = row[2], os = row[3];
|
|
369
|
+
|
|
370
|
+
it("test securityDeviceCheck - " + name + " allowed by exact rule", function (done) {
|
|
371
|
+
utils.securityDeviceCheck(deviceObj(ua, {"allow": [exactRule(b, os)]}), function (error, obj) {
|
|
372
|
+
assert.ifError(error);
|
|
373
|
+
assert.strictEqual(obj.device, ua);
|
|
374
|
+
done();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("test securityDeviceCheck - " + name + " denied by exact rule", function (done) {
|
|
379
|
+
utils.securityDeviceCheck(deviceObj(ua, {"deny": [exactRule(b, os)]}), function (error) {
|
|
380
|
+
assert.strictEqual(error, 156);
|
|
381
|
+
done();
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("test securityDeviceCheck - " + name + " not allowed by other family, os or version", function (done) {
|
|
386
|
+
let otherOs = exactRule(b, os);
|
|
387
|
+
otherOs.os.family = "NoSuchOS";
|
|
388
|
+
let otherVersion = exactRule(b, os);
|
|
389
|
+
otherVersion.major = {"min": b[1] + "1"};
|
|
390
|
+
let rules = {"allow": [{"family": "NoSuchBrowser"}, otherOs, otherVersion]};
|
|
391
|
+
utils.securityDeviceCheck(deviceObj(ua, rules), function (error) {
|
|
392
|
+
assert.strictEqual(error, 156);
|
|
393
|
+
done();
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
["constructor/1", "toString/2", "__proto__/1", "hasOwnProperty/1 (Linux; Android 10)"].forEach((ua) => {
|
|
399
|
+
it("test securityDeviceCheck - " + ua + " is not allowed as chrome", function (done) {
|
|
400
|
+
utils.securityDeviceCheck(deviceObj(ua, {"allow": [{"family": "chrome"}]}), function (error) {
|
|
401
|
+
assert.strictEqual(error, 156);
|
|
402
|
+
done();
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("test securityDeviceCheck - " + ua + " is not denied as chrome", function (done) {
|
|
407
|
+
utils.securityDeviceCheck(deviceObj(ua, {"deny": [{"family": "chrome"}]}), function (error, obj) {
|
|
408
|
+
assert.ifError(error);
|
|
409
|
+
assert.strictEqual(obj.device, ua);
|
|
410
|
+
done();
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("test securityDeviceCheck - empty User-Agent skips device rules", function (done) {
|
|
416
|
+
utils.securityDeviceCheck(deviceObj("", {"allow": [{"family": "chrome"}], "deny": [{"family": "*"}]}), function (error, obj) {
|
|
417
|
+
assert.ifError(error);
|
|
418
|
+
assert.ok(obj);
|
|
419
|
+
done();
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("test securityDeviceCheck - long padded User-Agent is checked quickly", function (done) {
|
|
424
|
+
let ua = "Mozilla/5.0 (" + "X".repeat(16 * 1024) + ") Gecko/20100101 Firefox/77.0";
|
|
425
|
+
let start = Date.now();
|
|
426
|
+
utils.securityDeviceCheck(deviceObj(ua, {"allow": [{"family": "chrome"}], "deny": [{"family": "IE"}]}), function (error) {
|
|
427
|
+
assert.ok(Date.now() - start < 200);
|
|
428
|
+
assert.strictEqual(error, 156);
|
|
429
|
+
done();
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
314
433
|
it("test securityGeoCheck - fail on deny: Geographic security configuration failed", (done) => {
|
|
315
434
|
let obj = {
|
|
316
435
|
req: {
|