Skip to content

Metadata API: Reject bool values for integer fields - #3070

Open
xgbtxy wants to merge 2 commits into
theupdateframework:developfrom
xgbtxy:fix/reject-bool-as-int-in-metadata
Open

xgbtxy wants to merge 2 commits into
theupdateframework:developfrom
xgbtxy:fix/reject-bool-as-int-in-metadata

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Dec 1, 2026

Copy link
Copy Markdown

Summary

In Python, bool is a subclass of int (isinstance(True, int) == True, with False == 0 and True == 1). As a result, TUF metadata integer validations in tuf/api/_payload.py accepted JSON boolean values (true/false) for integer fields required by the TUF specification:

  • Signed.version ("version": true accepted as 1, passing version <= 0)
  • Role.threshold ("threshold": true accepted as 1, passing threshold < 1)
  • BaseFile._validate_length used by MetaFile.length and TargetFile.length ("length": false accepted as 0 and "length": true accepted as 1, even passing verify_length_and_hashes(b"") / verify_length_and_hashes(b"a"))
  • MetaFile.version ("version": true accepted as 1)
  • SuccinctRoles.bit_length ("bit_length": true accepted as 1)

And when re-serialized via to_dict() / to_bytes(), those fields were emitted as JSON booleans (true/false) instead of integers.

Changes

  • tuf/api/_payload.py: Explicitly reject bool (isinstance(..., bool)) with ValueError in Signed.__init__, Role.__init__, BaseFile._validate_length, MetaFile.__init__, and SuccinctRoles.__init__.
  • tests/test_metadata_serialization.py & tests/test_api.py: Add regression test cases verifying boolean version, threshold, length, and bit_length values are rejected cleanly (and raise DeserializationError during metadata/DSSE deserialization).

Verification

  • Verified Metadata.from_bytes, MetaFile.from_dict, TargetFile.from_dict, Role.from_dict, and SuccinctRoles.from_dict all accepted boolean values before the fix and reject them cleanly after.
  • Full pytest tests -q: 208 passed, 207 subtests passed.

Signed-off-by: Yan Chen <yan8925298@gmail.com>
@xgbtxy
xgbtxy requested a review from a team as a code owner December 1, 2026 23:15
Copilot AI balanced review requested due to automatic review settings December 1, 2026 23:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@EmiRZx EmiRZx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tested locally, tests pass and true/false are now rejected. floats still get through though. 1.5 is accepted for version, threshold and length and written back as 1.5, while the new messages say these must be integers. type(x) is not int would cover bool and float in one check.

Signed-off-by: Yan Chen <yan8925298@gmail.com>
@xgbtxy

xgbtxy commented Dec 2, 2026

Copy link
Copy Markdown
Author

Good catch, @EmiRZx — updated the integer checks (version, threshold, length, bit_length) to use type(x) is not int and added float (1.5) regression tests in e1d2a29.

@EmiRZx EmiRZx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

checked e1d2a29. bools and floats are now rejected for version, threshold, length and bit_length, TargetFile included. bit_length=8.0 also gets the clear message now instead of the format code error. tests pass.

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.

4 participants