-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Chunk long cookies #1758
base: main
Are you sure you want to change the base?
Chunk long cookies #1758
Conversation
@jpolvto, I have tested a few times with a large JWT token and I can confirm it has worked on my side. |
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.
Thanks for the quick contrib!
We'd love to get this in soon, but as this code will affect nearly all users we need to thread carefully.
In addition to the issues pointed out in the code, I'd love to see unit test coverage of cookie getting and setting. This includes a test for the specific bug pointed out! If you need help on approach to testing, DM me on Discord.
I would also suggest implementing this workaround (which is what it is!) such that the behaviour for typical tokens doesn't change. Plus a test that indeed it doesn't.
One way to do this:
access_token_0
present -> look for_1
etc.access_token
present -> cookie fits, no need to look for_1
etc.
When setting cookies, we need to check for chunked cookies and, if they exist, ensure cookies beyond what we're currently setting get deleted!
As a bonus, this will provide a clear distinction between chunked and non-chunked codepaths, making it easy to remove this once a more elegant/sane response than sending extra 4k+
backend/chainlit/auth/cookie.py
Outdated
@@ -76,26 +76,60 @@ async def __call__(self, request: Request) -> Optional[str]: | |||
return token | |||
|
|||
|
|||
def reconstruct_token_from_cookies(request_cookies: dict) -> Optional[str]: |
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 a clearer signature.
Something like get_token_from_cookies(cookies: dict[str, str]) -> Optional[str]
.
backend/chainlit/auth/cookie.py
Outdated
|
||
return joined if joined != "" else None | ||
|
||
|
||
def set_auth_cookie(response: Response, token: str): |
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.
We should be careful here to ensure the case where we're overwriting cookies and not deleting old ones.
Imagine:
set_auth_cookie(r, 'incredibly_<...>_long_token')
setsaccess_token_0
,access_token_1
.set_auth_cookie(r, 'more_reasonable_token')
sets onlyaccess_token_0
.get_token_from_cookies(🍪)
returns the concatenation of the newaccess_token_0
and the oldaccess_token_1
.
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.
See prior review. ;)
Forgot to set status properly.
@jpolvto Happy to see your applying requested changes! If you need support writing tests, let me know. I've a bit of capacity available as this is blocking lot's of users. |
Splits the JWT token over multiple cookies if it's too big. Should resolve #1685. It's a bit hard to check, since my account doesn't have a large JWT token. @dokterbob