Skip to content

MPP-3852: Use cryptography for SNS signature validation, remove pyopenssl and pem #5235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 4, 2024

Conversation

jwhitlock
Copy link
Member

This builds on PR #5234. It changes emails/sns.py to use cryptography for PEM loading, certificate validation, and signature verification. It removes the dependencies pyopenssl and pem, which are no longer needed.

It adds a new exception VerificationFailed for exceptions, rather than replace OpenSSL.crypto.Error with cryptography.exceptions.InvalidSignature. This requires updating some tests and try blocks, which would have needed to update anyway, but does insulate the code from future changes.

Once this merges, I expect

@jwhitlock jwhitlock requested a review from groovecoder December 4, 2024 17:17
@groovecoder groovecoder self-assigned this Dec 4, 2024
Copy link
Member

@groovecoder groovecoder left a comment

Choose a reason for hiding this comment

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

Great fix and reduction in dependencies!

Comment on lines +19 to +20
NOTIFICATION_HASH_FORMAT = """\
Message
Copy link
Member

Choose a reason for hiding this comment

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

praise: nice little clean-up.

emails/sns.py Outdated
# Extract the first certificate in the file and confirm it's a valid
# PEM certificate
certificates = pem.parse(smart_bytes(pemfile))

certs = x509.load_pem_x509_certificates(pemfile)
Copy link
Member

Choose a reason for hiding this comment

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

quibble (non-blocking): this is 2x calls to x509.load_pem_x509_certificate[s] when we could maybe change the code to only call load_pem_x509_certificate 1x and return the single valid cert that it loaded?

Copy link
Member Author

Choose a reason for hiding this comment

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

I took it one step futher and changed _grab_keyfile, returning PEM bytes, to _get_signing_public_key, returning an RSAPublicKey. This way the certificate is loaded and validated the first time it is read, not every time it is fetched from the cache.

@groovecoder groovecoder assigned jwhitlock and unassigned groovecoder Dec 4, 2024
@jwhitlock jwhitlock added this pull request to the merge queue Dec 4, 2024
Merged via the queue into main with commit 4fb645d Dec 4, 2024
31 checks passed
@jwhitlock jwhitlock deleted the remove-pyopenssl-mpp-3852 branch December 4, 2024 21:48
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