@eggjs/security 4.0.1 → 5.0.0-beta.17

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 (252) hide show
  1. package/README.md +46 -66
  2. package/README.zh-CN.md +56 -68
  3. package/dist/agent.d.ts +10 -0
  4. package/dist/agent.js +15 -0
  5. package/dist/app/extend/agent.d.ts +9 -0
  6. package/dist/app/extend/agent.js +12 -0
  7. package/dist/app/extend/application.d.ts +12 -0
  8. package/dist/app/extend/application.js +32 -0
  9. package/dist/app/extend/context.d.ts +61 -0
  10. package/dist/app/extend/context.js +191 -0
  11. package/dist/app/extend/helper.d.ts +24 -0
  12. package/dist/app/extend/helper.js +7 -0
  13. package/dist/app/extend/response.d.ts +39 -0
  14. package/dist/app/extend/response.js +70 -0
  15. package/dist/app/middleware/securities.d.ts +8 -0
  16. package/dist/app/middleware/securities.js +39 -0
  17. package/dist/app.d.ts +10 -0
  18. package/dist/app.js +24 -0
  19. package/dist/config/config.default.d.ts +870 -0
  20. package/dist/config/config.default.js +166 -0
  21. package/dist/config/config.local.d.ts +6 -0
  22. package/dist/config/config.local.js +5 -0
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +5 -0
  25. package/dist/lib/extend/safe_curl.d.ts +20 -0
  26. package/dist/lib/extend/safe_curl.js +19 -0
  27. package/dist/lib/helper/cliFilter.d.ts +7 -0
  28. package/dist/lib/helper/cliFilter.js +18 -0
  29. package/dist/lib/helper/escape.d.ts +2 -0
  30. package/dist/lib/helper/escape.js +7 -0
  31. package/dist/lib/helper/escapeShellArg.d.ts +4 -0
  32. package/dist/lib/helper/escapeShellArg.js +7 -0
  33. package/dist/lib/helper/escapeShellCmd.d.ts +4 -0
  34. package/dist/lib/helper/escapeShellCmd.js +15 -0
  35. package/dist/lib/helper/index.d.ts +24 -0
  36. package/dist/lib/helper/index.js +25 -0
  37. package/dist/lib/helper/shtml.d.ts +6 -0
  38. package/dist/lib/helper/shtml.js +53 -0
  39. package/dist/lib/helper/sjs.d.ts +7 -0
  40. package/dist/lib/helper/sjs.js +36 -0
  41. package/dist/lib/helper/sjson.d.ts +4 -0
  42. package/dist/lib/helper/sjson.js +32 -0
  43. package/dist/lib/helper/spath.d.ts +7 -0
  44. package/dist/lib/helper/spath.js +16 -0
  45. package/dist/lib/helper/surl.d.ts +6 -0
  46. package/dist/lib/helper/surl.js +25 -0
  47. package/dist/lib/middlewares/csp.d.ts +7 -0
  48. package/dist/lib/middlewares/csp.js +46 -0
  49. package/dist/lib/middlewares/csrf.d.ts +7 -0
  50. package/dist/lib/middlewares/csrf.js +33 -0
  51. package/dist/lib/middlewares/dta.d.ts +6 -0
  52. package/dist/lib/middlewares/dta.js +13 -0
  53. package/dist/lib/middlewares/hsts.d.ts +7 -0
  54. package/dist/lib/middlewares/hsts.js +19 -0
  55. package/dist/lib/middlewares/index.d.ts +18 -0
  56. package/dist/lib/middlewares/index.js +27 -0
  57. package/dist/lib/middlewares/methodnoallow.d.ts +6 -0
  58. package/dist/lib/middlewares/methodnoallow.js +15 -0
  59. package/dist/lib/middlewares/noopen.d.ts +7 -0
  60. package/dist/lib/middlewares/noopen.js +17 -0
  61. package/dist/lib/middlewares/nosniff.d.ts +7 -0
  62. package/dist/lib/middlewares/nosniff.js +27 -0
  63. package/dist/lib/middlewares/referrerPolicy.d.ts +7 -0
  64. package/dist/lib/middlewares/referrerPolicy.js +31 -0
  65. package/dist/lib/middlewares/xframe.d.ts +7 -0
  66. package/dist/lib/middlewares/xframe.js +18 -0
  67. package/dist/lib/middlewares/xssProtection.d.ts +7 -0
  68. package/dist/lib/middlewares/xssProtection.js +17 -0
  69. package/dist/lib/utils.d.ts +24 -0
  70. package/dist/lib/utils.js +127 -0
  71. package/dist/types.d.ts +38 -0
  72. package/dist/types.js +1 -0
  73. package/package.json +75 -71
  74. package/dist/commonjs/agent.d.ts +0 -6
  75. package/dist/commonjs/agent.js +0 -14
  76. package/dist/commonjs/app/extend/agent.d.ts +0 -5
  77. package/dist/commonjs/app/extend/agent.js +0 -11
  78. package/dist/commonjs/app/extend/application.d.ts +0 -16
  79. package/dist/commonjs/app/extend/application.js +0 -35
  80. package/dist/commonjs/app/extend/context.d.ts +0 -68
  81. package/dist/commonjs/app/extend/context.js +0 -283
  82. package/dist/commonjs/app/extend/helper.d.ts +0 -12
  83. package/dist/commonjs/app/extend/helper.js +0 -10
  84. package/dist/commonjs/app/extend/response.d.ts +0 -41
  85. package/dist/commonjs/app/extend/response.js +0 -85
  86. package/dist/commonjs/app/middleware/securities.d.ts +0 -4
  87. package/dist/commonjs/app/middleware/securities.js +0 -55
  88. package/dist/commonjs/app.d.ts +0 -6
  89. package/dist/commonjs/app.js +0 -29
  90. package/dist/commonjs/config/config.default.d.ts +0 -871
  91. package/dist/commonjs/config/config.default.js +0 -357
  92. package/dist/commonjs/config/config.local.d.ts +0 -5
  93. package/dist/commonjs/config/config.local.js +0 -10
  94. package/dist/commonjs/index.d.ts +0 -1
  95. package/dist/commonjs/index.js +0 -4
  96. package/dist/commonjs/lib/extend/safe_curl.d.ts +0 -16
  97. package/dist/commonjs/lib/extend/safe_curl.js +0 -28
  98. package/dist/commonjs/lib/helper/cliFilter.d.ts +0 -4
  99. package/dist/commonjs/lib/helper/cliFilter.js +0 -20
  100. package/dist/commonjs/lib/helper/escape.d.ts +0 -2
  101. package/dist/commonjs/lib/helper/escape.js +0 -8
  102. package/dist/commonjs/lib/helper/escapeShellArg.d.ts +0 -1
  103. package/dist/commonjs/lib/helper/escapeShellArg.js +0 -8
  104. package/dist/commonjs/lib/helper/escapeShellCmd.d.ts +0 -1
  105. package/dist/commonjs/lib/helper/escapeShellCmd.js +0 -17
  106. package/dist/commonjs/lib/helper/index.d.ts +0 -21
  107. package/dist/commonjs/lib/helper/index.js +0 -26
  108. package/dist/commonjs/lib/helper/shtml.d.ts +0 -2
  109. package/dist/commonjs/lib/helper/shtml.js +0 -76
  110. package/dist/commonjs/lib/helper/sjs.d.ts +0 -4
  111. package/dist/commonjs/lib/helper/sjs.js +0 -52
  112. package/dist/commonjs/lib/helper/sjson.d.ts +0 -1
  113. package/dist/commonjs/lib/helper/sjson.js +0 -45
  114. package/dist/commonjs/lib/helper/spath.d.ts +0 -5
  115. package/dist/commonjs/lib/helper/spath.js +0 -28
  116. package/dist/commonjs/lib/helper/surl.d.ts +0 -2
  117. package/dist/commonjs/lib/helper/surl.js +0 -33
  118. package/dist/commonjs/lib/middlewares/csp.d.ts +0 -4
  119. package/dist/commonjs/lib/middlewares/csp.js +0 -68
  120. package/dist/commonjs/lib/middlewares/csrf.d.ts +0 -4
  121. package/dist/commonjs/lib/middlewares/csrf.js +0 -42
  122. package/dist/commonjs/lib/middlewares/dta.d.ts +0 -3
  123. package/dist/commonjs/lib/middlewares/dta.js +0 -14
  124. package/dist/commonjs/lib/middlewares/hsts.d.ts +0 -4
  125. package/dist/commonjs/lib/middlewares/hsts.js +0 -23
  126. package/dist/commonjs/lib/middlewares/index.d.ts +0 -13
  127. package/dist/commonjs/lib/middlewares/index.js +0 -28
  128. package/dist/commonjs/lib/middlewares/methodnoallow.d.ts +0 -3
  129. package/dist/commonjs/lib/middlewares/methodnoallow.js +0 -22
  130. package/dist/commonjs/lib/middlewares/noopen.d.ts +0 -4
  131. package/dist/commonjs/lib/middlewares/noopen.js +0 -17
  132. package/dist/commonjs/lib/middlewares/nosniff.d.ts +0 -4
  133. package/dist/commonjs/lib/middlewares/nosniff.js +0 -30
  134. package/dist/commonjs/lib/middlewares/referrerPolicy.d.ts +0 -4
  135. package/dist/commonjs/lib/middlewares/referrerPolicy.js +0 -36
  136. package/dist/commonjs/lib/middlewares/xframe.d.ts +0 -4
  137. package/dist/commonjs/lib/middlewares/xframe.js +0 -19
  138. package/dist/commonjs/lib/middlewares/xssProtection.d.ts +0 -4
  139. package/dist/commonjs/lib/middlewares/xssProtection.js +0 -16
  140. package/dist/commonjs/lib/utils.d.ts +0 -19
  141. package/dist/commonjs/lib/utils.js +0 -206
  142. package/dist/commonjs/package.json +0 -3
  143. package/dist/commonjs/types.d.ts +0 -10
  144. package/dist/commonjs/types.js +0 -5
  145. package/dist/esm/agent.d.ts +0 -6
  146. package/dist/esm/agent.js +0 -11
  147. package/dist/esm/app/extend/agent.d.ts +0 -5
  148. package/dist/esm/app/extend/agent.js +0 -8
  149. package/dist/esm/app/extend/application.d.ts +0 -16
  150. package/dist/esm/app/extend/application.js +0 -32
  151. package/dist/esm/app/extend/context.d.ts +0 -68
  152. package/dist/esm/app/extend/context.js +0 -244
  153. package/dist/esm/app/extend/helper.d.ts +0 -12
  154. package/dist/esm/app/extend/helper.js +0 -5
  155. package/dist/esm/app/extend/response.d.ts +0 -41
  156. package/dist/esm/app/extend/response.js +0 -82
  157. package/dist/esm/app/middleware/securities.d.ts +0 -4
  158. package/dist/esm/app/middleware/securities.js +0 -50
  159. package/dist/esm/app.d.ts +0 -6
  160. package/dist/esm/app.js +0 -26
  161. package/dist/esm/config/config.default.d.ts +0 -871
  162. package/dist/esm/config/config.default.js +0 -351
  163. package/dist/esm/config/config.local.d.ts +0 -5
  164. package/dist/esm/config/config.local.js +0 -8
  165. package/dist/esm/index.d.ts +0 -1
  166. package/dist/esm/index.js +0 -2
  167. package/dist/esm/lib/extend/safe_curl.d.ts +0 -16
  168. package/dist/esm/lib/extend/safe_curl.js +0 -25
  169. package/dist/esm/lib/helper/cliFilter.d.ts +0 -4
  170. package/dist/esm/lib/helper/cliFilter.js +0 -17
  171. package/dist/esm/lib/helper/escape.d.ts +0 -2
  172. package/dist/esm/lib/helper/escape.js +0 -3
  173. package/dist/esm/lib/helper/escapeShellArg.d.ts +0 -1
  174. package/dist/esm/lib/helper/escapeShellArg.js +0 -5
  175. package/dist/esm/lib/helper/escapeShellCmd.d.ts +0 -1
  176. package/dist/esm/lib/helper/escapeShellCmd.js +0 -14
  177. package/dist/esm/lib/helper/index.d.ts +0 -21
  178. package/dist/esm/lib/helper/index.js +0 -21
  179. package/dist/esm/lib/helper/shtml.d.ts +0 -2
  180. package/dist/esm/lib/helper/shtml.js +0 -70
  181. package/dist/esm/lib/helper/sjs.d.ts +0 -4
  182. package/dist/esm/lib/helper/sjs.js +0 -49
  183. package/dist/esm/lib/helper/sjson.d.ts +0 -1
  184. package/dist/esm/lib/helper/sjson.js +0 -39
  185. package/dist/esm/lib/helper/spath.d.ts +0 -5
  186. package/dist/esm/lib/helper/spath.js +0 -25
  187. package/dist/esm/lib/helper/surl.d.ts +0 -2
  188. package/dist/esm/lib/helper/surl.js +0 -30
  189. package/dist/esm/lib/middlewares/csp.d.ts +0 -4
  190. package/dist/esm/lib/middlewares/csp.js +0 -63
  191. package/dist/esm/lib/middlewares/csrf.d.ts +0 -4
  192. package/dist/esm/lib/middlewares/csrf.js +0 -37
  193. package/dist/esm/lib/middlewares/dta.d.ts +0 -3
  194. package/dist/esm/lib/middlewares/dta.js +0 -12
  195. package/dist/esm/lib/middlewares/hsts.d.ts +0 -4
  196. package/dist/esm/lib/middlewares/hsts.js +0 -21
  197. package/dist/esm/lib/middlewares/index.d.ts +0 -13
  198. package/dist/esm/lib/middlewares/index.js +0 -23
  199. package/dist/esm/lib/middlewares/methodnoallow.d.ts +0 -3
  200. package/dist/esm/lib/middlewares/methodnoallow.js +0 -20
  201. package/dist/esm/lib/middlewares/noopen.d.ts +0 -4
  202. package/dist/esm/lib/middlewares/noopen.js +0 -15
  203. package/dist/esm/lib/middlewares/nosniff.d.ts +0 -4
  204. package/dist/esm/lib/middlewares/nosniff.js +0 -28
  205. package/dist/esm/lib/middlewares/referrerPolicy.d.ts +0 -4
  206. package/dist/esm/lib/middlewares/referrerPolicy.js +0 -34
  207. package/dist/esm/lib/middlewares/xframe.d.ts +0 -4
  208. package/dist/esm/lib/middlewares/xframe.js +0 -17
  209. package/dist/esm/lib/middlewares/xssProtection.d.ts +0 -4
  210. package/dist/esm/lib/middlewares/xssProtection.js +0 -14
  211. package/dist/esm/lib/utils.d.ts +0 -19
  212. package/dist/esm/lib/utils.js +0 -194
  213. package/dist/esm/package.json +0 -3
  214. package/dist/esm/types.d.ts +0 -10
  215. package/dist/esm/types.js +0 -3
  216. package/dist/package.json +0 -4
  217. package/src/agent.ts +0 -14
  218. package/src/app/extend/agent.ts +0 -14
  219. package/src/app/extend/application.ts +0 -51
  220. package/src/app/extend/context.ts +0 -285
  221. package/src/app/extend/helper.ts +0 -5
  222. package/src/app/extend/response.ts +0 -95
  223. package/src/app/middleware/securities.ts +0 -63
  224. package/src/app.ts +0 -31
  225. package/src/config/config.default.ts +0 -379
  226. package/src/config/config.local.ts +0 -9
  227. package/src/index.ts +0 -1
  228. package/src/lib/extend/safe_curl.ts +0 -35
  229. package/src/lib/helper/cliFilter.ts +0 -20
  230. package/src/lib/helper/escape.ts +0 -3
  231. package/src/lib/helper/escapeShellArg.ts +0 -4
  232. package/src/lib/helper/escapeShellCmd.ts +0 -16
  233. package/src/lib/helper/index.ts +0 -21
  234. package/src/lib/helper/shtml.ts +0 -77
  235. package/src/lib/helper/sjs.ts +0 -57
  236. package/src/lib/helper/sjson.ts +0 -35
  237. package/src/lib/helper/spath.ts +0 -27
  238. package/src/lib/helper/surl.ts +0 -35
  239. package/src/lib/middlewares/csp.ts +0 -70
  240. package/src/lib/middlewares/csrf.ts +0 -44
  241. package/src/lib/middlewares/dta.ts +0 -13
  242. package/src/lib/middlewares/hsts.ts +0 -24
  243. package/src/lib/middlewares/index.ts +0 -23
  244. package/src/lib/middlewares/methodnoallow.ts +0 -23
  245. package/src/lib/middlewares/noopen.ts +0 -18
  246. package/src/lib/middlewares/nosniff.ts +0 -32
  247. package/src/lib/middlewares/referrerPolicy.ts +0 -39
  248. package/src/lib/middlewares/xframe.ts +0 -20
  249. package/src/lib/middlewares/xssProtection.ts +0 -17
  250. package/src/lib/utils.ts +0 -208
  251. package/src/types.ts +0 -16
  252. package/src/typings/index.d.ts +0 -4
