# GHSA-44W8-CF9C-7CWG: @muhammara/wasm maxOutputBytes is enforced only after the full output is built, allowing memory amplification

- Database: GitHub
- Severity: CVSS 5.3 (Medium)
- Published: 2026-11-02
- Modified: 2026-11-02

## Affected versions

- @muhammara/wasm (npm): through 1.2.2

## Description

## Summary

`@muhammara/wasm` documents `limits.maxOutputBytes` as the maximum size of produced PDF output, but enforces it only after the complete output has been built and copied inside the WebAssembly instance. A small, highly compressed PDF passed to `recrypt(..., { compress: false })` makes the instance allocate several times `maxOutputBytes` of linear memory before the limit is checked. With large enough inputs this can drive the instance to the 4 GB wasm32 memory ceiling, where it aborts and becomes unusable.

## Root Cause

`muhammara_wasm_recrypt()` writes the whole recrypted PDF into an `OutputStringBufferStream`, copies it with `ToString()`, copies it again into a `malloc` buffer, and returns it. Only then does `lib/recrypt.js` call `assertOutputSize(length)`:

```text
packages/wasm/src/wasm_api.cpp

    OutputStringBufferStream output;
    ...
    RecryptPDFKeepingInfo(&source, password, &output, ...);
    std::string pdf = output.ToString();               // second full copy
    unsigned char* result = std::malloc(pdf.size());   // third full copy
    std::memcpy(result, pdf.data(), pdf.size());

packages/wasm/lib/recrypt.js

    assertOutputSize(length);                          // limit checked here
```

With `compress: false`, FlateDecode streams are written decoded, so the output size is governed by the deflate expansion ratio (about 1000:1), not the input size. Peak memory is roughly three times the output plus the string buffer's growth headroom. Other output paths that call `assertOutputSize` after building the document (`lib/modifier.js`, `lib/writer-to-modify.js`, `lib/main.js`) follow the same check-after-build pattern.

## Impact

Resource exhaustion (CWE-409, CWE-770) in applications that recrypt untrusted PDFs with `@muhammara/wasm` and rely on `maxOutputBytes` to bound memory. Observed: an 82 KB input grew linear memory by 260 MB before `recrypt()` threw `RangeError: PDF output exceeds maxOutputBytes` with `maxOutputBytes` set to 64 MB. Output is still never returned above the limit; there is no out-of-bounds access. Inputs of a few MB scale the same amplification toward the wasm32 4 GB limit, at which point the instance aborts (`Cannot enlarge memory`).

The native packages have no `maxOutputBytes` option and are not covered by this advisory.

## Proof of Concept

A PDF with a page content stream of `/Filter /FlateDecode` whose compressed data expands to tens of MB (for example, 75 KB of deflate data inflating to 77 MB of zeros), then:

```javascript
import { createMuhammaraWasm } from "@muhammara/wasm";
const muhammara = await createMuhammaraWasm({ limits: { maxOutputBytes: 64 << 20 } });
muhammara.recrypt(bytes, { compress: false });
// throws "PDF output exceeds maxOutputBytes" only after linear memory grew by ~260 MB
```

## Affected Versions

- **Ecosystem:** `npm`
- **Package:** `@muhammara/wasm` `<= 1.2.2`
- **Patched versions:** none yet

## Suggested Fix

Enforce `maxOutputBytes` while writing: have the output stream fail once the written size exceeds the limit, so the writer stops instead of finishing the document. Avoid the extra full copies of the finished PDF (`ToString()` plus `malloc`/`memcpy`) by handing the buffer out once.

---

Found by the project's scheduled WebAssembly fuzzing.

## References

- https://github.com/julianhille/MuhammaraJS/security/advisories/GHSA-44w8-cf9c-7cwg

Page: https://cve.now/GHSA-44W8-CF9C-7CWG

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
