fix(classify): do not crash on invalid user-supplied regex - #148
fix(classify): do not crash on invalid user-supplied regex#148fanxing11 wants to merge 1 commit into
Conversation
Rule.__init__ used to bubble up re.error from re.compile() when a user saved an invalid regex pattern in the category editor (e.g. "Notepad++" on Python versions where "++" is not a valid possessive quantifier). aw-server then returned 500 Internal Server Error for the whole query, which broke every categorize()/tag() chart in the web UI, not just the offending rule. Catch re.error at compile time, log a warning, and disable that one rule (regex=None, match always False). Other rules keep working and events that would have matched the broken rule show up as "Uncategorized" instead of crashing the whole query. Fixes ActivityWatch/activitywatch#1340
| regex_str, | ||
| (re.IGNORECASE if self.ignore_case else 0) | re.UNICODE, | ||
| ) | ||
| except re.error as e: |
There was a problem hiding this comment.
Non-string regex errors escape
When a categorize or tag rule contains a truthy non-string regex value such as 123, re.compile raises TypeError, which bypasses this handler and still aborts the entire query with an internal server error.
| except re.error as e: | |
| except (re.error, TypeError) as e: |
Greptile SummaryThis PR prevents syntactically invalid string regexes from aborting classification queries.
Confidence Score: 3/5The PR should not merge until malformed non-string regex values are prevented from escaping the new recovery path and aborting classification queries. The change handles re.error correctly for malformed strings, but reachable query dictionaries can contain truthy non-string regex values for which re.compile raises an uncaught TypeError. Files Needing Attention: aw_transform/classify.py Important Files Changed
Reviews (1): Last reviewed commit: "fix(classify): do not crash on invalid u..." | Re-trigger Greptile |
Small drive-by fix for ActivityWatch/activitywatch#1340.
What happened
Rule.__init__(inaw_transform/classify.py) compiles user-suppliedregex patterns eagerly with
re.compile(...). If the pattern isinvalid,
re.erroris raised straight out of__init__, bubbles upthrough the query engine, and
aw-serverresponds with 500 InternalServer Error for the whole query. The web UI then shows nothing for
any categorize()/tag() chart, not just the rule that was broken.
The reproducer in the issue is
Notepad++(the user probably wantedNotepad\+\+). On Python <= 3.10 this raisesre.error: multiple repeat at position 8. On 3.11+++happens to be a valid possessivequantifier so that particular pattern no longer breaks, but the class
of bug still exists: any user typo like
*fooor(unclosedwilltake down the whole categorize query.
Fix
Catch
re.errorinRule.__init__, log a warning, and setself.regex = Noneso that rule matches nothing. Other rules keep working;events that would have matched the broken rule fall through to
Uncategorizedinstead of tanking the query. The webui can stillprovide inline validation on top of this later, but the server should
not 500 on user input.
Tests
Added two tests in
tests/test_transforms.py:test_rule_invalid_regex_does_not_raise— invalid pattern givesregex=None,match(event)returnsFalse.test_categorize_survives_invalid_regex— a bad rule mixed with agood rule: good rule still categorizes, unrelated events go to
Uncategorized(instead of the whole call raising).Existing
test_categorize/test_tagsunchanged and still pass.Fixes ActivityWatch/activitywatch#1340