Skip to content

feat: return the MIME tree beside the parsed message - #6

Merged
fedelemantuano merged 5 commits into
mainfrom
feature/message-structure
Nov 27, 2026
Merged

fedelemantuano merged 5 commits into
mainfrom
feature/message-structure

Conversation

@fedelemantuano

@fedelemantuano fedelemantuano commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

POST /api/parse returns the MIME tree of the email beside an unchanged
message, with mail-parser 4.12.0. Spec and ticket:
docs/features/message-structure/.

 POST /api/parse  ->  200
   {
     "mail_parser_version": "4.12.0",
-    "message": {...}
+    "message": {...},                 # unchanged: whole, or the request fails
+    "mime_tree": [{..., "cut": []}],  # at most 500 entries
+    "mime_tree_parts_left_out": 0     # entries not kept
   }
  • Dependency: mail-parser[outlook]>=4.12.0. The CLI runs with
    --mime-tree and prints the mail and the tree as two JSON documents.
  • Bounds of the tree (mime_tree.py), the same as MailXRay's:
    • the first 500 entries are kept;
    • every string is cut to 1024 characters, and its key is named in cut;
    • the kept entries hold at most 262,144 characters of text together;
    • entries past the bounds are counted, never dropped silently.
  • Errors: 4.12.0 logs to stderr, so the 422 detail is read from
    there. Before this change every 422 after the bump carried the generic
    error. A missing or undecodable tree is a 422.
  • Output cap: the CLI may print at most 16 MiB, the mail and the tree
    together. On Linux, prlimit --fsize enforces this on a temporary file
    in /tmp. Above the cap the request gets 422 with
    email too complex to parse within the service limits.
    • A real 5 MB email prints 5 to 7 MB.
    • Before the cap, 5 MB of control characters made a 30 MB response.
  • Memory: stdout is never decoded to text as a whole. Only the mail and
    the kept entries are decoded, each from its own slice of the bytes.

The README documents the contract and the limits.

Evidence

  • make pre-commit and make test-cov: 73 passed, 100% coverage.
  • Measured on Linux (Docker, 512 MiB, the child run as the service
    runs it). Details in the spec, under "Measurements" and "Output cap".
    • 34,000 to 40,000 parts and 970 levels of nesting pass.
    • 5 MB emails with base64 attachments pass, at the same peak as
      --json alone.
    • Emails of control characters get 422 in under 1.5 s, and uvicorn
      holds at most 42 MiB per parse.
  • Security review, two Medium findings, each fixed with a regression
    test:
    • One character outside the BMP made the server hold about 6x stdout.
      Two concurrent 85,000-part emails OOM-killed uvicorn (PID 1).
    • message had no bound: up to 30 MB of response and 114 MiB per
      parse.
    • Replaying the review's attack sequences: before the fix PID 1 was
      killed in 3 of 3 runs; after it, uvicorn stays at 54 to 57 MiB with
      oom_kill 0. Under 15 concurrent requests one child exceeded the
      10-second timeout (504).

Merge Danger

Door: two-way

Blast Radius: the response of POST /api/parse.

  • message is unchanged for every email that passes.
  • New 422 cases: output over 16 MiB, which means more than about 35,000
    MIME parts or emails full of control characters.
  • The live parser on spamscope.ai pins the image by digest, so nothing
    changes there until CodeGeeksAI/cloud-server#53 bumps it.

minor_release: the merge publishes v0.2.0.

@fedelemantuano fedelemantuano added the patch_release Bug fix or patch version bump label Nov 22, 2026
Record the bounds agreed with MailXRay (500 entries, 1024 characters per
value, mime_tree_parts_left_out and cut), mark the ticket unblocked by
mail-parser 4.12.0 and add the MIME tree terms to the glossary.
Raise the requirement to mail-parser[outlook]>=4.12.0 and run the CLI with
--mime-tree. POST /api/parse returns mime_tree and
mime_tree_parts_left_out beside an unchanged message.

- mime_tree.py decodes the tree one entry at a time and keeps the first
  500 entries, cuts every string value to 1024 characters (named in
  cut) and bounds the text of the kept entries to 262,144 characters,
  as MailXRay does. Entries past the bounds are counted, never held.
- 4.12.0 logs to stderr: the 422 detail is read from there, and stdout
  must start with the mail JSON. Without this every 422 carried the
  generic error after the bump.
- A missing or undecodable tree is unparsable output (422).
- Spec and ticket record the measurements under the child limits and
  the decisions they led to; README documents the contract.
Decoding all of stdout into one str let a single character outside the
BMP (Subject: 😀) store the whole output at four bytes per character.
With --mime-tree, 0.6 MB of input prints about 35 MB, so the server held
about six times that, and two concurrent emails of 85,000 parts got
uvicorn (PID 1) OOM-killed in a 512 MiB container.

The mail and each kept tree entry are now decoded from their own slice
of the bytes, found in the indent=2 layout the CLI prints; entries past
the bounds are only found and counted. Any other layout is unparsable
output (422). The same replay keeps PID 1 alive.
message had no bound: JSON prints a control character as six bytes and
a value can appear more than once (to, then raw and candidates of its
address defect). 5 MB of control characters in the body or in the
Received headers made a 30 MB response and held about 114 MiB in
uvicorn per parse; 3 MB in a Content-Type parameter made 18 MB.

The CLI now writes stdout to an anonymous temporary file in /tmp, which
prlimit --fsize caps at 16 MiB in the container, and the size is checked
after the child exits on every platform. An email that reaches the cap
gets 422 with TOO_COMPLEX_ERROR; message is whole or absent, never cut.
A real 5 MB email prints 5 to 7 MB. The largest MIME tree that passes
drops from about 70,000 parts to about 35,000.

Replayed on Linux with the service limits: the attack emails get 422 and
uvicorn stays below 60 MiB, against 229 MiB before.
@fedelemantuano fedelemantuano added minor_release New feature, backwards compatible and removed patch_release Bug fix or patch version bump labels Nov 27, 2026
@fedelemantuano fedelemantuano changed the title docs: spec and ticket for returning the MIME tree feat: return the MIME tree beside the parsed message Nov 27, 2026
@fedelemantuano
fedelemantuano merged commit d7a95b8 into main Nov 27, 2026
3 of 5 checks passed
@fedelemantuano
fedelemantuano deleted the feature/message-structure branch November 27, 2026 16:53

This branch was successfully deployed

1 active deployment
release — c2e35632 Deployed Nov 27, 2026 by fedelemantuano via release #4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor_release New feature, backwards compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant