FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Role and DelegatedRole hashing by Sachith77 · Pull Request #2973 · theupdateframework/python-tuf · GitHub

Fix Role and DelegatedRole hashing - #2973

Merged
jku merged 1 commit into
theupdateframework:developfrom
Sachith77:fix/role-hash-unhashable-types
Aug 18, 2026
Merged

Fix Role and DelegatedRole hashing#2973
jku merged 1 commit into
theupdateframework:developfrom
Sachith77:fix/role-hash-unhashable-types

Conversation

Copy link
Copy Markdown
Contributor

Description

While going through _payload.py, I noticed that Role.__hash__() fails because it tries to hash keyids (a list) and unrecognized_fields (a dict) directly.

This PR:

  • Converts keyids to a tuple before hashing.
  • Excludes unrecognized_fields since it may contain nested, unhashable JSON.
  • Fixes DelegatedRole.__hash__() using self.path instead of self.paths.
  • Converts paths and path_hash_prefixes to tuples before hashing.
  • Adds regression tests for hashing, equality, and set usage.

The full test suite passes locally with tox.

I noticed similar patterns in a few other metadata classes, but kept this PR focused on Role and DelegatedRole.

Signed-off-by: Sachith Reddy <sachith.24bcs10403@sst.scaler.com>
Sachith77 requested a review from a team as a code owner August 3, 2026 21:41

jku left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

yeah, it may have been a mistake to add hash() to begin with, I think it only happened because ruff complained about not having it and it seemed easy to do...

In any case these fixes seem reasonable to me, thanks

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the context on why hash() was added.

jku merged commit 7e6984f into theupdateframework:develop Aug 18, 2026
17 checks passed
Sachith77 added a commit to Sachith77/python-tuf that referenced this pull request Aug 24, 2026
Follow-up to theupdateframework#2973, which fixed Role.__hash__ and DelegatedRole.__hash__.
The same bug remains in the other implementations: Signed, Root, MetaFile,
Snapshot, Delegations, TargetFile, Targets and Metadata all pass a raw dict
to hash(), so hash() raises "TypeError: unhashable type: 'dict'".
Timestamp.__hash__ is itself correct but inherits the failure from Signed
and MetaFile.

All of these classes define __eq__, so __hash__ is required for them to be
usable in a set or as a dict key. test_metadata_eq_.py covers __eq__ for
exactly these classes but never calls hash(), which is why this went
unnoticed.

Hash a subset of immutable fields, as theupdateframework#2973 did. unrecognized_fields is
excluded throughout since it holds arbitrary nested JSON. Snapshot.meta and
Targets.targets contribute len() rather than their keys, to keep hashing
O(1) for roles with many entries.

Signed-off-by: Sachith Reddy <sachith.24bcs10403@sst.scaler.com>
jku mentioned this pull request Aug 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

2 participants


Back | FazBrowse Home | New Git URL