Skip to content

Fix PEM parsing resumption for empty labels - #135885

Merged
vcsjones merged 2 commits into
dotnet:mainfrom
vcsjones:fix-pem-find
Oct 15, 2026
Merged

vcsjones merged 2 commits into
dotnet:mainfrom
vcsjones:fix-pem-find

Conversation

@vcsjones

Copy link
Copy Markdown
Member

In #133137 we fixed parsing to continue parsing at a more optimal location to prevent attempts to re-parse content that we know in advance is going to fail.

The fix advanced state a little too far in the narrow case of a PEM having an empty label. Consider the following PEM:

-----BEGIN -----BEGIN TEST-----\nZm9v\n-----END TEST-----

Initially, this is parsed as the PreEB: -----BEGIN ----- which is valid empty label. The BEGIN TEST----- which immediately follows is invalid base64. Before this fix, it would attempt to resume parsing at BEGIN TEST-----\nZm9v\n-----END TEST-----. That wouldn't match because it resumed too far - it needs to resume where the "empty" label was initially found because the two PreEB's share a boundary.

The same fix needs to be applied when looking for the end.

Fixes #135845

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

@vcsjones
vcsjones merged commit 2ab4df2 into dotnet:main Oct 15, 2026
85 of 88 checks passed
@vcsjones

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@vcsjones

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@vcsjones
vcsjones deleted the fix-pem-find branch October 27, 2026 12:44
ViveliDuCh pushed a commit that referenced this pull request Nov 14, 2026
Backport of #135885 to release/10.0

/cc @vcsjones

## Customer Impact

- [x] Customer reported
- [ ] Found internally

In #133250 we fixed a performance
issue with parsing invalid inputs for PEM. This performance improvement
did not account for a particular edge case of empty labels, so
`PemEncoding.TryFind` would fail to find valid PEMs that were being
found previously.

## Regression

- [x] Yes
- [ ] No

Regressed in #133250.

## Testing

Fix was validated against the user-supplied reproduction case.
Additional test cases were added.

## Risk

Low.

Co-authored-by: Kevin Jones <[email protected]>
Co-authored-by: Jeremy Barton <[email protected]>
ViveliDuCh pushed a commit that referenced this pull request Nov 14, 2026
Backport of #135885 to release/11.0

/cc @vcsjones

## Customer Impact

- [x] Customer reported
- [ ] Found internally

In #133249 we fixed a performance
issue with parsing invalid inputs for PEM. This performance improvement
did not account for a particular edge case of empty labels, so
`PemEncoding.TryFind` would fail to find valid PEMs that were being
found previously.

## Regression

- [x] Yes
- [ ] No

Regressed in #133249.

## Testing

Fix was validated against the user-supplied reproduction case.
Additional test cases were added.

## Risk

Low.

Co-authored-by: Kevin Jones <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

X509Certificate2Collection.ImportFromPem silently drops a certificate preceded by -----BEGIN

2 participants