Skip to content

Commit cd4437c

Browse files
authored
[4.0.1 backport] CBG-4972 don't set SameSite:None without Secure (#7852)
1 parent 01df412 commit cd4437c

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

rest/cors_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,35 @@ func TestCORSLoginOriginPerDatabase(t *testing.T) {
412412
name string
413413
unsupportedOptions *db.UnsupportedOptions
414414
sameSite http.SameSite
415+
useTLS bool
415416
}{
416417
{
417-
name: "No unsupported options",
418+
name: "No unsupported options with TLS",
418419
unsupportedOptions: nil,
419420
sameSite: http.SameSiteNoneMode,
421+
useTLS: true,
420422
},
421423
{
422-
name: "With unsupported options",
424+
name: "No unsupported options without TLS",
425+
unsupportedOptions: nil,
426+
sameSite: 0, // go 1.25 doesn't have a constant for not present when reading from Set-Cookie, this could turn into SameSiteDefaultMode (1) in future
427+
useTLS: false,
428+
},
429+
{
430+
name: "With unsupported options and TLS",
423431
unsupportedOptions: &db.UnsupportedOptions{
424432
SameSiteCookie: base.Ptr("Strict"),
425433
},
426434
sameSite: http.SameSiteStrictMode,
435+
useTLS: true,
436+
},
437+
{
438+
name: "With unsupported options and no TLS",
439+
unsupportedOptions: &db.UnsupportedOptions{
440+
SameSiteCookie: base.Ptr("Strict"),
441+
},
442+
sameSite: http.SameSiteStrictMode, // forces strict mode even though this would result in an unusable cookie
443+
useTLS: false,
427444
},
428445
}
429446
for _, dbTestCases := range testCases {
@@ -432,6 +449,13 @@ func TestCORSLoginOriginPerDatabase(t *testing.T) {
432449
rt := NewRestTesterPersistentConfigNoDB(t)
433450
defer rt.Close()
434451

452+
// fake TLS on public port
453+
if dbTestCases.useTLS {
454+
rt.ServerContext().Config.API.HTTPS.TLSCertPath = "/pretend/valid/cert"
455+
} else {
456+
require.Empty(t, rt.ServerContext().Config.API.HTTPS.TLSCertPath)
457+
}
458+
435459
dbConfig := rt.NewDbConfig()
436460
dbConfig.Unsupported = dbTestCases.unsupportedOptions
437461
dbConfig.CORS = &auth.CORSConfig{
@@ -480,7 +504,7 @@ func TestCORSLoginOriginPerDatabase(t *testing.T) {
480504
cookie, err := http.ParseSetCookie(resp.Header().Get("Set-Cookie"))
481505
require.NoError(t, err)
482506
require.NotEmpty(t, cookie.Path)
483-
require.Equal(t, dbTestCases.sameSite, cookie.SameSite)
507+
require.Equal(t, dbTestCases.sameSite, cookie.SameSite, "Cookie=%#+v", cookie)
484508
reqHeaders["Cookie"] = fmt.Sprintf("%s=%s", cookie.Name, cookie.Value)
485509
}
486510
resp = rt.SendRequestWithHeaders(http.MethodDelete, "/{{.db}}/_session", "", reqHeaders)

rest/server_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config
948948
} else {
949949
dbcontext.CORS = sc.Config.API.CORS
950950
}
951-
if !dbcontext.CORS.IsEmpty() {
951+
if !dbcontext.CORS.IsEmpty() && dbcontext.Options.SecureCookieOverride {
952952
dbcontext.SameSiteCookieMode = http.SameSiteNoneMode
953953
}
954954
if config.Unsupported != nil && config.Unsupported.SameSiteCookie != nil {

rest/session_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ import (
2525

2626
func TestCORSLoginOriginOnSessionPost(t *testing.T) {
2727

28-
rt := NewRestTester(t, nil)
28+
rt := NewRestTesterPersistentConfigNoDB(t)
2929
defer rt.Close()
3030

31+
// force TLS mode to test SameSite=None cookie attribute
32+
rt.ServerContext().Config.API.HTTPS.TLSCertPath = "/pretend/valid/cert"
33+
34+
RequireStatus(t, rt.CreateDatabase("db", rt.NewDbConfig()), http.StatusCreated)
35+
3136
reqHeaders := map[string]string{
3237
"Origin": "http://example.com",
3338
}

0 commit comments

Comments
 (0)