-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Improve algolia.py input data validation #1046
Merged
arkid15r
merged 32 commits into
OWASP:main
from
ahmedxgouda:feature/improve-algolia-input-validation
Mar 14, 2025
Merged
Changes from 24 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
59b6e20
feat: add validation for search parameters in Algolia API
ahmedxgouda 652b93e
fix: ensure query validation only occurs if query is provided.
ahmedxgouda f648725
feat: refactor search parameter validation into dedicated functions
ahmedxgouda 356384d
Merge remote-tracking branch 'upstream/main' into feature/improve-alg…
ahmedxgouda 553f128
Revert "feat: refactor search parameter validation into dedicated fun…
ahmedxgouda 126ba14
Reapply "feat: refactor search parameter validation into dedicated fu…
ahmedxgouda 0bdecb8
Revert "Merge remote-tracking branch 'upstream/main' into feature/imp…
ahmedxgouda c9b22de
fix: reorder import statements in validators.py
ahmedxgouda b909229
feat: add module docstring for search parameter validators
ahmedxgouda bc31967
feat: refactor the returned lambda function parameter name to be unli…
ahmedxgouda 2208c9e
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda 025ac0b
Resolve conflicts.
ahmedxgouda 4fd7b04
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda 7483d64
Resolve conflicts manually.
ahmedxgouda 29885fc
Refactoring for the checks.
ahmedxgouda c299653
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda 4078f01
Fix formatting in algolia.py for consistency
ahmedxgouda 8294e1b
Refactor the validations to use warlus operator, and improving the pa…
ahmedxgouda 6fbe8de
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda c2c66e9
Separate the validation logic from the HTTP response logic.
ahmedxgouda 0518f12
Merge branch 'main' into feature/improve-algolia-input-validation
arkid15r 623aea9
Refactor validation logic in Algolia API to raise ValidationError for…
ahmedxgouda 407f92a
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda 1f2b483
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda f97a6f6
Update code
arkid15r a1288d2
Update hitsPerPage validation message and add tests for invalid Algol…
ahmedxgouda 1161e00
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda 575ec75
Create validators_test.py and add the relevant validations.
ahmedxgouda e476e5c
Merge branch 'main' into feature/improve-algolia-input-validation
ahmedxgouda b6a64ce
Refactoring for the checks.
ahmedxgouda a7a75fb
Merge branch 'main' into pr/ahmedxgouda/1046
arkid15r 5a6cc75
Update code
arkid15r 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 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 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,82 @@ | ||
"""Validators for the search parameters of the Algolia endpoint.""" | ||
|
||
import re | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.core.validators import validate_slug | ||
|
||
|
||
def validate_index_name(index_name): | ||
"""Validate index name.""" | ||
if not index_name or not isinstance(index_name, str): | ||
message = "indexName is required and must be a string." | ||
raise ValidationError(message) | ||
try: | ||
validate_slug(index_name) | ||
except ValidationError: | ||
message = ( | ||
"Invalid indexName value provided. " | ||
"Only alphanumeric characters hyphens and underscores are allowed." | ||
) | ||
raise ValidationError(message) from None | ||
|
||
|
||
def validate_limit(limit): | ||
"""Validate limit.""" | ||
if not isinstance(limit, int): | ||
message = "hitsPerPage must be an integer." | ||
raise ValidationError(message) | ||
|
||
max_limit = 100 | ||
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. We use 1000 in some cases. |
||
min_limit = 1 | ||
|
||
if limit < min_limit or limit > max_limit: | ||
message = "hitsPerPage value must be between 1 and 100." | ||
raise ValidationError(message) | ||
|
||
|
||
def validate_page(page): | ||
"""Validate page.""" | ||
if not isinstance(page, int): | ||
message = "page must be an integer." | ||
raise ValidationError(message) | ||
|
||
if page <= 0: | ||
message = "page value must be greater than 0." | ||
raise ValidationError(message) | ||
|
||
|
||
def validate_query(query): | ||
"""Validate query.""" | ||
if query: | ||
if not isinstance(query, str): | ||
message = "query must be a string." | ||
raise ValidationError(message) | ||
if not re.match(r"^[a-zA-Z0-9-_ ]*$", query): | ||
message = ( | ||
"Invalid query value provided. " | ||
"Only alphanumeric characters, hyphens, spaces and underscores are allowed." | ||
) | ||
raise ValidationError(message) | ||
|
||
|
||
def validate_facet_filters(facet_filters): | ||
"""Validate facet filters.""" | ||
if not isinstance(facet_filters, list): | ||
message = "facetFilters must be a list." | ||
raise ValidationError(message) | ||
|
||
|
||
def validate_search_params(data): | ||
"""Validate search parameters.""" | ||
index_name = data.get("indexName") | ||
limit = data.get("hitsPerPage", 25) | ||
page = data.get("page", 1) | ||
query = data.get("query", "") | ||
facet_filters = data.get("facetFilters", []) | ||
|
||
validate_index_name(index_name) | ||
validate_limit(limit) | ||
validate_page(page) | ||
validate_query(query) | ||
validate_facet_filters(facet_filters) |
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.
I reverted this for more strict check we had before.
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 did not review anything BTW. It is a glitch in GitHub pull requests extension in VS Code.
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.
Why do you think it is more strict?