Skip to content

Validate public certificate encodings before x5c construction - #983

Draft
Bogdan Gavril (bgavrilMS) wants to merge 1 commit into
devfrom
bgavril/publicCertValidation
Draft

Bogdan Gavril (bgavrilMS) wants to merge 1 commit into
devfrom
bgavril/publicCertValidation

Conversation

@bgavrilMS

Copy link
Copy Markdown
Member

Summary

Validate public certificate encodings before x5c construction.

  • Decode and parse certificate data using the existing cryptography dependency, then serialize certificate-only DER as base64.
  • Preserve PEM certificate chain order, combined PEM input containing other correctly labeled blocks, and legacy tagless base64-DER input with whitespace. Normalize the resulting x5c values by removing encoding whitespace.
  • Raise a generic ValueError during 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.
  • Leave manual thumbprint selection, no-thumbprint PEM processing, and PFX processing unchanged. Document the supported input forms and changed failure behavior.

Validation

Local isolated environment: Windows, Python 3.14.7, cryptography 50.0.2.

  • Confirmed the new regression expectations fail before the implementation change.
  • python -m pytest tests/test_application.py tests/test_optional_thumbprint.py -q --tb=short: 130 passed, 21 subtests passed.
  • Noninteractive unit suite: 462 passed, 12 skipped, 2 deselected, 690 subtests passed. Exact command:
python -m pytest -q --benchmark-skip --timeout=120 --ignore=tests/test_e2e.py --ignore=tests/test_e2e_manual.py --ignore=tests/test_fmi_e2e.py --ignore=tests/test_mi_e2e.py --ignore=tests/test_agentic_e2e.py --ignore=tests/test_broker.py --deselect=tests/test_cryptography.py::CryptographyTestCase::test_ceiling_should_be_latest_cryptography_version_plus_three --deselect=tests/test_cryptography.py::CryptographyTestCase::test_should_be_run_with_latest_version_of_cryptography --tb=short
  • git diff --check and python -m pip check: passed. No standalone CI linter is configured.

Limitations

  • This checks certificate encoding, not certificate trust, validity periods, or correspondence with the signing key.
  • Live E2E and interactive broker tests were not run. Benchmarks and the two CI warning-only dependency-version checks were excluded. Other supported Python and cryptography versions remain CI validation.

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
Copilot AI balanced review requested due to automatic review settings November 9, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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 cryptography and 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 thread msal/application.py
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)
Comment thread msal/application.py
if certificate_der != der:
raise ValueError()
certificates.append(base64.b64encode(certificate_der).decode("ascii"))
except ValueError:

This branch has not been deployed

No deployments
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