Skip to content

Commit 43e0aab

Browse files
committed
fix: resolve Bandit security warnings in metadata generators
- Add usedforsecurity=False to MD5 hash functions (B324) * MD5 used only for cache filenames (non-security purpose) * MD5 required by Debian/APT format specification (with #nosec) - Replace hardcoded /tmp paths with tempfile.gettempdir() (B108) - Add import tempfile to both scripts Bandit scan now reports: No issues identified
1 parent 626b8e4 commit 43e0aab

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

core/scripts/generate_apt_metadata.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
import subprocess
1515
import sys
16+
import tempfile
1617
from pathlib import Path
1718
from typing import Dict, List
1819

@@ -32,7 +33,8 @@ def get_deb_metadata(url: str, local_cache: Path) -> Dict:
3233
Download DEB and extract metadata.
3334
Cache locally to avoid repeated downloads.
3435
"""
35-
cache_file = local_cache / hashlib.md5(url.encode()).hexdigest()
36+
# MD5 used only for cache filename generation, not for security
37+
cache_file = local_cache / hashlib.md5(url.encode(), usedforsecurity=False).hexdigest() # nosec B324
3638

3739
if cache_file.exists():
3840
print(f"Using cached DEB: {cache_file.name}")
@@ -76,7 +78,8 @@ def get_deb_metadata(url: str, local_cache: Path) -> Dict:
7678
"Size": str(len(deb_content)),
7779
"Filename": url,
7880
"SHA256": hashlib.sha256(deb_content).hexdigest(),
79-
"MD5sum": hashlib.md5(deb_content).hexdigest(),
81+
# MD5sum required by Debian package format specification
82+
"MD5sum": hashlib.md5(deb_content, usedforsecurity=False).hexdigest(), # nosec B324
8083
}
8184

8285

@@ -122,11 +125,12 @@ def create_release_file(codename: str, arch: str, packages_dir: Path):
122125
"""
123126

124127
# Calculate checksums for Packages files
128+
# MD5Sum required by APT repository format specification
125129
release_content += "MD5Sum:\n"
126130
for file in [packages_file, packages_gz]:
127131
if file.exists():
128132
content = file.read_bytes()
129-
md5 = hashlib.md5(content).hexdigest()
133+
md5 = hashlib.md5(content, usedforsecurity=False).hexdigest() # nosec B324
130134
size = len(content)
131135
rel_path = file.relative_to(packages_dir.parent.parent)
132136
release_content += f" {md5} {size} {rel_path}\n"
@@ -165,7 +169,7 @@ def main():
165169
parser.add_argument("--arch", required=True, help="Architecture (amd64, arm64)")
166170
parser.add_argument(
167171
"--cache-dir",
168-
default="/tmp/deb-metadata-cache",
172+
default=f"{tempfile.gettempdir()}/deb-metadata-cache",
169173
help="Cache directory for downloaded DEBs",
170174
)
171175

core/scripts/generate_yum_metadata.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
import os
1515
import sys
16+
import tempfile
1617
import xml.etree.ElementTree as ET
1718
from pathlib import Path
1819
from typing import Dict, List
@@ -25,8 +26,8 @@ def get_rpm_metadata(url: str, local_cache: Path) -> Dict:
2526
Download RPM and extract metadata (name, version, arch, etc.).
2627
Cache locally to avoid repeated downloads.
2728
"""
28-
# Create cache filename from URL
29-
cache_file = local_cache / hashlib.md5(url.encode()).hexdigest()
29+
# Create cache filename from URL - MD5 used only for filename, not security
30+
cache_file = local_cache / hashlib.md5(url.encode(), usedforsecurity=False).hexdigest() # nosec B324
3031

3132
if cache_file.exists():
3233
print(f"Using cached RPM: {cache_file.name}")
@@ -170,7 +171,7 @@ def main():
170171
parser.add_argument("--arch", required=True, help="Architecture (x86_64, aarch64)")
171172
parser.add_argument(
172173
"--cache-dir",
173-
default="/tmp/rpm-metadata-cache",
174+
default=f"{tempfile.gettempdir()}/rpm-metadata-cache",
174175
help="Cache directory for downloaded RPMs",
175176
)
176177

0 commit comments

Comments
 (0)