Repository navigation
Conversation
…crash on too big float
Numbers like `1e400` are valid JSON, but Python parses them as `inf`.
When such a value is used as a related primary key, the lookup calls
`int(float('inf'))`, which raises `OverflowError`. This resulted in a
500 response instead of a validation error.
MehrazRumman
left a comment
There was a problem hiding this comment.
Read 9677388. The bug is real, and catching OverflowError here fixes it.
The checks I ran, with Python 3.14 and Django 5.2.19:
- With
parse_constant=strict_constant, asJSONParseruses,json.loads('1e400')givesinfand'-1e400'gives-inf, whileInfinityis rejected. So a plain JSON request body can send these values. AutoField.get_prep_value()raisesOverflowError: cannot convert float infinity to integerfor bothinfand-inf. NeitherPrimaryKeyRelatedFieldnorSlugRelatedFieldcatches that today.nanraisesValueErrorand already gets theincorrect_type/invalidmessage.
The tests cover inf and -inf for both fields, and the many=True case for SlugRelatedField. All 8 checks pass on 9677388. I didn't run the test suite myself.
One question about the message, inline.
| self.fail('does_not_exist', pk_value=data) | ||
| except (TypeError, ValueError): | ||
| self.fail('incorrect_type', data_type=type(data).__name__) | ||
| except OverflowError: |
There was a problem hiding this comment.
Should this be a new message, or does_not_exist?
Out-of-range integers already get does_not_exist on every Django version this supports. Django's IntegerFieldOverflow lookup (since 5.0) turns pk=10**30 into an empty result, so {"target": 100000000000000000000000000000} gives Invalid pk "100000000000000000000000000000" - object does not exist. With this PR, {"target": 1e400} would get Value too large to be a valid pk. instead. Both values are equally impossible as a pk.
The wording also doesn't fit -1e400, which gives -inf: that isn't "too large".
Reusing does_not_exist would give the same answer for both kinds of out-of-range number, and wouldn't add new strings to translate:
except (ObjectDoesNotExist, OverflowError):
self.fail('does_not_exist', pk_value=data)That gives Invalid pk "inf" - object does not exist. The same applies to SlugRelatedField at line 466, where does_not_exist would give Object with id=inf does not exist. If you'd rather keep a separate key, something like "out of range" would fit both signs.
There was a problem hiding this comment.
Right. It seems easier and correct. Such object will never exist.
I updated the code.
There was a problem hiding this comment.
Oh, I only saw that now after posting my comment. I agree it makes sense to keep the message consistent for backwards compatibility, but the FloatField works differently 🤔
…values crash on too big float
Numbers like
1e400are valid JSON, but Python parses them asinf. When such a value is used as a related primary key, the lookup callsint(float('inf')), which raisesOverflowError. This resulted in a 500 response instead of a validation error.Note: Before submitting a code change, please review our contributing guidelines.
Description
It fixes a bug which we discovered during pentests.
Inspired by #8716