@@ -1,77 +0,0 @@
1
- import type { BaseContextClass } from '@eggjs/core';
2
- import xss from 'xss';
3
- import { isSafeDomain, getFromUrl } from '../utils.js';
4
- import type { SecurityHelperOnTagAttrHandler } from '../../types.js';
5
-
6
- const BUILD_IN_ON_TAG_ATTR = Symbol('buildInOnTagAttr');
7
-
8
- // default rule: https://github.com/leizongmin/js-xss/blob/master/lib/default.js
9
- // add domain filter based on xss module
10
- // custom options http://jsxss.com/zh/options.html
11
- // eg: support a tag,filter attributes except for title : whiteList: {a: ['title']}
12
- export default function shtml(this: BaseContextClass, val: string) {
13
- if (typeof val !== 'string') {
14
- return val;
15
- }
16
-
17
- const securityOptions = this.ctx.securityOptions;
18
- let buildInOnTagAttrHandler: SecurityHelperOnTagAttrHandler | undefined;
19
- const shtmlConfig = {
20
- ...this.app.config.helper.shtml,
21
- ...securityOptions.shtml,
22
- [BUILD_IN_ON_TAG_ATTR]: buildInOnTagAttrHandler,
23
- };
24
- const domainWhiteList = this.app.config.security.domainWhiteList;
25
- const app = this.app;
26
- // filter href and src attribute if not in domain white list
27
- if (!shtmlConfig[BUILD_IN_ON_TAG_ATTR]) {
28
- shtmlConfig[BUILD_IN_ON_TAG_ATTR] = (_tag, name, value, isWhiteAttr) => {
29
- if (isWhiteAttr && (name === 'href' || name === 'src')) {
30
- if (!value) {
31
- return;
32
- }
33
-
34
- value = String(value);
35
- if (value[0] === '/' || value[0] === '#') {
36
- return;
37
- }
38
-
39
- const hostname = getFromUrl(value, 'hostname');
40
- if (!hostname) {
41
- return;
42
- }
43
-
44
- // If we don't have our hostname in the app.security.domainWhiteList,
45
- // Just check for `shtmlConfig.domainWhiteList` and `ctx.whiteList`.
46
- if (!isSafeDomain(hostname, domainWhiteList)) {
47
- // Check for `shtmlConfig.domainWhiteList` first (duplicated now)
48
- if (shtmlConfig.domainWhiteList && shtmlConfig.domainWhiteList.length > 0) {
49
- app.deprecate('[@eggjs/security/lib/helper/shtml] `config.helper.shtml.domainWhiteList` has been deprecate. Please use `config.security.domainWhiteList` instead.');
50
- if (!isSafeDomain(hostname, shtmlConfig.domainWhiteList)) {
51
- return '';
52
- }
53
- } else {
54
- return '';
55
- }
56
- }
57
- }
58
- };
59
-
60
- // avoid overriding user configuration 'onTagAttr'
61
- if (shtmlConfig.onTagAttr) {
62
- const customOnTagAttrHandler = shtmlConfig.onTagAttr;
63
- shtmlConfig.onTagAttr = function(tag, name, value, isWhiteAttr) {
64
- const result = customOnTagAttrHandler.apply(this, [ tag, name, value, isWhiteAttr ]);
65
- if (result !== undefined) {
66
- return result;
67
- }
68
- // fallback to build-in handler
69
- return shtmlConfig[BUILD_IN_ON_TAG_ATTR]!.apply(this, [ tag, name, value, isWhiteAttr ]);
70
- };
71
- } else {
72
- shtmlConfig.onTagAttr = shtmlConfig[BUILD_IN_ON_TAG_ATTR];
73
- }
74
- }
75
-
76
- return xss(val, shtmlConfig);
77
- }
@@ -1,57 +0,0 @@
1
- /**
2
- * Escape JavaScript to \xHH format
3
- */
4
-
5
- // escape \x00-\x7f
6
- // except 0-9,A-Z,a-z(\x2f-\x3a \x40-\x5b \x60-\x7b)
7
-
8
- // eslint-disable-next-line
9
- const MATCH_VULNERABLE_REGEXP = /[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]/;
10
- // eslint-enable-next-line
11
-
12
- const BASIC_ALPHABETS = new Set('abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
13
-
14
- const map: Record<string, string> = {
15
- '\t': '\\t',
16
- '\n': '\\n',
17
- '\r': '\\r',
18
- };
19
-
20
- export default function escapeJavaScript(text: string) {
21
- const str = '' + text;
22
- const match = MATCH_VULNERABLE_REGEXP.exec(str);
23
-
24
- if (!match) {
25
- return str;
26
- }
27
-
28
- let res = '';
29
- let index = 0;
30
- let lastIndex = 0;
31
- let ascii;
32
-
33
- for (index = match.index; index < str.length; index++) {
34
- ascii = str[index];
35
- if (BASIC_ALPHABETS.has(ascii)) {
36
- continue;
37
- } else {
38
- if (map[ascii] === undefined) {
39
- const code = ascii.charCodeAt(0);
40
- if (code > 127) {
41
- continue;
42
- } else {
43
- map[ascii] = '\\x' + code.toString(16);
44
- }
45
- }
46
- }
47
-
48
- if (lastIndex !== index) {
49
- res += str.substring(lastIndex, index);
50
- }
51
-
52
- lastIndex = index + 1;
53
- res += map[ascii];
54
- }
55
-
56
- return lastIndex !== index ? res + str.substring(lastIndex, index) : res;
57
- }
@@ -1,35 +0,0 @@
1
- import sjs from './sjs.js';
2
-
3
- /**
4
- * escape json
5
- * for output json in script
6
- */
7
-
8
- function sanitizeKey(obj: any) {
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
-
16
- for (const k in obj) {
17
- const escapedK = sjs(k);
18
- if (escapedK !== k) {
19
- obj[escapedK] = sanitizeKey(obj[k]);
20
- obj[k] = undefined;
21
- } else {
22
- obj[k] = sanitizeKey(obj[k]);
23
- }
24
- }
25
- return obj;
26
- }
27
-
28
- export default function jsonEscape(obj: any) {
29
- return JSON.stringify(sanitizeKey(obj), (_k, v) => {
30
- if (typeof v === 'string') {
31
- return sjs(v);
32
- }
33
- return v;
34
- });
35
- }
@@ -1,27 +0,0 @@
1
- /**
2
- * File Inclusion
3
- */
4
-
5
- import type { BaseContextClass } from '@eggjs/core';
6
-
7
- export default function pathFilter(this: BaseContextClass, path: string) {
8
- if (typeof path !== 'string') return path;
9
-
10
- const pathSource = path;
11
-
12
- while (path.indexOf('%') !== -1) {
13
- try {
14
- path = decodeURIComponent(path);
15
- } catch (e) {
16
- if (process.env.NODE_ENV !== 'production') {
17
- // Not a PROD env, logging with a warning.
18
- this.ctx.coreLogger.warn('[@eggjs/security/lib/helper/spath] : decode file path %j failed.', path);
19
- }
20
- break;
21
- }
22
- }
23
- if (path.indexOf('..') !== -1 || path[0] === '/') {
24
- return null;
25
- }
26
- return pathSource;
27
- }
@@ -1,35 +0,0 @@
1
- import type { BaseContextClass } from '@eggjs/core';
2
-
3
- const escapeMap: Record<string, string> = {
4
- '"': '&quot;',
5
- '<': '&lt;',
6
- '>': '&gt;',
7
- '\'': '&#x27;',
8
- };
9
-
10
- export default function surl(this: BaseContextClass, val: string) {
11
- // Just get the converted the protocolWhiteList in `Set` mode,
12
- // Avoid conversions in `foreach`
13
- const protocolWhiteListSet = this.app.config.security.__protocolWhiteListSet!;
14
-
15
- if (typeof val !== 'string') {
16
- return val;
17
- }
18
-
19
- // only test on absolute path
20
- if (val[0] !== '/') {
21
- const arr = val.split('://', 2);
22
- const protocol = arr.length > 1 ? arr[0].toLowerCase() : '';
23
- if (protocol === '' || !protocolWhiteListSet.has(protocol)) {
24
- if (this.app.config.env === 'local') {
25
- this.ctx.coreLogger.warn('[@eggjs/security/surl] url: %j, protocol: %j, ' +
26
- 'protocol is empty or not in white list, convert to empty string', val, protocol);
27
- }
28
- return '';
29
- }
30
- }
31
-
32
- return val.replace(/["'<>]/g, ch => {
33
- return escapeMap[ch];
34
- });
35
- }
@@ -1,70 +0,0 @@
1
- import extend from 'extend';
2
- import type { Context, Next } from '@eggjs/core';
3
- import { checkIfIgnore } from '../utils.js';
4
- import type { SecurityConfig } from '../../types.js';
5
-
6
- const HEADER = [
7
- 'x-content-security-policy',
8
- 'content-security-policy',
9
- ];
10
- const REPORT_ONLY_HEADER = [
11
- 'x-content-security-policy-report-only',
12
- 'content-security-policy-report-only',
13
- ];
14
-
15
- // Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
16
- const MSIE_REGEXP = / MSIE /i;
17
-
18
- export default (options: SecurityConfig['csp']) => {
19
- return async function csp(ctx: Context, next: Next) {
20
- await next();
21
-
22
- const opts = {
23
- ...options,
24
- ...ctx.securityOptions.csp,
25
- };
26
- if (checkIfIgnore(opts, ctx)) return;
27
-
28
- let finalHeader;
29
- const matchedOption = extend(true, {}, opts.policy);
30
- const bufArray = [];
31
-
32
- const headers = opts.reportOnly ? REPORT_ONLY_HEADER : HEADER;
33
- if (opts.supportIE && MSIE_REGEXP.test(ctx.get('user-agent'))) {
34
- finalHeader = headers[0];
35
- } else {
36
- finalHeader = headers[1];
37
- }
38
-
39
- for (const key in matchedOption) {
40
- const value = matchedOption[key];
41
- // Other arrays are splitted into strings EXCEPT `sandbox`
42
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox
43
- if (key === 'sandbox' && value === true) {
44
- bufArray.push(key);
45
- } else {
46
- let values = (Array.isArray(value) ? value : [ value ]) as string[];
47
- if (key === 'script-src') {
48
- const hasNonce = values.some(function(val) {
49
- return val.indexOf('nonce-') !== -1;
50
- });
51
-
52
- if (!hasNonce) {
53
- values.push('\'nonce-' + ctx.nonce + '\'');
54
- }
55
- }
56
-
57
- values = values.map(function(d) {
58
- if (d.startsWith('.')) {
59
- d = '*' + d;
60
- }
61
- return d;
62
- });
63
- bufArray.push(key + ' ' + values.join(' '));
64
- }
65
- }
66
- const headerString = bufArray.join(';');
67
- ctx.set(finalHeader, headerString);
68
- ctx.set('x-csp-nonce', ctx.nonce);
69
- };
70
- };
@@ -1,44 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import type { Context, Next } from '@eggjs/core';
3
- import typeis from 'type-is';
4
- import { checkIfIgnore } from '../utils.js';
5
- import type { SecurityConfig } from '../../types.js';
6
-
7
- const debug = debuglog('@eggjs/security/lib/middlewares/csrf');
8
-
9
- export default (options: SecurityConfig['csrf']) => {
10
- return function csrf(ctx: Context, next: Next) {
11
- if (checkIfIgnore(options, ctx)) {
12
- return next();
13
- }
14
-
15
- // ensure csrf token exists
16
- if ([ 'any', 'all', 'ctoken' ].includes(options.type)) {
17
- ctx.ensureCsrfSecret();
18
- }
19
-
20
- // supported requests
21
- const method = ctx.method;
22
- let isSupported = false;
23
- for (const eachRule of options.supportedRequests) {
24
- if (eachRule.path.test(ctx.path)) {
25
- if (eachRule.methods.includes(method)) {
26
- isSupported = true;
27
- break;
28
- }
29
- }
30
- }
31
- if (!isSupported) {
32
- return next();
33
- }
34
-
35
- if (options.ignoreJSON && typeis.is(ctx.get('content-type'), 'json')) {
36
- return next();
37
- }
38
-
39
- const body = ctx.request.body;
40
- debug('%s %s, got %j', ctx.method, ctx.url, body);
41
- ctx.assertCsrf();
42
- return next();
43
- };
44
- };
@@ -1,13 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { isSafePath } from '../utils.js';
3
-
4
- // https://en.wikipedia.org/wiki/Directory_traversal_attack
5
- export default () => {
6
- return function dta(ctx: Context, next: Next) {
7
- const path = ctx.path;
8
- if (!isSafePath(path, ctx)) {
9
- ctx.throw(400);
10
- }
11
- return next();
12
- };
13
- };
@@ -1,24 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- // Set Strict-Transport-Security header
6
- export default (options: SecurityConfig['hsts']) => {
7
- return async function hsts(ctx: Context, next: Next) {
8
- await next();
9
-
10
- const opts = {
11
- ...options,
12
- ...ctx.securityOptions.hsts,
13
- };
14
- if (checkIfIgnore(opts, ctx)) return;
15
-
16
- let val = 'max-age=' + opts.maxAge;
17
- // If opts.includeSubdomains is defined,
18
- // the rule is also valid for all the sub domains of the website
19
- if (opts.includeSubdomains) {
20
- val += '; includeSubdomains';
21
- }
22
- ctx.set('strict-transport-security', val);
23
- };
24
- };
@@ -1,23 +0,0 @@
1
- import csp from './csp.js';
2
- import csrf from './csrf.js';
3
- import dta from './dta.js';
4
- import hsts from './hsts.js';
5
- import methodnoallow from './methodnoallow.js';
6
- import noopen from './noopen.js';
7
- import nosniff from './nosniff.js';
8
- import referrerPolicy from './referrerPolicy.js';
9
- import xframe from './xframe.js';
10
- import xssProtection from './xssProtection.js';
11
-
12
- export default {
13
- csp,
14
- csrf,
15
- dta,
16
- hsts,
17
- methodnoallow,
18
- noopen,
19
- nosniff,
20
- referrerPolicy,
21
- xframe,
22
- xssProtection,
23
- };
@@ -1,23 +0,0 @@
1
- import { METHODS } from 'node:http';
2
- import type { Context, Next } from '@eggjs/core';
3
-
4
- const METHODS_NOT_ALLOWED = [ 'TRACE', 'TRACK' ];
5
- const safeHttpMethodsMap: Record<string, boolean> = {};
6
-
7
- for (const method of METHODS) {
8
- if (!METHODS_NOT_ALLOWED.includes(method)) {
9
- safeHttpMethodsMap[method.toUpperCase()] = true;
10
- }
11
- }
12
-
13
- // https://www.owasp.org/index.php/Cross_Site_Tracing
14
- // http://jsperf.com/find-by-map-with-find-by-array
15
- export default () => {
16
- return function notAllow(ctx: Context, next: Next) {
17
- // ctx.method is upper case
18
- if (!safeHttpMethodsMap[ctx.method]) {
19
- ctx.throw(405);
20
- }
21
- return next();
22
- };
23
- };
@@ -1,18 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- // @see http://blogs.msdn.com/b/ieinternals/archive/2009/06/30/internet-explorer-custom-http-headers.aspx
6
- export default (options: SecurityConfig['noopen']) => {
7
- return async function noopen(ctx: Context, next: Next) {
8
- await next();
9
-
10
- const opts = {
11
- ...options,
12
- ...ctx.securityOptions.noopen,
13
- };
14
- if (checkIfIgnore(opts, ctx)) return;
15
-
16
- ctx.set('x-download-options', 'noopen');
17
- };
18
- };
@@ -1,32 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- // status codes for redirects
6
- // @see https://github.com/jshttp/statuses/blob/master/index.js#L33
7
- const RedirectStatus: Record<number, boolean> = {
8
- 300: true,
9
- 301: true,
10
- 302: true,
11
- 303: true,
12
- 305: true,
13
- 307: true,
14
- 308: true,
15
- };
16
-
17
- export default (options: SecurityConfig['nosniff']) => {
18
- return async function nosniff(ctx: Context, next: Next) {
19
- await next();
20
-
21
- // ignore redirect response
22
- if (RedirectStatus[ctx.status]) return;
23
-
24
- const opts = {
25
- ...options,
26
- ...ctx.securityOptions.nosniff,
27
- };
28
- if (checkIfIgnore(opts, ctx)) return;
29
-
30
- ctx.set('x-content-type-options', 'nosniff');
31
- };
32
- };
@@ -1,39 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Referrer-Policy
6
- const ALLOWED_POLICIES_ENUM = [
7
- 'no-referrer',
8
- 'no-referrer-when-downgrade',
9
- 'origin',
10
- 'origin-when-cross-origin',
11
- 'same-origin',
12
- 'strict-origin',
13
- 'strict-origin-when-cross-origin',
14
- 'unsafe-url',
15
- '',
16
- ];
17
-
18
- export default (options: SecurityConfig['referrerPolicy']) => {
19
- return async function referrerPolicy(ctx: Context, next: Next) {
20
- await next();
21
-
22
- const opts = {
23
- ...options,
24
- // check refererPolicy for backward compatibility
25
- // typo on the old version
26
- // @see https://github.com/eggjs/security/blob/e3408408adec5f8d009d37f75126ed082481d0ac/lib/middlewares/referrerPolicy.js#L21C59-L21C72
27
- ...(ctx.securityOptions as any).refererPolicy,
28
- ...ctx.securityOptions.referrerPolicy,
29
- };
30
- if (checkIfIgnore(opts, ctx)) return;
31
-
32
- const policy = opts.value;
33
- if (!ALLOWED_POLICIES_ENUM.includes(policy)) {
34
- throw new Error('"' + policy + '" is not available.');
35
- }
36
-
37
- ctx.set('referrer-policy', policy);
38
- };
39
- };
@@ -1,20 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- export default (options: SecurityConfig['xframe']) => {
6
- return async function xframe(ctx: Context, next: Next) {
7
- await next();
8
-
9
- const opts = {
10
- ...options,
11
- ...ctx.securityOptions.xframe,
12
- };
13
- if (checkIfIgnore(opts, ctx)) return;
14
-
15
- // DENY, SAMEORIGIN, ALLOW-FROM
16
- // https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options?redirectlocale=en-US&redirectslug=The_X-FRAME-OPTIONS_response_header
17
- const value = opts.value || 'SAMEORIGIN';
18
- ctx.set('x-frame-options', value);
19
- };
20
- };
@@ -1,17 +0,0 @@
1
- import type { Context, Next } from '@eggjs/core';
2
- import { checkIfIgnore } from '../utils.js';
3
- import type { SecurityConfig } from '../../types.js';
4
-
5
- export default (options: SecurityConfig['xssProtection']) => {
6
- return async function xssProtection(ctx: Context, next: Next) {
7
- await next();
8
-
9
- const opts = {
10
- ...options,
11
- ...ctx.securityOptions.xssProtection,
12
- };
13
- if (checkIfIgnore(opts, ctx)) return;
14
-
15
- ctx.set('x-xss-protection', opts.value);
16
- };
17
- };