Opened 7 weeks ago
Closed 45 hours ago
#36191 closed Bug (fixed)
FileSystemStorage with allow_overwrite=True does not truncate previous file
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 , 7 weeks ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 7 weeks ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:3 by , 7 weeks ago
Has patch: | set |
---|
comment:4 by , 7 weeks 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 , 7 weeks ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 2 days 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 , 45 hours 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:
TabularUnified tests/file_storage/tests.py
"second content"Test passes with the suggested patch:
Would you like to submit a PR?