Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/plistlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ This module defines the following functions:
.. versionchanged:: 3.13
The keyword-only parameter *aware_datetime* has been added.

.. versionchanged:: next
A :exc:`ValueError` is now raised for an invalid ``<date>`` value in an
XML plist (for example ``2024-06Z``). Previously such values raised an
unspecified :exc:`TypeError`.


.. function:: loads(data, *, fmt=None, dict_type=dict, aware_datetime=False)

Expand Down
14 changes: 10 additions & 4 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,22 @@ def _decode_base64(s):

def _date_from_string(s, aware_datetime):
order = ('year', 'month', 'day', 'hour', 'minute', 'second')
gd = _dateParser.match(s).groupdict()
m = _dateParser.match(s)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject trailing content in XML date values

Using match() still accepts any value that merely starts with a valid plist timestamp, so inputs like <date>2024-01-01T00:00:00Zjunk</date> or <date>2024-01-01Z more</date> parse successfully instead of raising the newly documented ValueError for invalid <date> values. This leaves a class of malformed dates silently truncated; use a full-string match or anchor the regex so the whole element content is validated.

Useful? React with 👍 / 👎.

if m is None:
raise ValueError(f"invalid date string: {s!r}")
gd = m.groupdict()
lst = []
for key in order:
val = gd[key]
if val is None:
break
lst.append(int(val))
if aware_datetime:
return datetime.datetime(*lst, tzinfo=datetime.UTC)
return datetime.datetime(*lst)
try:
if aware_datetime:
return datetime.datetime(*lst, tzinfo=datetime.UTC)
return datetime.datetime(*lst)
except (TypeError, ValueError):
raise ValueError(f"invalid date string: {s!r}") from None


def _date_to_string(d, aware_datetime):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,17 @@ def test_invalidreal(self):
self.assertRaises(ValueError, plistlib.loads,
b"<plist><integer>not real</integer></plist>")

def test_invaliddate(self):
for xml in (b"<plist><date>not a date</date></plist>",
b"<plist><date></date></plist>",
b"<plist><date>2024Z</date></plist>",
b"<plist><date>2024-06Z</date></plist>",
b"<plist><date>2024-13-01T00:00:00Z</date></plist>"):
with self.subTest(xml=xml):
self.assertRaises(ValueError, plistlib.loads, xml)
self.assertRaises(ValueError, plistlib.loads, xml,
aware_datetime=True)

def test_integer_notations(self):
pl = b"<plist><integer>456</integer></plist>"
value = plistlib.loads(pl)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`plistlib` now raises :exc:`ValueError` instead of an obscure
:exc:`TypeError` when loading an invalid ``<date>`` value (for example
``2024-06Z``) from an XML plist.
Loading