Skip to content

Commit

Permalink
Add link unshortener
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtOfCode- committed Dec 5, 2017
1 parent 101003b commit f4dec15
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions helpers.py
Expand Up @@ -3,6 +3,7 @@
from collections import namedtuple
from datetime import datetime
from termcolor import colored
import requests

Response = namedtuple('Response', 'command_status message')

Expand Down Expand Up @@ -41,3 +42,32 @@ def only_blacklists_changed(diff):
files_changed = diff.split()
non_blacklist_files = [f for f in files_changed if f not in blacklist_files]
return not bool(non_blacklist_files)


# FAIR WARNING: Sending HEAD requests to resolve a shortened link is generally okay - there aren't
# as many exploits that work on just HEAD responses. If you specify sending a GET request, you
# acknowledge that this will fetch the full, potentially unsafe response from the shortener.
def unshorten_link(url, request_type='HEAD', explicitly_ignore_security_warning=False):
requesters = {
'GET': requests.get,
'HEAD': requests.head
}
if request_type not in requesters:
raise KeyError('Unavailable request_type {}'.format(request_type))
if request_type == 'GET' and not explicitly_ignore_security_warning:
raise SecurityError('Potentially unsafe request type GET not acknowledged')

requester = requesters[request_type]
response_code = 301
headers = {'User-Agent': 'SmokeDetector/git (+https://github.com/Charcoal-SE/SmokeDetector)'}
while response_code in [301, 302, 303, 307, 308]:
res = requester(url, headers=headers)
response_code = res.status_code
if 'Location' in res.headers:
url = res.headers['Location']

return url


class SecurityError(Exception):
pass
12 changes: 12 additions & 0 deletions test/test_helpers.py
@@ -0,0 +1,12 @@
import pytest
import helpers


@pytest.mark.parametrize('shortened, original', [
('https://goo.gl/kAb9rz', 'https://charcoal-se.org/'),
('https://bit.ly/2jhMbxn', 'https://charcoal-se.org/'),
('https://tinyurl.com/y7ba4pth', 'https://charcoal-se.org/'),
('https://tiny.cc/gsbbpy', 'https://charcoal-se.org/')
])
def test_unshorten_link(shortened, original):
assert helpers.unshorten_link(shortened) == original

0 comments on commit f4dec15

Please sign in to comment.