Skip to content

Reject NaN in minimum, maximum and exclusive bounds - #244

Open
kakkar87d wants to merge 1 commit into
horejsek:masterfrom
kakkar87d:numeric-range-reject-nan
Open

kakkar87d wants to merge 1 commit into
horejsek:masterfrom
kakkar87d:numeric-range-reject-nan

Conversation

@kakkar87d

Copy link
Copy Markdown
Contributor

minimum, maximum and the exclusive bounds accept NaN

The generated checks raise only when the value compares past the bound (if data < 1), and every comparison with NaN is false. json.loads (and so Flask's request.get_json()) accepts the NaN literal by default:

>>> v = fastjsonschema.compile({'type': 'number', 'minimum': 1, 'maximum': 100})
>>> v(json.loads('NaN'))
nan

The same happens with exclusiveMinimum / exclusiveMaximum in every draft. Code that relies on the schema for the range then gets a value for which amount > balance and amount <= 0 are both false.

The checks are now generated as if not data >= 1 and so on, so a value has to compare inside the bound to pass. Over 1200 value/schema combinations (ints, floats, infinity, Decimal, each keyword in draft 04 and 07) the only results that change are float NaN going from accepted to rejected, and the bytecode is the same length (the not only flips the jump). The new test in tests/test_number.py fails on master and passes here, the full suite passes on Python 3.11 and 3.13, and pylint output is unchanged. CHANGELOG.txt has an entry under UNRELEASED.

The generated range checks fail a value only when it compares past the
bound, for example `if data < 1`. Every comparison with NaN is false, so
NaN passed minimum, maximum, exclusiveMinimum and exclusiveMaximum in
all drafts. json.loads accepts NaN by default, so a schema such as
{"type": "number", "minimum": 1, "maximum": 100} accepted
json.loads('NaN').

Generate the checks as `if not data >= 1` and so on, so a value has to
compare inside the bound to pass. The bytecode is the same length, the
`not` only flips the jump.

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