From 7be49b96c88fe928a272e0bc56d3b99da9e9c9a2 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Mon, 30 Mar 2026 01:33:00 +0800 Subject: [PATCH 1/2] feat(cookie): add SameSite support to SetCookie method --- context.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 09035b2..f1d0968 100644 --- a/context.go +++ b/context.go @@ -1160,17 +1160,22 @@ func (c *Context) SetSameSite(samesite http.SameSite) { } // SetCookie 设置一个 HTTP cookie -func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) { +// sameSite 参数是可选的,如果不提供则使用通过 SetSameSite 设置的值 +func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool, sameSite ...http.SameSite) { if path == "" { path = "/" } + site := c.sameSite + if len(sameSite) > 0 { + site = sameSite[0] + } http.SetCookie(c.Writer, &http.Cookie{ Name: name, Value: url.QueryEscape(value), MaxAge: maxAge, Path: path, Domain: domain, - SameSite: c.sameSite, + SameSite: site, Secure: secure, HttpOnly: httpOnly, }) From 9f210deadf5ea3fe4a2ac3d3ed863090e3f60654 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Mon, 30 Mar 2026 01:42:10 +0800 Subject: [PATCH 2/2] fix(cookie): add warning log when multiple SameSite values provided --- context.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/context.go b/context.go index f1d0968..2e4d2bb 100644 --- a/context.go +++ b/context.go @@ -1167,6 +1167,9 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, } site := c.sameSite if len(sameSite) > 0 { + if len(sameSite) > 1 { + c.Warnf("SetCookie: only the first SameSite value will be used, got %d values", len(sameSite)) + } site = sameSite[0] } http.SetCookie(c.Writer, &http.Cookie{