From 71017a68a6e86b501592e7daf68be3ef4c4c28f2 Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Fri, 11 Mar 2022 00:19:01 -0500 Subject: [PATCH] Fixed #33571 -- Fixed static serving views crash when If-Modified-Since is empty. Regression in d6aff369ad33457ae2355b5b210faf1c4890ff35. --- django/views/static.py | 4 +++- tests/view_tests/tests/test_static.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/django/views/static.py b/django/views/static.py index 1c558a53ff..3d4075be2b 100644 --- a/django/views/static.py +++ b/django/views/static.py @@ -129,12 +129,14 @@ def was_modified_since(header=None, mtime=0, size=0): if header is None: raise ValueError matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, re.IGNORECASE) + if matches is None: + raise ValueError header_mtime = parse_http_date(matches[1]) header_len = matches[3] if header_len and int(header_len) != size: raise ValueError if int(mtime) > header_mtime: raise ValueError - except (AttributeError, ValueError, OverflowError): + except (ValueError, OverflowError): return True return False diff --git a/tests/view_tests/tests/test_static.py b/tests/view_tests/tests/test_static.py index 5ec054bf30..003e229859 100644 --- a/tests/view_tests/tests/test_static.py +++ b/tests/view_tests/tests/test_static.py @@ -191,3 +191,6 @@ class StaticUtilsTests(unittest.TestCase): mtime = 1343416141.107817 header = http_date(mtime) self.assertFalse(was_modified_since(header, mtime)) + + def test_was_modified_since_empty_string(self): + self.assertTrue(was_modified_since(header="", mtime=1))