Skip to content

fix: read Gmail bodies in their charset when the Content-Type header has unusual bytes - #1356

Open
aminbaa wants to merge 1 commit into
openclaw:mainfrom
aminbaa:fix/gmail-charset-header-offset
Open

aminbaa wants to merge 1 commit into
openclaw:mainfrom
aminbaa:fix/gmail-charset-header-offset

Conversation

@aminbaa

@aminbaa aminbaa commented Nov 28, 2026

Copy link
Copy Markdown

What Problem This Solves

When gog reads a Gmail message, it picks the body's charset from the part's Content-Type header, which the sender writes. If that header contains certain bytes before charset= (an invalid UTF-8 byte, İ U+0130, or the Kelvin sign K U+212A), gog reads the wrong charset label and shows the body as mojibake, or the read panics with slice bounds out of range.

User Impact

Messages with such a header now decode in their declared charset, and reading them no longer crashes. Output and flags are unchanged for every other message.

Why This Change Was Made

charsetLabelFromContentType found charset= in strings.ToLower(contentType) but sliced the original string at that index. Lower-casing changes the byte length of those characters, so the index pointed somewhere else in the original. The fix searches an ASCII-only lower-case copy, which has the same length as the original, so the index is valid in both. The mime.ParseMediaType path before it is unchanged; only its fallback is affected.

Evidence

  • New table test TestDecodeBodyCharset_HeaderBytesBeforeLabel (four cases). On main (cfee066), three cases return the wrong text and the fourth panics. With the fix, all pass, along with the rest of ./internal/gmailcontent.
  • A fuzz target, FuzzDecodeBodyCharset, is added. The same target found this bug in a downstream copy of this function in about 7 seconds; on this branch it ran 45 s (about 790k execs) clean with the fix.
  • make lint reports 0 issues.
  • Local fixtures only; not checked against a live Google account.

AI disclosure: the bug was found by fuzzing and the fix was written with an AI coding assistant (Claude); I reviewed the change and ran the tests above.

@clawsweeper

clawsweeper Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-28T02:41:38.489500Z
We couldn’t complete this review. Comment @codex review to try again.
4fb418e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@clawsweeper clawsweeper Bot added P2 Normal priority bug or improvement with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 28, 2026
@clawsweeper

clawsweeper Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

The PR preserves byte offsets when finding charset labels in malformed Content-Type headers.

Example: Decode body byte 0x93 with Content-Type text/plain; name=İ; charset=windows-1252;;.

  • Before: Unicode lowercasing shifts the charset offset, leaving the body incorrectly decoded.
  • After: ASCII-only lowercasing preserves the offset and the body decodes to “.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused implementation appears correct, but required real-behavior proof is absent.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Reading a message with malformed Content-Type bytes can display undecoded text or panic while extracting its charset.
Reason: The change repairs an existing decoding contract at its shared owner without introducing flags, defaults, or another decoding path.

Merge readiness

⛔ Blocked before merge - 1 item remains

This PR fixes a concrete charset-parsing defect still present on main; the production change is focused and preserves existing decoding behavior.

Reviewed head: 4fb418e4d419b891d42fb829683429847538fd51

Before merge

  • Add real behavior proof - The complete body and discussion contain unit tests, fuzzing, and lint claims, but no shipped-CLI result exercising the changed charset fallback against a real Gmail message; contributor proof remains required, with IPs, API keys, phone numbers, and non-public endpoints redacted. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.

Findings

  • [P3] [P3] Include the PR reference and contributor thanks — CHANGELOG.md:5

Tests

  • Missing end-to-end proof: Supply a redacted terminal capture or logs showing the fixed shipped gog gmail get --format full command reading a message whose malformed Content-Type reaches this fallback, with correct text and no panic.
Agent review details

How this fits together

Gmail read commands pass parsed message parts through the shared gmailcontent decoder, which converts base64url body data and MIME encodings into displayed text.

flowchart TD
 A[Gmail API message] --> B[Gmail read command]
 B --> C[Shared body selection]
 C --> D[Transfer decoding]
 D --> E[Charset header parsing]
 E --> F[Charset conversion]
 F --> G[Text or JSON output]
Loading

Technical review

Best possible solution:

Land the byte-preserving search in the existing shared decoder with its focused regression coverage and a policy-compliant changelog entry.

Do we have a high-confidence way to reproduce the issue?

Source inspection establishes the offset mismatch and out-of-range slice for \xb6ChArset=; the added tests provide precise inputs, but no target code was executed during this read-only review.

Is this the best way to solve the issue?

The ASCII-only search directly restores the byte-offset invariant while retaining MIME parsing, fallback extraction, charset conversion, and existing UTF-8 handling.

Full review comments:

  • [P3] [P3] Include the PR reference and contributor thanks — CHANGELOG.md:5
    The new Unreleased entry omits both the PR reference and contributor credit required by the root AGENTS.md changelog policy. Add the reference and thanks to @aminbaa so the release note identifies the contribution.
    Confidence: 1

Overall correctness: patch is correct
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against cfee06622536.

Provenance checked

  • Malformed Content-Type charset fallback keeps the original intent (9977c0b: Support ISO-2022-JP decoding with tolerant charset-label extraction and graceful decoding fallback; the later shared-decoder extraction carried this implementation forward.)

Testing

Proof path: unit tests only.

Security

None.

Evidence

Acceptance criteria:

  • git diff --check
  • git diff -- CHANGELOG.md

What I checked:

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Provide terminal output or logs exercising the repaired fallback through the shipped Gmail read command.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 27, 2026, 9:54 PM ET / November 28, 2026, 02:54 UTC.

…has unusual bytes

charsetLabelFromContentType searched for "charset=" in strings.ToLower(contentType)
but sliced the original string at that index. Lower-casing changes the byte length
of invalid UTF-8 bytes, U+0130 and U+212A, so a header with one of them before the
parameter read the wrong label (the body decoded as mojibake) or sliced out of range
(panic). Search an ASCII-only lower-case copy, which keeps every index.
@aminbaa
aminbaa force-pushed the fix/gmail-charset-header-offset branch from 4fb418e to aeb53a7 Compare November 28, 2026 03:53

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

impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal priority bug or improvement with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant