You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Chore: use newtypes to make the API more misuse-resistant #249
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)
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?
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:
defkey_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,
)
returnhashlib.sha256(public_bytes).digest()
should really return a KeyID newtype, like so:
KeyID=NewType("KeyID", bytes)
defkey_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,
)
returnKeyID(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).
Activity
woodruffw commentedon Jan 17, 2023
Some students at Purdue have volunteered to take a stab at this, so I've removed the request for help.
emboman13 commentedon Jan 18, 2023
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?
woodruffw commentedon Jan 18, 2023
I've added you, but I can't add other non-owners/members unless they actually comment on this issue first 🙂
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:should really return a
KeyIDnewtype, like so:...once you do that, you can use the
make linttarget to figure out which callsites need to be updated (sincemypywill complain about mismatching types betweenbytesandKeyID).omartounsi7 commentedon Jan 18, 2023
Thanks for the explanation!
woodruffw commentedon Jan 18, 2023
No problem! Feel free to ping me either here or on the Sigstore Slack if you have any other questions.
emilejbm commentedon Jan 18, 2023
Emile here! Thanks for all the help.
emboman13 commentedon Jan 26, 2023
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
woodruffw commentedon Jan 26, 2023
Would you mind opening a draft PR for your changes? That would make it much easier to do an initial round of review 🙂
woodruffw commentedon Feb 14, 2023
#474.