|
26 | 26 | from macaron.json_tools import json_extract
|
27 | 27 | from macaron.malware_analyzer.datetime_parser import parse_datetime
|
28 | 28 | from macaron.slsa_analyzer.package_registry.package_registry import PackageRegistry
|
29 |
| -from macaron.util import download_file_with_size_limit, send_get_http_raw, stream_file_with_size_limit |
| 29 | +from macaron.util import ( |
| 30 | + can_download_file, |
| 31 | + download_file_with_size_limit, |
| 32 | + send_get_http_raw, |
| 33 | + stream_file_with_size_limit, |
| 34 | +) |
30 | 35 |
|
31 | 36 | if TYPE_CHECKING:
|
32 | 37 | from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo
|
@@ -209,6 +214,23 @@ def cleanup_sourcecode_directory(
|
209 | 214 | raise InvalidHTTPResponseError(error_message) from error
|
210 | 215 | raise InvalidHTTPResponseError(error_message)
|
211 | 216 |
|
| 217 | + def can_download_package_sourcecode(self, url: str) -> bool: |
| 218 | + """Check if the package source code can be downloaded within the default file limits. |
| 219 | +
|
| 220 | + Parameters |
| 221 | + ---------- |
| 222 | + url: str |
| 223 | + The package source code url. |
| 224 | +
|
| 225 | + Returns |
| 226 | + ------- |
| 227 | + bool |
| 228 | + True if it can be downloaded within the size limits, otherwise False. |
| 229 | + """ |
| 230 | + size_limit = defaults.getint("slsa.verifier", "max_download_size", fallback=10000000) |
| 231 | + timeout = defaults.getint("downloads", "timeout", fallback=120) |
| 232 | + return can_download_file(url, size_limit, timeout=timeout) |
| 233 | + |
212 | 234 | def download_package_sourcecode(self, url: str) -> str:
|
213 | 235 | """Download the package source code from pypi registry.
|
214 | 236 |
|
@@ -624,6 +646,19 @@ def download_sourcecode(self) -> bool:
|
624 | 646 | logger.debug(error)
|
625 | 647 | return False
|
626 | 648 |
|
| 649 | + def can_download_sourcecode(self) -> bool: |
| 650 | + """Return whether the package source code can be downloaded within the download file size limits. |
| 651 | +
|
| 652 | + Returns |
| 653 | + ------- |
| 654 | + bool |
| 655 | + ``True`` if the source code can be downloaded; ``False`` if not. |
| 656 | + """ |
| 657 | + url = self.get_sourcecode_url() |
| 658 | + if url: |
| 659 | + return self.pypi_registry.can_download_package_sourcecode(url) |
| 660 | + return False |
| 661 | + |
627 | 662 | def get_sourcecode_file_contents(self, path: str) -> bytes:
|
628 | 663 | """
|
629 | 664 | Get the contents of a single source code file specified by the path.
|
|
0 commit comments