Skip to content

Fix that PrimaryKeyRelatedField and SlugRelatedField with int values crash on too big float - #10101

Open
shenek wants to merge 4 commits into
encode:mainfrom
shenek:strict-json-out-of-range-floats
Open

shenek wants to merge 4 commits into
encode:mainfrom
shenek:strict-json-out-of-range-floats

Conversation

@shenek

@shenek shenek commented Nov 10, 2026

Copy link
Copy Markdown

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.

Note: Before submitting a code change, please review our contributing guidelines.

Description

It fixes a bug which we discovered during pentests.
Inspired by #8716

…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 MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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, as JSONParser uses, json.loads('1e400') gives inf and '-1e400' gives -inf, while Infinity is rejected. So a plain JSON request body can send these values.
  • AutoField.get_prep_value() raises OverflowError: cannot convert float infinity to integer for both inf and -inf. Neither PrimaryKeyRelatedField nor SlugRelatedField catches that today. nan raises ValueError and already gets the incorrect_type / invalid message.

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.

Comment thread rest_framework/relations.py Outdated
self.fail('does_not_exist', pk_value=data)
except (TypeError, ValueError):
self.fail('incorrect_type', data_type=type(data).__name__)
except OverflowError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right. It seems easier and correct. Such object will never exist.

I updated the code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 🤔

@shenek
shenek requested a review from MehrazRumman November 10, 2026 12:42
@auvipy
auvipy requested review from auvipy and removed request for MehrazRumman November 10, 2026 13:23
Comment thread rest_framework/relations.py Resolved

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

Development

Successfully merging this pull request may close these issues.

4 participants