# HG changeset patch # User Thomas De Schampheleire # Date 2018-02-24 21:18:24 # Node ID 638ac4e653658173216906114e8fdf3960d80783 # Parent 9a69885497b5b520bab420077a5039fb5277d72b issues: gracefully handle invalid issue patterns issue_pat is provided by the admin and can be invalid due to bad syntax. In this case, a page load by a user could cause 500 Internal Server Error. Instead, add a try..except clause around the compilation of issue_pat, and skip invalid patterns. diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py +++ b/kallithea/lib/helpers.py @@ -1141,14 +1141,18 @@ def urlify_issues(newtext, repo_name): issue_pat = CONFIG.get(k) issue_server_link = CONFIG.get('issue_server_link%s' % suffix) issue_prefix = CONFIG.get('issue_prefix%s' % suffix) - if issue_pat and issue_server_link and issue_prefix is not None: # issue_prefix can be empty but should be present - log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix) - else: + if not issue_pat or not issue_server_link or issue_prefix is None: # issue_prefix can be empty but should be present log.error('skipping incomplete issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix) continue # Wrap tmp_urlify_issues_f with substitution of this pattern, while making sure all loop variables (and compiled regexpes) are bound - issue_re = re.compile(issue_pat) + try: + issue_re = re.compile(issue_pat) + except re.error as e: + log.error('skipping invalid issue pattern %r: %r -> %r %r. Error: %s', suffix, issue_pat, issue_server_link, issue_prefix, str(e)) + continue + + log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix) def issues_replace(match_obj, issue_server_link=issue_server_link, issue_prefix=issue_prefix): diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py +++ b/kallithea/tests/other/test_libs.py @@ -460,6 +460,10 @@ class TestLibs(TestController): (r'(?:\s*#)(\d+)', 'http://foo/{repo}/issue/{id}', '#', 'an issue #123 with extra whitespace', """an issue #123 with extra whitespace"""), + # invalid issue pattern + (r'(PR\d+', 'http://foo/{repo}/issue/{id}', '', + 'PR135', + """PR135"""), ]) def test_urlify_issues(self, issue_pat, issue_server, issue_prefix, sample, expected): from kallithea.lib.helpers import urlify_text