🐛 Make sure lastmodified is timezone aware
Otherwise we'll run into an error comparing with Flask's timezone aware headers. Fixes #4410
This commit is contained in:
1 parent
b21099b5a0
commit
5a5460d08a
3 files changed
+54
-7
No files matched your search
@@ -9,6 +9,8 @@ import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Union
|
||||
|
||||
import flask
|
||||
import flask.json
|
||||
@@ -33,6 +35,7 @@ from octoprint.settings import settings
|
||||
from octoprint.util import DefaultOrderedDict, deprecated, yaml
|
||||
from octoprint.util.json import JsonEncoding
|
||||
from octoprint.util.net import is_lan_address
|
||||
from octoprint.util.tz import UTC_TZ, is_timezone_aware
|
||||
|
||||
# ~~ monkey patching
|
||||
|
||||
@@ -1274,21 +1277,40 @@ def check_etag(etag):
|
||||
)
|
||||
|
||||
|
||||
def check_lastmodified(lastmodified):
|
||||
def check_lastmodified(lastmodified: Union[int, float, datetime]) -> bool:
|
||||
"""Compares the provided lastmodified value with the value of the If-Modified-Since header.
|
||||
|
||||
If ``lastmodified`` is an int or float, it's assumed to be a Unix timestamp and converted
|
||||
to a timezone aware datetime instance in UTC.
|
||||
|
||||
If ``lastmodified`` is a datetime instance, it needs to be timezone aware or the
|
||||
result will always be ``False``.
|
||||
|
||||
Args:
|
||||
lastmodified (Union[int, float, datetime]): The last modified value to compare against
|
||||
|
||||
Raises:
|
||||
ValueError: If anything but an int, float or datetime instance is passed
|
||||
|
||||
Returns:
|
||||
bool: true if the values indicate that the document is still up to date
|
||||
"""
|
||||
|
||||
if lastmodified is None:
|
||||
return False
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
if isinstance(lastmodified, (int, float)):
|
||||
# max(86400, lastmodified) is workaround for https://bugs.python.org/issue29097,
|
||||
# present in CPython 3.6.x up to 3.7.1.
|
||||
#
|
||||
# I think it's fair to say that we'll never encounter lastmodified values older than
|
||||
# 1970-01-02 so this is a safe workaround.
|
||||
lastmodified = datetime.fromtimestamp(max(86400, lastmodified)).replace(
|
||||
microsecond=0
|
||||
)
|
||||
#
|
||||
# Timestamps are defined as seconds since epoch aka 1970/01/01 00:00:00Z, so we
|
||||
# use UTC as timezone here.
|
||||
lastmodified = datetime.fromtimestamp(
|
||||
max(86400, lastmodified), tz=UTC_TZ
|
||||
).replace(microsecond=0)
|
||||
|
||||
if not isinstance(lastmodified, datetime):
|
||||
raise ValueError(
|
||||
@@ -1297,6 +1319,15 @@ def check_lastmodified(lastmodified):
|
||||
)
|
||||
)
|
||||
|
||||
if not is_timezone_aware(lastmodified):
|
||||
# datetime object is not timezone aware, we can't check lastmodified with that
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(
|
||||
"lastmodified is not timezone aware, cannot check against If-Modified-Since. In the future this will become an error!",
|
||||
stack_info=logger.isEnabledFor(logging.DEBUG),
|
||||
)
|
||||
return False
|
||||
|
||||
return (
|
||||
flask.request.method in ("GET", "HEAD")
|
||||
and flask.request.if_modified_since is not None
|
||||
|
||||
@@ -1603,6 +1603,8 @@ def _compute_date(files):
|
||||
import stat
|
||||
from datetime import datetime
|
||||
|
||||
from octoprint.util.tz import UTC_TZ
|
||||
|
||||
max_timestamp = 0
|
||||
for path in files:
|
||||
try:
|
||||
@@ -1617,7 +1619,11 @@ def _compute_date(files):
|
||||
|
||||
if max_timestamp:
|
||||
# we set the micros to 0 since microseconds are not speced for HTTP
|
||||
max_timestamp = datetime.fromtimestamp(max_timestamp).replace(microsecond=0)
|
||||
max_timestamp = (
|
||||
datetime.fromtimestamp(max_timestamp)
|
||||
.replace(microsecond=0)
|
||||
.replace(tzinfo=UTC_TZ)
|
||||
)
|
||||
return max_timestamp
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
|
||||
|
||||
import datetime
|
||||
|
||||
LOCAL_TZ = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
|
||||
UTC_TZ = datetime.timezone.utc
|
||||
|
||||
|
||||
def is_timezone_aware(dt: datetime.datetime) -> bool:
|
||||
return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None
|
||||
Reference in new issue
Block a user