-
-
Notifications
You must be signed in to change notification settings - Fork 2k
馃悰 bug: Fix cookie secure flag with SameSite None #3610
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -425,6 +425,10 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) { | |
| cookie.Path = "/" | ||
| } | ||
|
|
||
| if utils.ToLower(cookie.SameSite) == CookieSameSiteNoneMode && !cookie.Secure { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid allocations, you should use utils.EqualFold instead.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also related #3608
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sixcolors I'm closing this PR then. |
||
| cookie.Secure = true | ||
| } | ||
|
|
||
| if cookie.SessionOnly { | ||
| cookie.MaxAge = 0 | ||
| cookie.Expires = time.Time{} | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1112,6 +1112,49 @@ | |||||||||||
| require.Equal(t, "ps=v; path=/; secure; SameSite=None; Partitioned", c.Res().Get(HeaderSetCookie)) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // go test -run Test_Ctx_Cookie_SameSiteNoneAutoSecure | ||||||||||||
| func Test_Ctx_Cookie_SameSiteNoneAutoSecure(t *testing.T) { | ||||||||||||
| app := New() | ||||||||||||
| c := app.AcquireCtx(&fasthttp.RequestCtx{}) | ||||||||||||
| t.Cleanup(func() { | ||||||||||||
| app.ReleaseCtx(c) | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| testCases := []struct { | ||||||||||||
| description string | ||||||||||||
| sameSite string | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix formatting issue Static analysis tools have detected a formatting issue on this line. Please run - sameSite string
+ sameSite string馃摑 Committable suggestion
Suggested change
馃О Tools馃獩 GitHub Check: lint[failure] 1125-1125: 馃獩 GitHub Actions: golangci-lint[error] 1125-1125: File is not properly formatted (gofmt) 馃 Prompt for AI Agents |
||||||||||||
| }{ | ||||||||||||
| { | ||||||||||||
| description: "samesite is 'none'", | ||||||||||||
| sameSite: CookieSameSiteNoneMode, | ||||||||||||
| }, | ||||||||||||
| { | ||||||||||||
| description: "samesite is 'None'", | ||||||||||||
| sameSite: "None", | ||||||||||||
| }, | ||||||||||||
| { | ||||||||||||
| description: "samesite is 'NONE'", | ||||||||||||
| sameSite: "NONE", | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| for _, tc := range testCases { | ||||||||||||
| t.Run(tc.description, func(t *testing.T) { | ||||||||||||
| // Reset response header for each sub-test to ensure a clean state | ||||||||||||
| c.Response().Header.Reset() | ||||||||||||
|
|
||||||||||||
| ck := &Cookie{ | ||||||||||||
| Name: "auto", | ||||||||||||
| Value: "v", | ||||||||||||
| SameSite: tc.sameSite, | ||||||||||||
| } | ||||||||||||
| c.Res().Cookie(ck) | ||||||||||||
|
|
||||||||||||
| require.Equal(t, "auto=v; path=/; secure; SameSite=None", c.Res().Get(HeaderSetCookie)) | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // go test -run Test_Ctx_Cookie_Invalid | ||||||||||||
| func Test_Ctx_Cookie_Invalid(t *testing.T) { | ||||||||||||
| t.Parallel() | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.