• 
      

    Add a SHA256 hash checksum attribute to file attachments.

    Review Request #14788 — Created Jan. 27, 2026 and submitted

    Information

    Review Board
    release-7.1.x

    Reviewers

    This adds FileAttachment.sha256_checksum, a SHA256 file content hash
    of a file. We expose this attribute via the API so that callers can use
    it to checksum file attachments. We generate and set this hash in our
    upload file form, and in another spot where we create a file attachment for
    the original version of binary files in the diff viewer. Even if these
    two points don't cover all file attachment creation cases, the hash gets
    set on demand whenever the sha256_checksum attribute is accessed.

    This also adds a utility function for creating SHA256 hashes, and a
    type hint to FileDiff.get_repository() that was useful for this work.

    An upcoming change will point FileDiff.patched_sha256 and
    FileDiff.orig_sha256 to the corresponding
    FileAttachment.sha256_checksum for binary file diffs.

    • Ran unit tests.
    • Used in an upcoming RBTools change where I access the sha256_checksum
      attribute via the API.
    Summary ID
    Add a SHA256 hash checksum attribute to file attachments.
    This adds `FileAttachment.sha256_checksum`, a SHA256 file content hash of a file. We expose this attribute via the API so that callers can use it to checksum file attachments. We generate and set this hash in our upload file form, and in another spot where we create a file attachment for the original version of binary files in the diff viewer. Even if these two points don't cover all file attachment creation cases, the hash gets set on demand whenever the `sha256_checksum` attribute is accessed. This also adds a utility function for creating SHA256 hashes, and a type hint to `FileDiff.get_repository()` that was useful for this work.
    ab6513c7bbcf2d1e13372cb766912526f8d7dcd1
    Description From Last Updated

    Can we do the File import inside if TYPE_CHECKING?

    daviddavid

    The argument name is file_content

    daviddavid

    If we wanted to be defensive, we could do elif isinstance(file_content, file) (and ignore my comment about File in TYPE_CHECKING) …

    daviddavid

    Just to ensure that this works across various backends, we should probably put this all inside a with file_content: block.

    daviddavid

    Mind updating this to mention returning None?

    daviddavid

    Missing a trailing comma.

    daviddavid

    This can probably be a @cached_property, which would avoid the micromanagement around _sha256_checksum.

    chipx86chipx86

    Missing parens around the str | None.

    chipx86chipx86

    no newline at end of file Column: 55 Error code: W292

    reviewbotreviewbot
    david
    1. 
        
    2. reviewboard/diffviewer/diffutils.py (Diff revision 1)
       
       
      Show all issues

      Can we do the File import inside if TYPE_CHECKING?

    3. reviewboard/diffviewer/diffutils.py (Diff revision 1)
       
       
      Show all issues

      The argument name is file_content

    4. reviewboard/diffviewer/diffutils.py (Diff revision 1)
       
       
      Show all issues

      If we wanted to be defensive, we could do elif isinstance(file_content, file) (and ignore my comment about File in TYPE_CHECKING) and then have an else clause that uses typelets.runtime.raise_invalid_type

    5. reviewboard/diffviewer/diffutils.py (Diff revision 1)
       
       
       
       
       
       
       
       
       
       
       
      Show all issues

      Just to ensure that this works across various backends, we should probably put this all inside a with file_content: block.

      1. I think I want to avoid closing the file, and let the caller have control over that instead. We don't currently do this anywhere in the code, but it'd be useful to be able to call get_sha256() from a place where we've already got the file object in a context manager. We do a similar thing with guess_mimetype().

    6. Show all issues

      Mind updating this to mention returning None?

    7. Show all issues

      Missing a trailing comma.

    8. 
        
    maubin
    Review request changed
    Commits:
    Summary ID
    Add a SHA256 hash checksum attribute to file attachments.
    This adds `FileAttachment.sha256_checksum`, a SHA256 file content hash of a file. We expose this attribute via the API so that callers can use it to checksum file attachments. We generate and set this hash in our upload file form, and in another spot where we create a file attachment for the original version of binary files in the diff viewer. Even if these two points don't cover all file attachment creation cases, the hash gets set on demand whenever the `sha256_checksum` attribute is accessed. This also adds a utility function for creating SHA256 hashes, and a type hint to `FileDiff.get_repository()` that was useful for this work.
    565e3081d48ffc5415436b846bc09959a7f58c02
    Add a SHA256 hash checksum attribute to file attachments.
    This adds `FileAttachment.sha256_checksum`, a SHA256 file content hash of a file. We expose this attribute via the API so that callers can use it to checksum file attachments. We generate and set this hash in our upload file form, and in another spot where we create a file attachment for the original version of binary files in the diff viewer. Even if these two points don't cover all file attachment creation cases, the hash gets set on demand whenever the `sha256_checksum` attribute is accessed. This also adds a utility function for creating SHA256 hashes, and a type hint to `FileDiff.get_repository()` that was useful for this work.
    ad2f5f7033d15444987f7f946585c6192c5bc3c5

    Checks run (1 failed, 1 succeeded)

    flake8 failed.
    JSHint passed.

    flake8

    chipx86
    1. 
        
    2. reviewboard/attachments/models.py (Diff revision 2)
       
       
       
       
       
       
       
       
       
      Show all issues

      This can probably be a @cached_property, which would avoid the micromanagement around _sha256_checksum.

    3. reviewboard/attachments/models.py (Diff revision 2)
       
       
      Show all issues

      Missing parens around the str | None.

    4. 
        
    maubin
    david
    1. Ship It!
    2. 
        
    maubin
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-7.1.x (7277d57)