.docx file is a ZIP archive containing XML parts. Maliciously crafted archives — sometimes called ZIP bombs — can contain a small number of heavily compressed bytes that expand to gigabytes of data, or thousands of tiny entries that exhaust file descriptors. Safe Docx validates every archive before processing it to prevent resource exhaustion.
How the guards work
Before any.docx content is parsed, Safe Docx inspects the ZIP entries and enforces four independent limits. If any limit is exceeded, the file is rejected immediately with a structured error and no further processing occurs.
The limits are configurable through environment variables so you can tune them for your workload without modifying any code.
Guard limits
Maximum number of files (non-directory entries) allowed inside the
.docx ZIP archive.Standard Word documents contain between a few dozen and a few hundred parts. A legitimate document approaching or exceeding 2,000 entries is unusual. Raise this value only when working with documents that embed a very large number of images or embedded objects.Error code when exceeded: DOCX_ARCHIVE_TOO_MANY_ENTRIESMaximum total uncompressed size across all entries in the archive, in bytes. The default is 200 MB.This limit guards against ZIP bombs that pack enormous content into a small compressed file. Raise this value if you regularly work with legitimate documents that contain large embedded media.Error code when exceeded:
DOCX_ARCHIVE_UNCOMPRESSED_TOO_LARGEMaximum uncompressed size of any single entry inside the archive, in bytes. The default is 50 MB.A single XML part or embedded file exceeding 50 MB uncompressed is extremely rare in practice. Raise this value if your documents contain unusually large embedded objects such as high-resolution images or video attachments.Error code when exceeded:
DOCX_ARCHIVE_ENTRY_TOO_LARGEMaximum allowed compression ratio for any single entry, calculated as
uncompressed_size / compressed_size.A ratio above 200 is a strong indicator of a deliberately hostile archive. Standard DOCX XML compresses to ratios well below this threshold. Raise this value only if you have independently verified that your documents contain legitimate high-ratio entries, and do so with caution.Error code when exceeded: DOCX_ARCHIVE_COMPRESSION_RATIO_TOO_HIGHConfiguring the limits
Set any of the environment variables before starting the MCP server:What happens when a limit is exceeded
The tool that triggered the read returns a structured error response. The error includes:- An error code identifying which limit was exceeded (see the codes listed above)
- A message with the actual value that triggered the limit and the configured maximum
- A hint naming the environment variable to raise if the document is legitimate
When to raise the defaults
The defaults cover the vast majority of real-world Word documents. You may need to increase a limit when:- Your documents embed many high-resolution images and bump into the entry size or total size limits.
- A document management system produces
.docxfiles with a large number of generated parts. - You are processing documents that were converted from other formats and carry unusually verbose XML.
You can verify that the archive guards are working as expected by running the assumption test:
npm run test:run -w @usejunior/safe-docx -- test/assumption_archive_guard_limits.test.ts
