fix(security): prevent IDOR in DataSourceOauthBinding by adding tenant_id check#33840
fix(security): prevent IDOR in DataSourceOauthBinding by adding tenant_id check#33840xr843 wants to merge 6 commits intolanggenius:mainfrom
Conversation
…t_id check The patch method in DataSourceApi fetched a DataSourceOauthBinding by binding_id without verifying it belongs to the current user's tenant. An authenticated attacker could enable or disable data source bindings belonging to other tenants by supplying their IDs. Add tenant_id filtering to the query, consistent with how the get method already scopes bindings to the current tenant. Fixes langgenius#31839 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical security vulnerability (IDOR) by enforcing tenant-level access control for data source OAuth binding modifications. It ensures that users can only enable or disable data source bindings that belong to their own tenant, preventing unauthorized access and manipulation of other tenants' data. The change enhances data isolation and system security, backed by a dedicated unit test to confirm the correct implementation of tenant scoping. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
Pyrefly DiffNo changes detected. |
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a critical IDOR security vulnerability in the DataSourceApi's patch method by adding a tenant_id check when fetching a DataSourceOauthBinding. The fix is straightforward and effective. A new unit test is also added to verify that the query is correctly scoped to the tenant. I've added a suggestion to make the new test assertion more robust and pointed out some unused imports that can be cleaned up. Overall, this is a great security fix.
| def test_patch_binding_scoped_to_current_tenant(self, app, patch_tenant, mock_engine): | ||
| """Verify that the patch query includes tenant_id to prevent IDOR attacks.""" | ||
| from sqlalchemy import select as real_select | ||
|
|
||
| from models import DataSourceOauthBinding | ||
|
|
||
| api = DataSourceApi() | ||
| method = unwrap(api.patch) | ||
|
|
||
| binding = MagicMock(id="b1", disabled=True) | ||
|
|
||
| with ( | ||
| app.test_request_context("/"), | ||
| patch("controllers.console.datasets.data_source.Session") as mock_session_class, | ||
| patch("controllers.console.datasets.data_source.db.session.add"), | ||
| patch("controllers.console.datasets.data_source.db.session.commit"), | ||
| ): | ||
| mock_session = MagicMock() | ||
| mock_session_class.return_value.__enter__.return_value = mock_session | ||
| mock_session.execute.return_value.scalar_one_or_none.return_value = binding | ||
|
|
||
| method(api, "b1", "enable") | ||
|
|
||
| # Inspect the SELECT statement passed to session.execute | ||
| call_args = mock_session.execute.call_args | ||
| stmt = call_args[0][0] | ||
| compiled = stmt.compile(compile_kwargs={"literal_binds": True}) | ||
| compiled_where = str(compiled) | ||
|
|
||
| assert "tenant_id" in compiled_where, ( | ||
| "The patch query must filter by tenant_id to prevent IDOR vulnerabilities" | ||
| ) |
There was a problem hiding this comment.
The assertion assert "tenant_id" in compiled_where is a bit weak, as it only checks for the presence of the string "tenant_id". A more robust test would be to assert that the tenant_id is correctly used in the WHERE clause with the expected value from the tenant_ctx fixture (tenant-1). This ensures the filter is applied correctly.
Additionally, there are a couple of unused imports in this test method that can be removed:
from sqlalchemy import select as real_selecton line 181.from models import DataSourceOauthBindingon line 183.
def test_patch_binding_scoped_to_current_tenant(self, app, patch_tenant, mock_engine):
"""Verify that the patch query includes tenant_id to prevent IDOR attacks."""
api = DataSourceApi()
method = unwrap(api.patch)
binding = MagicMock(id="b1", disabled=True)
with (
app.test_request_context("/"),
patch("controllers.console.datasets.data_source.Session") as mock_session_class,
patch("controllers.console.datasets.data_source.db.session.add"),
patch("controllers.console.datasets.data_source.db.session.commit"),
):
mock_session = MagicMock()
mock_session_class.return_value.__enter__.return_value = mock_session
mock_session.execute.return_value.scalar_one_or_none.return_value = binding
method(api, "b1", "enable")
# Inspect the SELECT statement passed to session.execute
call_args = mock_session.execute.call_args
stmt = call_args[0][0]
compiled = stmt.compile(compile_kwargs={"literal_binds": True})
compiled_where = str(compiled)
assert "tenant_id = 'tenant-1'" in compiled_where, (
"The patch query must filter by tenant_id to prevent IDOR vulnerabilities"
)
Pyrefly DiffNo changes detected. |
Pyrefly DiffNo changes detected. |
Pyrefly DiffNo changes detected. |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Pyrefly DiffNo changes detected. |
Pyrefly DiffNo changes detected. |
|
Superseded by #33986 (consolidated with constant-time API key comparison fix). |
Summary
patchmethod inDataSourceApifetches aDataSourceOauthBindingbybinding_idwithout verifying it belongs to the current user's tenant, allowing an authenticated attacker to enable/disable data source bindings belonging to other tenants (classic IDOR / horizontal privilege escalation).tenant_idfiltering to thepatchquery, consistent with how thegetmethod in the same class already scopes bindings to the current tenant.tenant_idin the WHERE clause.Changes
api/controllers/console/datasets/data_source.py: Addcurrent_account_with_tenant()call andtenant_id=current_tenant_idto thefilter_by()in thepatchmethod.api/tests/unit_tests/controllers/console/datasets/test_data_source.py: Addtest_patch_binding_scoped_to_current_tenantto verify tenant scoping is present in the query.Test plan
cd api && python -m pytest tests/unit_tests/controllers/console/datasets/test_data_source.py -q— all existing tests passtest_patch_binding_scoped_to_current_tenantassertstenant_idappears in the compiled WHERE clauseFixes #31839
🤖 Generated with Claude Code