Skip to content

feat: make http session cookie path & duration configurable - #10564

Closed
vvaswani wants to merge 7 commits into
syncthing:mainfrom
vvaswani:10522-http
Closed

vvaswani wants to merge 7 commits into
syncthing:mainfrom
vvaswani:10522-http

Conversation

@vvaswani

@vvaswani vvaswani commented Feb 7, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Fixes #10522

Testing

Tested with:

go test ./lib/config -run 'TestGUISessionCookiePathPrepare|TestGUIConfigURL'
go test -tags noassets ./lib/api -run 'TestTokenManagerNoExpiry|TestSessionCookieMaxAgeNoExpiry|TestSessionCookiePathNormalization|TestTokenManager

Authorship

Your name and email will be added automatically to the AUTHORS file
based on the commit metadata.

@github-actions github-actions Bot added the enhancement New features or improvements of some kind, as opposed to a problem (bug) label Feb 7, 2026
@steadytao

Copy link
Copy Markdown
Contributor

Aligning the session duration with a configurable value rather than the hard-coded 7 days addresses the issue raised. However, there are a couple of things worth reviewing:

  • For noExpiryNano, it may be safer to use math.MaxInt64 or explicitly type the const as an int64 to avoid any potential shift/overflow edge cases with 1<<63.
  • When allowing a cookie to effectively never expire (using a large Max-Age), it may be worth noting that there are potential security implications if a session cookie is stolen.
  • For SessionCookiePath, prefixing with "/" is good. It may also be worth trimming whitespace or guarding against unexpected values.

It would also help to include a short “Changes Made” or brief testing note instead of just “Not tested”, even if only manual verification steps.

@vvaswani

Copy link
Copy Markdown
Contributor Author

Aligning the session duration with a configurable value rather than the hard-coded 7 days addresses the issue raised. However, there are a couple of things worth reviewing:

* For noExpiryNano, it may be safer to use math.MaxInt64 or explicitly type the const as an int64 to avoid any potential shift/overflow edge cases with 1<<63.

* When allowing a cookie to effectively never expire (using a large Max-Age), it may be worth noting that there are potential security implications if a session cookie is stolen.

* For SessionCookiePath, prefixing with "/" is good. It may also be worth trimming whitespace or guarding against unexpected values.

It would also help to include a short “Changes Made” or brief testing note instead of just “Not tested”, even if only manual verification steps.

Updated

@steadytao

Copy link
Copy Markdown
Contributor

LGTM.

Thanks for addressing the typing change and path normalisation, and for adding tests around no-expiry and cookie path handling

@vvaswani

Copy link
Copy Markdown
Contributor Author

@steadytao are you able to merge this now?

@steadytao

Copy link
Copy Markdown
Contributor

@vvaswani I do not have maintainer access to SyncThing, I am only a contributor and Jakob is currently busy and has been for some time. Please give him some time to review your PR

@calmh calmh left a comment

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.

Some notes and questions

Comment thread lib/api/tokenmanager.go Resolved
Comment thread lib/api/tokenmanager.go Outdated
tokens *tokenManager
}

const defaultSessionCookieDurationS = 7 * 24 * 60 * 60

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.

Maybe keep this among the other constants?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, moved

