Do not QueryEscape cookies

This commit is contained in:
mr.liusg 2024-06-21 16:43:21 +08:00 committed by GitHub
parent 9c081de9cd
commit 7b9bca9cef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View File

@ -946,7 +946,7 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
} }
http.SetCookie(c.Writer, &http.Cookie{ http.SetCookie(c.Writer, &http.Cookie{
Name: name, Name: name,
Value: url.QueryEscape(value), Value: value,
MaxAge: maxAge, MaxAge: maxAge,
Path: path, Path: path,
Domain: domain, Domain: domain,
@ -965,8 +965,7 @@ func (c *Context) Cookie(name string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
val, _ := url.QueryUnescape(cookie.Value) return cookie.Value, nil
return val, nil
} }
// Render writes the response headers and calls render.Render to render data. // Render writes the response headers and calls render.Render to render data.

View File

@ -685,6 +685,13 @@ func TestContextSetCookiePathEmpty(t *testing.T) {
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie")) assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie"))
} }
func TestContextSetCookieWithSpace(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("user", "gin test", 1, "/", "localhost", true, true)
assert.Equal(t, "user=\"gin test\"; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie"))
}
func TestContextGetCookie(t *testing.T) { func TestContextGetCookie(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "/get", nil) c.Request, _ = http.NewRequest("GET", "/get", nil)