fix(auth): use CHAINLIT_ROOT_PATH value as cookie path; constant-time OAuth state check#2934
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
Two bugs in `backend/chainlit/auth/cookie.py`:
1. **Cookie path computed incorrectly when CHAINLIT_ROOT_PATH is set**
The module-level initialisation read:
if _cookie_root_path := os.environ.get("CHAINLIT_ROOT_PATH", None):
_cookie_path = os.environ.get(_cookie_root_path, "/")
`os.environ.get(_cookie_root_path, "/")` used the *value* of
`CHAINLIT_ROOT_PATH` (e.g. `"/myapp"`) as a *key* to look up yet
another environment variable. Since `"/myapp"` is never a valid env
var name the lookup always returned the default `"/"`. Cookie deletion
in `set_auth_cookie` and `clear_auth_cookie` therefore used path `"/"`
instead of the intended root path, leaving stale cookies on clients
after logout or token rotation when the app is deployed under a
sub-path.
Fix: assign `_cookie_path = _cookie_root_path` directly.
2. **Non-constant-time OAuth state comparison**
`validate_oauth_state_cookie` used `oauth_state != state` (regular
string equality), which is not constant-time and can leak the expected
state value via timing side-channels. Replace with
`hmac.compare_digest`, which is the standard Python idiom for
constant-time secret comparison. Also added an explicit `None` guard
so a missing state cookie is rejected before the digest comparison.
Tests added:
- test_cookie_path_uses_chainlit_root_path_value
- test_validate_oauth_state_cookie_rejects_missing_state
- test_validate_oauth_state_cookie_accepts_correct_state
Contributor
There was a problem hiding this comment.
1 issue found across 2 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Collaborator
|
@devteamaegis Code looks good, just some linting nitpicks. Nice to finally have the cookie path fixed! |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes two issues in Chainlit’s backend auth cookie handling: (1) correctly deriving the cookie deletion path from CHAINLIT_ROOT_PATH, and (2) hardening OAuth state validation by using a constant-time comparison to reduce timing side-channel risk.
Changes:
- Fix module initialization so
_cookie_pathuses the value ofCHAINLIT_ROOT_PATH(instead of treating it as an env var key). - Update
validate_oauth_state_cookieto usehmac.compare_digest(and reject missing state cookies) for constant-time comparison. - Add unit tests covering the root-path cookie behavior and OAuth state validation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
backend/chainlit/auth/cookie.py |
Fixes cookie path derivation and makes OAuth state comparison constant-time. |
backend/tests/auth/test_cookie.py |
Adds tests for CHAINLIT_ROOT_PATH cookie path derivation and OAuth state validation behaviors. |
Comment on lines
+149
to
+155
| monkeypatch.setenv("CHAINLIT_ROOT_PATH", "/myapp") | ||
| monkeypatch.delenv("CHAINLIT_AUTH_COOKIE_PATH", raising=False) | ||
| importlib.reload(cookie_module) | ||
| assert cookie_module._cookie_path == "/myapp", ( | ||
| f"Expected _cookie_path to be '/myapp' but got '{cookie_module._cookie_path}'. " | ||
| "CHAINLIT_ROOT_PATH value should be used directly as the cookie path." | ||
| ) |
Comment on lines
26
to
30
| _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 = _cookie_root_path | ||
| else: | ||
| _cookie_path = os.environ.get("CHAINLIT_AUTH_COOKIE_PATH", "/") |
dokterbob
requested changes
May 26, 2026
dokterbob
left a comment
Collaborator
There was a problem hiding this comment.
Good find but some quite significant fixups suggested by copilot!
fa04ea9 to
05e77bf
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Two bugs in
backend/chainlit/auth/cookie.py.Bug 1 — Cookie path always
"/"whenCHAINLIT_ROOT_PATHis setRoot cause: Module-level initialisation used the value of
CHAINLIT_ROOT_PATHas an environment variable key:Impact:
set_auth_cookieandclear_auth_cookiedelete stale cookie chunks usingpath=_cookie_path. When an app is deployed under a sub-path andCHAINLIT_ROOT_PATHis configured, the path used for deletion is"/"instead of the intended path, so old chunks are never cleared after logout or token rotation.Fix:
Bug 2 — Non-constant-time OAuth state comparison
validate_oauth_state_cookieused a regular!=comparison:String equality (
!=) is not constant-time and can leak information about the expected state value through timing side-channels. While network jitter limits practical exploitability, the fix is trivial: usehmac.compare_digest, which is the standard Python idiom for comparing secrets. An explicitNoneguard was also added so a missing cookie is rejected before the digest call.Tests
Three new tests in
tests/auth/test_cookie.py:test_cookie_path_uses_chainlit_root_path_value_cookie_pathequalsCHAINLIT_ROOT_PATHvalue after reloadtest_validate_oauth_state_cookie_rejects_missing_statetest_validate_oauth_state_cookie_accepts_correct_stateSummary by cubic
Fixes auth cookie path to use
CHAINLIT_ROOT_PATHdirectly, so cookie cleanup works when the app runs under a sub-path. Switches OAuth state validation to constant-time comparison and rejects missing state cookies.CHAINLIT_ROOT_PATHas the cookiepath, ensuringset_auth_cookie/clear_auth_cookieremove stale chunks under sub-paths.hmac.compare_digestand raise when the state cookie is missing.Written for commit 05e77bf. Summary will update on new commits.