|
22 | 22 | import random |
23 | 23 | import re |
24 | 24 | import shutil |
| 25 | +import sys |
25 | 26 | import tarfile |
26 | 27 | import tempfile |
27 | 28 | import time |
@@ -591,7 +592,8 @@ def _create_or_update_code_dir( |
591 | 592 | download_file_from_url(source_directory, local_code_path, sagemaker_session) |
592 | 593 |
|
593 | 594 | with tarfile.open(name=local_code_path, mode="r:gz") as t: |
594 | | - t.extractall(path=code_dir) |
| 595 | + check_tarfile_data_filter_attribute() |
| 596 | + t.extractall(path=code_dir, filter="data") |
595 | 597 |
|
596 | 598 | elif source_directory: |
597 | 599 | if os.path.exists(code_dir): |
@@ -628,7 +630,8 @@ def _extract_model(model_uri, sagemaker_session, tmp): |
628 | 630 | else: |
629 | 631 | local_model_path = model_uri.replace("file://", "") |
630 | 632 | with tarfile.open(name=local_model_path, mode="r:gz") as t: |
631 | | - t.extractall(path=tmp_model_dir) |
| 633 | + check_tarfile_data_filter_attribute() |
| 634 | + t.extractall(path=tmp_model_dir, filter="data") |
632 | 635 | return tmp_model_dir |
633 | 636 |
|
634 | 637 |
|
@@ -1489,3 +1492,25 @@ def format_tags(tags: Tags) -> List[TagsDict]: |
1489 | 1492 | return [{"Key": str(k), "Value": str(v)} for k, v in tags.items()] |
1490 | 1493 |
|
1491 | 1494 | return tags |
| 1495 | + |
| 1496 | + |
| 1497 | +class PythonVersionError(Exception): |
| 1498 | + """Raise when a secure [/patched] version of Python is not used.""" |
| 1499 | + |
| 1500 | + |
| 1501 | +def check_tarfile_data_filter_attribute(): |
| 1502 | + """Check if tarfile has data_filter utility. |
| 1503 | +
|
| 1504 | + Tarfile-data_filter utility has guardrails against untrusted de-serialisation. |
| 1505 | +
|
| 1506 | + Raises: |
| 1507 | + PythonVersionError: if `tarfile.data_filter` is not available. |
| 1508 | + """ |
| 1509 | + # The function and it's usages can be deprecated post support of python >= 3.12 |
| 1510 | + if not hasattr(tarfile, "data_filter"): |
| 1511 | + raise PythonVersionError( |
| 1512 | + f"Since tarfile extraction is unsafe the operation is prohibited " |
| 1513 | + f"per PEP-721. Please update your Python [{sys.version}] " |
| 1514 | + f"to latest patch [refer to https://www.python.org/downloads/] " |
| 1515 | + f"to consume the security patch" |
| 1516 | + ) |
0 commit comments