Path traversal in Tempfile on windows OS due to unsanitized backslashes
The Tempfile library on Windows did not sanitise backslashes in the basename or extension arguments, allowing path traversal. An attacker who controls either argument could create files in arbitrary writable directories, potentially leading to remote code execution in Ruby on Rails applications.
bugdiscloseguys Follow with iamnoooob
Originally disclosed on HackerOne
- Triaged in 31 hours
- Fixed in 18 days
- Paid in 18 days
- Disclosed after 18 days
Summary by the researcher
A short blog on this - https://github.com/httpvoid/writeups/blob/main/Ruby-tempfile-mktmpdir-PT.md
The report
Hi team,
##Summary
We've noticed that both arguments (basename and ext) of Tempfile on Windows are vulnerable to a path traversal which could allow unintentional file creating in arbitrary writable directories.
Tempfile often has a user control either by basename or ext (or both).
PoC
irb(main):029:0> Tempfile.open(["\\..\\..\\..\\..\\..\\Users\\rootx\\malicious",".rb"])
=> #<Tempfile:C:/Users/rootx/AppData/Local/Temp\..\..\..\..\..\Users\rootx\malicious20210321-22472-fvuodx.rb>
irb(main):030:0> puts `dir C:\\Users\\rootx\\`
Volume in drive C has no label.
Volume Serial Number is C0F2-8D87
Directory of C:\Users\rootx
... REDACTED ...
21-03-2021 00:45 0 malicious20210321-22472-fvuodx.rb
... REDACTED ...
The same can be accomplished via ext argument.
Thanks, Harsh and Rahul, HTTPVoid
Impact
Unintentional file creation in an arbitrary directory. Could potentially cause RCE in RoR applications.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the library did not sanitise backslash characters in the basename and extension arguments on Windows, allowing directory traversal.
Impact
Unintentional file creation in an arbitrary writable directory, which could potentially cause remote code execution in Ruby on Rails applications.
What the attacker needed
Windows operating system; Attacker controls Tempfile basename or ext
How it was found
- Call Tempfile.open with a basename containing backslash-based traversal sequences.
- Observe that the resulting temporary file path includes the traversal and points outside the temp directory.
- List the target directory to confirm the file was created there.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Tempfile is a Ruby tool that creates temporary files. On Windows, the backslash character is used to separate folders in a file path. The library failed to clean up backslashes in the name you give it, so a name containing sequences like '..\..' could make the file appear outside the intended temporary folder. This means an attacker who can influence the file name could write files to other locations on the disk.
What is Path traversal?
Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.