security: fix 9 vulnerabilities (critical SHA-512 bug, PIN handling, atomic writes, CI/CD) - #2
Open
calu777 wants to merge 1 commit into
Open
security: fix 9 vulnerabilities (critical SHA-512 bug, PIN handling, atomic writes, CI/CD)#2calu777 wants to merge 1 commit into
calu777 wants to merge 1 commit into
Conversation
- hashes.py: fix sha512 branch silently calling hash_sha384 (critical — produced cryptographically invalid signatures with wrong DER OID) - general.py: validate PIN length (4-16 chars) before card transmission to prevent wasting retry attempts and risking card lock-out; store encoded PIN as bytearray and zero it in a finally block - signature.py: reject input files over 512 MB; write output atomically via .tmp + os.replace() to prevent corrupt files on interruption - certificate.py: fix broken f-string in debug line; replace print() with logging.debug(); validate TLV tag before accumulating response data; write output atomically via .tmp + os.replace() - apdu.py: raise ValueError early when lc > 255 instead of crashing with OverflowError at serialization time - i18n.py: ignore unsupported PERUDNIE_LANG values instead of raising KeyError at import time - release.yaml: replace deprecated PyPI username/password with OIDC Trusted Publishing via pypa/gh-action-pypi-publish
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Security audit of the codebase identified 9 vulnerabilities ranging from a critical cryptographic bug to medium/low hardening issues. This PR fixes all of them.
hashes.py:sha512branch was silently callinghash_sha384()— any signature produced with--hash-algorithm sha512was cryptographically invalid (SHA-384 digest under SHA-512 DER OID), making it unverifiable by any conformant verifiercommands/general.py: PIN sent to card without length validation, risking card lock-out (3–5 failed attempts permanently disables the DNIe); PIN stored as immutablestr/byteswith no zeroing after usecommands/signature.py: unbounded file read into RAM (no size limit); non-atomic output write (corrupt file on interruption)commands/certificate.py: broken f-string in debug line ("Select PKI: '{r:!r}'"literal instead of f-string); raw APDU data leaked to stdout viaprint(); TLV tag validated after accumulating data instead of before; non-atomic output writeapdu.py:lc > 255caused an unhandledOverflowErrorat serialization time instead of a clearValueErrorat construction timei18n.py: unsupportedPERUDNIE_LANGvalue (e.g.fr) caused aKeyErrorcrash at import time before any error handling could runrelease.yaml: PyPI publish used deprecated username/password secrets; replaced with OIDC Trusted Publishing viapypa/gh-action-pypi-publish(no stored credentials)Changes
src/peru_dnie/hashes.pysha512branch to callhash_sha512()src/peru_dnie/commands/general.pybytearray+ zeroing infinallyblocksrc/peru_dnie/commands/signature.py.tmp+os.replace()src/peru_dnie/commands/certificate.pyprint()withlogging.debug(); validate TLV before accumulate; atomic writesrc/peru_dnie/apdu.pyValueErrorin__attrs_post_init__whenlc > 255src/peru_dnie/i18n.pyPERUDNIE_LANGagainst unsupported values; add error messages for new validations.github/workflows/release.yamltwine+ password secrets withpypa/gh-action-pypi-publishOIDCCHANGELOG.mdNote on PyPI Trusted Publishing
The
release.yamlchange requires a one-time setup in PyPI: add a Trusted Publisher entry for this repository pointing to therelease.yamlworkflow. No secrets need to be stored. See the PyPI docs for instructions.Test plan
dniectl sign --hash-algorithm sha512 <file> <out>produces a valid signature verifiable withopenssl dgst -sha512 -verifydniectl sign --hash-algorithm sha256 <file> <out>still works correctly (regression check)dniectl signis rejected immediatelyPERUDNIE_LANG=frno longer crashes; falls back to system locale oren