-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Feat: on_shared_thread_view is only used to verify that thread can be… #2925
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
peleccom
wants to merge
4
commits into
Chainlit:main
Choose a base branch
from
peleccom:fix/fix-shared-thread-permisions
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a06d7b6
Feat: on_shared_thread_view is only used to verify that thread can be…
peleccom e2c88f9
Chore: Add tests for get shared thread permissions
peleccom e90f752
Feat: Add on_shared_thread_access_allowed callback to restrict access…
peleccom 4f6ee71
Merge branch 'main' into fix/fix-shared-thread-permisions
peleccom 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1152,3 +1152,140 @@ def test_health_check(test_client: TestClient): | |||||||||
| response = test_client.get("/health") | ||||||||||
| assert response.status_code == 200 | ||||||||||
| assert response.json() == {"status": "ok"} | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.mark.parametrize( | ||||||||||
| ("is_shared", "on_shared_thread_view_result", "allowed"), | ||||||||||
| [ | ||||||||||
| (True, True, True), | ||||||||||
| (True, False, True), | ||||||||||
| (True, ValueError("error"), True), | ||||||||||
|
Comment on lines
+1161
to
+1162
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. P1: Callback-denied and callback-error shared-thread cases are incorrectly expected to succeed, so this test would not catch a regression where Prompt for AI agents
Suggested change
|
||||||||||
| (False, True, True), | ||||||||||
| (False, False, False), | ||||||||||
| ], | ||||||||||
| ) | ||||||||||
| def test_get_shared_thread_access( | ||||||||||
| test_client: TestClient, | ||||||||||
| test_config: ChainlitConfig, | ||||||||||
| is_shared: bool, | ||||||||||
| on_shared_thread_view_result: bool | Exception, | ||||||||||
| allowed: bool, | ||||||||||
| ): | ||||||||||
| """Check if shared thread access is allowed based on is_shared and on_shared_thread_view result.""" | ||||||||||
| import chainlit.data as data_mod | ||||||||||
| from chainlit.server import app as _app, get_current_user as _get_current_user | ||||||||||
|
|
||||||||||
| viewer = PersistedUser( | ||||||||||
| id="viewer1", | ||||||||||
| createdAt=datetime.datetime.now().isoformat(), | ||||||||||
| identifier="viewer", | ||||||||||
| ) | ||||||||||
| _app.dependency_overrides[_get_current_user] = lambda: viewer | ||||||||||
|
|
||||||||||
| dl = AsyncMock() | ||||||||||
| dl.get_thread.return_value = { | ||||||||||
| "id": "shared-thread-1", | ||||||||||
| "name": "Shared Thread", | ||||||||||
| "userIdentifier": "author", | ||||||||||
| "metadata": {"is_shared": is_shared, "chat_profile": "pro"}, | ||||||||||
| } | ||||||||||
| dl.get_thread_author.return_value = "author" | ||||||||||
| dl.build_debug_url.return_value = "" | ||||||||||
|
|
||||||||||
| data_mod._data_layer = dl | ||||||||||
| data_mod._data_layer_initialized = True | ||||||||||
|
|
||||||||||
| async def deny_cb(thread, user): | ||||||||||
| if isinstance(on_shared_thread_view_result, Exception): | ||||||||||
| raise on_shared_thread_view_result | ||||||||||
| return on_shared_thread_view_result | ||||||||||
|
|
||||||||||
| test_config.code.on_shared_thread_view = deny_cb | ||||||||||
|
|
||||||||||
| r = test_client.get("/project/share/shared-thread-1") | ||||||||||
|
|
||||||||||
|
Comment on lines
+1201
to
+1206
|
||||||||||
| if allowed: | ||||||||||
| assert r.status_code == 200 | ||||||||||
| assert r.json()["id"] == "shared-thread-1" | ||||||||||
| else: | ||||||||||
| assert r.status_code == 404 | ||||||||||
| assert r.json() == {"detail": "Thread not found"} | ||||||||||
|
|
||||||||||
| # Cleanup | ||||||||||
| del _app.dependency_overrides[_get_current_user] | ||||||||||
| data_mod._data_layer = None | ||||||||||
| data_mod._data_layer_initialized = False | ||||||||||
| test_config.code.on_shared_thread_view = None | ||||||||||
|
Comment on lines
+1183
to
+1218
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.mark.parametrize( | ||||||||||
| ("is_shared", "access_allowed_result", "allowed"), | ||||||||||
| [ | ||||||||||
| (True, None, True), # No callback → falls through to is_shared (=200) | ||||||||||
| (True, True, True), # Callback allows → proceeds to return thread (=200) | ||||||||||
| (True, False, False), # Callback denies → 404 | ||||||||||
| (True, ValueError("err"), False), # Callback raises → 404 | ||||||||||
| (False, True, False), # is_shared=False blocks | ||||||||||
| ], | ||||||||||
| ) | ||||||||||
| def test_get_shared_thread_access_allowed( | ||||||||||
| test_client: TestClient, | ||||||||||
| test_config: ChainlitConfig, | ||||||||||
| is_shared: bool, | ||||||||||
| access_allowed_result: bool | Exception | None, | ||||||||||
| allowed: bool, | ||||||||||
| ): | ||||||||||
| """Check if shared thread access respects on_shared_thread_access_allowed callback.""" | ||||||||||
| import chainlit.data as data_mod | ||||||||||
| from chainlit.server import app as _app, get_current_user as _get_current_user | ||||||||||
|
|
||||||||||
| viewer = PersistedUser( | ||||||||||
| id="viewer1", | ||||||||||
| createdAt=datetime.datetime.now().isoformat(), | ||||||||||
| identifier="viewer", | ||||||||||
| ) | ||||||||||
| _app.dependency_overrides[_get_current_user] = lambda: viewer | ||||||||||
|
|
||||||||||
| dl = AsyncMock() | ||||||||||
| dl.get_thread.return_value = { | ||||||||||
| "id": "shared-thread-1", | ||||||||||
| "name": "Shared Thread", | ||||||||||
| "userIdentifier": "author", | ||||||||||
| "metadata": {"is_shared": is_shared, "chat_profile": "pro"}, | ||||||||||
| } | ||||||||||
| dl.get_thread_author.return_value = "author" | ||||||||||
| dl.build_debug_url.return_value = "" | ||||||||||
|
|
||||||||||
| data_mod._data_layer = dl | ||||||||||
| data_mod._data_layer_initialized = True | ||||||||||
|
|
||||||||||
| # Ensure on_shared_thread_view is not set (Tier 1+2 relies on is_shared fallback) | ||||||||||
| test_config.code.on_shared_thread_view = None | ||||||||||
|
|
||||||||||
| if access_allowed_result is not None: | ||||||||||
|
|
||||||||||
| async def access_cb(thread, user): | ||||||||||
| if isinstance(access_allowed_result, Exception): | ||||||||||
| raise access_allowed_result | ||||||||||
| return access_allowed_result | ||||||||||
|
|
||||||||||
| test_config.code.on_shared_thread_access_allowed = access_cb | ||||||||||
| else: | ||||||||||
| # Callback not defined — Tier 3 is skipped | ||||||||||
| test_config.code.on_shared_thread_access_allowed = None | ||||||||||
|
|
||||||||||
| r = test_client.get("/project/share/shared-thread-1") | ||||||||||
|
|
||||||||||
| if allowed: | ||||||||||
| assert r.status_code == 200 | ||||||||||
| assert r.json()["id"] == "shared-thread-1" | ||||||||||
| else: | ||||||||||
| assert r.status_code == 404 | ||||||||||
| assert r.json() == {"detail": "Thread not found"} | ||||||||||
|
|
||||||||||
| # Cleanup | ||||||||||
| del _app.dependency_overrides[_get_current_user] | ||||||||||
| data_mod._data_layer = None | ||||||||||
| data_mod._data_layer_initialized = False | ||||||||||
| test_config.code.on_shared_thread_view = None | ||||||||||
| test_config.code.on_shared_thread_access_allowed = None | ||||||||||
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.
P0: Authorization bypass: is_shared=True grants access even when on_shared_thread_view callback denies access, undermining the stated security goal
Prompt for AI agents