Repository navigation
feat: return the MIME tree beside the parsed message - #6
Merged
Merged
Conversation
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.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/parsereturns the MIME tree of the email beside an unchangedmessage, 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 }mail-parser[outlook]>=4.12.0. The CLI runs with--mime-treeand prints the mail and the tree as two JSON documents.mime_tree.py), the same as MailXRay's:cut;422detail is read fromthere. Before this change every
422after the bump carried the genericerror. A missing or undecodable tree is a
422.together. On Linux,
prlimit --fsizeenforces this on a temporary filein
/tmp. Above the cap the request gets422withemail too complex to parse within the service limits.the kept entries are decoded, each from its own slice of the bytes.
The README documents the contract and the limits.
Evidence
make pre-commitandmake test-cov: 73 passed, 100% coverage.runs it). Details in the spec, under "Measurements" and "Output cap".
--jsonalone.422in under 1.5 s, and uvicornholds at most 42 MiB per parse.
test:
Two concurrent 85,000-part emails OOM-killed uvicorn (PID 1).
messagehad no bound: up to 30 MB of response and 114 MiB perparse.
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 the10-second timeout (
504).Merge Danger
Door: two-way
Blast Radius: the response of
POST /api/parse.messageis unchanged for every email that passes.422cases: output over 16 MiB, which means more than about 35,000MIME parts or emails full of control characters.
changes there until CodeGeeksAI/cloud-server#53 bumps it.
minor_release: the merge publishesv0.2.0.