Skip to content

Commit e4d7e84

Browse files
authored
chore(encryptcookie)!: update default config (#2753)
* chore(encryptcookie)!: update default config docs(encryptcookie): enhance documentation and examples BREAKING CHANGE: removed the hardcoded "csrf_" from the Except. * docs(encryptcookie): reads or modifies cookies * chore(encryptcookie): csrf config example * docs(encryptcookie): md table spacing
1 parent a80b288 commit e4d7e84

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

docs/api/middleware/encryptcookie.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ id: encryptcookie
44

55
# Encrypt Cookie
66

7-
Encrypt middleware for [Fiber](https://github.com/gofiber/fiber) which encrypts cookie values. Note: this middleware does not encrypt cookie names.
7+
Encrypt Cookie is a middleware for [Fiber](https://github.com/gofiber/fiber) that secures your cookie values through encryption.
8+
9+
:::note
10+
This middleware encrypts cookie values and not the cookie names.
11+
:::
812

913
## Signatures
1014

@@ -18,7 +22,7 @@ func GenerateKey() string
1822

1923
## Examples
2024

21-
Import the middleware package that is part of the Fiber web framework
25+
To use the Encrypt Cookie middleware, first, import the middleware package as part of the Fiber web framework:
2226

2327
```go
2428
import (
@@ -27,23 +31,20 @@ import (
2731
)
2832
```
2933

30-
After you initiate your Fiber app, you can use the following possibilities:
34+
Once you've imported the middleware package, you can use it inside your Fiber app:
3135

3236
```go
33-
// Provide a minimal config
34-
// `Key` must be a 32 character string. It's used to encrypt the values, so make sure it is random and keep it secret.
35-
// You can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey()` to create a random key for you.
36-
// Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
37+
// Provide a minimal configuration
3738
app.Use(encryptcookie.New(encryptcookie.Config{
3839
Key: "secret-thirty-2-character-string",
3940
}))
4041

41-
// Get / reading out the encrypted cookie
42+
// Retrieve the encrypted cookie value
4243
app.Get("/", func(c *fiber.Ctx) error {
4344
return c.SendString("value=" + c.Cookies("test"))
4445
})
4546

46-
// Post / create the encrypted cookie
47+
// Create an encrypted cookie
4748
app.Post("/", func(c *fiber.Ctx) error {
4849
c.Cookie(&fiber.Cookie{
4950
Name: "test",
@@ -53,39 +54,48 @@ app.Post("/", func(c *fiber.Ctx) error {
5354
})
5455
```
5556

57+
:::note
58+
`Key` must be a 32 character string. It's used to encrypt the values, so make sure it is random and keep it secret.
59+
You can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey()` to create a random key for you.
60+
Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
61+
:::
62+
5663
## Config
5764

58-
| Property | Type | Description | Default |
59-
|:----------|:----------------------------------------------------|:----------------------------------------------------------------------------------------------------|:-----------------------------|
60-
| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
61-
| Except | `[]string` | Array of cookie keys that should not be encrypted. | `[]` |
62-
| Key | `string` | Base64 encoded unique key to encode & decode cookies. Required. Key length should be 32 characters. | (No default, required field) |
63-
| Encryptor | `func(decryptedString, key string) (string, error)` | Custom function to encrypt cookies. | `EncryptCookie` |
64-
| Decryptor | `func(encryptedString, key string) (string, error)` | Custom function to decrypt cookies. | `DecryptCookie` |
65+
| Property | Type | Description | Default |
66+
|:----------|:----------------------------------------------------|:------------------------------------------------------------------------------------------------------|:-----------------------------|
67+
| Next | `func(*fiber.Ctx) bool` | A function to skip this middleware when returned true. | `nil` |
68+
| Except | `[]string` | Array of cookie keys that should not be encrypted. | `[]` |
69+
| Key | `string` | A base64-encoded unique key to encode & decode cookies. Required. Key length should be 32 characters. | (No default, required field) |
70+
| Encryptor | `func(decryptedString, key string) (string, error)` | A custom function to encrypt cookies. | `EncryptCookie` |
71+
| Decryptor | `func(encryptedString, key string) (string, error)` | A custom function to decrypt cookies. | `DecryptCookie` |
6572

6673
## Default Config
6774

6875
```go
6976
var ConfigDefault = Config{
7077
Next: nil,
71-
Except: []string{"csrf_"},
78+
Except: []string{},
7279
Key: "",
7380
Encryptor: EncryptCookie,
7481
Decryptor: DecryptCookie,
7582
}
7683
```
7784

78-
## Usage of CSRF and Encryptcookie Middlewares with Custom Cookie Names
79-
Normally, encryptcookie middleware skips `csrf_` cookies. However, it won't work when you use custom cookie names for CSRF. You should update `Except` config to avoid this problem. For example:
85+
## Usage With Other Middlewares That Reads Or Modify Cookies
86+
Place the encryptcookie middleware before any other middleware that reads or modifies cookies. For example, if you are using the CSRF middleware, ensure that the encryptcookie middleware is placed before it. Failure to do so may prevent the CSRF middleware from reading the encrypted cookie.
87+
88+
You may also choose to exclude certain cookies from encryption. For instance, if you are using the CSRF middleware with a frontend framework like Angular, and the framework reads the token from a cookie, you should exclude that cookie from encryption. This can be achieved by adding the cookie name to the Except array in the configuration:
8089

8190
```go
8291
app.Use(encryptcookie.New(encryptcookie.Config{
83-
Key: "secret-thirty-2-character-string",
84-
Except: []string{"csrf_1"}, // exclude CSRF cookie
92+
Key: "secret-thirty-2-character-string",
93+
Except: []string{csrf.ConfigDefault.CookieName}, // exclude CSRF cookie
8594
}))
8695
app.Use(csrf.New(csrf.Config{
87-
KeyLookup: "form:test",
88-
CookieName: "csrf_1",
89-
CookieHTTPOnly: true,
96+
KeyLookup: "header:" + csrf.HeaderName,
97+
CookieSameSite: "Lax",
98+
CookieSecure: true,
99+
CookieHTTPOnly: false,
90100
}))
91101
```

middleware/encryptcookie/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Config struct {
3636
// ConfigDefault is the default config
3737
var ConfigDefault = Config{
3838
Next: nil,
39-
Except: []string{"csrf_"},
39+
Except: []string{},
4040
Key: "",
4141
Encryptor: EncryptCookie,
4242
Decryptor: DecryptCookie,

0 commit comments

Comments
 (0)