From 65d70fe417d60eec1e2f68b02fc973bd236595ae Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 12 Jul 2022 13:11:24 +0200 Subject: [PATCH] =?UTF-8?q?XEP-0203:=20Prevent=20na=C3=AFve=20datetime=20f?= =?UTF-8?q?rom=20being=20passed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The specification says “The format MUST adhere to the dateTime format specified in XEP-0082 and MUST be expressed in UTC.” We now respect this requirement, by rejecting naïve datetimes with a ValueError exception, and converting the passed datetime to UTC. Fixes #3471. --- slixmpp/plugins/xep_0203/stanza.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slixmpp/plugins/xep_0203/stanza.py b/slixmpp/plugins/xep_0203/stanza.py index f173d41c..a84cb52f 100644 --- a/slixmpp/plugins/xep_0203/stanza.py +++ b/slixmpp/plugins/xep_0203/stanza.py @@ -30,6 +30,10 @@ class Delay(ElementBase): def set_stamp(self, value): if isinstance(value, dt.datetime): + if value.tzinfo is None: + raise ValueError(f'Datetime provided without timezone information: {value}') + if value.tzinfo != dt.timezone.utc: + value = value.astimezone(dt.timezone.utc) value = xep_0082.format_datetime(value) self._set_attr('stamp', value)