@eggjs/security 5.0.0-beta.34 → 5.0.0-beta.36

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 +9 -5
  2. package/dist/agent.js +14 -10
  3. package/dist/app/extend/agent.d.ts +8 -4
  4. package/dist/app/extend/agent.js +12 -8
  5. package/dist/app/extend/application.d.ts +11 -7
  6. package/dist/app/extend/application.js +32 -32
  7. package/dist/app/extend/context.d.ts +55 -51
  8. package/dist/app/extend/context.js +185 -240
  9. package/dist/app/extend/helper.d.ts +5 -2
  10. package/dist/app/extend/helper.js +8 -6
  11. package/dist/app/extend/response.d.ts +38 -34
  12. package/dist/app/extend/response.js +69 -82
  13. package/dist/app/middleware/securities.d.ts +5 -2
  14. package/dist/app/middleware/securities.js +38 -52
  15. package/dist/app.d.ts +9 -5
  16. package/dist/app.js +22 -24
  17. package/dist/config/config.default.d.ts +34 -45
  18. package/dist/config/config.default.js +158 -362
  19. package/dist/config/config.local.d.ts +6 -3
  20. package/dist/config/config.local.js +6 -8
  21. package/dist/index.d.ts +20 -16
  22. package/dist/index.js +24 -21
  23. package/dist/lib/extend/safe_curl.d.ts +16 -13
  24. package/dist/lib/extend/safe_curl.js +17 -23
  25. package/dist/lib/helper/cliFilter.d.ts +4 -4
  26. package/dist/lib/helper/cliFilter.js +16 -15
  27. package/dist/lib/helper/escape.d.ts +2 -2
  28. package/dist/lib/helper/escape.js +7 -3
  29. package/dist/lib/helper/escapeShellArg.d.ts +4 -1
  30. package/dist/lib/helper/escapeShellArg.js +6 -4
  31. package/dist/lib/helper/escapeShellCmd.d.ts +4 -1
  32. package/dist/lib/helper/escapeShellCmd.js +14 -13
  33. package/dist/lib/helper/index.d.ts +22 -19
  34. package/dist/lib/helper/index.js +19 -15
  35. package/dist/lib/helper/shtml.d.ts +6 -2
  36. package/dist/lib/helper/shtml.js +52 -68
  37. package/dist/lib/helper/sjs.d.ts +4 -4
  38. package/dist/lib/helper/sjs.js +31 -44
  39. package/dist/lib/helper/sjson.d.ts +4 -1
  40. package/dist/lib/helper/sjson.js +28 -35
  41. package/dist/lib/helper/spath.d.ts +7 -5
  42. package/dist/lib/helper/spath.js +15 -24
  43. package/dist/lib/helper/surl.d.ts +6 -2
  44. package/dist/lib/helper/surl.js +22 -27
  45. package/dist/lib/middlewares/csp.d.ts +6 -3
  46. package/dist/lib/middlewares/csp.js +43 -54
  47. package/dist/lib/middlewares/csrf.d.ts +6 -3
  48. package/dist/lib/middlewares/csrf.js +31 -35
  49. package/dist/lib/middlewares/dta.d.ts +5 -2
  50. package/dist/lib/middlewares/dta.js +11 -10
  51. package/dist/lib/middlewares/hsts.d.ts +6 -3
  52. package/dist/lib/middlewares/hsts.js +17 -19
  53. package/dist/lib/middlewares/index.d.ts +24 -21
  54. package/dist/lib/middlewares/index.js +26 -22
  55. package/dist/lib/middlewares/methodnoallow.d.ts +5 -2
  56. package/dist/lib/middlewares/methodnoallow.js +13 -18
  57. package/dist/lib/middlewares/noopen.d.ts +6 -3
  58. package/dist/lib/middlewares/noopen.js +14 -13
  59. package/dist/lib/middlewares/nosniff.d.ts +6 -3
  60. package/dist/lib/middlewares/nosniff.js +22 -24
  61. package/dist/lib/middlewares/referrerPolicy.d.ts +6 -3
  62. package/dist/lib/middlewares/referrerPolicy.js +27 -30
  63. package/dist/lib/middlewares/xframe.d.ts +6 -3
  64. package/dist/lib/middlewares/xframe.js +16 -15
  65. package/dist/lib/middlewares/xssProtection.d.ts +6 -3
  66. package/dist/lib/middlewares/xssProtection.js +15 -12
  67. package/dist/lib/utils.d.ts +22 -17
  68. package/dist/lib/utils.js +112 -177
  69. package/dist/types.d.ts +38 -36
  70. package/dist/types.js +1 -2
  71. package/package.json +31 -37
