"""FORGED CHECKER for email/mime_message (checker forge, claude-sonnet-5). Gate: references accepted, every spoiler rejected and file-changing, empty file rejected, on 2 given specs plus 6 generated by make_specs.""" # ---- reference builder import email from email.message import EmailMessage from email.utils import formatdate, make_msgid from email.policy import default as default_policy def build_reference(spec: dict, out_path: str) -> None: msg = EmailMessage(policy=default_policy) msg["From"] = spec["from"] msg["To"] = spec["to"] msg["Subject"] = spec["subject"] msg["Date"] = formatdate(localtime=False) msg["Message-ID"] = make_msgid() msg.make_mixed() alt = EmailMessage(policy=default_policy) alt.make_alternative() alt.add_alternative(spec["plain"], subtype="plain", charset="utf-8") alt.add_alternative(spec["html"], subtype="html", charset="utf-8") msg.attach(alt) maintype, subtype = spec["attach_type"].split("/", 1) body_bytes = spec["attach_body"].encode("utf-8") msg.add_attachment( body_bytes, maintype=maintype, subtype=subtype, filename=spec["attach_name"], ) for part in msg.walk(): if part.get_content_maintype() == "text" and part.get_content_disposition() != "attachment": pass with open(out_path, "wb") as f: f.write(bytes(msg)) # ---- checker import email from email import policy as email_policy def _norm(s): if s is None: return None if s.endswith("\n"): return s[:-1] return s def grade(spec: dict, path: str) -> tuple[int, str]: try: with open(path, "rb") as f: raw = f.read() except Exception as e: return 0, f"could not open file: {e}" try: msg = email.message_from_bytes(raw, policy=email_policy.default) except Exception as e: return 0, f"failed to parse with email.parser: {e}" if msg.get("From") != spec["from"]: return 0, f"From mismatch: got {msg.get('From')!r}, expected {spec['from']!r}" if msg.get("To") != spec["to"]: return 0, f"To mismatch: got {msg.get('To')!r}, expected {spec['to']!r}" subj = msg.get("Subject") if subj != spec["subject"]: return 0, f"Subject mismatch: got {subj!r}, expected {spec['subject']!r}" if not msg.get("Date"): return 0, "missing Date header" if not msg.is_multipart(): return 0, "top-level message is not multipart" if msg.get_content_type() != "multipart/mixed": return 0, f"top-level content type is {msg.get_content_type()!r}, expected multipart/mixed" parts = msg.get_payload() if len(parts) != 2: return 0, f"expected 2 top-level parts, got {len(parts)}" alt_part, attach_part = parts[0], parts[1] if not alt_part.is_multipart() or alt_part.get_content_type() != "multipart/alternative": return 0, f"first part is not multipart/alternative (got {alt_part.get_content_type()!r})" alt_subparts = alt_part.get_payload() if len(alt_subparts) != 2: return 0, f"expected 2 alternative subparts, got {len(alt_subparts)}" plain_part, html_part = alt_subparts[0], alt_subparts[1] if plain_part.get_content_type() != "text/plain": return 0, f"first alternative subpart is {plain_part.get_content_type()!r}, expected text/plain" if html_part.get_content_type() != "text/html": return 0, f"second alternative subpart is {html_part.get_content_type()!r}, expected text/html" plain_charset = plain_part.get_content_charset() if plain_charset is None or plain_charset.lower() != "utf-8": return 0, f"plain part charset is {plain_charset!r}, expected utf-8" html_charset = html_part.get_content_charset() if html_charset is None or html_charset.lower() != "utf-8": return 0, f"html part charset is {html_charset!r}, expected utf-8" got_plain = plain_part.get_content() if _norm(got_plain) != _norm(spec["plain"]): return 0, f"plain text mismatch: got {got_plain!r}, expected {spec['plain']!r}" got_html = html_part.get_content() if _norm(got_html) != _norm(spec["html"]): return 0, f"html mismatch: got {got_html!r}, expected {spec['html']!r}" if attach_part.get_content_disposition() != "attachment": return 0, f"second top-level part disposition is {attach_part.get_content_disposition()!r}, expected attachment" fname = attach_part.get_filename() if fname != spec["attach_name"]: return 0, f"attachment filename mismatch: got {fname!r}, expected {spec['attach_name']!r}" if attach_part.get_content_type() != spec["attach_type"]: return 0, f"attachment content type mismatch: got {attach_part.get_content_type()!r}, expected {spec['attach_type']!r}" got_bytes = attach_part.get_content() if isinstance(got_bytes, str): got_bytes = got_bytes.encode("utf-8") expected_bytes = spec["attach_body"].encode("utf-8") if got_bytes != expected_bytes: return 0, f"attachment body mismatch: got {got_bytes!r}, expected {expected_bytes!r}" return 1, "ok" # ---- spoilers (the mutation gate) import email from email import policy as email_policy def _load(path): with open(path, "rb") as f: raw = f.read() return email.message_from_bytes(raw, policy=email_policy.default) def _save(msg, out_path): with open(out_path, "wb") as f: f.write(bytes(msg)) def spoil_subject(path: str) -> str: msg = _load(path) msg.replace_header("Subject", "Boring ASCII subject") out_path = path + ".spoil_subject.eml" _save(msg, out_path) return out_path def spoil_plain_text(path: str) -> str: msg = _load(path) for part in msg.walk(): if part.get_content_type() == "text/plain": part.set_content("This text has been tampered with.", subtype="plain", charset="utf-8") break out_path = path + ".spoil_plain.eml" _save(msg, out_path) return out_path def spoil_attachment_disposition(path: str) -> str: msg = _load(path) for part in msg.walk(): if part.get_content_disposition() == "attachment": del part["Content-Disposition"] part["Content-Disposition"] = "inline" break out_path = path + ".spoil_disposition.eml" _save(msg, out_path) return out_path def spoil_attachment_body(path: str) -> str: msg = _load(path) for part in msg.walk(): if part.get_content_disposition() == "attachment": part.set_payload(b"CORRUPTED BYTES") del part["Content-Transfer-Encoding"] from email import encoders encoders.encode_base64(part) break out_path = path + ".spoil_attach_body.eml" _save(msg, out_path) return out_path def spoil_missing_date(path: str) -> str: msg = _load(path) if "Date" in msg: del msg["Date"] out_path = path + ".spoil_no_date.eml" _save(msg, out_path) return out_path def spoil_html_charset(path: str) -> str: msg = _load(path) for part in msg.walk(): if part.get_content_type() == "text/html": payload = part.get_content() del part["Content-Type"] del part["Content-Transfer-Encoding"] part.set_payload(payload.encode("latin-1")) part["Content-Type"] = "text/html; charset=latin-1" part["Content-Transfer-Encoding"] = "8bit" break out_path = path + ".spoil_html_charset.eml" _save(msg, out_path) return out_path SPOILERS = [ ("spoil_subject", spoil_subject), ("spoil_plain_text", spoil_plain_text), ("spoil_attachment_disposition", spoil_attachment_disposition), ("spoil_attachment_body", spoil_attachment_body), ("spoil_missing_date", spoil_missing_date), ("spoil_html_charset", spoil_html_charset), ] # ---- variants (spec generator, gated: every generated spec builds and passes) import random import copy BASE_SPECS = [ { "from": "billing@neruva.io", "to": "kyle@example.test", "subject": "Facture r\u00e9gl\u00e9e: caf\u00e9 \u2615", "plain": "Your invoice is paid. Thank you.", "html": "

