Skip to content

fix pattern lookup match escaping on F expressions #61

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 1 commit into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions django_mongodb/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,28 @@ def is_null(self, compiler, connection):
return connection.mongo_operators["isnull"](lhs_mql, self.rhs)


# from https://www.pcre.org/current/doc/html/pcre2pattern.html#SEC4
REGEX_MATCH_ESCAPE_CHARS = (
("\\", r"\\"), # general escape character
("^", r"\^"), # start of string
({"$literal": "$"}, r"\$"), # end of string
(".", r"\."), # match any character
("[", r"\["), # start character class definition
("|", r"\|"), # start of alternative branch
("(", r"\("), # start group or control verb
(")", r"\)"), # end group or control verb
("*", r"\*"), # 0 or more quantifier
("+", r"\+"), # 1 or more quantifier
("?", r"\?"), # 0 or 1 quantifier
("{", r"\}"), # start min/max quantifier
)
Comment on lines +50 to +63
Copy link
Contributor

@Jibola Jibola Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will trigger 13 $replaceAll calls when doing lookups?

Copy link
Collaborator

@WaVEV WaVEV Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to do that replacements in python? and pass the final string. if it is, so we can use re.escape .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the value to be matched is a string, re.escape() handles it. This code is only for the case when regexMatch's regex is a column.



def pattern_lookup_prep_lookup_value(self, value):
if hasattr(self.rhs, "as_mql"):
# If value is a column reference, escape regex special characters.
# If value is a column reference, escape $regexMatch special chars.
# Analogous to PatternLookup.get_rhs_op() / pattern_esc.
for find, replacement in (("\\", r"\\"), ("%", r"\%"), ("_", r"\_")):
for find, replacement in REGEX_MATCH_ESCAPE_CHARS:
value = {"$replaceAll": {"input": value, "find": find, "replacement": replacement}}
else:
# If value is a literal, remove percent signs added by
Expand Down