@eggjs/security 5.0.0-beta.20 → 5.0.0-beta.21
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.d.ts +5 -9
- package/dist/agent.js +11 -15
- package/dist/app/extend/agent.d.ts +4 -9
- package/dist/app/extend/agent.js +8 -12
- package/dist/app/extend/application.d.ts +8 -4
- package/dist/app/extend/application.js +32 -4
- package/dist/app/extend/context.d.ts +58 -4
- package/dist/app/extend/context.js +244 -4
- package/dist/app/extend/helper.d.ts +10 -22
- package/dist/app/extend/helper.js +5 -17
- package/dist/app/extend/response.d.ts +35 -4
- package/dist/app/extend/response.js +83 -5
- package/dist/app/middleware/securities.d.ts +4 -8
- package/dist/app/middleware/securities.js +52 -49
- package/dist/app.d.ts +5 -9
- package/dist/app.js +26 -24
- package/dist/config/config.default.d.ts +867 -2
- package/dist/config/config.default.js +366 -3
- package/dist/config/config.local.d.ts +2 -5
- package/dist/config/config.local.js +8 -5
- package/dist/index.d.ts +4 -5
- package/dist/index.js +5 -8
- package/dist/lib/extend/safe_curl.d.ts +16 -3
- package/dist/lib/extend/safe_curl.js +25 -3
- package/dist/lib/helper/cliFilter.d.ts +4 -2
- package/dist/lib/helper/cliFilter.js +17 -3
- package/dist/lib/helper/escape.d.ts +2 -2
- package/dist/lib/helper/escape.js +3 -3
- package/dist/lib/helper/escapeShellArg.d.ts +1 -2
- package/dist/lib/helper/escapeShellArg.js +5 -3
- package/dist/lib/helper/escapeShellCmd.d.ts +1 -2
- package/dist/lib/helper/escapeShellCmd.js +14 -3
- package/dist/lib/helper/index.d.ts +19 -22
- package/dist/lib/helper/index.js +21 -13
- package/dist/lib/helper/shtml.d.ts +2 -2
- package/dist/lib/helper/shtml.js +69 -4
- package/dist/lib/helper/sjs.d.ts +4 -2
- package/dist/lib/helper/sjs.js +49 -3
- package/dist/lib/helper/sjson.d.ts +1 -2
- package/dist/lib/helper/sjson.js +39 -4
- package/dist/lib/helper/spath.d.ts +5 -2
- package/dist/lib/helper/spath.js +25 -3
- package/dist/lib/helper/surl.d.ts +2 -2
- package/dist/lib/helper/surl.js +30 -3
- package/dist/lib/middlewares/csp.d.ts +3 -6
- package/dist/lib/middlewares/csp.js +57 -4
- package/dist/lib/middlewares/csrf.d.ts +3 -6
- package/dist/lib/middlewares/csrf.js +37 -4
- package/dist/lib/middlewares/dta.d.ts +2 -5
- package/dist/lib/middlewares/dta.js +12 -4
- package/dist/lib/middlewares/hsts.d.ts +3 -6
- package/dist/lib/middlewares/hsts.js +21 -4
- package/dist/lib/middlewares/index.d.ts +11 -16
- package/dist/lib/middlewares/index.js +23 -14
- package/dist/lib/middlewares/methodnoallow.d.ts +2 -5
- package/dist/lib/middlewares/methodnoallow.js +20 -3
- package/dist/lib/middlewares/noopen.d.ts +3 -6
- package/dist/lib/middlewares/noopen.js +15 -4
- package/dist/lib/middlewares/nosniff.d.ts +3 -6
- package/dist/lib/middlewares/nosniff.js +28 -4
- package/dist/lib/middlewares/referrerPolicy.d.ts +3 -6
- package/dist/lib/middlewares/referrerPolicy.js +35 -4
- package/dist/lib/middlewares/xframe.d.ts +3 -6
- package/dist/lib/middlewares/xframe.js +17 -4
- package/dist/lib/middlewares/xssProtection.d.ts +3 -6
- package/dist/lib/middlewares/xssProtection.js +14 -4
- package/dist/lib/utils.d.ts +12 -17
- package/dist/lib/utils.js +192 -3
- package/dist/types.d.ts +36 -3
- package/dist/types.js +2 -3
- package/package.json +6 -6
- package/dist/application-COC0mYEe.js +0 -32
- package/dist/application-n5bk2L_z.d.ts +0 -12
- package/dist/cliFilter-7BSD8Nc_.js +0 -18
- package/dist/cliFilter-DKZxCxSe.d.ts +0 -7
- package/dist/config.default-AcwQOAG0.js +0 -166
- package/dist/config.default-D8v08Vox.d.ts +0 -870
- package/dist/context-C-N1IY85.d.ts +0 -95
- package/dist/context-e-QJTKfq.js +0 -191
- package/dist/csp-BW5AJd_f.js +0 -46
- package/dist/csrf-9aSLHiby.js +0 -33
- package/dist/dta-DVAKEpJ3.js +0 -13
- package/dist/escape-Dex_Pk9e.d.ts +0 -2
- package/dist/escape-p8-cW8c_.js +0 -7
- package/dist/escapeShellArg-BnzDicAC.d.ts +0 -4
- package/dist/escapeShellArg-C0v1ZeCl.js +0 -7
- package/dist/escapeShellCmd-CkAdyhtO.js +0 -15
- package/dist/escapeShellCmd-DQZZIHde.d.ts +0 -4
- package/dist/helper-DylzfQ_5.js +0 -25
- package/dist/hsts-CWMKNTEh.js +0 -19
- package/dist/methodnoallow-BAZONArS.js +0 -15
- package/dist/middlewares-CkQjv8t0.js +0 -27
- package/dist/noopen-C3jUBwoH.js +0 -17
- package/dist/nosniff-CcLkhX2I.js +0 -27
- package/dist/referrerPolicy-D4Uafq6c.js +0 -31
- package/dist/response-BFnHAJrV.js +0 -69
- package/dist/safe_curl-UlViaxoF.js +0 -19
- package/dist/safe_curl-mqZZv_YQ.d.ts +0 -20
- package/dist/shtml-CAquTzgV.d.ts +0 -6
- package/dist/shtml-CgF4kOx-.js +0 -53
- package/dist/sjs-Cbmkk5xS.js +0 -36
- package/dist/sjs-QZIJYS71.d.ts +0 -7
- package/dist/sjson-BetFnVR6.js +0 -32
- package/dist/sjson-O-vKJPws.d.ts +0 -4
- package/dist/spath-Bu9sy6Kz.js +0 -16
- package/dist/spath-DseDPHxf.d.ts +0 -7
- package/dist/surl-ClleTea7.js +0 -25
- package/dist/surl-JV70X_RZ.d.ts +0 -6
- package/dist/types-BZR2U30p.d.ts +0 -38
- package/dist/types-DnJpiSJb.js +0 -1
- package/dist/utils-Cajs5P8M.js +0 -127
- package/dist/xframe-q9fEZkVI.js +0 -18
- package/dist/xssProtection-D5QsHX-e.js +0 -17
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
//#region src/lib/extend/safe_curl.ts
|
|
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
|
-
}
|
|
17
|
-
|
|
18
|
-
//#endregion
|
|
19
|
-
export { safeCurlForApplication };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { SSRFCheckAddressFunction } from "./config.default-D8v08Vox.js";
|
|
2
|
-
import * as egg0 from "egg";
|
|
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<egg0.HttpClientResponse<T>>;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { HttpClientOptions, HttpClientRequestURL, HttpClientResponse, safeCurlForApplication };
|
package/dist/shtml-CAquTzgV.d.ts
DELETED
package/dist/shtml-CgF4kOx-.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { getFromUrl, isSafeDomain } from "./utils-Cajs5P8M.js";
|
|
2
|
-
import xss from "xss";
|
|
3
|
-
|
|
4
|
-
//#region src/lib/helper/shtml.ts
|
|
5
|
-
const BUILD_IN_ON_TAG_ATTR = Symbol("buildInOnTagAttr");
|
|
6
|
-
function shtml(val) {
|
|
7
|
-
if (typeof val !== "string") return val;
|
|
8
|
-
const securityOptions = this.ctx.securityOptions;
|
|
9
|
-
const shtmlConfig = {
|
|
10
|
-
...this.app.config.helper.shtml,
|
|
11
|
-
...securityOptions.shtml,
|
|
12
|
-
[BUILD_IN_ON_TAG_ATTR]: void 0
|
|
13
|
-
};
|
|
14
|
-
const domainWhiteList = this.app.config.security.domainWhiteList;
|
|
15
|
-
const app = this.app;
|
|
16
|
-
if (!shtmlConfig[BUILD_IN_ON_TAG_ATTR]) {
|
|
17
|
-
shtmlConfig[BUILD_IN_ON_TAG_ATTR] = (_tag, name, value, isWhiteAttr) => {
|
|
18
|
-
if (isWhiteAttr && (name === "href" || name === "src")) {
|
|
19
|
-
if (!value) return;
|
|
20
|
-
value = String(value);
|
|
21
|
-
if (value[0] === "/" || value[0] === "#") return;
|
|
22
|
-
const hostname = getFromUrl(value, "hostname");
|
|
23
|
-
if (!hostname) return;
|
|
24
|
-
if (!isSafeDomain(hostname, domainWhiteList)) if (shtmlConfig.domainWhiteList && shtmlConfig.domainWhiteList.length > 0) {
|
|
25
|
-
app.deprecate("[@eggjs/security/lib/helper/shtml] `config.helper.shtml.domainWhiteList` has been deprecate. Please use `config.security.domainWhiteList` instead.");
|
|
26
|
-
if (!isSafeDomain(hostname, shtmlConfig.domainWhiteList)) return "";
|
|
27
|
-
} else return "";
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
if (shtmlConfig.onTagAttr) {
|
|
31
|
-
const customOnTagAttrHandler = shtmlConfig.onTagAttr;
|
|
32
|
-
shtmlConfig.onTagAttr = function(tag, name, value, isWhiteAttr) {
|
|
33
|
-
const result = customOnTagAttrHandler.apply(this, [
|
|
34
|
-
tag,
|
|
35
|
-
name,
|
|
36
|
-
value,
|
|
37
|
-
isWhiteAttr
|
|
38
|
-
]);
|
|
39
|
-
if (result !== void 0) return result;
|
|
40
|
-
return shtmlConfig[BUILD_IN_ON_TAG_ATTR].apply(this, [
|
|
41
|
-
tag,
|
|
42
|
-
name,
|
|
43
|
-
value,
|
|
44
|
-
isWhiteAttr
|
|
45
|
-
]);
|
|
46
|
-
};
|
|
47
|
-
} else shtmlConfig.onTagAttr = shtmlConfig[BUILD_IN_ON_TAG_ATTR];
|
|
48
|
-
}
|
|
49
|
-
return xss(val, shtmlConfig);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//#endregion
|
|
53
|
-
export { shtml };
|
package/dist/sjs-Cbmkk5xS.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
//#region src/lib/helper/sjs.ts
|
|
2
|
-
/**
|
|
3
|
-
* Escape JavaScript to \xHH format
|
|
4
|
-
*/
|
|
5
|
-
const MATCH_VULNERABLE_REGEXP = /[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]/;
|
|
6
|
-
const BASIC_ALPHABETS = new Set("abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""));
|
|
7
|
-
const map = {
|
|
8
|
-
" ": "\\t",
|
|
9
|
-
"\n": "\\n",
|
|
10
|
-
"\r": "\\r"
|
|
11
|
-
};
|
|
12
|
-
function escapeJavaScript(text) {
|
|
13
|
-
const str = "" + text;
|
|
14
|
-
const match = MATCH_VULNERABLE_REGEXP.exec(str);
|
|
15
|
-
if (!match) return str;
|
|
16
|
-
let res = "";
|
|
17
|
-
let index = 0;
|
|
18
|
-
let lastIndex = 0;
|
|
19
|
-
let ascii;
|
|
20
|
-
for (index = match.index; index < str.length; index++) {
|
|
21
|
-
ascii = str[index];
|
|
22
|
-
if (BASIC_ALPHABETS.has(ascii)) continue;
|
|
23
|
-
else if (map[ascii] === void 0) {
|
|
24
|
-
const code = ascii.charCodeAt(0);
|
|
25
|
-
if (code > 127) continue;
|
|
26
|
-
else map[ascii] = "\\x" + code.toString(16);
|
|
27
|
-
}
|
|
28
|
-
if (lastIndex !== index) res += str.substring(lastIndex, index);
|
|
29
|
-
lastIndex = index + 1;
|
|
30
|
-
res += map[ascii];
|
|
31
|
-
}
|
|
32
|
-
return lastIndex !== index ? res + str.substring(lastIndex, index) : res;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
export { escapeJavaScript };
|
package/dist/sjs-QZIJYS71.d.ts
DELETED
package/dist/sjson-BetFnVR6.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { escapeJavaScript } from "./sjs-Cbmkk5xS.js";
|
|
2
|
-
|
|
3
|
-
//#region src/lib/helper/sjson.ts
|
|
4
|
-
/**
|
|
5
|
-
* escape json
|
|
6
|
-
* for output json in script
|
|
7
|
-
*/
|
|
8
|
-
function sanitizeKey(obj) {
|
|
9
|
-
if (typeof obj !== "object") return obj;
|
|
10
|
-
if (Array.isArray(obj)) return obj;
|
|
11
|
-
if (obj === null) return null;
|
|
12
|
-
if (typeof obj === "boolean") return obj;
|
|
13
|
-
if (typeof obj === "number") return obj;
|
|
14
|
-
if (Buffer.isBuffer(obj)) return obj.toString();
|
|
15
|
-
for (const k in obj) {
|
|
16
|
-
const escapedK = escapeJavaScript(k);
|
|
17
|
-
if (escapedK !== k) {
|
|
18
|
-
obj[escapedK] = sanitizeKey(obj[k]);
|
|
19
|
-
obj[k] = void 0;
|
|
20
|
-
} else obj[k] = sanitizeKey(obj[k]);
|
|
21
|
-
}
|
|
22
|
-
return obj;
|
|
23
|
-
}
|
|
24
|
-
function jsonEscape(obj) {
|
|
25
|
-
return JSON.stringify(sanitizeKey(obj), (_k, v) => {
|
|
26
|
-
if (typeof v === "string") return escapeJavaScript(v);
|
|
27
|
-
return v;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
|
-
export { jsonEscape };
|
package/dist/sjson-O-vKJPws.d.ts
DELETED
package/dist/spath-Bu9sy6Kz.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/lib/helper/spath.ts
|
|
2
|
-
function pathFilter(path) {
|
|
3
|
-
if (typeof path !== "string") return path;
|
|
4
|
-
const pathSource = path;
|
|
5
|
-
while (path.indexOf("%") !== -1) try {
|
|
6
|
-
path = decodeURIComponent(path);
|
|
7
|
-
} catch {
|
|
8
|
-
if (process.env.NODE_ENV !== "production") this.ctx.coreLogger.warn("[@eggjs/security/lib/helper/spath] : decode file path %j failed.", path);
|
|
9
|
-
break;
|
|
10
|
-
}
|
|
11
|
-
if (path.indexOf("..") !== -1 || path[0] === "/") return null;
|
|
12
|
-
return pathSource;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
export { pathFilter };
|
package/dist/spath-DseDPHxf.d.ts
DELETED
package/dist/surl-ClleTea7.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
//#region src/lib/helper/surl.ts
|
|
2
|
-
const escapeMap = {
|
|
3
|
-
"\"": """,
|
|
4
|
-
"<": "<",
|
|
5
|
-
">": ">",
|
|
6
|
-
"'": "'"
|
|
7
|
-
};
|
|
8
|
-
function surl(val) {
|
|
9
|
-
const protocolWhiteListSet = this.app.config.security.__protocolWhiteListSet;
|
|
10
|
-
if (typeof val !== "string") return val;
|
|
11
|
-
if (val[0] !== "/") {
|
|
12
|
-
const arr = val.split("://", 2);
|
|
13
|
-
const protocol = arr.length > 1 ? arr[0].toLowerCase() : "";
|
|
14
|
-
if (protocol === "" || !protocolWhiteListSet.has(protocol)) {
|
|
15
|
-
if (this.app.config.env === "local") this.ctx.coreLogger.warn("[@eggjs/security/surl] url: %j, protocol: %j, protocol is empty or not in white list, convert to empty string", val, protocol);
|
|
16
|
-
return "";
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return val.replace(/["'<>]/g, (ch) => {
|
|
20
|
-
return escapeMap[ch];
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
//#endregion
|
|
25
|
-
export { surl };
|
package/dist/surl-JV70X_RZ.d.ts
DELETED
package/dist/types-BZR2U30p.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { SecurityConfig, SecurityHelperConfig } from "./config.default-D8v08Vox.js";
|
|
2
|
-
import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "./safe_curl-mqZZv_YQ.js";
|
|
3
|
-
|
|
4
|
-
//#region src/types.d.ts
|
|
5
|
-
declare module 'egg' {
|
|
6
|
-
interface EggAppConfig {
|
|
7
|
-
/**
|
|
8
|
-
* security options
|
|
9
|
-
* @member Config#security
|
|
10
|
-
*/
|
|
11
|
-
security: SecurityConfig;
|
|
12
|
-
helper: SecurityHelperConfig;
|
|
13
|
-
}
|
|
14
|
-
interface Agent {
|
|
15
|
-
safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
|
|
16
|
-
}
|
|
17
|
-
interface Application {
|
|
18
|
-
injectCsrf(html: string): string;
|
|
19
|
-
injectNonce(html: string): string;
|
|
20
|
-
injectHijackingDefense(html: string): string;
|
|
21
|
-
safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
|
|
22
|
-
}
|
|
23
|
-
interface Context {
|
|
24
|
-
get securityOptions(): Partial<SecurityConfig & SecurityHelperConfig>;
|
|
25
|
-
isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
|
|
26
|
-
get nonce(): string;
|
|
27
|
-
get csrf(): string;
|
|
28
|
-
ensureCsrfSecret(rotate?: boolean): void;
|
|
29
|
-
rotateCsrfSecret(): void;
|
|
30
|
-
assertCsrf(): void;
|
|
31
|
-
safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
|
|
32
|
-
unsafeRedirect(url: string, alt?: string): void;
|
|
33
|
-
}
|
|
34
|
-
interface Response {
|
|
35
|
-
unsafeRedirect(url: string, alt?: string): void;
|
|
36
|
-
redirect(url: string, alt?: string): void;
|
|
37
|
-
}
|
|
38
|
-
}
|
package/dist/types-DnJpiSJb.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/utils-Cajs5P8M.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { normalize } from "node:path";
|
|
2
|
-
import matcher from "matcher";
|
|
3
|
-
import IP from "@eggjs/ip";
|
|
4
|
-
|
|
5
|
-
//#region src/lib/utils.ts
|
|
6
|
-
/**
|
|
7
|
-
* Check whether a domain is in the safe domain white list or not.
|
|
8
|
-
* @param {String} domain The inputted domain.
|
|
9
|
-
* @param {Array<string>} whiteList The white list for domain.
|
|
10
|
-
* @return {Boolean} If the `domain` is in the white list, return true; otherwise false.
|
|
11
|
-
*/
|
|
12
|
-
function isSafeDomain(domain, whiteList) {
|
|
13
|
-
if (typeof domain !== "string") return false;
|
|
14
|
-
domain = domain.toLowerCase();
|
|
15
|
-
const hostname = "." + domain;
|
|
16
|
-
return whiteList.some((rule) => {
|
|
17
|
-
if (rule.includes("*")) return matcher.isMatch(domain, rule);
|
|
18
|
-
if (domain === rule) return true;
|
|
19
|
-
if (!rule.startsWith(".")) rule = `.${rule}`;
|
|
20
|
-
return hostname.endsWith(rule);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
function isSafePath(path, ctx) {
|
|
24
|
-
path = "." + path;
|
|
25
|
-
if (path.includes("%")) try {
|
|
26
|
-
path = decodeURIComponent(path);
|
|
27
|
-
} catch {
|
|
28
|
-
if (ctx.app.config.env === "local" || ctx.app.config.env === "unittest") ctx.coreLogger.warn("[@eggjs/security: dta global block] : decode file path %j failed.", path);
|
|
29
|
-
}
|
|
30
|
-
const normalizePath = normalize(path);
|
|
31
|
-
return !(normalizePath.startsWith("../") || normalizePath.startsWith("..\\"));
|
|
32
|
-
}
|
|
33
|
-
function checkIfIgnore(opts, ctx) {
|
|
34
|
-
if (!opts.enable) return true;
|
|
35
|
-
return !opts.matching?.(ctx);
|
|
36
|
-
}
|
|
37
|
-
const IP_RE = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
|
|
38
|
-
const topDomains = {};
|
|
39
|
-
[
|
|
40
|
-
".net.cn",
|
|
41
|
-
".gov.cn",
|
|
42
|
-
".org.cn",
|
|
43
|
-
".com.cn"
|
|
44
|
-
].forEach((item) => {
|
|
45
|
-
topDomains[item] = 2 - item.split(".").length;
|
|
46
|
-
});
|
|
47
|
-
function getCookieDomain(hostname) {
|
|
48
|
-
if (IP_RE.test(hostname)) return hostname;
|
|
49
|
-
const splits = hostname.split(".");
|
|
50
|
-
let index = -2;
|
|
51
|
-
if (splits.length >= 4 && splits[splits.length - 3] === "test") index = -3;
|
|
52
|
-
let domain = getDomain(splits, index);
|
|
53
|
-
if (topDomains[domain]) domain = getDomain(splits, index + topDomains[domain]);
|
|
54
|
-
return domain;
|
|
55
|
-
}
|
|
56
|
-
function getDomain(splits, index) {
|
|
57
|
-
return "." + splits.slice(index).join(".");
|
|
58
|
-
}
|
|
59
|
-
function merge(origin, opts) {
|
|
60
|
-
if (!opts) return origin;
|
|
61
|
-
const res = {};
|
|
62
|
-
const originKeys = Object.keys(origin);
|
|
63
|
-
for (let i = 0; i < originKeys.length; i++) {
|
|
64
|
-
const key = originKeys[i];
|
|
65
|
-
res[key] = origin[key];
|
|
66
|
-
}
|
|
67
|
-
const keys = Object.keys(opts);
|
|
68
|
-
for (let i = 0; i < keys.length; i++) {
|
|
69
|
-
const key = keys[i];
|
|
70
|
-
res[key] = opts[key];
|
|
71
|
-
}
|
|
72
|
-
return res;
|
|
73
|
-
}
|
|
74
|
-
function preprocessConfig(config) {
|
|
75
|
-
const ssrf = config.ssrf;
|
|
76
|
-
if (ssrf && ssrf.ipBlackList && !ssrf.checkAddress) {
|
|
77
|
-
const blackList = ssrf.ipBlackList.map(getContains);
|
|
78
|
-
const exceptionList = (ssrf.ipExceptionList || []).map(getContains);
|
|
79
|
-
const hostnameExceptionList = ssrf.hostnameExceptionList;
|
|
80
|
-
ssrf.checkAddress = (ipAddresses, _family, hostname) => {
|
|
81
|
-
if (hostname && hostnameExceptionList) {
|
|
82
|
-
if (hostnameExceptionList.includes(hostname)) return true;
|
|
83
|
-
}
|
|
84
|
-
if (!Array.isArray(ipAddresses)) ipAddresses = [ipAddresses];
|
|
85
|
-
for (const ipAddress of ipAddresses) {
|
|
86
|
-
let address;
|
|
87
|
-
if (typeof ipAddress === "string") address = ipAddress;
|
|
88
|
-
else {
|
|
89
|
-
if (ipAddress.family === 6) continue;
|
|
90
|
-
address = ipAddress.address;
|
|
91
|
-
}
|
|
92
|
-
for (const exception of exceptionList) if (exception(address)) return true;
|
|
93
|
-
for (const contains of blackList) if (contains(address)) return false;
|
|
94
|
-
}
|
|
95
|
-
return true;
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
config.domainWhiteList = config.domainWhiteList || [];
|
|
99
|
-
config.domainWhiteList = config.domainWhiteList.map((domain) => domain.toLowerCase());
|
|
100
|
-
config.protocolWhiteList = config.protocolWhiteList || [];
|
|
101
|
-
config.protocolWhiteList = config.protocolWhiteList.map((protocol) => protocol.toLowerCase());
|
|
102
|
-
if (config.csrf && config.csrf.refererWhiteList) config.csrf.refererWhiteList = config.csrf.refererWhiteList.map((ref) => ref.toLowerCase());
|
|
103
|
-
const protocolWhiteListSet = new Set(config.protocolWhiteList);
|
|
104
|
-
protocolWhiteListSet.add("http");
|
|
105
|
-
protocolWhiteListSet.add("https");
|
|
106
|
-
protocolWhiteListSet.add("file");
|
|
107
|
-
protocolWhiteListSet.add("data");
|
|
108
|
-
Object.defineProperty(config, "__protocolWhiteListSet", {
|
|
109
|
-
value: protocolWhiteListSet,
|
|
110
|
-
enumerable: false
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
function getFromUrl(url, prop) {
|
|
114
|
-
try {
|
|
115
|
-
const parsed = new URL(url);
|
|
116
|
-
return Reflect.get(parsed, prop);
|
|
117
|
-
} catch {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
function getContains(ip) {
|
|
122
|
-
if (IP.isV4Format(ip) || IP.isV6Format(ip)) return (address) => address === ip;
|
|
123
|
-
return IP.cidrSubnet(ip).contains;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
//#endregion
|
|
127
|
-
export { checkIfIgnore, getCookieDomain, getFromUrl, isSafeDomain, isSafePath, merge, preprocessConfig };
|
package/dist/xframe-q9fEZkVI.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { checkIfIgnore } from "./utils-Cajs5P8M.js";
|
|
2
|
-
|
|
3
|
-
//#region src/lib/middlewares/xframe.ts
|
|
4
|
-
var xframe_default = (options) => {
|
|
5
|
-
return async function xframe(ctx, next) {
|
|
6
|
-
await next();
|
|
7
|
-
const opts = {
|
|
8
|
-
...options,
|
|
9
|
-
...ctx.securityOptions.xframe
|
|
10
|
-
};
|
|
11
|
-
if (checkIfIgnore(opts, ctx)) return;
|
|
12
|
-
const value = opts.value || "SAMEORIGIN";
|
|
13
|
-
ctx.set("x-frame-options", value);
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
export { xframe_default };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { checkIfIgnore } from "./utils-Cajs5P8M.js";
|
|
2
|
-
|
|
3
|
-
//#region src/lib/middlewares/xssProtection.ts
|
|
4
|
-
var xssProtection_default = (options) => {
|
|
5
|
-
return async function xssProtection(ctx, next) {
|
|
6
|
-
await next();
|
|
7
|
-
const opts = {
|
|
8
|
-
...options,
|
|
9
|
-
...ctx.securityOptions.xssProtection
|
|
10
|
-
};
|
|
11
|
-
if (checkIfIgnore(opts, ctx)) return;
|
|
12
|
-
ctx.set("x-xss-protection", opts.value);
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
//#endregion
|
|
17
|
-
export { xssProtection_default };
|