Opened 3 months ago
Closed 6 weeks ago
#36191 closed Bug (fixed)
FileSystemStorage with allow_overwrite=True does not truncate previous file
Reported by: | Gaël UTARD | Owned by: | Gaël UTARD |
---|---|---|---|
Component: | File uploads/storage | Version: | 5.1 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The new allow_overwrite parameter of FileSystemStorage class allows to overwrite an existing file. However, the previous file is not truncated. So, if the previous file was bigger than the new, the file gets corrupted.
It is possible to workaround with :
storage.OS_OPEN_FLAGS = storage.OS_OPEN_FLAGS & ~os.O_EXCL | os.O_TRUNC
Change History (10)
comment:1 by , 3 months ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 3 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:3 by , 3 months ago
Has patch: | set |
---|
comment:4 by , 3 months ago
Jacob, thanks for your confirmation of the bug, and confirmation of the fix I had in mind.
I pushed a PR, including your idea to assert the length of both contents in the test.
It's my first contribution to Django. I read the docs. I hope I did it right.
comment:5 by , 3 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 7 weeks ago
Has patch: | unset |
---|---|
Resolution: | fixed |
Status: | closed → new |
Triage Stage: | Ready for checkin → Accepted |
Version: | 5.1 → 5.2 |
The bug is still present in the file_move_safe()
function [1] which can be triggered under some circumstances when calling FileSystemStorage._save()
[2] (observed in 5.2 but I'm fairly sure the same is true in main
).
Because it's essentially the same bug I'm reopening this one instead of creating a new one, I hope that's OK.
I'm also leaving the Release Blocker status on due to the data corruption aspect of this issue.
[1] https://github.com/django/django/blob/f7f38f3a0b44d8c6d14344dae66b6ce52cd77b55/django/core/files/move.py#L57
[2] https://github.com/django/django/blob/f7f38f3a0b44d8c6d14344dae66b6ce52cd77b55/django/core/files/storage/filesystem.py#L99
comment:10 by , 6 weeks ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Triage Stage: | Accepted → Ready for checkin |
Version: | 5.2 → 5.1 |
Please open a new ticket so we can replicate and triage the issue
Thanks for the report, reproduced by adjusting this existing test case:
tests/file_storage/tests.py
"second content"Test passes with the suggested patch:
Would you like to submit a PR?