Repository navigation
Validate public certificate encodings before x5c construction - #983
Draft
Bogdan Gavril (bgavrilMS) wants to merge 1 commit into
Draft
Bogdan Gavril (bgavrilMS) wants to merge 1 commit into
Bogdan Gavril (bgavrilMS) wants to merge 1 commit into
Conversation
Parse public certificate data before constructing x5c and serialize only certificate DER. Preserve PEM chain order and whitespace-tolerant tagless base64 DER input while rejecting malformed or trailing data. Document the input validation and cover supported forms and setup errors. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 14b467bc-6ea2-4b1f-bd35-38f826d5a90c
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Certificate validation silently accepts truncated PEM chains and lets invalid-version exceptions escape instead of raising the documented ValueError.
2 open issues
What changed in this PR
This PR validates public certificate encodings before MSAL constructs the x5c header for credentials with a manual thumbprint.
Changes:
- Parse certificates using
cryptographyand normalize them to base64-encoded DER while preserving PEM chain order. - Reject malformed certificate data during credential setup and document supported input formats.
- Add regression tests using real certificates for normalization, invalid encodings, and manual-thumbprint behavior.
| File | Description |
|---|---|
| tests/test_optional_thumbprint.py | Use a real certificate fixture and test manual-thumbprint validation. |
| tests/test_application.py | Cover certificate normalization, chain ordering, combined PEM input, and malformed data. |
| msal/application.py | Validate certificate data before constructing x5c and document the behavior. |
Review effort: Balanced
Comment on lines
60
to
+62
| public_certificates = re.findall( | ||
| r'-----BEGIN CERTIFICATE-----(?P<cert_value>[^-]+)-----END CERTIFICATE-----', | ||
| public_cert_content, re.I) | ||
| if public_certificates: | ||
| return [cert.strip() for cert in public_certificates] | ||
| # The public cert tags are not found in the input, | ||
| # let's make best effort to exclude a private key pem file. | ||
| if "PRIVATE KEY" in public_cert_content: | ||
| r'-----BEGIN CERTIFICATE-----(?P<cert_value>.*?)-----END CERTIFICATE-----', | ||
| public_cert_content, re.I | re.S) |
| if certificate_der != der: | ||
| raise ValueError() | ||
| certificates.append(base64.b64encode(certificate_der).decode("ascii")) | ||
| except ValueError: |
This branch has not been deployed
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
Validate public certificate encodings before x5c construction.
ValueErrorduring credential setup for malformed certificate data that was previously forwarded, including empty input, invalid base64/DER, noncertificate data, and trailing DER bytes. The message does not include the supplied content.Validation
Local isolated environment: Windows, Python 3.14.7, cryptography 50.0.2.
python -m pytest tests/test_application.py tests/test_optional_thumbprint.py -q --tb=short: 130 passed, 21 subtests passed.git diff --checkandpython -m pip check: passed. No standalone CI linter is configured.Limitations