Skip to content

Reject non-ASCII digits in the date, date-time and time formats - #240

Open
kakkar87d wants to merge 1 commit into
horejsek:masterfrom
kakkar87d:date-time-ascii-digits
Open

kakkar87d wants to merge 1 commit into
horejsek:masterfrom
kakkar87d:date-time-ascii-digits

Conversation

@kakkar87d

Copy link
Copy Markdown
Contributor

date, date-time and time formats accept non-ASCII digits

The format regexes match digits with \d, which for a Python str pattern is any Unicode decimal digit, not only the 0-9 that RFC 3339 allows:

>>> v = fastjsonschema.compile({'format': 'date'})
>>> v('1963-06-1৪')                  # Bengali 4, accepted
>>> v('2018-02-05')  # fullwidth year, accepted
>>> datetime.date.fromisoformat('1963-06-1৪')
ValueError: Invalid isoformat string: '1963-06-1৪'

Format checks run on the data being validated, so a field declared as date or date-time can carry a value that passes the validator and then raises in fromisoformat(), or that int() reads as an ordinary number (int('১৯৬৩') == 1963) while other consumers reject it.

\d is now [0-9] in DATE_REGEX (shared by date and date-time), in the time part of date-time and in time. No other built-in format uses \d. For ASCII input the old and new regexes accept exactly the same strings (checked over 300k generated strings). The JSON-Schema-Test-Suite "invalid non-ASCII" cases for these three formats now pass in every draft. The cases added to tests/test_format.py fail on master and pass here, the rest of the suite passes on Python 3.10, 3.11 and 3.13, and pylint reports nothing new. CHANGELOG.txt has an entry under UNRELEASED.

The date, date-time and time format regexes matched digits with \d,
which in a Python str pattern is any Unicode decimal digit, while RFC
3339 allows only 0-9. A date with a Bengali 4 in the day or with a
fullwidth year was accepted, although date.fromisoformat() raises
ValueError on it. Match the digits with [0-9] instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant