@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.
Files changed (106) hide show
  1. package/dist/agent.js +1 -1
  2. package/dist/app/extend/agent.d.ts +2 -1
  3. package/dist/app/extend/agent.js +1 -1
  4. package/dist/app/extend/application.d.ts +3 -11
  5. package/dist/app/extend/application.js +2 -30
  6. package/dist/app/extend/context.d.ts +3 -60
  7. package/dist/app/extend/context.js +2 -189
  8. package/dist/app/extend/helper.d.ts +8 -8
  9. package/dist/app/extend/helper.js +11 -1
  10. package/dist/app/extend/response.d.ts +3 -38
  11. package/dist/app/extend/response.js +3 -68
  12. package/dist/app/middleware/securities.js +12 -1
  13. package/dist/app.js +2 -2
  14. package/dist/application-COC0mYEe.js +32 -0
  15. package/dist/application-n5bk2L_z.d.ts +12 -0
  16. package/dist/cliFilter-7BSD8Nc_.js +18 -0
  17. package/dist/cliFilter-DKZxCxSe.d.ts +7 -0
  18. package/dist/config/config.default.d.ts +1 -869
  19. package/dist/config/config.default.js +1 -164
  20. package/dist/config.default-AcwQOAG0.js +166 -0
  21. package/dist/config.default-D8v08Vox.d.ts +870 -0
  22. package/dist/context-C-N1IY85.d.ts +95 -0
  23. package/dist/context-e-QJTKfq.js +191 -0
  24. package/dist/csp-BW5AJd_f.js +46 -0
  25. package/dist/csrf-9aSLHiby.js +33 -0
  26. package/dist/dta-DVAKEpJ3.js +13 -0
  27. package/dist/escape-Dex_Pk9e.d.ts +2 -0
  28. package/dist/escape-p8-cW8c_.js +7 -0
  29. package/dist/escapeShellArg-BnzDicAC.d.ts +4 -0
  30. package/dist/escapeShellArg-C0v1ZeCl.js +7 -0
  31. package/dist/escapeShellCmd-CkAdyhtO.js +15 -0
  32. package/dist/escapeShellCmd-DQZZIHde.d.ts +4 -0
  33. package/dist/helper-DylzfQ_5.js +25 -0
  34. package/dist/hsts-CWMKNTEh.js +19 -0
  35. package/dist/index.d.ts +5 -1
  36. package/dist/index.js +6 -3
  37. package/dist/lib/extend/safe_curl.d.ts +2 -19
  38. package/dist/lib/extend/safe_curl.js +1 -17
  39. package/dist/lib/helper/cliFilter.d.ts +1 -6
  40. package/dist/lib/helper/cliFilter.js +1 -16
  41. package/dist/lib/helper/escape.d.ts +1 -1
  42. package/dist/lib/helper/escape.js +1 -5
  43. package/dist/lib/helper/escapeShellArg.d.ts +1 -3
  44. package/dist/lib/helper/escapeShellArg.js +1 -5
  45. package/dist/lib/helper/escapeShellCmd.d.ts +1 -3
  46. package/dist/lib/helper/escapeShellCmd.js +1 -13
  47. package/dist/lib/helper/index.d.ts +9 -9
  48. package/dist/lib/helper/index.js +11 -23
  49. package/dist/lib/helper/shtml.d.ts +1 -5
  50. package/dist/lib/helper/shtml.js +2 -51
  51. package/dist/lib/helper/sjs.d.ts +1 -6
  52. package/dist/lib/helper/sjs.js +1 -34
  53. package/dist/lib/helper/sjson.d.ts +1 -3
  54. package/dist/lib/helper/sjson.js +2 -30
  55. package/dist/lib/helper/spath.d.ts +1 -6
  56. package/dist/lib/helper/spath.js +1 -14
  57. package/dist/lib/helper/surl.d.ts +1 -5
  58. package/dist/lib/helper/surl.js +1 -23
  59. package/dist/lib/middlewares/csp.d.ts +1 -1
  60. package/dist/lib/middlewares/csp.js +2 -44
  61. package/dist/lib/middlewares/csrf.d.ts +1 -1
  62. package/dist/lib/middlewares/csrf.js +2 -31
  63. package/dist/lib/middlewares/dta.js +2 -11
  64. package/dist/lib/middlewares/hsts.d.ts +1 -1
  65. package/dist/lib/middlewares/hsts.js +2 -17
  66. package/dist/lib/middlewares/index.d.ts +12 -12
  67. package/dist/lib/middlewares/index.js +12 -25
  68. package/dist/lib/middlewares/methodnoallow.js +1 -13
  69. package/dist/lib/middlewares/noopen.d.ts +1 -1
  70. package/dist/lib/middlewares/noopen.js +2 -15
  71. package/dist/lib/middlewares/nosniff.d.ts +1 -1
  72. package/dist/lib/middlewares/nosniff.js +2 -25
  73. package/dist/lib/middlewares/referrerPolicy.d.ts +1 -1
  74. package/dist/lib/middlewares/referrerPolicy.js +2 -29
  75. package/dist/lib/middlewares/xframe.d.ts +1 -1
  76. package/dist/lib/middlewares/xframe.js +2 -16
  77. package/dist/lib/middlewares/xssProtection.d.ts +1 -1
  78. package/dist/lib/middlewares/xssProtection.js +2 -15
  79. package/dist/lib/utils.d.ts +1 -1
  80. package/dist/lib/utils.js +1 -125
  81. package/dist/methodnoallow-BAZONArS.js +15 -0
  82. package/dist/middlewares-CkQjv8t0.js +27 -0
  83. package/dist/noopen-C3jUBwoH.js +17 -0
  84. package/dist/nosniff-CcLkhX2I.js +27 -0
  85. package/dist/referrerPolicy-D4Uafq6c.js +31 -0
  86. package/dist/response-BFnHAJrV.js +69 -0
  87. package/dist/safe_curl-UlViaxoF.js +19 -0
  88. package/dist/safe_curl-mqZZv_YQ.d.ts +20 -0
  89. package/dist/shtml-CAquTzgV.d.ts +6 -0
  90. package/dist/shtml-CgF4kOx-.js +53 -0
  91. package/dist/sjs-Cbmkk5xS.js +36 -0
  92. package/dist/sjs-QZIJYS71.d.ts +7 -0
  93. package/dist/sjson-BetFnVR6.js +32 -0
  94. package/dist/sjson-O-vKJPws.d.ts +4 -0
  95. package/dist/spath-Bu9sy6Kz.js +16 -0
  96. package/dist/spath-DseDPHxf.d.ts +7 -0
  97. package/dist/surl-ClleTea7.js +25 -0
  98. package/dist/surl-JV70X_RZ.d.ts +6 -0
  99. package/dist/types-BZR2U30p.d.ts +38 -0
  100. package/dist/types-DnJpiSJb.js +1 -0
  101. package/dist/types.d.ts +3 -38
  102. package/dist/types.js +2 -0
  103. package/dist/utils-Cajs5P8M.js +127 -0
  104. package/dist/xframe-q9fEZkVI.js +18 -0
  105. package/dist/xssProtection-D5QsHX-e.js +17 -0
  106. package/package.json +5 -5
package/dist/types.d.ts CHANGED
@@ -1,38 +1,3 @@
1
- import { SecurityConfig, SecurityHelperConfig } from "./config/config.default.js";
2
- import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "./lib/extend/safe_curl.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
- }
1
+ import "./config.default-D8v08Vox.js";
2
+ import "./safe_curl-mqZZv_YQ.js";
3
+ import "./types-BZR2U30p.js";
package/dist/types.js CHANGED
@@ -1 +1,3 @@
1
+ import "./types-DnJpiSJb.js";
2
+
1
3
  export { };
@@ -0,0 +1,127 @@
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 };
@@ -0,0 +1,18 @@
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 };
@@ -0,0 +1,17 @@
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/security",
3
- "version": "5.0.0-beta.19",
3
+ "version": "5.0.0-beta.20",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -84,7 +84,7 @@
84
84
  "zod": "^3.24.1"
85
85
  },
86
86
  "peerDependencies": {
87
- "egg": "4.1.0-beta.19"
87
+ "egg": "4.1.0-beta.20"
88
88
  },
89
89
  "devDependencies": {
90
90
  "@types/escape-html": "^1.0.4",
@@ -100,9 +100,9 @@
100
100
  "tsdown": "^0.15.4",
101
101
  "typescript": "^5.9.3",
102
102
  "vitest": "4.0.0-beta.16",
103
- "@eggjs/mock": "7.0.0-beta.19",
104
- "@eggjs/supertest": "9.0.0-beta.19",
105
- "@eggjs/tsconfig": "3.1.0-beta.19"
103
+ "@eggjs/mock": "7.0.0-beta.20",
104
+ "@eggjs/tsconfig": "3.1.0-beta.20",
105
+ "@eggjs/supertest": "9.0.0-beta.20"
106
106
  },
107
107
  "files": [
108
108
  "dist"