Skip to content

Chore: use newtypes to make the API more misuse-resistant #249

Closed

Description

@woodruffw

This issue is just a reminder for myself.

We have a few internal APIs that take sequences of str parameters, where each is a base64 (or not-base64) encoded signature, PEM-encoded certificate, etc. It's easy to potentially forget a layer of base64 encoding or decoding with these.

Some typing.NewType usage would help here:

  • b64str for a string that's base64-encoded
  • pemcert for a string that contains a PEM-encoded certificate (not wrapped in another layer of base64)

Activity

  1. added
    enhancement
    on Oct 11, 2022
  2. self-assigned this
    on Oct 11, 2022
  3. removed their assignment
    on Jan 12, 2023
  4. added
    help wanted
    and removed
    help wanted
    on Jan 12, 2023
  5. woodruffw commented on Jan 17, 2023

    @woodruffw
    MemberAuthor

    Some students at Purdue have volunteered to take a stab at this, so I've removed the request for help.

  6. emboman13 commented on Jan 18, 2023

    @emboman13
    Contributor

    Hello, one of the aforementioned Purdue students here, could myself, omartounsi7, and emilejbm be added to this issue? Furthermore, do you have any locations in the codebase to start with so we could have a good idea what to look for in the codebase?

  7. woodruffw commented on Jan 18, 2023

    @woodruffw
    MemberAuthor

    I've added you, but I can't add other non-owners/members unless they actually comment on this issue first 🙂

    Furthermore, do you have any locations in the codebase to start with so we could have a good idea what to look for in the codebase?

    I'd start by looking at sigstore/_utils.py -- there are a bunch of helpers in there that take or return values that should be newtyped. For example:

    def key_id(key: PublicKey) -> bytes:
        """
        Returns an RFC 6962-style "key ID" for the given public key.
    
        See: <https://www.rfc-editor.org/rfc/rfc6962#section-3.2>
        """
        public_bytes = key.public_bytes(
            encoding=serialization.Encoding.DER,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        )
    
        return hashlib.sha256(public_bytes).digest()

    should really return a KeyID newtype, like so:

    KeyID = NewType("KeyID", bytes)
    
    def key_id(key: PublicKey) -> KeyID:
        """
        Returns an RFC 6962-style "key ID" for the given public key.
    
        See: <https://www.rfc-editor.org/rfc/rfc6962#section-3.2>
        """
        public_bytes = key.public_bytes(
            encoding=serialization.Encoding.DER,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        )
    
        return KeyID(hashlib.sha256(public_bytes).digest())

    ...once you do that, you can use the make lint target to figure out which callsites need to be updated (since mypy will complain about mismatching types between bytes and KeyID).

  8. omartounsi7 commented on Jan 18, 2023

    @omartounsi7
    Contributor

    Thanks for the explanation!

  9. woodruffw commented on Jan 18, 2023

    @woodruffw
    MemberAuthor

    No problem! Feel free to ping me either here or on the Sigstore Slack if you have any other questions.

  10. emilejbm commented on Jan 18, 2023

    @emilejbm
    Contributor

    Emile here! Thanks for all the help.

  11. emboman13 commented on Jan 26, 2023

    @emboman13
    Contributor

    We're currently working on https://github.com/emilejbm/sigstore-python. We've changed some files to use NewTypes specified in newtypes.py. Give us a shout if anything looks incorrect or if we've misinterpreted what you wanted us to do. Specifically, changes to sign.py and verify/models.py

  12. woodruffw commented on Jan 26, 2023

    @woodruffw
    MemberAuthor

    Give us a shout if anything looks incorrect or if we've misinterpreted what you wanted us to do. Specifically, changes to sign.py and verify/models.py

    Would you mind opening a draft PR for your changes? That would make it much easier to do an initial round of review 🙂

  13. woodruffw commented on Feb 14, 2023

    @woodruffw
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

choreenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions