feat: decrypt password-protected OOXML when a password is supplied (#102)

MR
8d ago
+659-49

Fixes #102

Summary

Password-protected .docx/.xlsx/.pptx convert when the caller supplies a password, instead of always ending in Encrypted. Plaintext files are untouched; without a password nothing changes.

What changed

  • to_markdown_with_password(path, password) and to_markdown_bytes_with_password(bytes, format, password): an encrypted OOXML package (OLE container with EncryptionInfo/EncryptedPackage, detected by the same shape probe_ole already classifies) is decrypted via office-crypto 0.3 and converted as its plaintext zip, so all existing resource limits apply unchanged. No password / empty password / wrong password → Encrypted, exactly as today. Legacy RC4 binary formats (.doc/.ppt/.xls) stay out of scope.
  • Wrong-password detection: office-crypto does not check the EncryptionInfo password verifier (a wrong key decrypts to noise with an Ok status), so the decrypted payload's zip signature (PK) gates success. Documented in src/package/crypto.rs.
  • CLI: -p/--password, with ANYDOC_PASSWORD env fallback since argv leaks into shell history and ps.
  • Node, Python, wasm bindings gain an optional trailing password argument (toMarkdownBytes(bytes, format?, password?), to_markdown_bytes(data, format=None, password=None)); existing signatures keep working.

Fixture provenance

tests/fixtures/encrypted/agile-sha512-docx--errors.docx is an agile SHA-512 encrypted .docx taken from office-crypto's MIT-licensed test suite (password testPassword). It carries the corpus sweep's --errors annotation for the no-password path; tests/fixtures/docx/ detection invariants are unaffected.

Verification

  • New integration tests (tests/encrypted.rs, 5 cases): convert-with-password returns the expected body; no/wrong/empty password → Encrypted; plaintext file with a stray password converts identically.
  • Unit tests for the crypto wrapper incl. the non-zip-payload gate.
  • Full suite green (288 lib + snapshot corpus + encrypted), clippy clean, fmt clean.
  • CLI end-to-end: with ANYDOC_PASSWORD=testPassword the fixture converts; without it, error: document is encrypted.

This change was prepared with AI assistance under human direction and review.


Summary by cubic

Decrypts password-protected OOXML when a password is supplied and fixes DOCX list numbering to continue across instances that share an abstract, only restarting overridden levels. Previously encrypted OOXML always returned Encrypted; wrong/empty passwords still do, while plaintext files are unchanged.

  • New APIs to_markdown_with_password(path, password) and to_markdown_bytes_with_password(bytes, format, password) decrypt then convert; exports is_encrypted_ooxml and decrypt_ooxml. Validates the decrypted payload as a zip, rejects wrong passwords before size limits, enforces a 512 MB decrypted-package budget, then applies the usual package limits unchanged.
  • CLI: -p/--password with ANYDOC_PASSWORD fallback; a missing --password value is a usage error. Password-bearing path conversions route through byte-level conversion after decryption, using content detection with the path extension as fallback; assets come from the same decrypted bytes.
  • Bindings: optional trailing password in Node (toMarkdownBytes(bytes, format?, password?)), Python (to_markdown_bytes(data, format=None, password=None)), and wasm; existing call sites continue to work.
  • DOCX numbering: counters key by the resolved abstract so instances sharing one abstract continue a single sequence; only lvlOverride/startOverride levels restart, triggered on first use under the overriding instance.
  • Merged upstream main, which added the PDF Parse mode; the Node wrappers keep password routing and parseMode working together.