Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion middleware/encryptcookie/encryptcookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func New(config ...Config) fiber.Handler {
if !isDisabled(keyString, cfg.Except) {
decryptedValue, err := cfg.Decryptor(keyString, string(value), cfg.Key)
if err != nil {
c.Request().Header.DelCookieBytes(key)
c.Request().Header.SetCookie(string(key), "")
} else {
c.Request().Header.SetCookie(string(key), decryptedValue)
}
Expand Down
32 changes: 32 additions & 0 deletions middleware/encryptcookie/encryptcookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@
require.ErrorIs(t, err, ErrInvalidEncryptedValue)
}

func Test_Middleware_Decrypt_Invalid_Cookie_Does_Not_Panic(t *testing.T) {
t.Parallel()

testKey := GenerateKey(32)
app := fiber.New()

app.Use(New(Config{
Key: testKey,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "sidebar:state"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mentioned problem has no relation with sidebar:state key. it is happening on every unexpected and non encrypted cookie.

}))

app.Get("/", func(c fiber.Ctx) error {
return c.SendString("value=" + c.Cookies("test"))
})

// Send a request with an unencrypted/invalid cookie value
// This should not panic and should clear the cookie value
req := httptest.NewRequest(fiber.MethodGet, "/", http.NoBody)
req.AddCookie(&http.Cookie{
Name: "test",
Value: "plaintext-unencrypted-value",
})

resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)

// The cookie value should be empty since decryption failed
body := make([]byte, 64)
n, _ := resp.Body.Read(body)

Check failure on line 138 in middleware/encryptcookie/encryptcookie_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `resp.Body.Read` is not checked (errcheck)
require.Equal(t, "value=", string(body[:n]))
}

func Test_Middleware_EncryptionErrorPropagates(t *testing.T) {
t.Parallel()

Expand Down
Loading