From 33f2e6d5df8262d911261cfeafe6127ec93cd0be Mon Sep 17 00:00:00 2001 From: Andrew Grant Date: Sun, 3 May 2020 23:12:32 -0400 Subject: [PATCH] Avoid leaking our credentials via a nefarious API request --- OpenBench/utils.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/OpenBench/utils.py b/OpenBench/utils.py index e78f7e3..c5e16b3 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -134,14 +134,21 @@ def getBranch(request, errors, name): target = repo.replace('github.com', 'api.github.com/repos') target = pathjoin(target, url, branch).rstrip('/') + + # Avoid leaking our credentials to other sites + if not target.startswith('https://api.github.com/'): + errors.append('OpenBench may only reach Github\'s API') + return (None, None, None, None) + + # Check for a (User, Token) credentials file + if os.path.exists('credentials'): + with open('credentials') as fin: + user, token = fin.readlines()[0].rstrip().split() + auth = requests.auth.HTTPBasicAuth(user, token) + else: auth = None + + try: - - if os.path.exists('credentials'): - with open('credentials') as fin: - user, token = fin.readlines()[0].rstrip().split() - auth = requests.auth.HTTPBasicAuth(user, token) - else: auth = None - data = requests.get(target, auth=auth).json() data = data if bysha else data['commit']