package/dist/agent.d.ts CHANGED
@@ -1,6 +1,10 @@
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>;
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>;
6
8
  }
9
+ //#endregion
10
+ export { AgentBoot as default };
package/dist/agent.js CHANGED
@@ -1,11 +1,15 @@
1
1
  import { preprocessConfig } from "./lib/utils.js";
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=
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 };
@@ -1,5 +1,9 @@
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>>;
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>>;
5
7
  }
8
+ //#endregion
9
+ export { SecurityAgent as default };
@@ -1,8 +1,12 @@
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
+ 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,8 +1,12 @@
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>>;
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>>;
8
10
  }
11
+ //#endregion
12
+ export { SecurityApplication as default };
@@ -1,32 +1,32 @@
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRWxDLE9BQU8sRUFDTCxzQkFBc0IsR0FJdkIsTUFBTSwrQkFBK0IsQ0FBQztBQUV2QyxNQUFNLFVBQVUsR0FBRyxzRUFBc0UsQ0FBQztBQUMxRixNQUFNLGlCQUFpQixHQUFHLHdEQUF3RCxDQUFDO0FBRW5GLE1BQU0sQ0FBQyxPQUFPLE9BQU8sbUJBQW9CLFNBQVEsV0FBVztJQUMxRCxVQUFVLENBQUMsSUFBWTtRQUNyQixJQUFJLEdBQUcsSUFBSSxDQUFDLE9BQU8sQ0FBQyxpQ0FBaUMsRUFBRSxDQUFDLENBQUMsRUFBRSxFQUFFLEVBQUUsRUFBRSxFQUFFLEVBQUU7WUFDbkUsTUFBTSxLQUFLLEdBQUcsRUFBRSxDQUFDO1lBQ2pCLElBQUksS0FBSyxDQUFDLE9BQU8sQ0FBQyxjQUFjLENBQUMsS0FBSyxDQUFDLENBQUMsSUFBSSxLQUFLLENBQUMsT0FBTyxDQUFDLGNBQWMsQ0FBQyxLQUFLLENBQUMsQ0FBQyxFQUFFLENBQUM7Z0JBQ2pGLE9BQU8sRUFBRSxHQUFHLEtBQUssR0FBRyxTQUFTLENBQUM7WUFDaEMsQ0FBQztZQUNELE9BQU8sRUFBRSxHQUFHLEtBQUssR0FBRyxVQUFVLENBQUM7UUFDakMsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLElBQUksQ0FBQztJQUNkLENBQUM7SUFFRCxXQUFXLENBQUMsSUFBWTtRQUN0QixJQUFJLEdBQUcsSUFBSSxDQUFDLE9BQU8sQ0FBQywyQ0FBMkMsRUFBRSxDQUFDLENBQUMsRUFBRSxFQUFFLEVBQUUsRUFBRSxFQUFFLEVBQUU7WUFDN0UsSUFBSSxDQUFDLEVBQUUsQ0FBQyxRQUFRLENBQUMsUUFBUSxDQUFDLEVBQUUsQ0FBQztnQkFDM0IsRUFBRSxJQUFJLHdCQUF3QixDQUFDO1lBQ2pDLENBQUM7WUFDRCxPQUFPLFNBQVMsR0FBRyxFQUFFLEdBQUcsR0FBRyxHQUFHLEVBQUUsR0FBRyxXQUFXLENBQUM7UUFDakQsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLElBQUksQ0FBQztJQUNkLENBQUM7SUFFRCxzQkFBc0IsQ0FBQyxJQUFZO1FBQ2pDLE9BQU8saUJBQWlCLEdBQUcsSUFBSSxHQUFHLGlCQUFpQixDQUFDO0lBQ3RELENBQUM7SUFFRCxLQUFLLENBQUMsUUFBUSxDQUFVLEdBQXlCLEVBQUUsT0FBMkI7UUFDNUUsT0FBTyxNQUFNLHNCQUFzQixDQUFJLElBQUksRUFBRSxHQUFHLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDN0QsQ0FBQztDQUNGIn0=
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,52 +1,56 @@
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';
5
- export default class SecurityContext extends Context {
6
- response: SecurityResponse;
7
- get securityOptions(): Partial<SecurityConfig>;
8
- /**
9
- * Check whether the specific `domain` is in / matches the whiteList or not.
10
- * @param {string} domain The assigned domain.
11
- * @param {Array<string>} [customWhiteList] The custom white list for domain.
12
- * @return {boolean} If the domain is in / matches the whiteList, return true;
13
- * otherwise false.
14
- */
15
- isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
16
- get nonce(): string;
17
- /**
18
- * get csrf token, general use in template
19
- * @return {String} csrf token
20
- * @public
21
- */
22
- get csrf(): string;
23
- /**
24
- * get csrf secret from session or cookie
25
- * @return {String} csrf secret
26
- * @private
27
- */
28
- private getCsrfSecret;
29
- /**
30
- * ensure csrf secret exists in session or cookie.
31
- * @param {Boolean} [rotate] reset secret even if the secret exists
32
- * @public
33
- */
34
- ensureCsrfSecret(rotate?: boolean): void;
35
- private getInputToken;
36
- /**
37
- * rotate csrf secret exists in session or cookie.
38
- * must rotate the secret when user login
39
- * @public
40
- */
41
- rotateCsrfSecret(): void;
42
- /**
43
- * assert csrf token/referer is present
44
- * @public
45
- */
46
- assertCsrf(): void;
47
- private csrfCtokenCheck;
48
- private csrfRefererCheck;
49
- private logCsrfNotice;
50
- safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
51
- unsafeRedirect(url: string, alt?: string): void;
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
7
+ declare class SecurityContext extends Context {
8
+ response: SecurityResponse;
9
+ get securityOptions(): Partial<SecurityConfig>;
10
+ /**
11
+ * Check whether the specific `domain` is in / matches the whiteList or not.
12
+ * @param {string} domain The assigned domain.
13
+ * @param {Array<string>} [customWhiteList] The custom white list for domain.
14
+ * @return {boolean} If the domain is in / matches the whiteList, return true;
15
+ * otherwise false.
16
+ */
17
+ isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
18
+ get nonce(): string;
19
+ /**
20
+ * get csrf token, general use in template
21
+ * @return {String} csrf token
22
+ * @public
23
+ */
24
+ get csrf(): string;
25
+ /**
26
+ * get csrf secret from session or cookie
27
+ * @return {String} csrf secret
28
+ * @private
29
+ */
30
+ private getCsrfSecret;
31
+ /**
32
+ * ensure csrf secret exists in session or cookie.
33
+ * @param {Boolean} [rotate] reset secret even if the secret exists
34
+ * @public
35
+ */
36
+ ensureCsrfSecret(rotate?: boolean): void;
37
+ private getInputToken;
38
+ /**
39
+ * rotate csrf secret exists in session or cookie.
40
+ * must rotate the secret when user login
41
+ * @public
42
+ */
43
+ rotateCsrfSecret(): void;
44
+ /**
45
+ * assert csrf token/referer is present
46
+ * @public
47
+ */
48
+ assertCsrf(): void;
49
+ private csrfCtokenCheck;
50
+ private csrfRefererCheck;
51
+ private logCsrfNotice;
52
+ safeCurl<T = any>(url: HttpClientRequestURL, options?: HttpClientOptions): Promise<HttpClientResponse<T>>;
53
+ unsafeRedirect(url: string, alt?: string): void;
52
54
  }
55
+ //#endregion
56
+ export { SecurityContext as default };