From d9a6756b24ae9db0f6bdcc150847d7899e0a50dc Mon Sep 17 00:00:00 2001 From: Aaron Gable Date: Wed, 3 Jan 2018 15:56:08 -0800 Subject: [PATCH] Blacklist http(s): from being parsed as a git footer It's unlikely that anyone would ever intend to have a git trailer whose key is "http", but since URLs are often long, it is rather likely that someone would put a URL on a line all by itself. When that happens, don't accidentally interpret it as a footer. R=iannucci, tandrii Bug: 766234 Change-Id: I3101119c4e49e20339487618cc1719452b026d90 Reviewed-on: https://chromium-review.googlesource.com/849484 Commit-Queue: Aaron Gable Reviewed-by: Andrii Shyshkalov --- git_footers.py | 6 +++--- tests/git_footers_test.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/git_footers.py b/git_footers.py index 3f12d1735..92c7dab42 100755 --- a/git_footers.py +++ b/git_footers.py @@ -15,6 +15,7 @@ import git_common as git FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): *(.*)$') CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/\-\.]+)@{#(\d+)}$') +FOOTER_KEY_BLACKLIST = set(['http', 'https']) def normalize_name(header): @@ -24,10 +25,9 @@ def normalize_name(header): def parse_footer(line): """Returns footer's (key, value) if footer is valid, else None.""" match = FOOTER_PATTERN.match(line) - if match: + if match and match.group(1) not in FOOTER_KEY_BLACKLIST: return (match.group(1), match.group(2)) - else: - return None + return None def parse_footers(message): diff --git a/tests/git_footers_test.py b/tests/git_footers_test.py index 5aa3734df..b89adbcaa 100755 --- a/tests/git_footers_test.py +++ b/tests/git_footers_test.py @@ -89,6 +89,24 @@ My commit message is my best friend. It is my life. I must master it. 'For': ['example'], 'And-Only-Valid': ['footers taken']}) + def testAvoidingURLs(self): + message = ('Someone accidentally put a URL in the footers.\n' + '\n' + 'Followed: by\n' + 'http://domain.tld\n' + 'Some: footers') + self.assertEqual(git_footers.split_footers(message), + (['Someone accidentally put a URL in the footers.', + ''], + ['Followed: by', + 'http://domain.tld', + 'Some: footers'], + [('Followed', 'by'), + ('Some', 'footers')])) + self.assertEqual(git_footers.parse_footers(message), + {'Followed': ['by'], + 'Some': ['footers']}) + def testGetFooterChangeId(self): msg = '\n'.join(['whatever', '',