misc: limit filename length (#72567)

This commit is contained in:
Frédéric Péters 2022-12-17 18:14:45 +01:00
parent 6216d0ed09
commit 8dea3ea312
2 changed files with 23 additions and 0 deletions

View File

@ -129,6 +129,10 @@ class Document(models.Model):
self.content_hash = utils.sha256_of_file(self.content)
if not self.mime_type:
self.mime_type = utils.get_mime_type(self.content.file.name) or ''
if self.content.name and len(self.content.name) > 200:
file_root, file_ext = os.path.splitext(self.content.name)
file_root, file_ext = file_root[:150], file_ext[:50]
self.content.name = file_root + file_ext
super().save(*args, **kwargs)
@property

View File

@ -139,3 +139,22 @@ def test_push_document_slashed_name(app, admin_user, john_doe):
assert doc.get_download_url() == '/%s/download/monfichier%%252018-06-2017.pdf' % doc.pk
login(app, user=john_doe)
app.get(doc.get_download_url(), status=200)
def test_push_document_long_filename(app, admin_user, john_doe):
login(app)
url = '/api/documents/push/'
data = {
'user_email': john_doe.email,
'origin': 'wcs',
'file_b64_content': base64.b64encode(b'whatever').decode(),
'file_name': '%s.pdf' % ('abc' * 100),
}
response = app.post_json(url, params=data, status=200)
assert response.json['result'] == 1
assert models.Document.objects.count() == 1
doc = models.UserDocument.objects.first()
assert doc.filename.startswith('abc') and doc.filename.endswith('.pdf')
assert doc.get_download_url().endswith('.pdf')
login(app, user=john_doe)
app.get(doc.get_download_url(), status=200)