Skip to content

Commit f3d86a9

Browse files
committed
chore: update
1 parent 0ce844c commit f3d86a9

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ More encryption than the original version, using a more secure aes-256-gcm algor
1010

1111
```ts
1212
import * as Cookies from '@midwayjs/cookies');
13-
ctx.cookies = new Cookies(ctx, keys[, defaultCookieOptions]);
13+
ctx.cookies = new Cookies(ctx, keys[, defaultCookieOptions[, defaultGetCookieOptions]]);
1414
ctx.cookies.set('foo', 'bar', { encrypt: true });
1515
ctx.cookies.get('foo', { encrypt: true });
1616
```
@@ -41,6 +41,15 @@ Read a cookie through `cookies.get(key, value, options)`. The parameters support
4141
- signed - Whether `Boolean` needs to verify the cookie, and pass the signed parameter when cooperating with the set. At this time, the front-end cannot tamper with the cookie. The default is true.
4242
- encrypt - Whether `Boolean` needs to decrypt the cookie, and pass the encrypt parameter when cooperating with the set. At this time, the front-end cannot read the real cookie value, and the default is false.
4343

44+
You can also set default options for `get` method by passing `defaultGetCookieOptions` when initializing Cookies:
45+
46+
```ts
47+
const cookies = new Cookies(ctx, keys, defaultCookieOptions, { signed: false });
48+
// Now cookies.get('foo') will use signed: false by default
49+
```
50+
51+
**⚠️ Security Warning: Setting `signed: false` in `defaultGetCookieOptions` is dangerous as it disables cookie signature verification by default. This makes your application vulnerable to cookie tampering attacks. Only use this option if you fully understand the security implications and have a specific reason to disable signature verification.**
52+
4453
## Delete cookie
4554

4655
Use `cookie.set(key, null)` to delete a cookie. If the `signed` parameter is passed, the signature will also be deleted.

README.zh_CN.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
```ts
1212
import * as Cookies from '@midwayjs/cookies');
13-
ctx.cookies = new Cookies(ctx, keys[, defaultCookieOptions]);
13+
ctx.cookies = new Cookies(ctx, keys[, defaultCookieOptions[, defaultGetCookieOptions]]);
1414
ctx.cookies.set('foo', 'bar', { encrypt: true });
1515
ctx.cookies.get('foo', { encrypt: true });
1616
```
@@ -47,6 +47,15 @@ ctx.cookies.get('foo', { encrypt: true });
4747
- signed - `Boolean` 是否需要对 cookie 进行验签,需要配合 set 时传递 signed 参数,此时前端无法篡改这个 cookie,默认为 true。
4848
- encrypt - `Boolean` 是否需要对 cookie 进行解密,需要配合 set 时传递 encrypt 参数,此时前端无法读到真实的 cookie 值,默认为 false。
4949

50+
你也可以在初始化 Cookies 时通过 `defaultGetCookieOptions` 参数设置 `get` 方法的默认选项:
51+
52+
```ts
53+
const cookies = new Cookies(ctx, keys, defaultCookieOptions, { signed: false });
54+
// 现在 cookies.get('foo') 将默认使用 signed: false
55+
```
56+
57+
**⚠️ 安全警告:在 `defaultGetCookieOptions` 中设置 `signed: false` 是非常危险的行为,因为它会默认禁用 cookie 签名验证。这会使你的应用容易受到 cookie 篡改攻击。只有在完全理解安全影响并有特定原因需要禁用签名验证的情况下,才应该使用此选项。**
58+
5059
## 删除 cookie
5160

5261
通过 `cookie.set(key, null)` 来删除一个 cookie。如果传递了 `signed` 参数,签名也会被删除。

0 commit comments

Comments
 (0)