changeset 7228:638ac4e65365

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.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Sat, 24 Feb 2018 21:18:24 +0100
parents 9a69885497b5
children e5a7f8f41370
files kallithea/lib/helpers.py kallithea/tests/other/test_libs.py
diffstat 2 files changed, 12 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Sat Feb 17 21:59:08 2018 +0100
+++ b/kallithea/lib/helpers.py	Sat Feb 24 21:18:24 2018 +0100
@@ -1141,14 +1141,18 @@
             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):
--- a/kallithea/tests/other/test_libs.py	Sat Feb 17 21:59:08 2018 +0100
+++ b/kallithea/tests/other/test_libs.py	Sat Feb 24 21:18:24 2018 +0100
@@ -460,6 +460,10 @@
         (r'(?:\s*#)(\d+)', 'http://foo/{repo}/issue/{id}', '#',
             'an issue   #123       with extra whitespace',
             """an issue <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a>       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