FA-76351 / Email MIME structure / Open access
Pick the part to render from multipart/alternative: charset default and case · case 01
Parts without a charset parameter, or with an upper-case charset name, are treated as unrenderable.
ROOT CAUSE
The charset default and case decision evaluates `.get('charset', '').lower()` where the contract requires `.get('charset', 'us-ascii').lower()`.
VERIFIED REPAIR
Use `.get('charset', 'us-ascii').lower()` for the charset default and case decision and keep every other rule of the model unchanged.
Unsuccessful approach: Keeping the us-ascii default but comparing case-sensitively still rejects UTF-8 written in capitals. The attempted `.get('charset', 'us-ascii')` still disagrees with a fixture.
Case contract
Alternatives are ordered by increasing fidelity, so the LAST part whose effective type is in `supported` wins. A multipart/related alternative counts as its root type: the child whose cid equals the start parameter (which carries angle brackets, cids are stored bare) or else the first child. A text/* part is only renderable when its charset (default us-ascii, case-insensitive) is us-ascii, utf-8 or iso-8859-1. With nothing renderable the first part is shown as a fallback. Result {index, type, fallback}.
Why this case matters
Choosing the wrong alternative shows raw markup, a degraded body or an unreadable charset to the reader.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(part, supported):
def effective(p):
if p['type'] == 'multipart/related':
start = p.get('params', {}).get('start')
for c in p['parts']:
if start is not None and c.get('cid') == start.strip('<>'):
return c['type']
return p['parts'][0]['type'] if p['parts'] else None
return p['type']
ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
best = None
for i, p in enumerate(part['parts']):
t = effective(p)
cs = p.get('params', {}).get('charset', '').lower()
if t in supported and (not t.startswith('text/') or cs in ok_charsets):
best = i
if best is None:
return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| richest supported last | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'} | Failed |
| unsupported last part | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Failed |
| related root via start | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Failed |
| upper-case charset | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| missing charset | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Failed |
| unknown charset html | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 0, 'type': 'text/plain'} | Failed |
| nothing supported | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | Passed |
SHA-256 / c2202b7d34d0b719760b7c13038f215b6a42b352c57e15881fd0c27dac8d844d
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(part, supported):
def effective(p):
if p['type'] == 'multipart/related':
start = p.get('params', {}).get('start')
for c in p['parts']:
if start is not None and c.get('cid') == start.strip('<>'):
return c['type']
return p['parts'][0]['type'] if p['parts'] else None
return p['type']
ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
best = None
for i, p in enumerate(part['parts']):
t = effective(p)
cs = p.get('params', {}).get('charset', 'us-ascii')
if t in supported and (not t.startswith('text/') or cs in ok_charsets):
best = i
if best is None:
return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| richest supported last | {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'} | {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'} | Passed |
| unsupported last part | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| related root via start | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| upper-case charset | {'fallback': True, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Failed |
| missing charset | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| unknown charset html | {'fallback': False, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 0, 'type': 'text/plain'} | Passed |
| nothing supported | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | Passed |
SHA-256 / 6c3f54ad6841e3b279778d2d1c19bd406f3596ec0e1a326d845cd34441a401d4
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
N = 1
observations = []
def solve(part, supported):
def effective(p):
if p['type'] == 'multipart/related':
start = p.get('params', {}).get('start')
for c in p['parts']:
if start is not None and c.get('cid') == start.strip('<>'):
return c['type']
return p['parts'][0]['type'] if p['parts'] else None
return p['type']
ok_charsets = ('us-ascii', 'utf-8', 'iso-8859-1')
best = None
for i, p in enumerate(part['parts']):
t = effective(p)
cs = p.get('params', {}).get('charset', 'us-ascii').lower()
if t in supported and (not t.startswith('text/') or cs in ok_charsets):
best = i
if best is None:
return {'index': 0, 'type': effective(part['parts'][0]), 'fallback': True}
return {'index': best, 'type': effective(part['parts'][best]), 'fallback': False}
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown1'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root1@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root1@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 2: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown2'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root2@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root2@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 3: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown3'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root3@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root3@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 4: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown4'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root4@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root4@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'UTF-8'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})], 5: [('richest supported last', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-amp-html'}]}, ['text/plain', 'text/html', 'text/x-amp-html']), {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'}), ('unsupported last part', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}, {'type': 'text/x-unknown5'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('related root via start', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'multipart/related', 'params': {'start': '<root5@x>'}, 'parts': [{'type': 'image/png', 'cid': 'img@x'}, {'type': 'text/html', 'cid': 'root5@x'}]}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('upper-case charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain', 'params': {'charset': 'UTF-8'}}, {'type': 'text/html', 'params': {'charset': 'ISO-8859-1'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('missing charset', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 1, 'type': 'text/html'}), ('unknown charset html', ({'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html', 'params': {'charset': 'x-klingon'}}]}, ['text/plain', 'text/html']), {'fallback': False, 'index': 0, 'type': 'text/plain'}), ('nothing supported', ({'type': 'multipart/alternative', 'parts': [{'type': 'application/x-foo'}, {'type': 'text/x-bar'}]}, ['text/plain']), {'fallback': True, 'index': 0, 'type': 'application/x-foo'})]}
for _label, _args, _expected in _CASES[N]:
check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| richest supported last | {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'} | {'fallback': False, 'index': 2, 'type': 'text/x-amp-html'} | Passed |
| unsupported last part | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| related root via start | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| upper-case charset | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| missing charset | {'fallback': False, 'index': 1, 'type': 'text/html'} | {'fallback': False, 'index': 1, 'type': 'text/html'} | Passed |
| unknown charset html | {'fallback': False, 'index': 0, 'type': 'text/plain'} | {'fallback': False, 'index': 0, 'type': 'text/plain'} | Passed |
| nothing supported | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | {'fallback': True, 'index': 0, 'type': 'application/x-foo'} | Passed |
SHA-256 / 2aedc2c63759913515e0d457a408af5dfedb8aa56be4c947e76fe1d6860e4b29
Verification & scope
Stipulated offline model over pre-parsed MIME dictionaries; not a conforming MIME parser, generator or mail client. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:49:15.492957+00:00.
Case digest / e8bcafaf34c367a9db1da700b0ef8daa06f2c9e075c8aaaa4426a2be7c622bf7