Skip to content

Commit 0d156a1

Browse files
committed
chore: unsafe session cookies for development
1 parent 0db91f5 commit 0d156a1

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

internal/config/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ sessions:
2222
- issuer: https://renkulab.io/auth/realms/Renku
2323
audience: renku
2424
authorizedParty: renku-cli
25+
unsafeCookieTemplate: false
2526
revproxy:
2627
renkuBaseUrl: "https://renkulab.io"
2728
externalGitlabUrl:

internal/config/session.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type SessionConfig struct {
1111
// NOTE: UnsafeNoCookieHandler should only be used for testing, in production this has to be false/unset
1212
// without this there is no CSRF protection on the oauth callback endpoint
1313
UnsafeNoCookieHandler bool
14+
// NOTE: Unsafe cookie template should only be used for testing. It is NOT SAFE for production.
15+
UnsafeCookieTemplate bool
1416
}
1517

1618
type AuthorizationVerifier struct {
@@ -29,5 +31,8 @@ func (c *SessionConfig) Validate(e RunningEnvironment) error {
2931
if e != Development && c.UnsafeNoCookieHandler {
3032
return fmt.Errorf("a cookie handler needs to be configured in production")
3133
}
34+
if e != Development && c.UnsafeCookieTemplate {
35+
return fmt.Errorf("a safe cookie template needs to be configured in production")
36+
}
3237
return nil
3338
}

internal/sessions/session_store.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,24 @@ func WithConfig(c config.SessionConfig) SessionStoreOption {
312312
}
313313
sessions.cookieHandler = securecookie.New(cookieHashKey, cookieEncKey)
314314
}
315+
if c.UnsafeCookieTemplate {
316+
unsafeCookieTmpl := func() http.Cookie {
317+
defaultTmpl := defaultSessionCookieTemplate()
318+
return http.Cookie{
319+
Name: defaultTmpl.Name,
320+
Path: defaultTmpl.Path,
321+
Domain: defaultTmpl.Domain,
322+
Expires: defaultTmpl.Expires,
323+
MaxAge: defaultTmpl.MaxAge,
324+
// NOTE: Secure needs to be true so that the SameSite = None works
325+
// Enables calling a deployed backend from a local ui client version running on localhost
326+
Secure: true,
327+
HttpOnly: false,
328+
SameSite: http.SameSiteNoneMode,
329+
}
330+
}
331+
sessions.cookieTemplate = unsafeCookieTmpl
332+
}
315333

316334
sessions.sessionMaker = NewSessionMaker(WithIdleSessionTTLSeconds(c.IdleSessionTTLSeconds), WithMaxSessionTTLSeconds(c.MaxSessionTTLSeconds))
317335

@@ -333,16 +351,18 @@ func WithCookieHandler(h models.CookieHandler) SessionStoreOption {
333351
}
334352
}
335353

354+
func defaultSessionCookieTemplate() http.Cookie {
355+
return http.Cookie{
356+
Name: SessionCookieName,
357+
Path: "/",
358+
Secure: true,
359+
HttpOnly: true,
360+
SameSite: http.SameSiteLaxMode}
361+
}
362+
336363
func NewSessionStore(options ...SessionStoreOption) (*SessionStore, error) {
337364
sessions := SessionStore{
338-
cookieTemplate: func() http.Cookie {
339-
return http.Cookie{
340-
Name: SessionCookieName,
341-
Path: "/",
342-
Secure: true,
343-
HttpOnly: true,
344-
SameSite: http.SameSiteLaxMode}
345-
},
365+
cookieTemplate: defaultSessionCookieTemplate,
346366
}
347367
for _, opt := range options {
348368
err := opt(&sessions)

0 commit comments

Comments
 (0)