Skip to content
Closed
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
4 changes: 4 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
cookie.Path = "/"
}

if utils.ToLower(cookie.SameSite) == CookieSameSiteNoneMode && !cookie.Secure {
Comment thread
gaby marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To avoid allocations, you should use utils.EqualFold instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also related #3608

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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{}
Expand Down
43 changes: 43 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 1125 in ctx_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofmt)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

鈿狅笍 Potential issue

Fix formatting issue

Static analysis tools have detected a formatting issue on this line. Please run gofmt to fix the indentation.

-		sameSite	string
+		sameSite    string
馃摑 Committable suggestion

鈥硷笍 IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sameSite string
testCases := []struct {
description string
sameSite string
}{
馃О Tools
馃獩 GitHub Check: lint

[failure] 1125-1125:
File is not properly formatted (gofmt)

馃獩 GitHub Actions: golangci-lint

[error] 1125-1125: File is not properly formatted (gofmt)

馃 Prompt for AI Agents
In ctx_test.go at line 1125, the declaration of the sameSite string variable has
a formatting issue. Run the `gofmt` tool on this file to automatically fix the
indentation and formatting according to Go standards.

}{
{
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()
Expand Down
Loading