Skip to content

replace raw header errors with typed MalformedHeaderError with kind and offset - #17

Merged
thunderkatz merged 3 commits into
masterfrom
better-errors
Nov 5, 2026
Merged

thunderkatz merged 3 commits into
masterfrom
better-errors

Conversation

@thunderkatz

@thunderkatz thunderkatz commented Nov 3, 2026 •

Copy link
Copy Markdown

Summary

https://app.notion.com/p/sublimesecurity/Enhance-header-repair-for-positional-issues-and-rejoins-40d04655fc9d81ea8193ca14d13b901a

  • Replaces the raw-string malformed MIME header ... errors (initial line, line without colon, invalid key) with *MalformedHeaderError{Kind, Offset}.
  • Offset is the byte offset from the start of the header block of the offending line (or key). The key error previously carried no position.
  • readLineSlice now counts consumed bytes, so offsets are exact for CRLF and LF input. For part headers the offset is relative to the start of that part's header.
  • Adds IsMalformedHeader; MalformedPartHeaderError still wraps the new error, so errors.As works on either. Both Is* helpers use errors.AsType.
  • The offending text is not included in the error, since it's derivable from Offset + Kind

Test plan

  • go test ./...
  • New TestReadHeader_malformedHeaderError covers each kind and offsets (LF and CRLF)

🤖 Generated with Claude Code

Replace the raw-string "malformed MIME header" errors with
*MalformedHeaderError, carrying a Kind (initial line, line, key) and the
byte Offset from the start of the header block. The key error previously
reported no position at all.

readLineSlice now counts consumed bytes so offsets are exact for both
CRLF and LF input. Adds IsMalformedHeader, and switches both Is* helpers
to errors.AsType.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@thunderkatz
thunderkatz requested a balanced review from Copilot November 4, 2026 20:23
@thunderkatz thunderkatz changed the title textproto: typed MalformedHeaderError with kind and offset replace raw header errors with typed MalformedHeaderError with kind and offset Nov 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Multipart recovery tests do not verify both wrapped error types via errors.As or confirm that offsets reset per part, including malformed lines after valid LF- and CRLF-terminated headers.

1 open issue
What changed in this PR

This PR adds typed MIME header errors so callers can identify malformed headers and their byte offsets without exposing offending header text.

Changes:

  • Introduces MalformedHeaderError, error kinds, and IsMalformedHeader.
  • Tracks consumed bytes for LF and CRLF input, including part-relative offsets in multipart headers.
  • Adds tests for malformed initial lines, missing colons, and invalid keys.
File Description
textproto/​multipart.go Tracks part-relative offsets and updates error detection to errors.AsType.
textproto/​header.go Defines typed errors and tracks byte offsets during header parsing.
textproto/​header_test.go Tests malformed-header kinds and offsets for LF and CRLF input.

Review effort: Balanced

Comment thread textproto/multipart.go Resolved
Check that NextPart's MalformedPartHeaderError wraps a
MalformedHeaderError and that the offset restarts at each part's header,
for both LF and CRLF line endings.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the typed MIME header error changes.

1 open issue

Review effort: Balanced

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@thunderkatz
thunderkatz marked this pull request as ready for review November 5, 2026 14:25
@thunderkatz
thunderkatz requested a review from a team November 5, 2026 14:25

@dlynch-sublime dlynch-sublime left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@thunderkatz
thunderkatz merged commit 15aaf0c into master Nov 5, 2026
3 checks passed
@thunderkatz
thunderkatz deleted the better-errors branch November 5, 2026 15:14
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.

3 participants