Merge remote-tracking branch 'origin/master' into renovate/frontend-dependencies

This commit is contained in:
Georges-Antoine Assi
2026-10-01 12:35:12 -04:00
2 changed files with 79 additions and 9 deletions

View File

@@ -72,33 +72,47 @@ async def add_firmware(
not_found_detail=f"Platform with ID {platform_id} not found",
)
uploaded_firmware = []
firmware_path = fs_firmware_handler.get_firmware_fs_structure(db_platform.fs_slug)
# write_file stores each upload under its sanitized basename, so the lookup,
# scan and row must use that same name. Check them all before writing any.
uploads: list[tuple[UploadFile, str]] = []
for file in files:
if not file.filename:
log.warning("Empty filename, skipping")
continue
try:
file_name = fs_firmware_handler._sanitize_filename(file.filename)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid upload filename: {exc}",
) from exc
uploads.append((file, file_name))
uploaded_firmware = []
firmware_path = fs_firmware_handler.get_firmware_fs_structure(db_platform.fs_slug)
for file, file_name in uploads:
log.info(
f"Uploading firmware {hl(file.filename)} to {hl(db_platform.custom_name or db_platform.name, color=BLUE)}"
f"Uploading firmware {hl(file_name)} to {hl(db_platform.custom_name or db_platform.name, color=BLUE)}"
)
await fs_firmware_handler.write_file(file=file, path=firmware_path)
await fs_firmware_handler.write_file(
file=file, path=firmware_path, filename=file_name
)
db_firmware = db_firmware_handler.get_firmware_by_filename(
platform_id=db_platform.id, file_name=file.filename
platform_id=db_platform.id, file_name=file_name
)
# Scan or update firmware
scanned_firmware = await scan_firmware(
platform=db_platform,
file_name=file.filename,
file_name=file_name,
firmware=db_firmware,
)
is_verified = Firmware.verify_file_hashes(
platform_slug=db_platform.slug,
file_name=file.filename,
file_name=file_name,
file_size_bytes=scanned_firmware.file_size_bytes,
md5_hash=scanned_firmware.md5_hash,
sha1_hash=scanned_firmware.sha1_hash,
@@ -124,7 +138,7 @@ async def add_firmware(
AuditAction.FIRMWARE_UPLOAD,
request,
AuditTarget.of_platform(db_platform),
{"file_names": [file.filename for file in files if file.filename]},
{"file_names": [file_name for _, file_name in uploads]},
)
return {

View File

@@ -1,9 +1,13 @@
"""Tests for `GET /api/firmware`."""
from pathlib import Path
from unittest import mock
import pytest
from fastapi import status
from handler.database import db_firmware_handler
def test_get_firmware_requires_auth(client, firmware):
response = client.get("/api/firmware")
@@ -131,3 +135,55 @@ def test_head_firmware_content_404s_when_the_file_is_gone(
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
@pytest.fixture
def _isolated_firmware_dir(tmp_path, monkeypatch):
"""Point the shared fs_firmware_handler at the test's tmp dir."""
from handler.filesystem import fs_firmware_handler
new_base = Path(tmp_path).resolve()
monkeypatch.setattr(fs_firmware_handler, "base_path", new_base)
return new_base
@pytest.mark.parametrize(
"file_name",
["sub/scph1001.bin", "../scph1001.bin"],
ids=["nested", "traversal"],
)
def test_add_firmware_records_the_name_it_wrote(
client, access_token, platform, _isolated_firmware_dir, file_name
):
"""The disk write takes the basename of the upload's filename, so the
row has to name that same file, not the raw name the client sent."""
response = client.post(
"/api/firmware",
params={"platform_id": platform.id},
files={"files": (file_name, b"BIOS", "application/octet-stream")},
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
rows = db_firmware_handler.list_firmware(platform_ids=[platform.id])
assert [row.file_name for row in rows] == ["scph1001.bin"]
assert (_isolated_firmware_dir / rows[0].file_path / rows[0].file_name).is_file()
def test_add_firmware_rejects_an_overlong_name_before_writing(
client, access_token, platform, _isolated_firmware_dir
):
"""A bad name anywhere in the batch is a 400, and nothing is written."""
response = client.post(
"/api/firmware",
params={"platform_id": platform.id},
files=[
("files", ("ok.bin", b"BIOS", "application/octet-stream")),
("files", ("x" * 1000 + ".bin", b"BIOS", "application/octet-stream")),
],
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert db_firmware_handler.list_firmware(platform_ids=[platform.id]) == []
assert not any(p.is_file() for p in _isolated_firmware_dir.rglob("*"))