docx/malformed-content-types-missing

A docx archive whose [Content_Types].xml part has been removed. Every OPC package MUST carry that part at the archive root; its absence is structurally fatal. A well-behaved library raises a typed opc-level exception (PackageNotFoundError or subclass); a bare KeyError leaking from the zip layer is a library bug.

Library verdicts: python-docx: — docxjs: —

Metadata

Feature iddocx/malformed-content-types-missing
Formatdocx
Categorymalformed-recovery
Spececma-376-5-part-2 § 10.1.2.2 Types

XPath assertions

No XPath assertions declared.

Render assertions

No render assertions declared.

Generator source

scripts/gen_malformed_content_types_missing_docx.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/malformed-content-types-missing.docx``.

Known-good docx with ``[Content_Types].xml`` dropped from the archive.
Every OPC package MUST carry a ``[Content_Types].xml`` at the root —
without it the package is structurally unreadable. A well-behaved
library should raise a *typed* opc-level exception
(``PackageNotFoundError`` or a subclass), NOT let a bare
``KeyError("[Content_Types].xml")`` leak through from the zipfile
layer.
"""

from __future__ import annotations

import sys
import tempfile
from pathlib import Path

from docx import Document

sys.path.insert(0, str(Path(__file__).resolve().parent))
from _malformed_common import rewrite_zip

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "malformed-content-types-missing.docx"


def _drop_content_types(name: str, data: bytes) -> bytes | None:
    if name == "[Content_Types].xml":
        return None
    return data


def main() -> int:
    _OUT.parent.mkdir(parents=True, exist_ok=True)
    with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as tmp:
        good = Path(tmp.name)
    try:
        doc = Document()
        doc.add_paragraph("body text (content types part will be dropped)")
        doc.save(good, reproducible=True)
        rewrite_zip(good, _OUT, edit=_drop_content_types)
    finally:
        good.unlink(missing_ok=True)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "id": "docx/malformed-content-types-missing",
  "title": "Malformed input: [Content_Types].xml missing",
  "format": "docx",
  "category": "malformed-recovery",
  "summary": "A docx archive whose [Content_Types].xml part has been removed. Every OPC package MUST carry that part at the archive root; its absence is structurally fatal. A well-behaved library raises a typed opc-level exception (PackageNotFoundError or subclass); a bare KeyError leaking from the zip layer is a library bug.",
  "roles": [
    "authoring"
  ],
  "spec": {
    "source": "ecma-376-5-part-2",
    "clause": "10.1.2.2",
    "element": "Types",
    "notes": "ECMA-376 part 2 clause 10.1.2.2 mandates exactly one [Content_Types].xml per package."
  },
  "fixtures": {
    "machine": "docx/malformed-content-types-missing"
  },
  "generator": {
    "python": "scripts/gen_malformed_content_types_missing_docx.py"
  },
  "behavior_assertions": [
    {
      "id": "raises-typed-exception",
      "kind": "library_raises",
      "library": "python-docx",
      "method": "docx.Document",
      "args": [
        "{fixture_path}"
      ],
      "expected_exception": [
        "PackageNotFoundError",
        "OpcError",
        "KeyError"
      ],
      "forbidden_exception": "KeyError",
      "description": "The library must raise a typed opc-level exception (PackageNotFoundError, OpcError, or similar). A bare builtin KeyError — which is what python-docx currently lets leak — is explicitly forbidden. Accepting KeyError as one of the expected classes keeps the assertion satisfiable in principle, but the forbidden_exception clause flags it when the raised type is EXACTLY KeyError with no typed wrapper."
    }
  ]
}

Fixture

Download malformed-content-types-missing.docx (18.1 KB)

Reference preview

No rendered reference is available for this case.

Spec notes

ECMA-376 part 2 clause 10.1.2.2 mandates exactly one [Content_Types].xml per package.