entropy: exempt small content from check

Small data has unreliable entropy measurement due to sample size.
MIN_ENTROPY_SIZE (default 256 bytes) sets the threshold.
This commit is contained in:
Username
2025-12-20 08:48:13 +01:00
parent 8addf2d9e8
commit 7deba711d4
4 changed files with 23 additions and 6 deletions

View File

@@ -250,9 +250,11 @@ class TestEntropyEnforcement:
def test_plaintext_rejected(self, entropy_client):
"""Plaintext content should be rejected when entropy required."""
# Must be >= MIN_ENTROPY_SIZE (256 bytes) to trigger check
plaintext = b"Hello, this is plain English text. " * 10 # ~350 bytes
response = entropy_client.post(
"/",
data=b"Hello, this is plain English text with low entropy.",
data=plaintext,
content_type="text/plain",
)
assert response.status_code == 400
@@ -287,12 +289,23 @@ class TestEntropyEnforcement:
def test_repeated_bytes_rejected(self, entropy_client):
"""Repeated bytes have zero entropy and should be rejected."""
# Must be >= MIN_ENTROPY_SIZE (256 bytes) to trigger check
response = entropy_client.post(
"/",
data=b"a" * 1000,
data=b"a" * 500,
content_type="text/plain",
)
assert response.status_code == 400
data = response.get_json()
assert data["entropy"] == 0.0
def test_small_content_exempt(self, entropy_client):
"""Small content should be exempt from entropy check."""
# Content < MIN_ENTROPY_SIZE (256 bytes) should pass
response = entropy_client.post(
"/",
data=b"Small plaintext content",
content_type="text/plain",
)
assert response.status_code == 201