Skip to main content
Menu
Advisories

Memory corruption in native Recipe replaceText()/removeText() on RC4 or AES-128 encrypted PDFs

GHSA-3P2C-CW9W-9746Published Updated GitHub

Description

Native Recipe#replaceText() and Recipe#removeText() corrupt memory and crash the Node.js process (SIGSEGV, or glibc free(): invalid pointer) when the source PDF is encrypted with RC4 or AES-128 and opens without a password, the common "owner password only" restriction. Any service that replaces or removes text in uploaded PDFs can be crashed with such a file.

Root Cause

packages/native-core/lib/recipe/replaceText.js writes each rewritten content stream with objectsContext.endPDFStream(stream).endIndirectObject() (in replaceContentStream() and rewriteStream()). endPDFStream() already ends the indirect object, so the object is ended twice. On an encrypting writer the second end makes the vendored PDF-Writer XCryptor::OnObjectEnd() remove an entry from an empty key list, which is undefined behaviour and corrupts the heap. Unencrypted and AES-256 outputs only get a stray endobj, as AES-256 writers keep no per-object key list.

packages/native-core/lib/recipe/replaceText.js:263                                  replaceContentStream: endPDFStream(replacementStream).endIndirectObject()
packages/native-core/lib/recipe/replaceText.js:301                                  rewriteStream: endPDFStream(stream).endIndirectObject()
packages/native-with-source/src/deps/PDFWriter/ObjectsContext.cpp:544,550           EndPDFStream: already calls EndIndirectObject()
packages/native-with-source/src/deps/PDFWriter/XCryptor.cpp:81                     OnObjectEnd: mEncryptionKeysStack.pop_back() on an empty list

(Line numbers refer to the 7.2.3 source.)

Impact

  • Native (@muhammara/native, @muhammara/native-with-source, @muhammara/native-core): new Recipe(src, out) on an RC4- or AES-128-encrypted PDF that opens without a password, then replaceText() with a match or removeText() on a page that shows text, then endPDF(), crashes the process. Confirmed on 7.2.3 for RC4-128 and AES-128 files written by qpdf.
  • Wasm (@muhammara/wasm): not affected. Its Recipe made the same endIndirectObject() call, but the Wasm objects context documents and implements endIndirectObject() after endPDFStream() as a no-op that only resets its state, so the object was ended once. Confirmed on 1.2.3 with the same files.

Proof of Concept

Encrypt any PDF whose first page shows "Hello" with an owner password and an empty user password using RC4-128 or AES-128 (for example with qpdf), then in native: new Recipe(path, outPath).replaceText("Hello", "Bye", 1).endPDF().

Affected Versions

  • Ecosystem: npm
  • Packages: @muhammara/native-core, @muhammara/native-with-source and @muhammara/native >= 7.0.0-beta.1, <= 7.2.3 (replaceText() was added in 7.0.0-beta.1, removeText() in 7.0.0-rc.1)
  • Patched versions: @muhammara/native-core, @muhammara/native-with-source and @muhammara/native 7.2.4
  • Confirmed on: 7.2.3

@muhammara/wasm is not affected.

Patches

Fixed in @muhammara/native-core, @muhammara/native-with-source and @muhammara/native 7.2.4.

replaceText() and removeText() now end each rewritten stream once, so encrypted outputs are written correctly and unencrypted and AES-256 outputs no longer get a stray endobj. @muhammara/wasm 1.2.4 drops its no-op call too, so both Recipes write these streams the same way; its output does not change. The "Replace an Indirect Object" example of the native low-level modification guide, which ended the object a second time the same way, is corrected too.

Workarounds

Do not run replaceText() or removeText() on encrypted PDFs with affected versions (check createReader(path).isEncrypted() first), or use @muhammara/wasm.

Native low-level code that calls endIndirectObject() right after endPDFStream(), as the "Replace an Indirect Object" example of the 7.2.3 low-level modification guide did, crashes the same way on such PDFs, with 7.2.4 as well: remove that call, since endPDFStream() already ends the object.

References

  • Related: #1030 (ending an indirect object without a matching start on an encrypted writer).

---

Found by the project's scheduled WebAssembly fuzzing and the follow-up review of its results.

Affected versions

References

GitHub · julianhille/MuhammaraJS