@midwayjs/security 3.0.13 → 3.1.2
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/dist/configuration.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -1
- package/dist/middleware/helper.d.ts +5 -0
- package/dist/middleware/helper.js +100 -0
- package/package.json +12 -10
package/dist/configuration.js
CHANGED
|
@@ -20,12 +20,14 @@ const noopen_1 = require("./middleware/noopen");
|
|
|
20
20
|
const _1 = require(".");
|
|
21
21
|
const xssProtection_1 = require("./middleware/xssProtection");
|
|
22
22
|
const csp_1 = require("./middleware/csp");
|
|
23
|
+
const helper_1 = require("./middleware/helper");
|
|
23
24
|
let SecurityConfiguration = class SecurityConfiguration {
|
|
24
25
|
async onReady() {
|
|
25
26
|
this.applicationManager
|
|
26
27
|
.getApplications(['koa', 'faas', 'express', 'egg'])
|
|
27
28
|
.forEach(app => {
|
|
28
29
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
30
|
+
app.useMiddleware(helper_1.SecurityHelper);
|
|
29
31
|
if ((_a = this.security.csrf) === null || _a === void 0 ? void 0 : _a.enable) {
|
|
30
32
|
app.useMiddleware(csrf_1.CSRFMiddleware);
|
|
31
33
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -21,4 +25,5 @@ __exportStar(require("./middleware/noopen"), exports);
|
|
|
21
25
|
__exportStar(require("./middleware/nosniff"), exports);
|
|
22
26
|
__exportStar(require("./middleware/xssProtection"), exports);
|
|
23
27
|
__exportStar(require("./middleware/csp"), exports);
|
|
28
|
+
__exportStar(require("./middleware/helper"), exports);
|
|
24
29
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.SecurityHelper = void 0;
|
|
10
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
11
|
+
const base_1 = require("./base");
|
|
12
|
+
const escape = require("escape-html");
|
|
13
|
+
const xss_1 = require("xss");
|
|
14
|
+
let SecurityHelper = class SecurityHelper extends base_1.BaseMiddleware {
|
|
15
|
+
async compatibleMiddleware(context, req, res, next) {
|
|
16
|
+
context.security = {
|
|
17
|
+
escape,
|
|
18
|
+
html: (htmlCode) => (0, xss_1.default)(htmlCode),
|
|
19
|
+
js: safeJS,
|
|
20
|
+
json: safeJSON,
|
|
21
|
+
};
|
|
22
|
+
return next();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
SecurityHelper = __decorate([
|
|
26
|
+
(0, decorator_1.Middleware)()
|
|
27
|
+
], SecurityHelper);
|
|
28
|
+
exports.SecurityHelper = SecurityHelper;
|
|
29
|
+
const MATCH_VULNERABLE_REGEXP = /[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]/;
|
|
30
|
+
const BASIC_ALPHABETS = new Set('abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
|
|
31
|
+
const map = {
|
|
32
|
+
'\t': '\\t',
|
|
33
|
+
'\n': '\\n',
|
|
34
|
+
'\r': '\\r',
|
|
35
|
+
};
|
|
36
|
+
const safeJS = (jsCode) => {
|
|
37
|
+
jsCode = `${jsCode || ''}`;
|
|
38
|
+
const match = MATCH_VULNERABLE_REGEXP.exec(jsCode);
|
|
39
|
+
if (!match) {
|
|
40
|
+
return jsCode;
|
|
41
|
+
}
|
|
42
|
+
let res = '';
|
|
43
|
+
let index = 0;
|
|
44
|
+
let lastIndex = 0;
|
|
45
|
+
let ascii;
|
|
46
|
+
for (index = match.index; index < jsCode.length; index++) {
|
|
47
|
+
ascii = jsCode[index];
|
|
48
|
+
if (BASIC_ALPHABETS.has(ascii)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
if (map[ascii] === undefined) {
|
|
53
|
+
const code = ascii.charCodeAt(0);
|
|
54
|
+
if (code > 127) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
map[ascii] = '\\x' + code.toString(16);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (lastIndex !== index) {
|
|
63
|
+
res += jsCode.substring(lastIndex, index);
|
|
64
|
+
}
|
|
65
|
+
lastIndex = index + 1;
|
|
66
|
+
res += map[ascii];
|
|
67
|
+
}
|
|
68
|
+
return lastIndex !== index ? res + jsCode.substring(lastIndex, index) : res;
|
|
69
|
+
};
|
|
70
|
+
function sanitizeKey(obj) {
|
|
71
|
+
if (typeof obj !== 'object')
|
|
72
|
+
return obj;
|
|
73
|
+
if (Array.isArray(obj))
|
|
74
|
+
return obj;
|
|
75
|
+
if (obj === null)
|
|
76
|
+
return null;
|
|
77
|
+
if (obj instanceof Boolean)
|
|
78
|
+
return obj;
|
|
79
|
+
if (obj instanceof Number)
|
|
80
|
+
return obj;
|
|
81
|
+
if (obj instanceof Buffer)
|
|
82
|
+
return obj.toString();
|
|
83
|
+
for (const k in obj) {
|
|
84
|
+
const escapedK = safeJS(k);
|
|
85
|
+
if (escapedK !== k) {
|
|
86
|
+
obj[escapedK] = sanitizeKey(obj[k]);
|
|
87
|
+
obj[k] = undefined;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
obj[k] = sanitizeKey(obj[k]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return obj;
|
|
94
|
+
}
|
|
95
|
+
const safeJSON = (object) => {
|
|
96
|
+
return JSON.stringify(sanitizeKey(object), (k, v) => {
|
|
97
|
+
return typeof v === 'string' ? safeJS(v) : v;
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=helper.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/security",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Midway Security Component",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -23,19 +23,21 @@
|
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"csrf": "3.1.0",
|
|
26
|
+
"escape-html": "^1.0.3",
|
|
26
27
|
"nanoid": "3.3.1",
|
|
27
28
|
"picomatch": "2.3.1",
|
|
28
|
-
"platform": "1.3.6"
|
|
29
|
+
"platform": "1.3.6",
|
|
30
|
+
"xss": "^1.0.11"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
|
-
"@midwayjs/core": "^3.
|
|
33
|
+
"@midwayjs/core": "^3.1.2",
|
|
32
34
|
"@midwayjs/decorator": "^3.0.10",
|
|
33
|
-
"@midwayjs/express": "^3.
|
|
34
|
-
"@midwayjs/faas": "^3.
|
|
35
|
-
"@midwayjs/koa": "^3.
|
|
36
|
-
"@midwayjs/mock": "^3.
|
|
37
|
-
"@midwayjs/serverless-app": "^3.
|
|
38
|
-
"@midwayjs/web": "^3.
|
|
35
|
+
"@midwayjs/express": "^3.1.2",
|
|
36
|
+
"@midwayjs/faas": "^3.1.2",
|
|
37
|
+
"@midwayjs/koa": "^3.1.2",
|
|
38
|
+
"@midwayjs/mock": "^3.1.2",
|
|
39
|
+
"@midwayjs/serverless-app": "^3.1.2",
|
|
40
|
+
"@midwayjs/web": "^3.1.2"
|
|
39
41
|
},
|
|
40
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "4ff3aa892b76d016f0ea123c7f9520d054d5c96b"
|
|
41
43
|
}
|