Tolerate malformed part headers in multipart parsing - #11
Conversation
26bf54b to
ef07c32
Compare
When a MIME part's header block contains a line with no colon (e.g. "X-Notice this line is not a valid header field"), ReadHeader fails and previously newPart() aborted the entire multipart parse. Now newPart() retries populateHeaders() from the reader's new position after the bad line. If the remaining lines form a valid header block the part is returned alongside a MalformedPartHeaderError, allowing callers to extract the part body (e.g. a phishing HTML payload). If retry also fails the part is discarded cleanly to the next boundary so subsequent parts are still reachable. NextPart() propagates (part, error) when recovery succeeds. Walk() and the message-level multipartReader.NextPart() treat IsMalformedPartHeader the same as IsUnknownEncoding / IsUnknownCharset. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ef07c32 to
35f6b77
Compare
…ability signal Callers check part != nil to determine whether the recovered part is usable, regardless of whether err is non-nil. The invariant mirrors the MalformedHeaderError removal: non-nil return value means the values are safe to use. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The `err != nil` guard is redundant in the else-if chain: NextPart only returns a non-nil part during header recovery, which always produces a non-nil error, so `part != nil` is sufficient. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ader Recovery from a malformed part header line was landing as an untyped, un-greppable error, distinguishable only by checking whether NextPart's returned part was non-nil. That's inconsistent with how the rest of the package signals this kind of lenient-recovery condition (IsUnknownEncoding, IsUnknownCharset), and forces every error-classification cascade in a caller to be manually audited rather than mechanically checked for the new case. Bring back textproto.MalformedPartHeaderError/IsMalformedPartHeader, and add a message-level IsMalformedPartHeader that forwards to it so callers of the higher-level Entity/MultipartReader API don't need to import textproto directly. Entity.Walk uses the typed check instead of a bare part != nil. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Since the original author is no longer with the team, picked this back up to close a gap before pulling it into go-mantis. The removal of Restored |
Summary
My Summary:
When there is an empty line in the header section, which is a malformed line, Apple Mail still renders the HTML perfectly. We need to parse equally permissively to effectively detect phishes. When there are malformed lines in the headers, skip them and see if you can still form a valid header with subsequent lines. Return the recovered parts along with the error so the caller can decide how to proceed.
🤖 Generated with Claude Code