Comment thread lib/config/guiconfiguration.go Outdated
c.SessionCookiePath = strings.TrimSpace(c.SessionCookiePath)
if c.SessionCookiePath == "" {
c.SessionCookiePath = "/"
} else if !strings.HasPrefix(c.SessionCookiePath, "/") {

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.

This check also covers the condition of the one above, so could be the only one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

True, changed

Comment thread lib/api/tokenmanager.go Outdated
Comment on lines +233 to +239
path := strings.TrimSpace(m.guiCfg.SessionCookiePath)
if path == "" {
return "/"
}
if !strings.HasPrefix(path, "/") {
return "/" + path
}

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.

Why do we repeat these checks here for every request, isn't it enough that we validated the config?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Mainly defensive but if you think it's not needed, I have updated it.

Comment thread lib/api/tokenmanager.go Outdated
Comment on lines +218 to +220
// this cookie is still only as safe as client storage;
// if stolen it remains valid until explicitly logged out.
return math.MaxInt32

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.

Can you expand on what you mean with this comment? I'm sure it's true, but not clear on how it relates to the code/maxint32?

@vvaswani vvaswani Apr 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This comment was added as per the request of @steadytao in #10564 (comment)

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.

Nonetheless, what does it mean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated comment to make it clearer

@calmh calmh changed the title feat: make http session duration configurable feat: make http session cookie path & duration configurable Apr 5, 2026

@calmh calmh left a comment

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.

(breaks tests, though frankly not sure why)

Signed-off-by: Vikram Vaswani <[email protected]>
@vvaswani

vvaswani commented Apr 5, 2026

Copy link
Copy Markdown
Contributor Author

(breaks tests, though frankly not sure why)

Fixed, please recheck when you can @calmh

@vvaswani
vvaswani requested a review from calmh April 5, 2026 19:59
Comment thread lib/config/guiconfiguration.go Outdated
Comment on lines +187 to +189
func (c GUIConfiguration) PreparedCopy() GUIConfiguration {
c.prepare()
return c

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.

Wait, why is this needed? I'm suspicious that we suddenly need to add another pattern for config handling that we didn't need before... Seems like we're adding a footgun of some kind that we should be handling differently.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PreparedCopy() is basically a normalization for read/comparison without mutating the original struct, because prepare() is already creating a mutation. Let me know if you have a better approach though.

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.

But why? Surely we do prepare() on load and unmarshal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was using PreparedCopy() because I understand that GUIConfiguration can also be constructed or copied outside the normal config paths, and I wanted to handle the case where callers want normalized values. However based on your comment I changed it now to use the existing Copy(). Tests are passing. LMK if this is better?

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.

I feel like this is heading down a strange path. :) We shouldn't need to change anything about how the configs are created, validated, or compared based on this change. The additional validation in prepare() is fine, and it's already called at the points it needs to be. If the problem you're trying to solve is in tests, the proper solution is to make sure the input data in the tests is correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have removed PreparedCopy() in my previous commit and using the existing prepare() now. Let me know if that's better.

Signed-off-by: Vikram Vaswani <[email protected]>

@calmh calmh left a comment

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.

I'll be more explicit

Comment thread lib/config/wrapper.go Outdated
// The returned Wrapper is a suture.Service, thus needs to be started (added to
// a supervisor).
func Wrap(path string, cfg Configuration, myID protocol.DeviceID, evLogger events.Logger) Wrapper {
cfg.GUI.prepare()

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.

This should not be be here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

Comment on lines +189 to +192
path := strings.TrimSpace(c.SessionCookiePath)
if !strings.HasPrefix(path, "/") {
path = "/" + path
}

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.

This belonged better in prepare() where you had it, imho, instead of doing the fixup on every call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread lib/api/tokenmanager.go Outdated
}

const (
noExpiryNano int64 = math.MaxInt64

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.

Again suggesting this should just be left at zero instead; are there downsides to that approach I'm not seeing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TBH this was added in response to the comment by @steadytao which says For noExpiryNano, it may be safer to use math.MaxInt64 or explicitly type the const as an int64 to avoid any potential shift/overflow edge cases with 1<<63.

I have changed it to 0 - however one point to note is that in Check() and saveLocked(), anything that is not noExpiryNano is treated as a Unix timestamp. Since 0 < now, a zero token is immediately expired and gets deleted. I'm not sure if this makes a tangible difference though.

Comment thread lib/api/tokenmanager.go Outdated
Comment on lines +236 to +238
func (m *tokenCookieManager) sessionCookiePath() string {
return m.guiCfg.EffectiveSessionCookiePath()
}

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.

Not really required any more

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deleted

Comment thread lib/api/api.go Outdated
Comment on lines +489 to +498
fromGUI := from.GUI.Copy()
toGUI := to.GUI.Copy()

if toGUI == fromGUI {
// No GUI changes, we're done here.
return true
}

if to.GUI.Theme != from.GUI.Theme {
s.statics.setTheme(to.GUI.Theme)
if toGUI.Theme != fromGUI.Theme {
s.statics.setTheme(toGUI.Theme)

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.

These changes should not be required I think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted

@vvaswani

vvaswani commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

Modified as per your suggestions @calmh - thanks for your patience. Also ran tests, seems OK.

vvaswani added 2 commits April 9, 2026 01:13
Signed-off-by: Vikram Vaswani <[email protected]>

fix: add feedback

Signed-off-by: Vikram Vaswani <[email protected]>
Signed-off-by: Vikram Vaswani <[email protected]>
@vvaswani

vvaswani commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor Author

I messed up the rebase, closing and reopening as new in #10632

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New features or improvements of some kind, as opposed to a problem (bug)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make duration of HTTP sessions configurable

3 participants