xlsx/malformed-content-types-missing

An xlsx archive whose [Content_Types].xml has been removed. The library must raise a typed exception (InvalidFileException); a bare KeyError leaking from the zip layer is a library bug.

Library verdicts: python-xlsx: — xlsxjs: —

Metadata

Feature idxlsx/malformed-content-types-missing
Formatxlsx
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_xlsx.py

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

Known-good one-sheet workbook with ``[Content_Types].xml`` dropped
from the archive. python-xlsx must raise a typed
``InvalidFileException`` — not a bare ``KeyError`` from the underlying
zipfile reader.
"""

from __future__ import annotations

import datetime as _dt
import sys
import tempfile
from pathlib import Path

from xlsx import Workbook

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" / "xlsx" / "malformed-content-types-missing.xlsx"


def _drop(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=".xlsx", delete=False) as tmp:
        good = Path(tmp.name)
    try:
        wb = Workbook()
        wb.properties.created = _dt.datetime(1980, 1, 1, 0, 0, 0)
        wb.properties.modified = _dt.datetime(1980, 1, 1, 0, 0, 0)
        wb.active["A1"] = "content-types-missing workbook"
        wb.save(good, zip_date_time=_dt.datetime(1980, 1, 1, 0, 0, 0))
        rewrite_zip(good, _OUT, edit=_drop)
    finally:
        good.unlink(missing_ok=True)
    print(_OUT)
    return 0


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

Manifest (expanded)

{
  "id": "xlsx/malformed-content-types-missing",
  "title": "Malformed input: [Content_Types].xml missing",
  "format": "xlsx",
  "category": "malformed-recovery",
  "summary": "An xlsx archive whose [Content_Types].xml has been removed. The library must raise a typed exception (InvalidFileException); 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"
  },
  "fixtures": {
    "machine": "xlsx/malformed-content-types-missing"
  },
  "generator": {
    "python": "scripts/gen_malformed_content_types_missing_xlsx.py"
  },
  "behavior_assertions": [
    {
      "id": "raises-typed-exception",
      "kind": "library_raises",
      "library": "python-xlsx",
      "method": "xlsx.load_workbook",
      "args": [
        "{fixture_path}"
      ],
      "expected_exception": [
        "InvalidFileException",
        "OpcError",
        "KeyError"
      ],
      "forbidden_exception": "KeyError",
      "description": "Must raise a typed exception (InvalidFileException). A bare KeyError is explicitly forbidden (that's what python-xlsx leaks today)."
    }
  ]
}

Fixture

Download malformed-content-types-missing.xlsx (4.3 KB)

Reference preview

No rendered reference is available for this case.