From 86265e039bd1300eb285fa68ea725e921d217699 Mon Sep 17 00:00:00 2001 From: Dv2Corn <320380254+Dv2Corn@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:52:45 +0000 Subject: [PATCH] fix(context): return cookie values that are not query-escaped --- context.go | 6 +++++- context_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 582d00f2..ed40dc9f 100644 --- a/context.go +++ b/context.go @@ -1193,7 +1193,11 @@ func (c *Context) Cookie(name string) (string, error) { if err != nil { return "", err } - val, _ := url.QueryUnescape(cookie.Value) + val := cookie.Value + // A value that cannot be unescaped was not written by SetCookie, return it as received. + if unescaped, err := url.QueryUnescape(val); err == nil { + val = unescaped + } return val, nil } diff --git a/context_test.go b/context_test.go index b82e04d5..44b25a72 100644 --- a/context_test.go +++ b/context_test.go @@ -1142,6 +1142,27 @@ func TestContextGetCookie(t *testing.T) { require.Error(t, err) } +func TestContextGetCookieNotEscaped(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest(http.MethodGet, "/get", nil) + // Cookies set by other software are not necessarily query-escaped. + c.Request.Header.Set("Cookie", "discount=50%; progress=100%done; user=John+Doe%21") + + for name, want := range map[string]string{ + "discount": "50%", + "progress": "100%done", + "user": "John Doe!", + } { + std, err := c.Request.Cookie(name) + require.NoError(t, err) + assert.NotEmpty(t, std.Value) + + cookie, err := c.Cookie(name) + require.NoError(t, err) + assert.Equal(t, want, cookie) + } +} + func TestContextBodyAllowedForStatus(t *testing.T) { assert.False(t, bodyAllowedForStatus(http.StatusContinue)) assert.False(t, bodyAllowedForStatus(http.StatusProcessing))