Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: Enforce key length for EncryptCookie middleware default functions #3056

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions docs/middleware/encryptcookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ This middleware encrypts cookie values and not the cookie names.
// Intitializes the middleware
func New(config ...Config) fiber.Handler

// Returns a random 32 character long string
func GenerateKey() string
// Returns a random 16, 24, 32 bytes encoded string
func GenerateKey(length) string
gaby marked this conversation as resolved.
Show resolved Hide resolved
```

## Examples
Expand Down Expand Up @@ -55,9 +55,9 @@ app.Post("/", func(c fiber.Ctx) error {
```

:::note
`Key` must be a 32 character string. It's used to encrypt the values, so make sure it is random and keep it secret.
You can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey()` to create a random key for you.
Make sure not to set `Key` to `encryptcookie.GenerateKey()` because that will create a new key every run.
`Key` must be a 16, 24, or 32 bytes encoded string. It's used to encrypt the values, so make sure it is random and keep it secret.
For example, you can run `openssl rand -base64 32` or call `encryptcookie.GenerateKey(32)` to create a random key for you.
Make sure not to set `Key` to `encryptcookie.GenerateKey(32)` because that will create a new key every run of the application.
gaby marked this conversation as resolved.
Show resolved Hide resolved
:::
gaby marked this conversation as resolved.
Show resolved Hide resolved

## Config
Expand Down Expand Up @@ -99,3 +99,22 @@ app.Use(csrf.New(csrf.Config{
CookieHTTPOnly: false,
}))
```

## Encryption Algorithms
The default Encryptor and Decryptor functions use `AES-256-GCM` for encryption and decryption. If you need to use `AES-128` or `AES-192` instead, you can do so by changing the length of the key when calling `encryptcookie.GenerateKey(length)` or by providing a key of one of the following lengths:

- AES-128 requires a 16-byte key.
- AES-192 requires a 24-byte key.
- AES-256 requires a 32-byte key.

For example, to generate a key for AES-128:

```go
key := encryptcookie.GenerateKey(16)
```

And for AES-192:

```go
key := encryptcookie.GenerateKey(24)
```
gaby marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ We've updated several fields from a single string (containing comma-separated va

We've added support for `zstd` compression on top of `gzip`, `deflate`, and `brotli`.

### EncryptCookie

Added support for specifying Key length when using `encryptcookie.GenerateKey(length)`. This allows the user to generate keys compatible with `AES-128`, `AES-192`, and `AES-256` (Default).

### Session

:::caution
Expand Down
10 changes: 5 additions & 5 deletions middleware/encryptcookie/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ type Config struct {

// Base64 encoded unique key to encode & decode cookies.
//
// Required. Key length should be 32 characters.
// You may use `encryptcookie.GenerateKey()` to generate a new key.
// Required. Key length should be 16, 24, or 32 bytes when decoded
// if using the default EncryptCookie and DecryptCookie functions.
// You may use `encryptcookie.GenerateKey(length)` to generate a new key.
Key string

// Custom function to encrypt cookies.
//
// Optional. Default: EncryptCookie
// Optional. Default: EncryptCookie (using AES-256-GCM)
gaby marked this conversation as resolved.
Show resolved Hide resolved
Encryptor func(decryptedString, key string) (string, error)

// Custom function to decrypt cookies.
//
// Optional. Default: DecryptCookie
// Optional. Default: DecryptCookie (using AES-256-GCM)
gaby marked this conversation as resolved.
Show resolved Hide resolved
Decryptor func(encryptedString, key string) (string, error)
}

Expand All @@ -52,7 +53,6 @@ func configDefault(config ...Config) Config {
cfg = config[0]

// Set default values

if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
Expand Down
Loading
Loading