Skip to content

Bug: catalog --write truncates an existing output before rejecting invalid Unicode #22

Closed

Description

@drevendev

Reproduced on current master

Review date: 2026-10-19. Baseline c2cbcf07ae67263ec7dbeb7cfb5822ea65b49c38; tools/catalog.py, blob db02dd26a6d0fea6e9d782aead5a8db12f44fdba. Python 3.13.5.

A valid-shaped resource with JSON title "broken\ud800title" passes validate() with no errors. Running catalog.py --data input.json --output existing.md --write then returns exit 1 because the rendered string cannot be encoded as UTF-8. However, Path.write_text() has already opened/truncated the output: an existing nonempty sentinel file becomes zero bytes.

Impact / priority

P1: destructive failure in a documented command. Invalid metadata should not erase a previously generated document. This is an input-validation/output-integrity defect, not a claim of remote exploitation.

Minimal reproduction

  1. Make a temporary copy of catalog/resources.json, changing one title to the literal JSON escape \ud800 within otherwise ordinary text.
  2. Put known nonempty content in a temporary output file.
  3. Run python tools/catalog.py --data <temporary-json> --output <temporary-output> --write.
  4. Observe the Unicode error, exit 1, and an empty output file.

Acceptance

  • Reject strings that cannot be rendered/encoded under the documented UTF-8 contract before touching output.
  • Render and encode successfully before replacement; use a same-directory temporary file and atomic replacement where appropriate so failed writes preserve existing output.
  • Add a regression asserting unchanged existing bytes after invalid Unicode, plus successful valid non-BMP Unicode output.
  • Preserve exit-code behavior and generated-output determinism.
  • Run python tools/catalog.py --write, python tools/catalog.py, the full unittest suite and documentation-link checks on the actual candidate.

Primary background: https://docs.python.org/3/library/json.html (unpaired Unicode surrogates can survive JSON decoding). The destructive behavior above was reproduced locally on the exact source, not inferred only from that documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions