mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed CVE-2022-36359 -- Escaped filename in Content-Disposition header.
Thanks to Motoyasu Saburi for the report.
This commit is contained in:
@@ -143,6 +143,41 @@ class FileResponseTests(SimpleTestCase):
|
||||
'%s; filename="%s"' % (header_disposition, header_filename),
|
||||
)
|
||||
|
||||
def test_content_disposition_escaping(self):
|
||||
# fmt: off
|
||||
tests = [
|
||||
(
|
||||
'multi-part-one";\" dummy".txt',
|
||||
r"multi-part-one\";\" dummy\".txt"
|
||||
),
|
||||
]
|
||||
# fmt: on
|
||||
# Non-escape sequence backslashes are path segments on Windows, and are
|
||||
# eliminated by an os.path.basename() check in FileResponse.
|
||||
if sys.platform != "win32":
|
||||
# fmt: off
|
||||
tests += [
|
||||
(
|
||||
'multi-part-one\\";\" dummy".txt',
|
||||
r"multi-part-one\\\";\" dummy\".txt"
|
||||
),
|
||||
(
|
||||
'multi-part-one\\";\\\" dummy".txt',
|
||||
r"multi-part-one\\\";\\\" dummy\".txt"
|
||||
)
|
||||
]
|
||||
# fmt: on
|
||||
for filename, escaped in tests:
|
||||
with self.subTest(filename=filename, escaped=escaped):
|
||||
response = FileResponse(
|
||||
io.BytesIO(b"binary content"), filename=filename, as_attachment=True
|
||||
)
|
||||
response.close()
|
||||
self.assertEqual(
|
||||
response.headers["Content-Disposition"],
|
||||
f'attachment; filename="{escaped}"',
|
||||
)
|
||||
|
||||
def test_content_disposition_buffer(self):
|
||||
response = FileResponse(io.BytesIO(b"binary content"))
|
||||
self.assertFalse(response.has_header("Content-Disposition"))
|
||||
|
||||
Reference in New Issue
Block a user