-
Notifications
You must be signed in to change notification settings - Fork 0
fix(api): refactors the SQL LIKE pattern escaping logic to use a centralized utility function, ensuring consistent and secure handling of special characters across all database queries. #38
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
base: coderabbit_full_base_fixapi_refactors_the_sql_like_pattern_escaping_logic_to_use_a_centralized__utility_function_ensuring_consistent_and_secure_handling_of_special_characters_across_all_database_queri
Are you sure you want to change the base?
Changes from all commits
9fcc127
98c38c2
8dbf5d4
fe99d63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1195,18 +1195,24 @@ def process_metadata_filter_func( | |
|
|
||
| json_field = DatasetDocument.doc_metadata[metadata_name].as_string() | ||
|
|
||
| from libs.helper import escape_like_pattern | ||
|
|
||
| match condition: | ||
| case "contains": | ||
| filters.append(json_field.like(f"%{value}%")) | ||
| escaped_value = escape_like_pattern(str(value)) | ||
| filters.append(json_field.like(f"%{escaped_value}%", escape="\\")) | ||
|
|
||
| case "not contains": | ||
| filters.append(json_field.notlike(f"%{value}%")) | ||
| escaped_value = escape_like_pattern(str(value)) | ||
| filters.append(json_field.notlike(f"%{escaped_value}%")) | ||
|
Comment on lines
1205
to
+1207
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. Missing escape clause in The "not contains" case escapes the value but doesn't specify the 🐛 Proposed fix: Add escape clause case "not contains":
escaped_value = escape_like_pattern(str(value))
- filters.append(json_field.notlike(f"%{escaped_value}%"))
+ filters.append(json_field.notlike(f"%{escaped_value}%", escape="\\"))🤖 Prompt for AI Agents |
||
|
|
||
| case "start with": | ||
| filters.append(json_field.like(f"{value}%")) | ||
| escaped_value = escape_like_pattern(str(value)) | ||
| filters.append(json_field.like(f"{escaped_value}%", escape="\\")) | ||
|
|
||
| case "end with": | ||
| filters.append(json_field.like(f"%{value}")) | ||
| escaped_value = escape_like_pattern(str(value)) | ||
| filters.append(json_field.like(f"%{escaped_value}", escape="\\")) | ||
|
|
||
| case "is" | "=": | ||
| if isinstance(value, str): | ||
|
|
||
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.
Escape character mismatch will cause incorrect search behavior.
The
escape_like_patternfunction escapes special characters using backslash (\), but line 298 specifiesESCAPE '|'in the SQL query. This mismatch means the escaping will not work as intended.For example, searching for "50% discount" will produce
50\% discountin the pattern, but the database expects|as the escape character, so it will interpret\%as a literal backslash followed by a percent wildcard.🐛 Proposed fix: Use consistent escape character
escaped_query = escape_like_pattern(query) query_pattern = f"%{escaped_query}%" sql = f""" SELECT TOP {top_k} id, text, meta FROM {self.schema}.{self.table_name} - WHERE text LIKE ? ESCAPE '|' + WHERE text LIKE ? ESCAPE '\\' """📝 Committable suggestion
🤖 Prompt for AI Agents