Skip to content

Commit

Permalink
Add function to safely extract tarfile
Browse files Browse the repository at this point in the history
  • Loading branch information
FDauphin committed Sep 12, 2024
1 parent e47b812 commit 56debdb
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions notebooks/WFC3/mast_api_psf/download_psf_cutouts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"With the `.tar.gz` file downloaded, we extract the cutouts."
"With the `.tar.gz` file downloaded, we safely extract the cutouts. `get_safe_members` ensures only safe files are extracted."
]
},
{
Expand All @@ -304,10 +304,23 @@
"metadata": {},
"outputs": [],
"source": [
"tar = tarfile.open(filename_bundle, 'r:gz')\n",
"path_mast = tar.getnames()[0] # path of extracted directory\n",
"tar.extractall()\n",
"tar.close()"
"def get_safe_members(members):\n",
" for member in members:\n",
" member_name = member.name\n",
" condition = (\n",
" member_name.startswith('..') or \\\n",
" member_name.startswith('/') or \\\n",
" member_name.startswith('\\\\')\n",
" )\n",
" if not condition:\n",
" yield member\n",
" \n",
"with tarfile.open(filename_bundle, 'r:gz') as tar:\n",
" path_mast = tar.getnames()[0]\n",
" print (f'Path to MAST PSF Cutouts: {path_mast}')\n",
" members = tar.getmembers()\n",
" safe_members = get_safe_members(members)\n",
" tar.extractall(members=safe_members)"
]
},
{
Expand Down

0 comments on commit 56debdb

Please sign in to comment.