-
Notifications
You must be signed in to change notification settings - Fork 99
Validate search query values before sending to solr #2161
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import luqum.tree | ||
| from luqum.parser import parser | ||
|
|
||
| FIELD_TYPES_MAP = { | ||
| "samplerate": int, | ||
| "bitrate": int, | ||
| "bitdepth": int, | ||
| "channels": int, | ||
| "duration": float, | ||
| } | ||
|
|
||
| COMPLEX_EXPR_TYPES = ( | ||
| luqum.tree.Range, | ||
| luqum.tree.OpenRange, | ||
| luqum.tree.From, | ||
| luqum.tree.To, | ||
| luqum.tree.FieldGroup, | ||
| luqum.tree.Boost, | ||
| ) | ||
|
|
||
|
|
||
| def parse_filter(filter_string): | ||
| """Parse a Lucene-style filter string into its list of top-level nodes. | ||
| Empty string -> []. Raises luqum.exceptions.ParseError on malformed input. | ||
| """ | ||
| if not filter_string: | ||
| return [] | ||
| tree = parser.parse(filter_string) | ||
| return [tree] if type(tree) == luqum.tree.SearchField else tree.children | ||
|
|
||
|
|
||
| def validate_filter_types(nodes) -> str | None: | ||
| """Validate that in the given top-level filter nodes, the values of fields | ||
| with a known type (FIELD_TYPES_MAP) can be cast to that type. Return a | ||
| human-readable error string on first failure, or None on success. | ||
| """ | ||
| for node in nodes: | ||
| if type(node) != luqum.tree.SearchField: | ||
| continue | ||
| if node.name not in FIELD_TYPES_MAP: | ||
| continue | ||
| expr = node.expr | ||
| if isinstance(expr, COMPLEX_EXPR_TYPES): | ||
| # Ranges/inequalities/groups/boosts etc. e.g. samplerate:[a TO b] | ||
| # validate directly in solr | ||
| continue | ||
| if isinstance(expr, luqum.tree.Phrase): | ||
| # String in "quotes" | ||
| value = expr.value[1:-1] | ||
| else: | ||
| value = str(expr) | ||
| if value == "*": | ||
| # `field:*` is a valid solr query, continue | ||
| continue | ||
| expected_type = FIELD_TYPES_MAP[node.name] | ||
| # This does let through some values that are valid in python but not solr | ||
| # (e.g. int(1_000)), but we accept these few cases because they are rare | ||
| try: | ||
| expected_type(value) | ||
| except (ValueError, TypeError): | ||
| return f"Filter parsing error: '{node.name}' value must be {expected_type.__name__}, got {value!r}" | ||
| return None | ||
|
|
||
|
|
||
| def find_dropped_filters(nodes) -> list[str]: | ||
| """Return a list of human-readable representations of filters that | ||
| SearchQueryProcessor would silently drop. | ||
|
|
||
| Hand-made URLs sometimes use `%2B` instead of a space between filters, which | ||
| decodes as a `+` instead of a space. Lucene reads that `+` as a MUST prefix, | ||
| so the following filter parses as a top-level Plus-wrapped node: | ||
|
|
||
| sent: f=tag%3A%22tap%22%2Busername%3A%22ascap%22 | ||
| decoded: tag:"tap"+username:"ascap" | ||
| expected: tag:"tap" username:"ascap" | ||
| parsed: [SearchField('tag', '"tap"'), Plus(SearchField('username', '"ascap"'))] | ||
| expected [SearchField('tag', '"tap"'), SearchField('username', '"ascap"')] | ||
|
|
||
| SearchQueryProcessor currently drops the second part of the search query, so | ||
| this function returns ['username:"ascap"']. | ||
|
|
||
| Empty list means nothing was removed. | ||
| """ | ||
| dropped = [] | ||
| for node in nodes: | ||
| if isinstance(node, luqum.tree.Plus) and isinstance(node.a, luqum.tree.SearchField): | ||
| # str() of a luqum node can keep trailing whitespace from the source string | ||
| dropped.append(str(node.a).strip()) | ||
| return dropped | ||
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.
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.
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.
We are starting to "hardcode" search fields information in several places (see
freesound/freesound/settings.py
Line 546 in 176ce1e
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.
hmm, yeah. we could have a dict of dataclasses or something - with field name, type, default weights, facet values, etc. it should end up consolidating all search field settings into a single location