Skip to content

Commit 9da4965

Browse files
keflavichbsipocz
authored andcommitted
wrap filenames in os.path.basename to prevent possible security exploit
add missing os import, fix a whitespace error basename on esasky paths too
1 parent 6f7ae8b commit 9da4965

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

astroquery/alma/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,8 @@ def download_files(self, files, *, savedir=None, cache=True,
837837
raise (ex)
838838

839839
try:
840-
filename = re.search("filename=(.*)",
841-
check_filename.headers['Content-Disposition']).groups()[0]
840+
filename = os.path.basename(re.search("filename=(.*)",
841+
check_filename.headers['Content-Disposition']).groups()[0])
842842
except KeyError:
843843
log.info(f"Unable to find filename for {file_link} "
844844
"(missing Content-Disposition in header). "

astroquery/esa/iso/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from astroquery.utils.tap.core import TapPlus
1313
from astroquery.query import BaseQuery
1414
import shutil
15+
import os
1516
from email.message import Message
1617
from requests import HTTPError
1718
from pathlib import Path
@@ -211,8 +212,8 @@ def get_postcard(self, tdt, *, filename=None, verbose=False):
211212
response = self._request('HEAD', link)
212213
response.raise_for_status()
213214

214-
filename = re.findall('filename="(.+)"', response.headers[
215-
"Content-Disposition"])[0]
215+
filename = os.path.basename(re.findall('filename="(.+)"', response.headers[
216+
"Content-Disposition"])[0])
216217
else:
217218

218219
filename = filename + ".png"

astroquery/esa/xmm_newton/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ def get_postcard(self, observation_id, *, image_type="OBS_EPIC",
182182
if filename is None:
183183
response = self._request('HEAD', link)
184184
response.raise_for_status()
185-
filename = re.findall('filename="(.+)"', response.headers[
186-
"Content-Disposition"])[0]
185+
filename = os.path.basename(re.findall('filename="(.+)"', response.headers[
186+
"Content-Disposition"])[0])
187187
else:
188188
filename = observation_id + ".png"
189189

@@ -318,7 +318,7 @@ def _get_username_and_password(self, credentials_file):
318318

319319
def _create_filename(self, filename, observation_id, suffixes):
320320
if filename is not None:
321-
filename = os.path.splitext(filename)[0]
321+
filename = os.path.basename(os.path.splitext(filename)[0])
322322
else:
323323
filename = observation_id
324324
filename += "".join(suffixes)

astroquery/esasky/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,20 +1660,20 @@ def _extract_file_name_from_response_header(self, headers):
16601660

16611661
if ".gz" in content_disposition[start_index:].lower():
16621662
end_index = (content_disposition.lower().index(".gz", start_index + 1) + len(".gz"))
1663-
return content_disposition[start_index: end_index]
1663+
return os.path.basename(content_disposition[start_index: end_index])
16641664
elif self.__FITS_STRING in content_disposition[start_index:].lower():
16651665
end_index = (
16661666
content_disposition.lower().index(self.__FITS_STRING, start_index + 1) + len(self.__FITS_STRING))
1667-
return content_disposition[start_index: end_index]
1667+
return os.path.basename(content_disposition[start_index: end_index])
16681668
elif self.__FTZ_STRING in content_disposition[start_index:].upper():
16691669
end_index = (content_disposition.upper().index(self.__FTZ_STRING, start_index + 1) + len(self.__FTZ_STRING))
1670-
return content_disposition[start_index: end_index]
1670+
return os.path.basename(content_disposition[start_index: end_index])
16711671
elif ".fit" in content_disposition[start_index:].upper():
16721672
end_index = (content_disposition.upper().index(".fit", start_index + 1) + len(".fit"))
1673-
return content_disposition[start_index: end_index]
1673+
return os.path.basename(content_disposition[start_index: end_index])
16741674
elif self.__TAR_STRING in content_disposition[start_index:].lower():
16751675
end_index = (content_disposition.lower().index(self.__TAR_STRING, start_index + 1) + len(self.__TAR_STRING))
1676-
return content_disposition[start_index: end_index]
1676+
return os.path.basename(content_disposition[start_index: end_index])
16771677
else:
16781678
return ""
16791679

astroquery/utils/tap/conn/tapconn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import mimetypes
2020
import platform
2121
import time
22-
22+
import os
2323
from astroquery.utils.tap.xmlparser import utils
2424
from astroquery.utils.tap import taputils
2525
from astroquery import version
@@ -585,7 +585,7 @@ def get_file_from_header(self, headers):
585585
if content_disposition is not None:
586586
p = content_disposition.find('filename="')
587587
if p >= 0:
588-
filename = content_disposition[p+10:len(content_disposition)-1]
588+
filename = os.path.basename(content_disposition[p+10:len(content_disposition)-1])
589589
content_encoding = self.find_header(headers, 'Content-Encoding')
590590
if content_encoding is not None:
591591
if "gzip" == content_encoding.lower():

0 commit comments

Comments
 (0)