-
Notifications
You must be signed in to change notification settings - Fork 100
fixed potential sql injection with new tags feature #751
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
Merged
Merged
Changes from all commits
Commits
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
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
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
Binary file not shown.
Binary file not shown.
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <!-- BEGIN TAG_FILTER_INJECTION_FIX.md BLOCK --> | ||
|
|
||
| # Tag Filter Injection Fix | ||
|
|
||
| ## Issue Description | ||
|
|
||
| Tag filter inputs from user query parameters (`?tags=...`) and JSON request bodies were passed through `normalize_tag()` which only trims whitespace and lowercases, without validating the character set. While Cosmos DB queries used parameterized values (preventing direct SQL injection), the `build_tags_filter()` function in `functions_search.py` constructed OData filter strings via string interpolation, creating a potential OData injection vector in Azure AI Search. | ||
|
|
||
| ## Root Cause | ||
|
|
||
| The `validate_tags()` function enforces a strict `^[a-z0-9_-]+$` character whitelist when **saving** tags, but this validation was not applied when **filtering** by tags. The filter path only used `normalize_tag()` (strip + lowercase), allowing arbitrary characters to reach query construction code. | ||
|
|
||
| ## Version | ||
|
|
||
| - **Fixed in**: v0.238.025 | ||
| - **Affected versions**: Prior versions with tag filtering | ||
|
|
||
| ## Technical Details | ||
|
|
||
| ### Files Modified | ||
|
|
||
| | File | Change | | ||
| |------|--------| | ||
| | `application/single_app/functions_documents.py` | Added `sanitize_tags_for_filter()` function | | ||
| | `application/single_app/route_backend_documents.py` | Replaced `normalize_tag` with `sanitize_tags_for_filter` in tag filter | | ||
| | `application/single_app/route_backend_group_documents.py` | Replaced `normalize_tag` with `sanitize_tags_for_filter` in tag filter | | ||
| | `application/single_app/route_backend_public_documents.py` | Replaced `normalize_tag` with `sanitize_tags_for_filter` in tag filter | | ||
| | `application/single_app/functions_search.py` | Hardened `build_tags_filter()` to validate tags before OData interpolation | | ||
| | `application/single_app/config.py` | Version bump to 0.238.025 | | ||
|
|
||
| ### Code Changes | ||
|
|
||
| **New function `sanitize_tags_for_filter()`**: Accepts either a comma-separated string (from query params) or a list of strings (from JSON bodies). Normalizes each tag, validates against `^[a-z0-9_-]+$`, enforces the 50-character limit, deduplicates, and silently drops invalid entries. | ||
|
|
||
| **Route file updates**: The inline `normalize_tag()` + split pattern was replaced with a single call to `sanitize_tags_for_filter()`, which handles splitting, normalizing, and validating internally. | ||
|
|
||
| **`build_tags_filter()` hardening**: Replaced the single-quote escaping approach with `sanitize_tags_for_filter()` validation. Since validated tags can only contain `[a-z0-9_-]`, no escaping is necessary and OData injection is impossible. | ||
|
|
||
| ### Defense-in-Depth Layers | ||
|
|
||
| 1. **Character whitelist**: `^[a-z0-9_-]+$` prevents any injection-significant characters | ||
| 2. **Parameterized Cosmos DB queries**: Tag values passed as parameters, not interpolated | ||
| 3. **Tag normalization**: Lowercase + trim before validation | ||
| 4. **Length limit**: 50-character maximum per tag | ||
|
|
||
| ## Testing | ||
|
|
||
| - **Functional test**: `functional_tests/test_tag_filter_sanitization.py` | ||
| - Covers: valid tags, special character rejection, SQL injection attempts, OData injection attempts, edge cases (empty/None/numeric input), length limits, deduplication | ||
|
|
||
| ## Impact | ||
|
|
||
| - No functional behavior change for valid tag filters (tags stored in the system already pass `^[a-z0-9_-]+$` validation) | ||
| - Invalid characters in tag filters are silently dropped rather than passed through to queries | ||
| - OData filter injection via `build_tags_filter()` is now prevented by input validation | ||
|
|
||
| <!-- END TAG_FILTER_INJECTION_FIX.md BLOCK --> |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.