From ece5592421cdfbcd2d7b90a9cddb73e12dbedf62 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 29 Jan 2025 14:11:10 -0800 Subject: [PATCH] Protocol.receive: post age bug fix, handle missing timezone fixes https://console.cloud.google.com/errors/detail/CIXMq_WGs6KuSQ;locations=global;time=P30D?project=bridgy-federated --- protocol.py | 7 +++++-- tests/test_protocol.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/protocol.py b/protocol.py index 41039d32..cca49183 100644 --- a/protocol.py +++ b/protocol.py @@ -1,6 +1,6 @@ """Base protocol class and common code.""" import copy -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import logging import os import re @@ -965,7 +965,10 @@ def receive(from_cls, obj, authed_as=None, internal=False, received_at=None): if obj.type == 'post': if published := inner_obj_as1.get('published'): try: - age = util.now() - util.parse_iso8601(published) + published_dt = util.parse_iso8601(published) + if not published_dt.tzinfo: + published_dt = published_dt.replace(tzinfo=timezone.utc) + age = util.now() - published_dt if age > CREATE_MAX_AGE: error(f'Ignoring, too old, {age} is over {CREATE_MAX_AGE}', status=204) diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 73a01520..01272a2d 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -3050,6 +3050,21 @@ def test_too_old(self): self.assertEqual([], Fake.sent) self.assertEqual([], OtherFake.sent) + def test_too_old_published_without_timezone(self): + Follower.get_or_create(to=self.user, from_=self.alice) + + with self.assertRaises(NoContent): + Fake.receive_as1({ + 'id': 'fake:post', + 'objectType': 'note', + 'author': 'fake:user', + 'published': '2021-12-14T03:04:05', # NOW - 2w + }) + self.assertIsNone(Object.get_by_id('fake:post')) + + self.assertEqual([], Fake.sent) + self.assertEqual([], OtherFake.sent) + def test_receive_activity_lease(self): Follower.get_or_create(to=self.user, from_=self.alice)