diff --git a/sdk/longlink/database/companies.py b/sdk/longlink/database/companies.py index 5562abca7..2147061dc 100644 --- a/sdk/longlink/database/companies.py +++ b/sdk/longlink/database/companies.py @@ -97,19 +97,23 @@ def validate_description(cls, value: str | None) -> str | None: raise ValueError("description exceeds database byte capacity 65535") return value - @field_validator("logo") + @field_validator("logo", mode="before") @classmethod - def validate_logo(cls, value: bytes | None) -> bytes | None: - """Limit image bytes to the database capacity and require a WebP container.""" + def validate_logo(cls, value: object) -> bytes | None: + """Check optional WebP container signatures and byte limits without decoding.""" - if value is not None and len(value) > cls.MAX_LOGO_BYTES: + # Reject coercible values before inspecting image contents. + if value is None: + return None + if not isinstance(value, bytes): + raise ValueError("Logo must contain valid bytes") + if len(value) > cls.MAX_LOGO_BYTES: raise ValueError(f"Logo must contain at most {cls.MAX_LOGO_BYTES} bytes of image data") - # Reject other image formats without adding an image decoder dependency. - if value is not None and ( - len(value) < 12 or value[:4] != b"RIFF" or value[8:12] != b"WEBP" or int.from_bytes(value[4:8], "little") != len(value) - 8 - ): + # Match the WebP signature and declared container size, excluding trailing bytes. + if len(value) < 12 or value[:4] != b"RIFF" or value[8:12] != b"WEBP" or int.from_bytes(value[4:8], "little") != len(value) - 8: raise ValueError("Company logos must use the WebP format") + return value @property diff --git a/sdk/tests/database/test_base.py b/sdk/tests/database/test_base.py index 1fc997991..6970e1c3e 100644 --- a/sdk/tests/database/test_base.py +++ b/sdk/tests/database/test_base.py @@ -278,7 +278,7 @@ def test_company_collector_rejects_descriptions_exceeding_byte_capacity(descript Company.model_validate(profile) -@pytest.mark.parametrize("size", [None, 65535]) +@pytest.mark.parametrize("size", [None, 12, 65534, 65535]) def test_company_collector_preserves_logos_within_byte_capacity(size: int | None) -> None: """Preserve nulls and signature-correct bytes at the storage boundary.""" @@ -291,7 +291,7 @@ def test_company_collector_preserves_logos_within_byte_capacity(size: int | None validated = Company.validate_logo(image) # Assert - assert company.logo == image + assert company.logo is image assert validated is image @@ -309,7 +309,19 @@ def test_company_collector_rejects_logo_byte_overflow() -> None: Company.validate_logo(image) -@pytest.mark.parametrize("image", [b"\x89PNG\r\n\x1a\n", b"", b"random bytes", b"RIFF\x00\x00\x00\x00WEBP"]) +@pytest.mark.parametrize( + "image", + [ + pytest.param(b"\x89PNG\r\n\x1a\n", id="png"), + pytest.param(b"", id="svg"), + pytest.param(b"random bytes", id="wrong-riff-signature"), + pytest.param(b"RIFF\x04\x00\x00\x00WAVE", id="wrong-webp-signature"), + pytest.param(b"RIFF\x04\x00\x00\x00WEB", id="truncated-header"), + pytest.param(b"RIFF\x00\x00\x00\x00WEBP", id="declared-size-too-small"), + pytest.param(b"RIFF\x08\x00\x00\x00WEBP", id="declared-size-too-large"), + pytest.param(b"RIFF\x04\x00\x00\x00WEBPjunk", id="trailing-junk"), + ], +) def test_company_collector_rejects_non_webp_images(image: bytes) -> None: """Reject other formats and inconsistent WebP container lengths.""" @@ -319,6 +331,30 @@ def test_company_collector_rejects_non_webp_images(image: bytes) -> None: # Act and assert with pytest.raises(ValidationError, match="WebP"): Company.model_validate(profile) + with pytest.raises(ValueError, match="WebP"): + Company.validate_logo(image) + + +@pytest.mark.parametrize( + "image", + [ + pytest.param("RIFF\x04\x00\x00\x00WEBP", id="string"), + pytest.param(bytearray(b"RIFF\x04\x00\x00\x00WEBP"), id="bytearray"), + pytest.param(memoryview(b"RIFF\x04\x00\x00\x00WEBP"), id="memoryview"), + pytest.param(42, id="integer"), + ], +) +def test_company_rejects_logo_type_coercion(image: object) -> None: + """Require bytes before Pydantic can coerce strings or mutable buffers.""" + + # Arrange + profile = {"uid": "CHE123456789", "name": "Company", "slug": "company", "logo": image} + + # Act and assert + with pytest.raises(ValidationError): + Company.model_validate(profile) + with pytest.raises(ValueError, match="Logo"): + Company.validate_logo(image) @pytest.mark.parametrize(