Harden security

This commit is contained in:
2026-03-05 05:42:19 +00:00
parent 06735bdfb1
commit 438e561da0
75 changed files with 6260 additions and 278 deletions

View File

@@ -0,0 +1,36 @@
from django.test import SimpleTestCase, override_settings
from core.security.attachments import (
validate_attachment_metadata,
validate_attachment_url,
)
class AttachmentSecurityTests(SimpleTestCase):
def test_blocks_html_payload(self):
with self.assertRaises(ValueError):
validate_attachment_metadata(
filename="payload.html",
content_type="text/html",
size=32,
)
@override_settings(ATTACHMENT_MAX_BYTES=10)
def test_blocks_oversized_payload(self):
with self.assertRaises(ValueError):
validate_attachment_metadata(
filename="dump.bin",
content_type="application/octet-stream",
size=32,
)
def test_blocks_private_url_by_default(self):
with self.assertRaises(ValueError):
validate_attachment_url("http://localhost/internal")
@override_settings(ATTACHMENT_ALLOW_PRIVATE_URLS=True)
def test_allows_private_url_when_explicitly_enabled(self):
self.assertEqual(
"http://localhost/internal",
validate_attachment_url("http://localhost/internal"),
)