@eggjs/security 5.0.0-beta.19 → 5.0.0-beta.20
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/agent.js +1 -1
- package/dist/app/extend/agent.d.ts +2 -1
- package/dist/app/extend/agent.js +1 -1
- package/dist/app/extend/application.d.ts +3 -11
- package/dist/app/extend/application.js +2 -30
- package/dist/app/extend/context.d.ts +3 -60
- package/dist/app/extend/context.js +2 -189
- package/dist/app/extend/helper.d.ts +8 -8
- package/dist/app/extend/helper.js +11 -1
- package/dist/app/extend/response.d.ts +3 -38
- package/dist/app/extend/response.js +3 -68
- package/dist/app/middleware/securities.js +12 -1
- package/dist/app.js +2 -2
- package/dist/application-COC0mYEe.js +32 -0
- package/dist/application-n5bk2L_z.d.ts +12 -0
- package/dist/cliFilter-7BSD8Nc_.js +18 -0
- package/dist/cliFilter-DKZxCxSe.d.ts +7 -0
- package/dist/config/config.default.d.ts +1 -869
- package/dist/config/config.default.js +1 -164
- package/dist/config.default-AcwQOAG0.js +166 -0
- package/dist/config.default-D8v08Vox.d.ts +870 -0
- package/dist/context-C-N1IY85.d.ts +95 -0
- package/dist/context-e-QJTKfq.js +191 -0
- package/dist/csp-BW5AJd_f.js +46 -0
- package/dist/csrf-9aSLHiby.js +33 -0
- package/dist/dta-DVAKEpJ3.js +13 -0
- package/dist/escape-Dex_Pk9e.d.ts +2 -0
- package/dist/escape-p8-cW8c_.js +7 -0
- package/dist/escapeShellArg-BnzDicAC.d.ts +4 -0
- package/dist/escapeShellArg-C0v1ZeCl.js +7 -0
- package/dist/escapeShellCmd-CkAdyhtO.js +15 -0
- package/dist/escapeShellCmd-DQZZIHde.d.ts +4 -0
- package/dist/helper-DylzfQ_5.js +25 -0
- package/dist/hsts-CWMKNTEh.js +19 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +6 -3
- package/dist/lib/extend/safe_curl.d.ts +2 -19
- package/dist/lib/extend/safe_curl.js +1 -17
- package/dist/lib/helper/cliFilter.d.ts +1 -6
- package/dist/lib/helper/cliFilter.js +1 -16
- package/dist/lib/helper/escape.d.ts +1 -1
- package/dist/lib/helper/escape.js +1 -5
- package/dist/lib/helper/escapeShellArg.d.ts +1 -3
- package/dist/lib/helper/escapeShellArg.js +1 -5
- package/dist/lib/helper/escapeShellCmd.d.ts +1 -3
- package/dist/lib/helper/escapeShellCmd.js +1 -13
- package/dist/lib/helper/index.d.ts +9 -9
- package/dist/lib/helper/index.js +11 -23
- package/dist/lib/helper/shtml.d.ts +1 -5
- package/dist/lib/helper/shtml.js +2 -51
- package/dist/lib/helper/sjs.d.ts +1 -6
- package/dist/lib/helper/sjs.js +1 -34
- package/dist/lib/helper/sjson.d.ts +1 -3
- package/dist/lib/helper/sjson.js +2 -30
- package/dist/lib/helper/spath.d.ts +1 -6
- package/dist/lib/helper/spath.js +1 -14
- package/dist/lib/helper/surl.d.ts +1 -5
- package/dist/lib/helper/surl.js +1 -23
- package/dist/lib/middlewares/csp.d.ts +1 -1
- package/dist/lib/middlewares/csp.js +2 -44
- package/dist/lib/middlewares/csrf.d.ts +1 -1
- package/dist/lib/middlewares/csrf.js +2 -31
- package/dist/lib/middlewares/dta.js +2 -11
- package/dist/lib/middlewares/hsts.d.ts +1 -1
- package/dist/lib/middlewares/hsts.js +2 -17
- package/dist/lib/middlewares/index.d.ts +12 -12
- package/dist/lib/middlewares/index.js +12 -25
- package/dist/lib/middlewares/methodnoallow.js +1 -13
- package/dist/lib/middlewares/noopen.d.ts +1 -1
- package/dist/lib/middlewares/noopen.js +2 -15
- package/dist/lib/middlewares/nosniff.d.ts +1 -1
- package/dist/lib/middlewares/nosniff.js +2 -25
- package/dist/lib/middlewares/referrerPolicy.d.ts +1 -1
- package/dist/lib/middlewares/referrerPolicy.js +2 -29
- package/dist/lib/middlewares/xframe.d.ts +1 -1
- package/dist/lib/middlewares/xframe.js +2 -16
- package/dist/lib/middlewares/xssProtection.d.ts +1 -1
- package/dist/lib/middlewares/xssProtection.js +2 -15
- package/dist/lib/utils.d.ts +1 -1
- package/dist/lib/utils.js +1 -125
- package/dist/methodnoallow-BAZONArS.js +15 -0
- package/dist/middlewares-CkQjv8t0.js +27 -0
- package/dist/noopen-C3jUBwoH.js +17 -0
- package/dist/nosniff-CcLkhX2I.js +27 -0
- package/dist/referrerPolicy-D4Uafq6c.js +31 -0
- package/dist/response-BFnHAJrV.js +69 -0
- package/dist/safe_curl-UlViaxoF.js +19 -0
- package/dist/safe_curl-mqZZv_YQ.d.ts +20 -0
- package/dist/shtml-CAquTzgV.d.ts +6 -0
- package/dist/shtml-CgF4kOx-.js +53 -0
- package/dist/sjs-Cbmkk5xS.js +36 -0
- package/dist/sjs-QZIJYS71.d.ts +7 -0
- package/dist/sjson-BetFnVR6.js +32 -0
- package/dist/sjson-O-vKJPws.d.ts +4 -0
- package/dist/spath-Bu9sy6Kz.js +16 -0
- package/dist/spath-DseDPHxf.d.ts +7 -0
- package/dist/surl-ClleTea7.js +25 -0
- package/dist/surl-JV70X_RZ.d.ts +6 -0
- package/dist/types-BZR2U30p.d.ts +38 -0
- package/dist/types-DnJpiSJb.js +1 -0
- package/dist/types.d.ts +3 -38
- package/dist/types.js +2 -0
- package/dist/utils-Cajs5P8M.js +127 -0
- package/dist/xframe-q9fEZkVI.js +18 -0
- package/dist/xssProtection-D5QsHX-e.js +17 -0
- package/package.json +5 -5
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { SecurityConfig } from "./config.default-D8v08Vox.js";
|
|
2
|
+
import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "./safe_curl-mqZZv_YQ.js";
|
|
3
|
+
import { Context, Response } from "egg";
|
|
4
|
+
|
|
5
|
+
//#region src/app/extend/response.d.ts
|
|
6
|
+
declare class SecurityResponse extends Response {
|
|
7
|
+
ctx: SecurityContext;
|
|
8
|
+
/**
|
|
9
|
+
* This is an unsafe redirection, and we WON'T check if the
|
|
10
|
+
* destination url is safe or not.
|
|
11
|
+
* Please DO NOT use this method unless in some very special cases,
|
|
12
|
+
* otherwise there may be security vulnerabilities.
|
|
13
|
+
*
|
|
14
|
+
* @function Response#unsafeRedirect
|
|
15
|
+
* @param {String} url URL to forward
|
|
16
|
+
* @example
|
|
17
|
+
* ```js
|
|
18
|
+
* ctx.response.unsafeRedirect('http://www.domain.com');
|
|
19
|
+
* ctx.unsafeRedirect('http://www.domain.com');
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
unsafeRedirect(url: string, alt?: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* A safe redirection, and we'll check if the URL is in
|
|
25
|
+
* a safe domain or not.
|
|
26
|
+
* We've overridden the default Koa's implementation by adding a
|
|
27
|
+
* white list as the filter for that.
|
|
28
|
+
*
|
|
29
|
+
* @function Response#redirect
|
|
30
|
+
* @param {String} url URL to forward
|
|
31
|
+
* @example
|
|
32
|
+
* ```js
|
|
33
|
+
* ctx.response.redirect('/login');
|
|
34
|
+
* ctx.redirect('/login');
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
redirect(url: string, alt?: string): void;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/app/extend/context.d.ts
|
|
41
|
+
declare const CSRF_SECRET: unique symbol;
|
|
42
|
+
declare const LOG_CSRF_NOTICE: unique symbol;
|
|
43
|
+
declare const INPUT_TOKEN: unique symbol;
|
|
44
|
+
declare const CSRF_REFERER_CHECK: unique symbol;
|
|
45
|
+
declare const CSRF_CTOKEN_CHECK: unique symbol;
|
|
46
|
+
declare class SecurityContext extends Context {
|
|
47
|
+
response: SecurityResponse;
|
|
48
|
+
get securityOptions(): Partial<SecurityConfig>;
|
|
49
|
+
/**
|
|
50
|
+
* Check whether the specific `domain` is in / matches the whiteList or not.
|
|
51
|
+
* @param {string} domain The assigned domain.
|
|
52
|
+
* @param {Array<string>} [customWhiteList] The custom white list for domain.
|
|
53
|
+
* @return {boolean} If the domain is in / matches the whiteList, return true;
|
|
54
|
+
* otherwise false.
|
|
55
|
+
*/
|
|
56
|
+
isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
|
|
57
|
+
get nonce(): string;
|
|
58
|
+
/**
|
|
59
|
+
* get csrf token, general use in template
|
|
60
|
+
* @return {String} csrf token
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
get csrf(): string;
|
|
64
|
+
/**
|
|
65
|
+
* get csrf secret from session or cookie
|
|
66
|
+
* @return {String} csrf secret
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
get [CSRF_SECRET](): string;
|
|
70
|
+
/**
|
|
71
|
+
* ensure csrf secret exists in session or cookie.
|
|
72
|
+
* @param {Boolean} [rotate] reset secret even if the secret exists
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
75
|
+
ensureCsrfSecret(rotate?: boolean): void;
|
|
76
|
+
get [INPUT_TOKEN](): string;
|
|
77
|
+
/**
|
|
78
|
+
* rotate csrf secret exists in session or cookie.
|
|
79
|
+
* must rotate the secret when user login
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
rotateCsrfSecret(): void;
|
|
83
|
+
/**
|
|
84
|
+
* assert csrf token/referer is present
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
assertCsrf(): void;
|
|
88
|
+
[CSRF_CTOKEN_CHECK](): "missing csrf token" | "invalid csrf token" | undefined;
|
|
89
|
+
[CSRF_REFERER_CHECK](): "missing csrf referer or origin" | "invalid csrf referer or origin" | undefined;
|
|
90
|
+
[LOG_CSRF_NOTICE](msg: string): void;
|
|
91
|
+
safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
|
|
92
|
+
unsafeRedirect(url: string, alt?: string): void;
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
export { SecurityContext, SecurityResponse };
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { checkIfIgnore, getFromUrl, isSafeDomain } from "./utils-Cajs5P8M.js";
|
|
2
|
+
import { Context } from "egg";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
4
|
+
import { nanoid } from "nanoid/non-secure";
|
|
5
|
+
import Tokens from "csrf";
|
|
6
|
+
|
|
7
|
+
//#region src/app/extend/context.ts
|
|
8
|
+
const debug = debuglog("egg/security/app/extend/context");
|
|
9
|
+
const tokens = new Tokens();
|
|
10
|
+
const CSRF_SECRET = Symbol("egg-security#CSRF_SECRET");
|
|
11
|
+
const _CSRF_SECRET = Symbol("egg-security#_CSRF_SECRET");
|
|
12
|
+
const NEW_CSRF_SECRET = Symbol("egg-security#NEW_CSRF_SECRET");
|
|
13
|
+
const LOG_CSRF_NOTICE = Symbol("egg-security#LOG_CSRF_NOTICE");
|
|
14
|
+
const INPUT_TOKEN = Symbol("egg-security#INPUT_TOKEN");
|
|
15
|
+
const NONCE_CACHE = Symbol("egg-security#NONCE_CACHE");
|
|
16
|
+
const SECURITY_OPTIONS = Symbol("egg-security#SECURITY_OPTIONS");
|
|
17
|
+
const CSRF_REFERER_CHECK = Symbol("egg-security#CSRF_REFERER_CHECK");
|
|
18
|
+
const CSRF_CTOKEN_CHECK = Symbol("egg-security#CSRF_CTOKEN_CHECK");
|
|
19
|
+
function findToken(obj, keys) {
|
|
20
|
+
if (!obj) return;
|
|
21
|
+
if (!keys || !keys.length) return;
|
|
22
|
+
if (typeof keys === "string") return obj[keys];
|
|
23
|
+
for (const key of keys) if (obj[key]) return obj[key];
|
|
24
|
+
}
|
|
25
|
+
var SecurityContext = class extends Context {
|
|
26
|
+
get securityOptions() {
|
|
27
|
+
if (!this[SECURITY_OPTIONS]) this[SECURITY_OPTIONS] = {};
|
|
28
|
+
return this[SECURITY_OPTIONS];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check whether the specific `domain` is in / matches the whiteList or not.
|
|
32
|
+
* @param {string} domain The assigned domain.
|
|
33
|
+
* @param {Array<string>} [customWhiteList] The custom white list for domain.
|
|
34
|
+
* @return {boolean} If the domain is in / matches the whiteList, return true;
|
|
35
|
+
* otherwise false.
|
|
36
|
+
*/
|
|
37
|
+
isSafeDomain(domain, customWhiteList) {
|
|
38
|
+
const domainWhiteList = customWhiteList && customWhiteList.length > 0 ? customWhiteList : this.app.config.security.domainWhiteList;
|
|
39
|
+
return isSafeDomain(domain, domainWhiteList);
|
|
40
|
+
}
|
|
41
|
+
get nonce() {
|
|
42
|
+
if (!this[NONCE_CACHE]) this[NONCE_CACHE] = nanoid(16);
|
|
43
|
+
return this[NONCE_CACHE];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* get csrf token, general use in template
|
|
47
|
+
* @return {String} csrf token
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
get csrf() {
|
|
51
|
+
const secret = this[NEW_CSRF_SECRET] || this[CSRF_SECRET];
|
|
52
|
+
debug("get csrf token, NEW_CSRF_SECRET: %s, _CSRF_SECRET: %s", this[NEW_CSRF_SECRET], this[CSRF_SECRET]);
|
|
53
|
+
return secret ? tokens.create(secret) : "";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* get csrf secret from session or cookie
|
|
57
|
+
* @return {String} csrf secret
|
|
58
|
+
* @private
|
|
59
|
+
*/
|
|
60
|
+
get [CSRF_SECRET]() {
|
|
61
|
+
if (this[_CSRF_SECRET]) return this[_CSRF_SECRET];
|
|
62
|
+
let { useSession, sessionName, cookieName: cookieNames, cookieOptions } = this.app.config.security.csrf;
|
|
63
|
+
if (useSession) this[_CSRF_SECRET] = this.session[sessionName] || "";
|
|
64
|
+
else {
|
|
65
|
+
if (!Array.isArray(cookieNames)) cookieNames = [cookieNames];
|
|
66
|
+
for (const cookieName of cookieNames) {
|
|
67
|
+
this[_CSRF_SECRET] = this.cookies.get(cookieName, { signed: cookieOptions.signed }) || "";
|
|
68
|
+
if (this[_CSRF_SECRET]) break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return this[_CSRF_SECRET];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* ensure csrf secret exists in session or cookie.
|
|
75
|
+
* @param {Boolean} [rotate] reset secret even if the secret exists
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
ensureCsrfSecret(rotate) {
|
|
79
|
+
if (this[CSRF_SECRET] && !rotate) return;
|
|
80
|
+
debug("ensure csrf secret, exists: %s, rotate; %s", this[CSRF_SECRET], rotate);
|
|
81
|
+
const secret = tokens.secretSync();
|
|
82
|
+
this[NEW_CSRF_SECRET] = secret;
|
|
83
|
+
let { useSession, sessionName, cookieDomain, cookieName: cookieNames, cookieOptions } = this.app.config.security.csrf;
|
|
84
|
+
if (useSession) this.session[sessionName] = secret;
|
|
85
|
+
else {
|
|
86
|
+
if (typeof cookieDomain === "function") cookieDomain = cookieDomain(this);
|
|
87
|
+
const cookieOpts = {
|
|
88
|
+
domain: cookieDomain,
|
|
89
|
+
...cookieOptions
|
|
90
|
+
};
|
|
91
|
+
if (!Array.isArray(cookieNames)) cookieNames = [cookieNames];
|
|
92
|
+
for (const cookieName of cookieNames) this.cookies.set(cookieName, secret, cookieOpts);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
get [INPUT_TOKEN]() {
|
|
96
|
+
const { headerName, bodyName, queryName } = this.app.config.security.csrf;
|
|
97
|
+
const token = findToken(this.request.query, queryName) || findToken(this.request.body, bodyName) || headerName && this.request.get(headerName);
|
|
98
|
+
debug("get token: %j, secret: %j", token, this[CSRF_SECRET]);
|
|
99
|
+
return token;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* rotate csrf secret exists in session or cookie.
|
|
103
|
+
* must rotate the secret when user login
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
rotateCsrfSecret() {
|
|
107
|
+
if (!this[NEW_CSRF_SECRET] && this[CSRF_SECRET]) this.ensureCsrfSecret(true);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* assert csrf token/referer is present
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
assertCsrf() {
|
|
114
|
+
if (checkIfIgnore(this.app.config.security.csrf, this)) {
|
|
115
|
+
debug("%s, ignore by csrf options", this.path);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const { type } = this.app.config.security.csrf;
|
|
119
|
+
let message;
|
|
120
|
+
const messages = [];
|
|
121
|
+
switch (type) {
|
|
122
|
+
case "ctoken":
|
|
123
|
+
message = this[CSRF_CTOKEN_CHECK]();
|
|
124
|
+
if (message) this.throw(403, message);
|
|
125
|
+
break;
|
|
126
|
+
case "referer":
|
|
127
|
+
message = this[CSRF_REFERER_CHECK]();
|
|
128
|
+
if (message) this.throw(403, message);
|
|
129
|
+
break;
|
|
130
|
+
case "all":
|
|
131
|
+
message = this[CSRF_CTOKEN_CHECK]();
|
|
132
|
+
if (message) this.throw(403, message);
|
|
133
|
+
message = this[CSRF_REFERER_CHECK]();
|
|
134
|
+
if (message) this.throw(403, message);
|
|
135
|
+
break;
|
|
136
|
+
case "any":
|
|
137
|
+
message = this[CSRF_CTOKEN_CHECK]();
|
|
138
|
+
if (!message) return;
|
|
139
|
+
messages.push(message);
|
|
140
|
+
message = this[CSRF_REFERER_CHECK]();
|
|
141
|
+
if (!message) return;
|
|
142
|
+
messages.push(message);
|
|
143
|
+
this.throw(403, `both ctoken and referer check error: ${messages.join(", ")}`);
|
|
144
|
+
break;
|
|
145
|
+
default: this.throw(`invalid type ${type}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
[CSRF_CTOKEN_CHECK]() {
|
|
149
|
+
if (!this[CSRF_SECRET]) {
|
|
150
|
+
debug("missing csrf token");
|
|
151
|
+
this[LOG_CSRF_NOTICE]("missing csrf token");
|
|
152
|
+
return "missing csrf token";
|
|
153
|
+
}
|
|
154
|
+
const token = this[INPUT_TOKEN];
|
|
155
|
+
if (token !== this[CSRF_SECRET] && !tokens.verify(this[CSRF_SECRET], token)) {
|
|
156
|
+
debug("verify secret and token error");
|
|
157
|
+
this[LOG_CSRF_NOTICE]("invalid csrf token");
|
|
158
|
+
const { rotateWhenInvalid } = this.app.config.security.csrf;
|
|
159
|
+
if (rotateWhenInvalid) this.rotateCsrfSecret();
|
|
160
|
+
return "invalid csrf token";
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
[CSRF_REFERER_CHECK]() {
|
|
164
|
+
const { refererWhiteList } = this.app.config.security.csrf;
|
|
165
|
+
const referer = (this.headers.referer ?? this.headers.origin ?? "").toLowerCase();
|
|
166
|
+
if (!referer) {
|
|
167
|
+
debug("missing csrf referer or origin");
|
|
168
|
+
this[LOG_CSRF_NOTICE]("missing csrf referer or origin");
|
|
169
|
+
return "missing csrf referer or origin";
|
|
170
|
+
}
|
|
171
|
+
const host = getFromUrl(referer, "host");
|
|
172
|
+
const domainList = refererWhiteList.concat(this.host);
|
|
173
|
+
if (!host || !isSafeDomain(host, domainList)) {
|
|
174
|
+
debug("verify referer or origin error");
|
|
175
|
+
this[LOG_CSRF_NOTICE]("invalid csrf referer or origin");
|
|
176
|
+
return "invalid csrf referer or origin";
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
[LOG_CSRF_NOTICE](msg) {
|
|
180
|
+
if (this.app.config.env === "local") this.logger.warn(`${msg}. See https://eggjs.org/zh-CN/core/security/#%E5%AE%89%E5%85%A8%E5%A8%81%E8%83%81-csrf-%E7%9A%84%E9%98%B2%E8%8C%83`);
|
|
181
|
+
}
|
|
182
|
+
async safeCurl(url, options) {
|
|
183
|
+
return await this.app.safeCurl(url, options);
|
|
184
|
+
}
|
|
185
|
+
unsafeRedirect(url, alt) {
|
|
186
|
+
this.response.unsafeRedirect(url, alt);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
//#endregion
|
|
191
|
+
export { SecurityContext };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { checkIfIgnore } from "./utils-Cajs5P8M.js";
|
|
2
|
+
import extend from "extend";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/middlewares/csp.ts
|
|
5
|
+
const HEADER = ["x-content-security-policy", "content-security-policy"];
|
|
6
|
+
const REPORT_ONLY_HEADER = ["x-content-security-policy-report-only", "content-security-policy-report-only"];
|
|
7
|
+
const MSIE_REGEXP = / MSIE /i;
|
|
8
|
+
var csp_default = (options) => {
|
|
9
|
+
return async function csp(ctx, next) {
|
|
10
|
+
await next();
|
|
11
|
+
const opts = {
|
|
12
|
+
...options,
|
|
13
|
+
...ctx.securityOptions.csp
|
|
14
|
+
};
|
|
15
|
+
if (checkIfIgnore(opts, ctx)) return;
|
|
16
|
+
let finalHeader;
|
|
17
|
+
const matchedOption = extend(true, {}, opts.policy);
|
|
18
|
+
const bufArray = [];
|
|
19
|
+
const headers = opts.reportOnly ? REPORT_ONLY_HEADER : HEADER;
|
|
20
|
+
if (opts.supportIE && MSIE_REGEXP.test(ctx.get("user-agent"))) finalHeader = headers[0];
|
|
21
|
+
else finalHeader = headers[1];
|
|
22
|
+
for (const key in matchedOption) {
|
|
23
|
+
const value = matchedOption[key];
|
|
24
|
+
if (key === "sandbox" && value === true) bufArray.push(key);
|
|
25
|
+
else {
|
|
26
|
+
let values = Array.isArray(value) ? value : [value];
|
|
27
|
+
if (key === "script-src") {
|
|
28
|
+
if (!values.some(function(val) {
|
|
29
|
+
return val.indexOf("nonce-") !== -1;
|
|
30
|
+
})) values.push("'nonce-" + ctx.nonce + "'");
|
|
31
|
+
}
|
|
32
|
+
values = values.map(function(d) {
|
|
33
|
+
if (d.startsWith(".")) d = "*" + d;
|
|
34
|
+
return d;
|
|
35
|
+
});
|
|
36
|
+
bufArray.push(key + " " + values.join(" "));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const headerString = bufArray.join(";");
|
|
40
|
+
ctx.set(finalHeader, headerString);
|
|
41
|
+
ctx.set("x-csp-nonce", ctx.nonce);
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { csp_default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { checkIfIgnore } from "./utils-Cajs5P8M.js";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
import typeis from "type-is";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/middlewares/csrf.ts
|
|
6
|
+
const debug = debuglog("egg/security/lib/middlewares/csrf");
|
|
7
|
+
var csrf_default = (options) => {
|
|
8
|
+
return function csrf(ctx, next) {
|
|
9
|
+
if (checkIfIgnore(options, ctx)) return next();
|
|
10
|
+
if ([
|
|
11
|
+
"any",
|
|
12
|
+
"all",
|
|
13
|
+
"ctoken"
|
|
14
|
+
].includes(options.type)) ctx.ensureCsrfSecret();
|
|
15
|
+
const method = ctx.method;
|
|
16
|
+
let isSupported = false;
|
|
17
|
+
for (const eachRule of options.supportedRequests) if (eachRule.path.test(ctx.path)) {
|
|
18
|
+
if (eachRule.methods.includes(method)) {
|
|
19
|
+
isSupported = true;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (!isSupported) return next();
|
|
24
|
+
if (options.ignoreJSON && typeis.is(ctx.get("content-type"), "json")) return next();
|
|
25
|
+
const body = ctx.request.body;
|
|
26
|
+
debug("%s %s, got %j", ctx.method, ctx.url, body);
|
|
27
|
+
ctx.assertCsrf();
|
|
28
|
+
return next();
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { csrf_default };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isSafePath } from "./utils-Cajs5P8M.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/middlewares/dta.ts
|
|
4
|
+
var dta_default = () => {
|
|
5
|
+
return function dta(ctx, next) {
|
|
6
|
+
const path = ctx.path;
|
|
7
|
+
if (!isSafePath(path, ctx)) ctx.throw(400);
|
|
8
|
+
return next();
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { dta_default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/lib/helper/escapeShellCmd.ts
|
|
2
|
+
const BASIC_ALPHABETS = new Set("#&;`|*?~<>^()[]{}$;'\",\nÿ".split(""));
|
|
3
|
+
function escapeShellCmd(text) {
|
|
4
|
+
const str = "" + text;
|
|
5
|
+
let res = "";
|
|
6
|
+
let ascii;
|
|
7
|
+
for (let index = 0; index < str.length; index++) {
|
|
8
|
+
ascii = str[index];
|
|
9
|
+
if (!BASIC_ALPHABETS.has(ascii)) res += ascii;
|
|
10
|
+
}
|
|
11
|
+
return res;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { escapeShellCmd };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { cliFilter } from "./cliFilter-7BSD8Nc_.js";
|
|
2
|
+
import { escape_default } from "./escape-p8-cW8c_.js";
|
|
3
|
+
import { escapeShellArg } from "./escapeShellArg-C0v1ZeCl.js";
|
|
4
|
+
import { escapeShellCmd } from "./escapeShellCmd-CkAdyhtO.js";
|
|
5
|
+
import { shtml } from "./shtml-CgF4kOx-.js";
|
|
6
|
+
import { escapeJavaScript } from "./sjs-Cbmkk5xS.js";
|
|
7
|
+
import { jsonEscape } from "./sjson-BetFnVR6.js";
|
|
8
|
+
import { pathFilter } from "./spath-Bu9sy6Kz.js";
|
|
9
|
+
import { surl } from "./surl-ClleTea7.js";
|
|
10
|
+
|
|
11
|
+
//#region src/lib/helper/index.ts
|
|
12
|
+
var helper_default = {
|
|
13
|
+
cliFilter,
|
|
14
|
+
escape: escape_default,
|
|
15
|
+
escapeShellArg,
|
|
16
|
+
escapeShellCmd,
|
|
17
|
+
shtml,
|
|
18
|
+
sjs: escapeJavaScript,
|
|
19
|
+
sjson: jsonEscape,
|
|
20
|
+
spath: pathFilter,
|
|
21
|
+
surl
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { helper_default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { checkIfIgnore } from "./utils-Cajs5P8M.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/middlewares/hsts.ts
|
|
4
|
+
var hsts_default = (options) => {
|
|
5
|
+
return async function hsts(ctx, next) {
|
|
6
|
+
await next();
|
|
7
|
+
const opts = {
|
|
8
|
+
...options,
|
|
9
|
+
...ctx.securityOptions.hsts
|
|
10
|
+
};
|
|
11
|
+
if (checkIfIgnore(opts, ctx)) return;
|
|
12
|
+
let val = `max-age=${opts.maxAge}`;
|
|
13
|
+
if (opts.includeSubdomains) val = `${val}; includeSubdomains`;
|
|
14
|
+
ctx.set("strict-transport-security", val);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { hsts_default };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import "./
|
|
3
|
-
import "./
|
|
1
|
+
import "./utils-Cajs5P8M.js";
|
|
2
|
+
import "./safe_curl-UlViaxoF.js";
|
|
3
|
+
import "./application-COC0mYEe.js";
|
|
4
|
+
import "./context-e-QJTKfq.js";
|
|
5
|
+
import "./response-BFnHAJrV.js";
|
|
6
|
+
import "./types-DnJpiSJb.js";
|
|
4
7
|
|
|
5
8
|
export { };
|
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { EggApplicationCore } from "egg";
|
|
4
|
-
|
|
5
|
-
//#region src/lib/extend/safe_curl.d.ts
|
|
6
|
-
type HttpClient = EggApplicationCore['HttpClient'];
|
|
7
|
-
type HttpClientParameters = Parameters<HttpClient['prototype']['request']>;
|
|
8
|
-
type HttpClientRequestURL = HttpClientParameters[0];
|
|
9
|
-
type HttpClientOptions = HttpClientParameters[1] & {
|
|
10
|
-
checkAddress?: SSRFCheckAddressFunction;
|
|
11
|
-
};
|
|
12
|
-
type HttpClientResponse<T = any> = Awaited<ReturnType<HttpClient['prototype']['request']>> & {
|
|
13
|
-
data: T;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* safe curl with ssrf protection
|
|
17
|
-
*/
|
|
18
|
-
declare function safeCurlForApplication<T = any>(app: EggApplicationCore, url: HttpClientRequestURL, options?: HttpClientOptions): Promise<egg10.HttpClientResponse<T>>;
|
|
19
|
-
//#endregion
|
|
1
|
+
import "../../config.default-D8v08Vox.js";
|
|
2
|
+
import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse, safeCurlForApplication } from "../../safe_curl-mqZZv_YQ.js";
|
|
20
3
|
export { HttpClientOptions, HttpClientRequestURL, HttpClientResponse, safeCurlForApplication };
|
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
const SSRF_HTTPCLIENT = Symbol("SSRF_HTTPCLIENT");
|
|
3
|
-
/**
|
|
4
|
-
* safe curl with ssrf protection
|
|
5
|
-
*/
|
|
6
|
-
async function safeCurlForApplication(app, url, options = {}) {
|
|
7
|
-
const ssrfConfig = app.config.security.ssrf;
|
|
8
|
-
if (ssrfConfig?.checkAddress) options.checkAddress = ssrfConfig.checkAddress;
|
|
9
|
-
else app.logger.warn("[@eggjs/security] please configure `config.security.ssrf` first");
|
|
10
|
-
if (ssrfConfig?.checkAddress) {
|
|
11
|
-
let httpClient = app[SSRF_HTTPCLIENT];
|
|
12
|
-
if (!httpClient) httpClient = app[SSRF_HTTPCLIENT] = app.createHttpClient({ checkAddress: ssrfConfig.checkAddress });
|
|
13
|
-
return await httpClient.request(url, options);
|
|
14
|
-
}
|
|
15
|
-
return await app.curl(url, options);
|
|
16
|
-
}
|
|
1
|
+
import { safeCurlForApplication } from "../../safe_curl-UlViaxoF.js";
|
|
17
2
|
|
|
18
|
-
//#endregion
|
|
19
3
|
export { safeCurlForApplication };
|
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* remote command execution
|
|
4
|
-
*/
|
|
5
|
-
const BASIC_ALPHABETS = new Set("abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ.-_".split(""));
|
|
6
|
-
function cliFilter(text) {
|
|
7
|
-
const str = "" + text;
|
|
8
|
-
let res = "";
|
|
9
|
-
let ascii;
|
|
10
|
-
for (let index = 0; index < str.length; index++) {
|
|
11
|
-
ascii = str[index];
|
|
12
|
-
if (BASIC_ALPHABETS.has(ascii)) res += ascii;
|
|
13
|
-
}
|
|
14
|
-
return res;
|
|
15
|
-
}
|
|
1
|
+
import { cliFilter } from "../../cliFilter-7BSD8Nc_.js";
|
|
16
2
|
|
|
17
|
-
//#endregion
|
|
18
3
|
export { cliFilter as default };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import escapeHTML from "escape-
|
|
1
|
+
import { escapeHTML } from "../../escape-Dex_Pk9e.js";
|
|
2
2
|
export { escapeHTML as default };
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
function escapeShellArg(text) {
|
|
3
|
-
return "'" + ("" + text).replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "'";
|
|
4
|
-
}
|
|
1
|
+
import { escapeShellArg } from "../../escapeShellArg-C0v1ZeCl.js";
|
|
5
2
|
|
|
6
|
-
//#endregion
|
|
7
3
|
export { escapeShellArg as default };
|
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
const BASIC_ALPHABETS = new Set("#&;`|*?~<>^()[]{}$;'\",\nÿ".split(""));
|
|
3
|
-
function escapeShellCmd(text) {
|
|
4
|
-
const str = "" + text;
|
|
5
|
-
let res = "";
|
|
6
|
-
let ascii;
|
|
7
|
-
for (let index = 0; index < str.length; index++) {
|
|
8
|
-
ascii = str[index];
|
|
9
|
-
if (!BASIC_ALPHABETS.has(ascii)) res += ascii;
|
|
10
|
-
}
|
|
11
|
-
return res;
|
|
12
|
-
}
|
|
1
|
+
import { escapeShellCmd } from "../../escapeShellCmd-CkAdyhtO.js";
|
|
13
2
|
|
|
14
|
-
//#endregion
|
|
15
3
|
export { escapeShellCmd as default };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import cliFilter from "
|
|
2
|
-
import escapeShellArg from "
|
|
3
|
-
import escapeShellCmd from "
|
|
4
|
-
import shtml from "
|
|
5
|
-
import escapeJavaScript from "
|
|
6
|
-
import jsonEscape from "
|
|
7
|
-
import pathFilter from "
|
|
8
|
-
import surl from "
|
|
9
|
-
import escapeHTML from "
|
|
1
|
+
import { cliFilter } from "../../cliFilter-DKZxCxSe.js";
|
|
2
|
+
import { escapeShellArg } from "../../escapeShellArg-BnzDicAC.js";
|
|
3
|
+
import { escapeShellCmd } from "../../escapeShellCmd-DQZZIHde.js";
|
|
4
|
+
import { shtml } from "../../shtml-CAquTzgV.js";
|
|
5
|
+
import { escapeJavaScript } from "../../sjs-QZIJYS71.js";
|
|
6
|
+
import { jsonEscape } from "../../sjson-O-vKJPws.js";
|
|
7
|
+
import { pathFilter } from "../../spath-DseDPHxf.js";
|
|
8
|
+
import { surl } from "../../surl-JV70X_RZ.js";
|
|
9
|
+
import { escapeHTML } from "../../escape-Dex_Pk9e.js";
|
|
10
10
|
|
|
11
11
|
//#region src/lib/helper/index.d.ts
|
|
12
12
|
declare const _default: {
|