Your invoice is paid.

", "attach_name": "invoice-101.txt", "attach_type": "text/plain", "attach_body": "INV-101 PAID 49.00", }, { "from": "noreply@acme.test", "to": "ops@acme.test", "subject": "R\u00e9sum\u00e9 du d\u00e9ploiement \u2014 \u5efa\u7acb", "plain": "Deployment finished with no errors.", "html": "

Deployed

", "attach_name": "report.csv", "attach_type": "text/csv", "attach_body": "step,status\nbuild,ok\n", }, ] FROM_DOMAINS = ["neruva.io", "acme.test", "example.org", "mailhub.net", "corp.example"] FROM_USERS = ["billing", "noreply", "alerts", "support", "sysadmin"] TO_DOMAINS = ["example.test", "acme.test", "customer.example", "partner.test"] TO_USERS = ["kyle", "ops", "jane", "team", "watch"] SUBJECTS = [ "Facture r\u00e9gl\u00e9e: caf\u00e9 \u2615", "R\u00e9sum\u00e9 du d\u00e9ploiement \u2014 \u5efa\u7acb", "Confirmation de commande \u2013 \u00e9toile", "\u30ec\u30dd\u30fc\u30c8: \u30b7\u30b9\u30c6\u30e0\u72b6\u614b", "\u041e\u0442\u0447\u0435\u0442 \u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u2014 \u0433\u043e\u0442\u043e\u0432\u043e", ] PLAIN_TEXTS = [ "Your invoice is paid. Thank you.", "Deployment finished with no errors.", "All systems operational.", "Order confirmed, please review details.", "The backup completed successfully.", ] HTML_BODIES = [ "

Your invoice is paid.

", "

Deployed

", "

All systems are operational.

", "

Order confirmed.

", "

Backup succeeded.

", ] ATTACH_CHOICES = [ ("invoice-101.txt", "text/plain", "INV-101 PAID 49.00"), ("report.csv", "text/csv", "step,status\nbuild,ok\n"), ("summary.txt", "text/plain", "Summary: all good."), ("data.csv", "text/csv", "id,value\n1,42\n"), ("notes.txt", "text/plain", "Additional notes here."), ] def make_specs(n: int, seed: int) -> list[dict]: rng = random.Random(seed) specs = [] for i in range(n): if i < len(BASE_SPECS) and rng.random() < 0.2: specs.append(copy.deepcopy(BASE_SPECS[i % len(BASE_SPECS)])) continue from_user = rng.choice(FROM_USERS) from_domain = rng.choice(FROM_DOMAINS) to_user = rng.choice(TO_USERS) to_domain = rng.choice(TO_DOMAINS) subject = rng.choice(SUBJECTS) plain = rng.choice(PLAIN_TEXTS) html = rng.choice(HTML_BODIES) attach_name, attach_type, attach_body = rng.choice(ATTACH_CHOICES) spec = { "from": f"{from_user}@{from_domain}", "to": f"{to_user}@{to_domain}", "subject": subject, "plain": plain, "html": html, "attach_name": attach_name, "attach_type": attach_type, "attach_body": attach_body, } specs.append(spec) return specs