Make auth cookie SameSite configurable

Add same-site to auth cookie config with lax as the default,
PROBOD_AUTH_COOKIE_SAMESITE bootstrap mapping, and validation
that rejects none unless Secure is enabled.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
Cursor Agent
2026-07-29 16:37:28 +00:00
committed by Bryan Frimin
parent cd6c46212a
commit 62d0ab68c4
9 changed files with 266 additions and 26 deletions

View File

@@ -23,8 +23,34 @@ package probodconfig
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)
type CookieSameSite string
const (
CookieSameSiteLax CookieSameSite = "lax"
CookieSameSiteStrict CookieSameSite = "strict"
CookieSameSiteNone CookieSameSite = "none"
)
func ParseCookieSameSite(raw string) (CookieSameSite, error) {
switch strings.ToLower(strings.TrimSpace(raw)) {
case "", string(CookieSameSiteLax):
return CookieSameSiteLax, nil
case string(CookieSameSiteStrict):
return CookieSameSiteStrict, nil
case string(CookieSameSiteNone):
return CookieSameSiteNone, nil
default:
return "", fmt.Errorf(
"invalid same-site value %q: must be lax, strict, or none",
raw,
)
}
}
type AuthConfig struct {
Cookie CookieConfig `json:"cookie"`
Password PasswordConfig `json:"password"`
@@ -55,11 +81,38 @@ type OAuth2SigningKeyConfig struct {
}
type CookieConfig struct {
Domain string `json:"domain,omitempty"`
Secret string `json:"secret"`
Duration int `json:"duration"`
Name string `json:"name,omitempty"`
Secure bool `json:"secure"`
Domain string `json:"domain,omitempty"`
Secret string `json:"secret"`
Duration int `json:"duration"`
Name string `json:"name,omitempty"`
Secure bool `json:"secure"`
SameSite CookieSameSite `json:"same-site,omitempty"`
}
func (c CookieConfig) HTTPSameSite() (http.SameSite, error) {
switch c.SameSite {
case "", CookieSameSiteLax:
return http.SameSiteLaxMode, nil
case CookieSameSiteStrict:
return http.SameSiteStrictMode, nil
case CookieSameSiteNone:
return http.SameSiteNoneMode, nil
default:
return 0, fmt.Errorf("invalid cookie same-site value %q", c.SameSite)
}
}
func (c CookieConfig) Validate() error {
sameSite, err := c.HTTPSameSite()
if err != nil {
return err
}
if sameSite == http.SameSiteNoneMode && !c.Secure {
return fmt.Errorf("cookie same-site none requires secure cookies")
}
return nil
}
type PasswordConfig struct {

View File

@@ -0,0 +1,121 @@
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package probodconfig_test
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/probodconfig"
)
func TestParseCookieSameSite(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want probodconfig.CookieSameSite
wantErr bool
}{
{name: "empty", input: "", want: probodconfig.CookieSameSiteLax},
{name: "lax", input: "lax", want: probodconfig.CookieSameSiteLax},
{name: "Lax", input: "Lax", want: probodconfig.CookieSameSiteLax},
{name: "strict", input: "strict", want: probodconfig.CookieSameSiteStrict},
{name: "none", input: "none", want: probodconfig.CookieSameSiteNone},
{name: "invalid", input: "cross-site", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := probodconfig.ParseCookieSameSite(tt.input)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestCookieConfig_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cookie probodconfig.CookieConfig
wantErr bool
}{
{
name: "lax without secure",
cookie: probodconfig.CookieConfig{
SameSite: probodconfig.CookieSameSiteLax,
Secure: false,
},
},
{
name: "none requires secure",
cookie: probodconfig.CookieConfig{
SameSite: probodconfig.CookieSameSiteNone,
Secure: false,
},
wantErr: true,
},
{
name: "none with secure",
cookie: probodconfig.CookieConfig{
SameSite: probodconfig.CookieSameSiteNone,
Secure: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.cookie.Validate()
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestCookieConfig_HTTPSameSite(t *testing.T) {
t.Parallel()
cookie := probodconfig.CookieConfig{SameSite: probodconfig.CookieSameSiteStrict}
got, err := cookie.HTTPSameSite()
require.NoError(t, err)
assert.Equal(t, http.SameSiteStrictMode, got)
}