Skip to content
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

fix sanitizer forbid colon within quotes #469

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion numexpr/necompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def stringToExpression(s, types, context, sanitize: bool=True):
# We also cannot ban `.\d*j`, where `\d*` is some digits (or none), e.g. 1.5j, 1.j
if sanitize:
no_whitespace = re.sub(r'\s+', '', s)
if _blacklist_re.search(no_whitespace) is not None:
skip_quotes = re.sub(r'(\'[^\']*\')', '', no_whitespace)
if _blacklist_re.search(skip_quotes) is not None:
raise ValueError(f'Expression {s} has forbidden control characters.')

old_ctx = expressions._context.get_current_context()
Expand Down
8 changes: 8 additions & 0 deletions numexpr/tests/test_numexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,14 @@ def test_sanitize(self):
evaluate('c.real')
evaluate('c.imag')

# pass imaginary unit j
evaluate('1.5j')
evaluate('3.j')

# pass forbidden characters within quotes
x = np.array(['a', 'b'], dtype=bytes)
evaluate("x == 'b:'")


def test_no_sanitize(self):
try: # Errors on compile() after eval()
Expand Down