Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #13

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions git-dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import socket
import subprocess
import sys
import traceback
import urllib.parse
import urllib3

Expand All @@ -29,7 +30,7 @@ def printf(fmt, *args, file=sys.stdout):

def is_html(response):
''' Return True if the response is a HTML webpage '''
return '<html>' in response.text
return "html" in response.headers["Content-Type"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be text/html?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, good catch.



def get_indexed_files(response):
Expand Down Expand Up @@ -84,6 +85,16 @@ def get_referenced_sha1(obj_file):
return objs


def verify_response(response):
if response.status_code != 200:
return False, f"[-] %s/%s responded with status code {response.status_code}\n"
elif "Content-Length" in response.headers and response.headers["Content-Length"] == 0:
return False, "[-] %s/%s responded with a zero-length body\n"
elif "Content-Type" in response.headers and "text/html" in response.headers["Content-Type"]:
return False, "[-] %s/%s responded with HTML\n"
else:
return True, True

class Worker(multiprocessing.Process):
''' Worker for process_tasks '''

Expand All @@ -105,7 +116,12 @@ def run(self):
if task is None: # end signal
return

result = self.do_task(task, *self.args)
try:
result = self.do_task(task, *self.args)
except Exception as e:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need e here, we could just except:?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right, good catch

print("Task %s raised exception:" % task)
traceback.print_exc()
result = []

assert isinstance(result, list), 'do_task() should return a list of tasks'

Expand Down Expand Up @@ -180,10 +196,13 @@ def do_task(self, filepath, url, directory, retry, timeout):
allow_redirects=False,
stream=True,
timeout=timeout)) as response:
verification = verify_response(response)
if not verification[0]:
printf(verification[1], url, filepath)
return []
Comment on lines +199 to +202
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below: Instead of using verification[0] and verification[1] we could use unpacking:

valid, error_message = verify_response(response)
if not valid:
    printf(error_message, url, filepath)
    return []

printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

if response.status_code != 200:
return []


abspath = os.path.abspath(os.path.join(directory, filepath))
create_intermediate_dirs(abspath)
Expand All @@ -205,20 +224,19 @@ def do_task(self, filepath, url, directory, retry, timeout):
stream=True,
timeout=timeout)) as response:
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

if (response.status_code in (301, 302) and
'Location' in response.headers and
response.headers['Location'].endswith(filepath + '/')):
return [filepath + '/']

if response.status_code != 200:
return []

if filepath.endswith('/'): # directory index
assert is_html(response)

return [filepath + filename for filename in get_indexed_files(response)]
else: # file
verification = verify_response(response)
if not verification[0]:
printf(verification[1], url, filepath)
return []
abspath = os.path.abspath(os.path.join(directory, filepath))
create_intermediate_dirs(abspath)

Expand All @@ -237,10 +255,12 @@ def do_task(self, filepath, url, directory, retry, timeout):
response = self.session.get('%s/%s' % (url, filepath),
allow_redirects=False,
timeout=timeout)
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

if response.status_code != 200:
verification = verify_response(response)
if not verification[0]:
printf(verification[1], url, filepath)
return []
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

abspath = os.path.abspath(os.path.join(directory, filepath))
create_intermediate_dirs(abspath)
Expand Down Expand Up @@ -269,11 +289,14 @@ def do_task(self, obj, url, directory, retry, timeout):
response = self.session.get('%s/%s' % (url, filepath),
allow_redirects=False,
timeout=timeout)
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

if response.status_code != 200:
verification = verify_response(response)
if not verification[0]:
printf(verification[1], url, filepath)
return []

printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code)

abspath = os.path.abspath(os.path.join(directory, filepath))
create_intermediate_dirs(abspath)

Expand Down Expand Up @@ -309,13 +332,12 @@ def fetch_git(url, directory, jobs, retry, timeout):
response = requests.get('%s/.git/HEAD' % url, verify=False, allow_redirects=False)
printf('[%d]\n', response.status_code)

if response.status_code != 200:
printf('error: %s/.git/HEAD does not exist\n', url, file=sys.stderr)
return 1
elif not response.text.startswith('ref:'):
printf('error: %s/.git/HEAD is not a git HEAD file\n', url, file=sys.stderr)
Comment on lines -315 to -316
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep this check?

verification = verify_response(response)
if not verification[0]:
printf(verification[1], url, "/.git/HEAD")
return 1


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: remove the empty newline

# check for directory listing
printf('[-] Testing %s/.git/ ', url)
response = requests.get('%s/.git/' % url, verify=False, allow_redirects=False)
Expand Down