Skip to content

cookie_session: set path to / so router wildcards don't mess up sessions#387

Open
dbp wants to merge 1 commit intocamlworks:masterfrom
dbp:master
Open

cookie_session: set path to / so router wildcards don't mess up sessions#387
dbp wants to merge 1 commit intocamlworks:masterfrom
dbp:master

Conversation

@dbp
Copy link

@dbp dbp commented Feb 25, 2025

There is currently a bug (one example is in #307) where the cookie sessions are not persisted when they are set from within a wildcard route. The issue seems to be that Cookie.set_cookie, when not given a path argument, defaults to using Router.prefix request. This means that if you have code like:

Dream.scope "/somepath" [
  Dream.get "/**" (fun req -> ... Dream.set_session_field req "foo" "bar"
]

The field "foo" is lost, since the cookie that is set is scoped to /somepath, and is thus not loaded by the session middleware.

A complete reproduction is the following repo:
https://github.com/dbp/dream-session-bug-repro

I'm not sure why the cookies default to the router prefix (perhaps that is a bug), but if that is intended behavior, the included patch fixes this behavior by making the cookie session set the path to be "/", so it does not use a default.

If the cookie_sessions middleware is intended to be able to work on scoped subsites, then this is not the right fix, but also, if that is the case, then there needs to be a larger restructuring of the code, as there would need to be some way of the code indicating which session the field was being written to.

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

Labels

Development

Successfully merging this pull request may close these issues.

2 participants