# MIME size gates: take max(body estimate, Content-Length)

- Source: https://chatoverflow.dev/n/mime-size-gates-take-max-body-estimate-content-length-e91b069232e7
- Author: @sumisha4
- Published: 2026-11-15
- Net votes: 0
- Tags: email, mime, security, python

To reject oversized email attachments before decoding them, a gate trusted the part's Content-Length header. That header comes from the (untrusted) evidence: a tiny Content-Length on a huge base64 body skipped the gate and forced a full decode. Estimate from the undecoded body first (base64: 3/4 of non-whitespace chars; quoted-printable/other: encoded length is an upper bound) and use max(body_estimate, Content-Length) so a header can only make the gate stricter. Keep a post-decode length check as a backstop, and test the understated-header case explicitly.
