Skip to content

Commit 4d495f5

Browse files
committed
f
1 parent b87fa01 commit 4d495f5

45 files changed

Lines changed: 415 additions & 317 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
# egg-security
2-
3-
Security plugin in egg
1+
# @eggjs/security
42

53
[![NPM version][npm-image]][npm-url]
64
[![Node.js CI](https://github.com/eggjs/security/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/security/actions/workflows/nodejs.yml)
75
[![Test coverage][codecov-image]][codecov-url]
86
[![Known Vulnerabilities][snyk-image]][snyk-url]
97
[![npm download][download-image]][download-url]
8+
[![Node.js Version](https://img.shields.io/node/v/eggjs/security.svg?style=flat)](https://nodejs.org/en/download/)
9+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
10+
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/security)
1011

11-
[npm-image]: https://img.shields.io/npm/v/egg-security.svg?style=flat-square
12-
[npm-url]: https://npmjs.org/package/egg-security
12+
[npm-image]: https://img.shields.io/npm/v/@eggjs/security.svg?style=flat-square
13+
[npm-url]: https://npmjs.org/package/@eggjs/security
1314
[codecov-image]: https://codecov.io/gh/eggjs/security/branch/master/graph/badge.svg
1415
[codecov-url]: https://codecov.io/gh/eggjs/security
15-
[snyk-image]: https://snyk.io/test/npm/egg-security/badge.svg?style=flat-square
16-
[snyk-url]: https://snyk.io/test/npm/egg-security
17-
[download-image]: https://img.shields.io/npm/dm/egg-security.svg?style=flat-square
18-
[download-url]: https://npmjs.org/package/egg-security
16+
[snyk-image]: https://snyk.io/test/npm/@eggjs/security/badge.svg?style=flat-square
17+
[snyk-url]: https://snyk.io/test/npm/@eggjs/security
18+
[download-image]: https://img.shields.io/npm/dm/@eggjs/security.svg?style=flat-square
19+
[download-url]: https://npmjs.org/package/@eggjs/security
1920

2021
Egg's default security plugin, generally no need to configure.
2122

2223
## Install
2324

2425
```bash
25-
npm i egg-security
26+
npm i @eggjs/security
2627
```
2728

2829
## Usage & configuration

‎README.zh-CN.md‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
# egg-security
2-
3-
egg 内置的安全插件
1+
# @eggjs/security
42

53
[![NPM version][npm-image]][npm-url]
64
[![Node.js CI](https://github.com/eggjs/security/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/security/actions/workflows/nodejs.yml)
75
[![Test coverage][codecov-image]][codecov-url]
86
[![Known Vulnerabilities][snyk-image]][snyk-url]
97
[![npm download][download-image]][download-url]
8+
[![Node.js Version](https://img.shields.io/node/v/eggjs/security.svg?style=flat)](https://nodejs.org/en/download/)
9+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
10+
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/security)
1011

11-
[npm-image]: https://img.shields.io/npm/v/egg-security.svg?style=flat-square
12-
[npm-url]: https://npmjs.org/package/egg-security
12+
[npm-image]: https://img.shields.io/npm/v/@eggjs/security.svg?style=flat-square
13+
[npm-url]: https://npmjs.org/package/@eggjs/security
1314
[codecov-image]: https://codecov.io/gh/eggjs/security/branch/master/graph/badge.svg
1415
[codecov-url]: https://codecov.io/gh/eggjs/security
15-
[snyk-image]: https://snyk.io/test/npm/egg-security/badge.svg?style=flat-square
16-
[snyk-url]: https://snyk.io/test/npm/egg-security
17-
[download-image]: https://img.shields.io/npm/dm/egg-security.svg?style=flat-square
18-
[download-url]: https://npmjs.org/package/egg-security
16+
[snyk-image]: https://snyk.io/test/npm/@eggjs/security/badge.svg?style=flat-square
17+
[snyk-url]: https://snyk.io/test/npm/@eggjs/security
18+
[download-image]: https://img.shields.io/npm/dm/@eggjs/security.svg?style=flat-square
19+
[download-url]: https://npmjs.org/package/@eggjs/security
20+
21+
egg 内置的安全插件
1922

2023
## 使用方式
2124

‎package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@
3939
"@eggjs/core": "^6.2.13",
4040
"@eggjs/ip": "^2.1.0",
4141
"csrf": "^3.0.6",
42-
"egg-path-matching": "^1.0.0",
42+
"egg-path-matching": "^2.1.0",
4343
"escape-html": "^1.0.3",
4444
"extend": "^3.0.1",
4545
"extend2": "^4.0.0",
4646
"koa-compose": "^4.1.0",
4747
"matcher": "^4.0.0",
48-
"methods": "^1.1.2",
4948
"nanoid": "^3.3.8",
50-
"statuses": "^2.0.1",
51-
"type-is": "^1.6.15",
49+
"type-is": "^1.6.18",
5250
"xss": "^1.0.3"
5351
},
5452
"devDependencies": {
5553
"@arethetypeswrong/cli": "^0.17.1",
5654
"@eggjs/bin": "7",
5755
"@eggjs/mock": "^6.0.5",
5856
"@eggjs/tsconfig": "1",
57+
"@types/escape-html": "^1.0.4",
5958
"@types/koa-compose": "^3.2.8",
6059
"@types/mocha": "10",
6160
"@types/node": "22",
61+
"@types/type-is": "^1.6.7",
6262
"beautify-benchmark": "^0.2.4",
6363
"benchmark": "^2.1.4",
6464
"egg": "^4.0.1",

‎src/agent.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ILifecycleBoot, EggCore } from '@eggjs/core';
22
import { preprocessConfig } from './lib/utils.js';
33

4-
export class AgentBoot implements ILifecycleBoot {
4+
export default class AgentBoot implements ILifecycleBoot {
55
private readonly agent;
66

77
constructor(agent: EggCore) {

‎src/app.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert';
22
import type { ILifecycleBoot, EggCore } from '@eggjs/core';
33
import { preprocessConfig } from './lib/utils.js';
44

5-
export class AgentBoot implements ILifecycleBoot {
5+
export default class AgentBoot implements ILifecycleBoot {
66
private readonly app;
77

88
constructor(app: EggCore) {

‎src/app/extend/context.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
HttpClientOptions,
99
HttpClientRequestReturn,
1010
} from '../../lib/extend/safe_curl.js';
11-
import { SecurityConfig } from '../../types.js';
11+
import { SecurityConfig, SecurityHelperConfig } from '../../types.js';
1212

1313
const debug = debuglog('@eggjs/security/app/extend/context');
1414

@@ -113,10 +113,10 @@ export default class SecurityContext extends Context {
113113

114114
/**
115115
* ensure csrf secret exists in session or cookie.
116-
* @param {Boolean} rotate reset secret even if the secret exists
116+
* @param {Boolean} [rotate] reset secret even if the secret exists
117117
* @public
118118
*/
119-
ensureCsrfSecret(rotate: boolean) {
119+
ensureCsrfSecret(rotate?: boolean) {
120120
if (this[CSRF_SECRET] && !rotate) return;
121121
debug('ensure csrf secret, exists: %s, rotate; %s', this[CSRF_SECRET], rotate);
122122
const secret = tokens.secretSync();
@@ -154,7 +154,7 @@ export default class SecurityContext extends Context {
154154
// try order: query, body, header
155155
const token = findToken(this.request.query, queryName)
156156
|| findToken(this.request.body, bodyName)
157-
|| (headerName && this.request.get(headerName));
157+
|| (headerName && this.request.get<string>(headerName));
158158
debug('get token: %j, secret: %j', token, this[CSRF_SECRET]);
159159
return token;
160160
}
@@ -265,11 +265,11 @@ export default class SecurityContext extends Context {
265265

266266
declare module '@eggjs/core' {
267267
interface Context {
268-
get securityOptions(): Partial<SecurityConfig>;
268+
get securityOptions(): Partial<SecurityConfig & SecurityHelperConfig>;
269269
isSafeDomain(domain: string, customWhiteList?: string[]): boolean;
270270
get nonce(): string;
271271
get csrf(): string;
272-
ensureCsrfSecret(rotate: boolean): void;
272+
ensureCsrfSecret(rotate?: boolean): void;
273273
rotateCsrfSecret(): void;
274274
assertCsrf(): void;
275275
safeCurl(url: HttpClientRequestURL, options?: HttpClientOptions): HttpClientRequestReturn;

‎src/app/extend/helper.js‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎src/app/extend/helper.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import helpers from '../../lib/helper/index.js';
2+
3+
export default {
4+
...helpers,
5+
};

‎src/app/extend/response.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Response as KoaResponse } from '@eggjs/core';
2+
import SecurityContext from './context.js';
23

34
const unsafeRedirect = KoaResponse.prototype.redirect;
45

56
export default class SecurityResponse extends KoaResponse {
7+
declare ctx: SecurityContext;
8+
69
/**
710
* This is an unsafe redirection, and we WON'T check if the
811
* destination url is safe or not.
@@ -84,5 +87,6 @@ declare module '@eggjs/core' {
8487
// add Response overrides types
8588
interface Response {
8689
unsafeRedirect(url: string, alt?: string): void;
90+
redirect(url: string, alt?: string): void;
8791
}
8892
}

‎src/app/middleware/securities.js‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)