-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: scope cookies to CHAINLIT_ROOT_PATH for multi-app deployments #2825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gonzalo123
wants to merge
5
commits into
Chainlit:main
Choose a base branch
from
gonzalo123:fix/cookie-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5a6b4a0
fix: scope cookies to CHAINLIT_ROOT_PATH for multi-app deployments
gonzalo123 19139a3
fix: clean up legacy cookies at path="/" after scoping to CHAINLIT_RO…
gonzalo123 9f63b7f
fix: clean up legacy cookies for leftover chunks and strengthen test …
gonzalo123 7247fe4
Merge branch 'Chainlit:main' into fix/cookie-path
gonzalo123 14c30fb
docs: add TODO to remove legacy cookie cleanup in next major release
gonzalo123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| ) | ||
| _cookie_secure = _cookie_samesite == "none" | ||
| if _cookie_root_path := os.environ.get("CHAINLIT_ROOT_PATH", None): | ||
| _cookie_path = os.environ.get(_cookie_root_path, "/") | ||
| _cookie_path = os.environ.get("CHAINLIT_AUTH_COOKIE_PATH", _cookie_root_path) | ||
| else: | ||
| _cookie_path = os.environ.get("CHAINLIT_AUTH_COOKIE_PATH", "/") | ||
| _state_cookie_lifetime = int( | ||
|
|
@@ -34,6 +34,22 @@ | |
| _state_cookie_name = "oauth_state" | ||
|
|
||
|
|
||
| def _delete_legacy_cookies(response: Response, *names: str): | ||
| """Delete cookies at path='/' left over from pre-scoped versions. | ||
|
|
||
| Only acts when _cookie_path != '/' to avoid no-op deletes in | ||
| single-app deployments. | ||
|
|
||
| TODO: Remove this function in the next major release. | ||
| """ | ||
| if _cookie_path == "/": | ||
| return | ||
| for name in names: | ||
| response.delete_cookie( | ||
| key=name, path="/", secure=_cookie_secure, samesite=_cookie_samesite | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Unconditional root-path legacy cookie deletion can remove same-named cookies from other apps on the same host, causing cross-app session/logout disruption in multi-app deployments. Prompt for AI agents |
||
| ) | ||
|
|
||
|
|
||
| class OAuth2PasswordBearerWithCookie(SecurityBase): | ||
| """ | ||
| OAuth2 password flow with cookie support with fallback to bearer token. | ||
|
|
@@ -132,23 +148,27 @@ def set_auth_cookie(request: Request, response: Response, token: str): | |
| response.set_cookie( | ||
| key=k, | ||
| value=chunk, | ||
| path=_cookie_path, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| httponly=True, | ||
| secure=_cookie_secure, | ||
| samesite=_cookie_samesite, | ||
| max_age=config.project.user_session_timeout, | ||
| ) | ||
| _delete_legacy_cookies(response, k) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
|
|
||
| existing_cookies.discard(k) | ||
| else: | ||
| # Default (shorter cookies) | ||
| response.set_cookie( | ||
| key=_auth_cookie_name, | ||
| value=token, | ||
| path=_cookie_path, | ||
| httponly=True, | ||
| secure=_cookie_secure, | ||
| samesite=_cookie_samesite, | ||
| max_age=config.project.user_session_timeout, | ||
| ) | ||
| _delete_legacy_cookies(response, _auth_cookie_name) | ||
|
|
||
| existing_cookies.discard(_auth_cookie_name) | ||
|
|
||
|
|
@@ -157,6 +177,7 @@ def set_auth_cookie(request: Request, response: Response, token: str): | |
| response.delete_cookie( | ||
| key=k, path=_cookie_path, secure=_cookie_secure, samesite=_cookie_samesite | ||
| ) | ||
| _delete_legacy_cookies(response, k) | ||
|
|
||
|
|
||
| def clear_auth_cookie(request: Request, response: Response): | ||
|
|
@@ -172,17 +193,20 @@ def clear_auth_cookie(request: Request, response: Response): | |
| response.delete_cookie( | ||
| key=k, path=_cookie_path, secure=_cookie_secure, samesite=_cookie_samesite | ||
| ) | ||
| _delete_legacy_cookies(response, k) | ||
|
|
||
|
|
||
| def set_oauth_state_cookie(response: Response, token: str): | ||
| response.set_cookie( | ||
| _state_cookie_name, | ||
| token, | ||
| path=_cookie_path, | ||
| httponly=True, | ||
| samesite=_cookie_samesite, | ||
| secure=_cookie_secure, | ||
| max_age=_state_cookie_lifetime, | ||
| ) | ||
| _delete_legacy_cookies(response, _state_cookie_name) | ||
|
|
||
|
|
||
| def validate_oauth_state_cookie(request: Request, state: str): | ||
|
|
@@ -196,4 +220,5 @@ def validate_oauth_state_cookie(request: Request, state: str): | |
|
|
||
| def clear_oauth_state_cookie(response: Response): | ||
| """Oauth complete, delete state token.""" | ||
| response.delete_cookie(_state_cookie_name) # Do we set path here? | ||
| response.delete_cookie(_state_cookie_name, path=_cookie_path) | ||
| _delete_legacy_cookies(response, _state_cookie_name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest adding a comment here explaining why it was introduced and a suggestion when it may be removed (e.g. next major release).