fix(security): apply adhoc SQL guards in datasource validate_expression - #21
Open
devin-ai-integration[bot] wants to merge 1 commit into
Open
devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Bug: the datasource validate_expression endpoint executed attacker-supplied SQL fragments against the analytics DB without the sanitization the primary adhoc-expression path applies, enabling RLS/subquery bypass and (on drivers that allow it) stacked queries. Run the processed expression through validate_adhoc_subquery() and sanitize_clause() before building/executing the validation query, matching _process_sql_expression(). Co-Authored-By: Sohil Kshirsagar <sohil@usetusk.ai>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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
The datasource
validate_expressionendpoint (POST /api/v1/datasource/{type}/{id}/validate_expression) took an attacker-supplied SQL fragment and executed it against the datasource's analytics DB without the guards the normal adhoc-expression path applies.ExploreMixin._build_validation_query()embeds the raw expression viasa.text()/sa.literal_column()and_execute_validation_query()runs it withcon.execute(...)— never callingvalidate_adhoc_subquery()orsanitize_clause()(unlike_process_sql_expression()). This allowed RLS/subquery bypass (cross-tenant reads), error-based exfiltration via scalar subselects, and — on drivers that permit multiple statements — stacked queries, all with mere datasource READ access.Fix: route the processed expression through the same guards as the primary adhoc path before it is ever embedded in a query or executed.
In
validate_expression, after template processing:New helper mirrors
_process_sql_expression:Effect:
validate_adhoc_subquery()blocks sub-queries unlessALLOW_ADHOC_SUBQUERYis enabled, and injects RLS predicates into referenced tables when it is — closing the RLS bypass / cross-tenant read and the scalar-subquery error-based exfiltration vector.sanitize_clause()re-parses the clause, strips comments, and rejects malformed / multi-statement SQL — closing the comment-terminated stacked-query vector.except Exceptioninvalidate_expressionconverts them into a normalvalid=Falsevalidation result, so malformed/blocked input is reported to the user without ever reaching the database.Note: the
_execute_validation_queryflow is otherwise unchanged. Two further hardening ideas from the report are intentionally out of scope here to keep the change focused: (1) switching to a parse-only validation that never executes against the live DB / redacting raw driver error strings, and (2) requiring a write/edit-level permission on the endpoint instead of datasource READ. Happy to follow up on either.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
TESTING INSTRUCTIONS
pytest tests/unit_tests/models/test_validate_expression.py(11 passed). Added regression tests assert that sub-queries and multi-statement/malformed expressions are rejected and never reach_execute_validation_query, while valid column/metric/where/having expressions still pass. Verified withpre-commit run mypy/ruff/ruff-formatagainst the pinnedruff==0.9.7.Manual: as a user with only READ on a datasource,
POST .../validate_expressionwith{"expression": "id IN (SELECT x FROM other_tenant)", "expression_type": "where"}now returns a validation error ("Custom SQL fields cannot contain sub-queries.") instead of executing the sub-query.ADDITIONAL INFORMATION
Link to Devin session: https://app.devin.ai/sessions/6e4f1ff1081b4e5da71ffa564af7df34
Requested by: @sohil-kshirsagar
Devin Review