Skip to content

Commit fbe8a26

Browse files
committed
🐛(back) validate document content in serializer
We recently extract images url in the content. For this, we assume that the document content is always in base64. We enforce this assumption by checking if it's a valide base64 in the serializer.
1 parent 3e974be commit fbe8a26

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
## Fixed
12+
13+
- 🐛(back) validate document content in serializer #822
14+
1115
## [3.0.0] - 2025-03-28
1216

1317
## Added

src/backend/core/api/serializers.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Client serializers for the impress core app."""
22

3+
import binascii
34
import mimetypes
5+
from base64 import b64decode
46

57
from django.conf import settings
68
from django.db.models import Q
@@ -299,6 +301,18 @@ def validate_id(self, value):
299301

300302
return value
301303

304+
def validate_content(self, value):
305+
"""Validate the content field."""
306+
if not value:
307+
return None
308+
309+
try:
310+
b64decode(value, validate=True)
311+
except binascii.Error as err:
312+
raise serializers.ValidationError("Invalid base64 content.") from err
313+
314+
return value
315+
302316
def save(self, **kwargs):
303317
"""
304318
Process the content field to extract attachment keys and update the document's

src/backend/core/tests/documents/test_api_documents_update.py

+19
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,22 @@ def test_api_documents_update_administrator_or_owner_of_another(via, mock_user_t
328328
other_document.refresh_from_db()
329329
other_document_values = serializers.DocumentSerializer(instance=other_document).data
330330
assert other_document_values == old_document_values
331+
332+
333+
def test_api_documents_update_invalid_content():
334+
"""
335+
Updating a document with a non base64 encoded content should raise a validation error.
336+
"""
337+
user = factories.UserFactory(with_owned_document=True)
338+
client = APIClient()
339+
client.force_login(user)
340+
341+
document = factories.DocumentFactory(users=[[user, "owner"]])
342+
343+
response = client.put(
344+
f"/api/v1.0/documents/{document.id!s}/",
345+
{"content": "invalid content"},
346+
format="json",
347+
)
348+
assert response.status_code == 400
349+
assert response.json() == {"content": ["Invalid base64 content."]}

0 commit comments

Comments
 (0)