@eggjs/security 5.0.0-beta.19 → 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.
Files changed (71) hide show
  1. package/dist/agent.d.ts +5 -9
  2. package/dist/agent.js +10 -14
  3. package/dist/app/extend/agent.d.ts +4 -8
  4. package/dist/app/extend/agent.js +8 -12
  5. package/dist/app/extend/application.d.ts +7 -11
  6. package/dist/app/extend/application.js +32 -32
  7. package/dist/app/extend/context.d.ts +52 -55
  8. package/dist/app/extend/context.js +241 -188
  9. package/dist/app/extend/helper.d.ts +10 -22
  10. package/dist/app/extend/helper.js +5 -7
  11. package/dist/app/extend/response.d.ts +34 -38
  12. package/dist/app/extend/response.js +82 -69
  13. package/dist/app/middleware/securities.d.ts +4 -8
  14. package/dist/app/middleware/securities.js +52 -38
  15. package/dist/app.d.ts +5 -9
  16. package/dist/app.js +24 -22
  17. package/dist/config/config.default.d.ts +784 -787
  18. package/dist/config/config.default.js +356 -156
  19. package/dist/config/config.local.d.ts +2 -5
  20. package/dist/config/config.local.js +8 -5
  21. package/dist/index.d.ts +4 -1
  22. package/dist/index.js +2 -2
  23. package/dist/lib/extend/safe_curl.d.ts +9 -13
  24. package/dist/lib/extend/safe_curl.js +23 -17
  25. package/dist/lib/helper/cliFilter.d.ts +1 -4
  26. package/dist/lib/helper/cliFilter.js +15 -16
  27. package/dist/lib/helper/escape.d.ts +2 -2
  28. package/dist/lib/helper/escape.js +3 -7
  29. package/dist/lib/helper/escapeShellArg.d.ts +1 -4
  30. package/dist/lib/helper/escapeShellArg.js +4 -6
  31. package/dist/lib/helper/escapeShellCmd.d.ts +1 -4
  32. package/dist/lib/helper/escapeShellCmd.js +13 -14
  33. package/dist/lib/helper/index.d.ts +19 -22
  34. package/dist/lib/helper/index.js +15 -19
  35. package/dist/lib/helper/shtml.d.ts +2 -6
  36. package/dist/lib/helper/shtml.js +68 -52
  37. package/dist/lib/helper/sjs.d.ts +1 -4
  38. package/dist/lib/helper/sjs.js +44 -31
  39. package/dist/lib/helper/sjson.d.ts +1 -4
  40. package/dist/lib/helper/sjson.js +35 -28
  41. package/dist/lib/helper/spath.d.ts +5 -7
  42. package/dist/lib/helper/spath.js +24 -15
  43. package/dist/lib/helper/surl.d.ts +2 -6
  44. package/dist/lib/helper/surl.js +27 -22
  45. package/dist/lib/middlewares/csp.d.ts +3 -6
  46. package/dist/lib/middlewares/csp.js +54 -43
  47. package/dist/lib/middlewares/csrf.d.ts +3 -6
  48. package/dist/lib/middlewares/csrf.js +35 -31
  49. package/dist/lib/middlewares/dta.d.ts +2 -5
  50. package/dist/lib/middlewares/dta.js +10 -11
  51. package/dist/lib/middlewares/hsts.d.ts +3 -6
  52. package/dist/lib/middlewares/hsts.js +19 -17
  53. package/dist/lib/middlewares/index.d.ts +11 -16
  54. package/dist/lib/middlewares/index.js +22 -26
  55. package/dist/lib/middlewares/methodnoallow.d.ts +2 -5
  56. package/dist/lib/middlewares/methodnoallow.js +18 -13
  57. package/dist/lib/middlewares/noopen.d.ts +3 -6
  58. package/dist/lib/middlewares/noopen.js +13 -15
  59. package/dist/lib/middlewares/nosniff.d.ts +3 -6
  60. package/dist/lib/middlewares/nosniff.js +24 -23
  61. package/dist/lib/middlewares/referrerPolicy.d.ts +3 -6
  62. package/dist/lib/middlewares/referrerPolicy.js +31 -27
  63. package/dist/lib/middlewares/xframe.d.ts +3 -6
  64. package/dist/lib/middlewares/xframe.js +15 -16
  65. package/dist/lib/middlewares/xssProtection.d.ts +3 -6
  66. package/dist/lib/middlewares/xssProtection.js +12 -15
  67. package/dist/lib/utils.d.ts +12 -17
  68. package/dist/lib/utils.js +177 -112
  69. package/dist/types.d.ts +35 -37
  70. package/dist/types.js +2 -1
  71. package/package.json +6 -6
