Repository navigation
Commit 4f2cdfd
authored
fix: anchor tar member validation to extract_path in sagemaker-core (#6195)
custom_extractall_tarfile falls back to filtering members with
_get_safe_members when tarfile.data_filter is unavailable (Python < 3.12,
before the 3.9.17 / 3.10.12 / 3.11.4 backports). That fallback had two
containment defects that combined to let an archive member be written
outside the extraction directory:
- _get_safe_members anchored its check to the process working directory
(_get_resolved_path("")) rather than the directory the archive is
extracted into. It now takes the base as an argument, and
custom_extractall_tarfile passes the resolved extract_path.
- _is_bad_path compared paths with str.startswith, so a sibling directory
sharing a textual prefix with the base (e.g. base "/tmp/extract" and
"/tmp/extract-evil/f") was treated as contained. Containment is now
checked with os.path.commonpath, via a shared _is_within_base helper
that _validate_extracted_paths uses as well.
Absolute member paths are now rejected outright, since joinpath would
otherwise silently discard the base for them.
Both defects were required for the escape: relative member resolution is
base-independent under normpath, so a plain "../../x" member does not
escape on its own. The working-directory anchoring is what made a
prefix-matching sibling ("<cwd>" vs "<cwd>evil") pass validation while
extraction still wrote outside extract_path. _validate_extracted_paths
only walks extract_path, so it did not catch the escape either.
This is the shared utility every v3 extraction path routes through (model
unpack, local/image.py, serve TGI and DJL prepare, pipeline repack). An
already-correct implementation of the same logic exists in
sagemaker-mlops/src/sagemaker/mlops/workflow/_repack_model.py; this brings
common_utils.py in line with it.
Adds regression tests covering the end-to-end escape, the sibling-prefix
bypass, absolute members, and that validation is anchored to extract_path.1 parent 44410e2 commit 4f2cdfd
2 files changed
Lines changed: 205 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
1734 | 1734 | | |
1735 | 1735 | | |
1736 | 1736 | | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
1737 | 1759 | | |
1738 | 1760 | | |
1739 | 1761 | | |
| |||
1747 | 1769 | | |
1748 | 1770 | | |
1749 | 1771 | | |
1750 | | - | |
1751 | | - | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
1752 | 1777 | | |
1753 | 1778 | | |
1754 | 1779 | | |
| |||
1768 | 1793 | | |
1769 | 1794 | | |
1770 | 1795 | | |
1771 | | - | |
| 1796 | + | |
1772 | 1797 | | |
1773 | 1798 | | |
1774 | 1799 | | |
1775 | 1800 | | |
1776 | 1801 | | |
1777 | 1802 | | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
1778 | 1806 | | |
1779 | 1807 | | |
1780 | 1808 | | |
1781 | 1809 | | |
1782 | | - | |
1783 | | - | |
1784 | 1810 | | |
1785 | 1811 | | |
1786 | 1812 | | |
| |||
1811 | 1837 | | |
1812 | 1838 | | |
1813 | 1839 | | |
1814 | | - | |
| 1840 | + | |
1815 | 1841 | | |
1816 | 1842 | | |
1817 | 1843 | | |
1818 | 1844 | | |
1819 | 1845 | | |
1820 | 1846 | | |
1821 | 1847 | | |
1822 | | - | |
| 1848 | + | |
1823 | 1849 | | |
1824 | 1850 | | |
1825 | 1851 | | |
| |||
1843 | 1869 | | |
1844 | 1870 | | |
1845 | 1871 | | |
1846 | | - | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
1847 | 1876 | | |
1848 | 1877 | | |
1849 | 1878 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1228 | 1228 | | |
1229 | 1229 | | |
1230 | 1230 | | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
1231 | 1398 | | |
1232 | 1399 | | |
1233 | 1400 | | |
| |||
0 commit comments