Text Filters - "Extract Text" filter was not being error checked properly when using a RegEx (#1902)

pull/1903/head
dgtlmoon 1 year ago committed by GitHub
parent 349111eb35
commit 9765d56a23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,8 @@ from wtforms.validators import ValidationError
# each select <option data-enabled="enabled-0-0" # each select <option data-enabled="enabled-0-0"
from changedetectionio.blueprint.browser_steps.browser_steps import browser_step_ui_config from changedetectionio.blueprint.browser_steps.browser_steps import browser_step_ui_config
from changedetectionio import content_fetcher from changedetectionio import content_fetcher, html_tools
from changedetectionio.notification import ( from changedetectionio.notification import (
valid_notification_formats, valid_notification_formats,
) )
@ -284,11 +285,10 @@ class ValidateListRegex(object):
def __call__(self, form, field): def __call__(self, form, field):
for line in field.data: for line in field.data:
if line[0] == '/' and line[-1] == '/': if re.search(html_tools.PERL_STYLE_REGEX, line, re.IGNORECASE):
# Because internally we dont wrap in /
line = line.strip('/')
try: try:
re.compile(line) regex = html_tools.perl_style_slash_enclosed_regex_to_options(line)
re.compile(regex)
except re.error: except re.error:
message = field.gettext('RegEx \'%s\' is not a valid regular expression.') message = field.gettext('RegEx \'%s\' is not a valid regular expression.')
raise ValidationError(message % (line)) raise ValidationError(message % (line))

@ -202,3 +202,35 @@ def test_check_filter_and_regex_extract(client, live_server):
# Should not be here # Should not be here
assert b'Some text that did change' not in res.data assert b'Some text that did change' not in res.data
def test_regex_error_handling(client, live_server):
#live_server_setup(live_server)
# Add our URL to the import page
test_url = url_for('test_endpoint', _external=True)
res = client.post(
url_for("import_page"),
data={"urls": test_url},
follow_redirects=True
)
assert b"1 Imported" in res.data
### test regex error handling
res = client.post(
url_for("edit_page", uuid="first"),
data={"extract_text": '/something bad\d{3/XYZ',
"url": test_url,
"fetch_backend": "html_requests"},
follow_redirects=True
)
with open('/tmp/fuck.html', 'wb') as f:
f.write(res.data)
assert b'is not a valid regular expression.' in res.data
res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True)
assert b'Deleted' in res.data

Loading…
Cancel
Save