Skip to content

Commit

Permalink
Correction of what could be a potential security problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
franckferman committed Oct 23, 2023
1 parent c6d8ddb commit b5089b6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/MetaDetective/MetaDetective.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,14 @@ def fetch_links_from_url(url: str) -> List[str]:
List[str]: List of links found on the page.
"""
try:
response = urllib.request.urlopen(url)
validated_url = valid_url(url)
response = urllib.request.urlopen(validated_url)
data = response.read().decode()
parser = LinkParser()
parser.feed(data)
return parser.links
except Exception as e:
print(f"ERROR: Unable to fetch data from {url}. Reason: {e}")
print(f"ERROR: Unable to fetch data from {validated_url}. Reason: {e}")
return []


Expand Down Expand Up @@ -686,13 +687,14 @@ def download_file(url: str, download_dir: str) -> None:
download_dir (str): Directory where the file should be saved.
"""
try:
local_filename = os.path.join(download_dir, os.path.basename(urlparse(url).path))
with urllib.request.urlopen(url) as response, open(local_filename, 'wb') as out_file:
validated_url = valid_url(url)
local_filename = os.path.join(download_dir, os.path.basename(urlparse(validated_url).path))
with urllib.request.urlopen(validated_url) as response, open(local_filename, 'wb') as out_file:
data = response.read()
out_file.write(data)
print(f"INFO: Downloaded {url} to {local_filename}")
print(f"INFO: Downloaded {validated_url} to {local_filename}")
except Exception as e:
print(f"ERROR: Failed to download {url}. Reason: {e}")
print(f"ERROR: Failed to download {validated_url}. Reason: {e}")


def worker_thread(q: 'queue.Queue[Tuple[str, int, str, bool]]',
Expand Down

0 comments on commit b5089b6

Please sign in to comment.