package/dist/agent.d.ts CHANGED
@@ -1,10 +1,6 @@
1
- import { Agent, ILifecycleBoot } from "egg";
2
-
3
- //#region src/agent.d.ts
4
- declare class AgentBoot implements ILifecycleBoot {
5
- private readonly agent;
6
- constructor(agent: Agent);
7
- configWillLoad(): Promise<void>;
1
+ import type { ILifecycleBoot, Agent } from 'egg';
2
+ export default class AgentBoot implements ILifecycleBoot {
3
+ private readonly agent;
4
+ constructor(agent: Agent);
5
+ configWillLoad(): Promise<void>;
8
6
  }
9
- //#endregion
10
- export { AgentBoot as default };
package/dist/agent.js CHANGED
@@ -1,15 +1,11 @@
1
1
  import { preprocessConfig } from "./lib/utils.js";
2
-
3
- //#region src/agent.ts
4
- var AgentBoot = class {
5
- agent;
6
- constructor(agent) {
7
- this.agent = agent;
8
- }
9
- async configWillLoad() {
10
- preprocessConfig(this.agent.config.security);
11
- }
12
- };
13
-
14
- //#endregion
15
- export { AgentBoot as default };
2
+ export default class AgentBoot {
3
+ agent;
4
+ constructor(agent) {
5
+ this.agent = agent;
6
+ }
7
+ async configWillLoad() {
8
+ preprocessConfig(this.agent.config.security);
9
+ }
10
+ }
11
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvYWdlbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sZ0JBQWdCLENBQUM7QUFFbEQsTUFBTSxDQUFDLE9BQU8sT0FBTyxTQUFTO0lBQ1gsS0FBSyxDQUFDO0lBRXZCLFlBQVksS0FBWTtRQUN0QixJQUFJLENBQUMsS0FBSyxHQUFHLEtBQUssQ0FBQztJQUNyQixDQUFDO0lBRUQsS0FBSyxDQUFDLGNBQWM7UUFDbEIsZ0JBQWdCLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDL0MsQ0FBQztDQUNGIn0=
@@ -1,9 +1,5 @@
1
- import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "../../lib/extend/safe_curl.js";
2
- import { Agent } from "egg";
3
-
4
- //#region src/app/extend/agent.d.ts
5
- declare class SecurityAgent extends Agent {
6
- safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
1
+ import { Agent } from 'egg';
2
+ import { type HttpClientRequestURL, type HttpClientOptions, type HttpClientResponse } from '../../lib/extend/safe_curl.ts';
3
+ export default class SecurityAgent extends Agent {
4
+ safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
7
5
  }
8
- //#endregion
9
- export { SecurityAgent as default };
@@ -1,12 +1,8 @@
1
- import { safeCurlForApplication } from "../../lib/extend/safe_curl.js";
2
- import { Agent } from "egg";
3
-
4
- //#region src/app/extend/agent.ts
5
- var SecurityAgent = class extends Agent {
6
- async safeCurl(url, options) {
7
- return await safeCurlForApplication(this, url, options);
8
- }
9
- };
10
-
11
- //#endregion
12
- export { SecurityAgent as default };
1
+ import { Agent } from 'egg';
2
+ import { safeCurlForApplication, } from "../../lib/extend/safe_curl.js";
3
+ export default class SecurityAgent extends Agent {
4
+ async safeCurl(url, options) {
5
+ return await safeCurlForApplication(this, url, options);
6
+ }
7
+ }
8
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hZ2VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsS0FBSyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRTVCLE9BQU8sRUFDTCxzQkFBc0IsR0FJdkIsTUFBTSwrQkFBK0IsQ0FBQztBQUV2QyxNQUFNLENBQUMsT0FBTyxPQUFPLGFBQWMsU0FBUSxLQUFLO0lBQzlDLEtBQUssQ0FBQyxRQUFRLENBQVUsR0FBeUIsRUFBRSxPQUEyQjtRQUM1RSxPQUFPLE1BQU0sc0JBQXNCLENBQUksSUFBSSxFQUFFLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztJQUM3RCxDQUFDO0NBQ0YifQ==
@@ -1,12 +1,8 @@
1
- import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "../../lib/extend/safe_curl.js";
2
- import { Application } from "egg";
3
-
4
- //#region src/app/extend/application.d.ts
5
- declare class SecurityApplication extends Application {
6
- injectCsrf(html: string): string;
7
- injectNonce(html: string): string;
8
- injectHijackingDefense(html: string): string;
9
- safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
1
+ import { Application } from 'egg';
2
+ import { type HttpClientRequestURL, type HttpClientOptions, type HttpClientResponse } from '../../lib/extend/safe_curl.ts';
3
+ export default class SecurityApplication extends Application {
4
+ injectCsrf(html: string): string;
5
+ injectNonce(html: string): string;
6
+ injectHijackingDefense(html: string): string;
7
+ safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
10
8
  }
11
- //#endregion
12
- export { SecurityApplication as default };
@@ -1,32 +1,32 @@
1
- import { safeCurlForApplication } from "../../lib/extend/safe_curl.js";
2
- import { Application } from "egg";
3
-
4
- //#region src/app/extend/application.ts
5
- const INPUT_CSRF = "\r\n<input type=\"hidden\" name=\"_csrf\" value=\"{{ctx.csrf}}\" /></form>";
6
- const INJECTION_DEFENSE = "<!--for injection--><!--</html>--><!--for injection-->";
7
- var SecurityApplication = class extends Application {
8
- injectCsrf(html) {
9
- html = html.replace(/(<form.*?>)([\s\S]*?)<\/form>/gi, (_, $1, $2) => {
10
- const match = $2;
11
- if (match.indexOf("name=\"_csrf\"") !== -1 || match.indexOf("name='_csrf'") !== -1) return $1 + match + "</form>";
12
- return $1 + match + INPUT_CSRF;
13
- });
14
- return html;
15
- }
16
- injectNonce(html) {
17
- html = html.replace(/<script(.*?)>([\s\S]*?)<\/script[^>]*?>/gi, (_, $1, $2) => {
18
- if (!$1.includes("nonce=")) $1 += " nonce=\"{{ctx.nonce}}\"";
19
- return "<script" + $1 + ">" + $2 + "<\/script>";
20
- });
21
- return html;
22
- }
23
- injectHijackingDefense(html) {
24
- return INJECTION_DEFENSE + html + INJECTION_DEFENSE;
25
- }
26
- async safeCurl(url, options) {
27
- return await safeCurlForApplication(this, url, options);
28
- }
29
- };
30
-
31
- //#endregion
32
- export { SecurityApplication as default };
1
+ import { Application } from 'egg';
2
+ import { safeCurlForApplication, } from "../../lib/extend/safe_curl.js";
3
+ const INPUT_CSRF = '\r\n<input type="hidden" name="_csrf" value="{{ctx.csrf}}" /></form>';
4
+ const INJECTION_DEFENSE = '<!--for injection--><!--</html>--><!--for injection-->';
5
+ export default class SecurityApplication extends Application {
6
+ injectCsrf(html) {
7
+ html = html.replace(/(<form.*?>)([\s\S]*?)<\/form>/gi, (_, $1, $2) => {
8
+ const match = $2;
9
+ if (match.indexOf('name="_csrf"') !== -1 || match.indexOf("name='_csrf'") !== -1) {
10
+ return $1 + match + '</form>';
11
+ }
12
+ return $1 + match + INPUT_CSRF;
13
+ });
14
+ return html;
15
+ }
16
+ injectNonce(html) {
17
+ html = html.replace(/<script(.*?)>([\s\S]*?)<\/script[^>]*?>/gi, (_, $1, $2) => {
18
+ if (!$1.includes('nonce=')) {
19
+ $1 += ' nonce="{{ctx.nonce}}"';
20
+ }
21
+ return '<script' + $1 + '>' + $2 + '</script>';
22
+ });
23
+ return html;
24
+ }
25
+ injectHijackingDefense(html) {
26
+ return INJECTION_DEFENSE + html + INJECTION_DEFENSE;
27
+ }
28
+ async safeCurl(url, options) {
29
+ return await safeCurlForApplication(this, url, options);
30
+ }
31
+ }
32
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJu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BQztnQkFDM0IsRUFBRSxJQUFJLHdCQUF3QixDQUFDO1lBQ2pDLENBQUM7WUFDRCxPQUFPLFNBQVMsR0FBRyxFQUFFLEdBQUcsR0FBRyxHQUFHLEVBQUUsR0FBRyxXQUFXLENBQUM7UUFDakQsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLElBQUksQ0FBQztJQUNkLENBQUM7SUFFRCxzQkFBc0IsQ0FBQyxJQUFZO1FBQ2pDLE9BQU8saUJBQWlCLEdBQUcsSUFBSSxHQUFHLGlCQUFpQixDQUFDO0lBQ3RELENBQUM7SUFFRCxLQUFLLENBQUMsUUFBUSxDQUFVLEdBQXlCLEVBQUUsT0FBMkI7UUFDNUUsT0FBTyxNQUFNLHNCQUFzQixDQUFJLElBQUksRUFBRSxHQUFHLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDN0QsQ0FBQztDQUNGIn0=
@@ -1,61 +1,58 @@
1
- import { SecurityConfig } from "../../config/config.default.js";
2
- import { HttpClientOptions, HttpClientRequestURL, HttpClientResponse } from "../../lib/extend/safe_curl.js";
3
- import SecurityResponse from "./response.js";
4
- import { Context } from "egg";
5
-
6
- //#region src/app/extend/context.d.ts
1
+ import { Context } from 'egg';
2
+ import type { HttpClientRequestURL, HttpClientOptions, HttpClientResponse } from '../../lib/extend/safe_curl.ts';
3
+ import type { SecurityConfig } from '../../config/config.default.ts';
4
+ import type SecurityResponse from './response.ts';
7
5
  declare const CSRF_SECRET: unique symbol;
8
6
  declare const LOG_CSRF_NOTICE: unique symbol;
9
7
  declare const INPUT_TOKEN: unique symbol;
10
8
  declare const CSRF_REFERER_CHECK: unique symbol;
11
9
  declare const CSRF_CTOKEN_CHECK: unique symbol;
12
- declare class SecurityContext extends Context {
13
- response: SecurityResponse;
14
- get securityOptions(): Partial<SecurityConfig>;
15
- /**
16
- * Check whether the specific `domain` is in / matches the whiteList or not.
17
- * @param {string} domain The assigned domain.
18
- * @param {Array<string>} [customWhiteList] The custom white list for domain.
19
- * @return {boolean} If the domain is in / matches the whiteList, return true;
20
- * otherwise false.
21
- */
22
- isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
23
- get nonce(): string;
24
- /**
25
- * get csrf token, general use in template
26
- * @return {String} csrf token
27
- * @public
28
- */
29
- get csrf(): string;
30
- /**
31
- * get csrf secret from session or cookie
32
- * @return {String} csrf secret
33
- * @private
34
- */
35
- get [CSRF_SECRET](): string;
36
- /**
37
- * ensure csrf secret exists in session or cookie.
38
- * @param {Boolean} [rotate] reset secret even if the secret exists
39
- * @public
40
- */
41
- ensureCsrfSecret(rotate?: boolean): void;
42
- get [INPUT_TOKEN](): string;
43
- /**
44
- * rotate csrf secret exists in session or cookie.
45
- * must rotate the secret when user login
46
- * @public
47
- */
48
- rotateCsrfSecret(): void;
49
- /**
50
- * assert csrf token/referer is present
51
- * @public
52
- */
53
- assertCsrf(): void;
54
- [CSRF_CTOKEN_CHECK](): "missing csrf token" | "invalid csrf token" | undefined;
55
- [CSRF_REFERER_CHECK](): "missing csrf referer or origin" | "invalid csrf referer or origin" | undefined;
56
- [LOG_CSRF_NOTICE](msg: string): void;
57
- safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
58
- unsafeRedirect(url: string, alt?: string): void;
10
+ export default class SecurityContext extends Context {
11
+ response: SecurityResponse;
12
+ get securityOptions(): Partial<SecurityConfig>;
13
+ /**
14
+ * Check whether the specific `domain` is in / matches the whiteList or not.
15
+ * @param {string} domain The assigned domain.
16
+ * @param {Array<string>} [customWhiteList] The custom white list for domain.
17
+ * @return {boolean} If the domain is in / matches the whiteList, return true;
18
+ * otherwise false.
19
+ */
20
+ isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
21
+ get nonce(): string;
22
+ /**
23
+ * get csrf token, general use in template
24
+ * @return {String} csrf token
25
+ * @public
26
+ */
27
+ get csrf(): string;
28
+ /**
29
+ * get csrf secret from session or cookie
30
+ * @return {String} csrf secret
31
+ * @private
32
+ */
33
+ get [CSRF_SECRET](): string;
34
+ /**
35
+ * ensure csrf secret exists in session or cookie.
36
+ * @param {Boolean} [rotate] reset secret even if the secret exists
37
+ * @public
38
+ */
39
+ ensureCsrfSecret(rotate?: boolean): void;
40
+ get [INPUT_TOKEN](): string;
41
+ /**
42
+ * rotate csrf secret exists in session or cookie.
43
+ * must rotate the secret when user login
44
+ * @public
45
+ */
46
+ rotateCsrfSecret(): void;
47
+ /**
48
+ * assert csrf token/referer is present
49
+ * @public
50
+ */
51
+ assertCsrf(): void;
52
+ [CSRF_CTOKEN_CHECK](): "missing csrf token" | "invalid csrf token" | undefined;
53
+ [CSRF_REFERER_CHECK](): "missing csrf referer or origin" | "invalid csrf referer or origin" | undefined;
54
+ [LOG_CSRF_NOTICE](msg: string): void;
55
+ safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
56
+ unsafeRedirect(url: string, alt?: string): void;
59
57
  }
60
- //#endregion
61
- export { SecurityContext as default };
58
+ export {};