Skip to content

fix(pem): clear line buffer after InvalidData and validate END label#239

Open
sumleo wants to merge 2 commits intorusticata:masterfrom
sumleo:fix/pem-parsing
Open

fix(pem): clear line buffer after InvalidData and validate END label#239
sumleo wants to merge 2 commits intorusticata:masterfrom
sumleo:fix/pem-parsing

Conversation

@sumleo
Copy link

@sumleo sumleo commented Feb 15, 2026

Summary

Two fixes in PEM parsing:

  1. Missing line buffer clear after InvalidData: After encountering an InvalidData error (non-UTF-8 data in comment section), the line buffer was not cleared before continuing the loop. Since read_line appends to the buffer, subsequent reads would accumulate partial data from the failed read, potentially corrupting the -----BEGIN header detection.

  2. END label not validated against BEGIN label: The END marker was accepted without verifying its label matches the BEGIN label. For example, a PEM block starting with -----BEGIN CERTIFICATE----- would accept -----END PRIVATE KEY----- as its terminator. Now validates that the END label matches.

Test plan

  • Full test suite passes with cargo test --all-features
  • Existing PEM tests continue to pass (valid PEM files have matching labels)

@cpu
Copy link
Collaborator

cpu commented Feb 22, 2026

Without weighing in on the fix or the diff yet this needs a reformat and I think should come with test coverage for both fixes. My preference would be one commit per fix + accompanying test.

When `read_line` returns `ErrorKind::InvalidData` (invalid UTF-8), the
line buffer was not cleared before `continue`, so stale data from a
previous successful read could leak into the next iteration and corrupt
the parsed result.

Add `line.clear()` before `continue` to prevent stale data accumulation.
Add test verifying PEM with invalid UTF-8 comment lines is handled
correctly.
The PEM parser did not check that the END label matched the BEGIN label,
silently accepting mismatched PEM blocks (e.g., BEGIN CERTIFICATE / END
PRIVATE KEY). Add validation that rejects mismatched labels with
InvalidHeader error.

Convert `label` from `&str` to `String` to avoid lifetime issues with
the END label comparison. Add test verifying mismatched labels produce
an error.
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