The vulnerability is a classic path traversal in the tmp npm package, affecting versions prior to 0.2.6. The root cause was the lack of input sanitization for the prefix, postfix, and dir options provided to the main functions of the library: file(), dir(), and tmpName(). An attacker could provide path traversal sequences (e.g., ../) in these options, which were then used to construct a file or directory path. The Node.js path.join() function would normalize these paths, allowing the final location to be outside the intended temporary directory, leading to arbitrary file/directory creation.
The analysis of the patch commit efa4a06f24374797ae32ab2b6ae39b7a611ae429 confirms this. The fix involves two key changes to internal helper functions:
- A new function,
_assertPath(), was added to explicitly block any strings containing .. from being used in prefix and postfix. This validation is enforced in the _assertOptionsBase() function, which prepares options for the public API calls.
- The validation logic within
_getRelativePath() and _getRelativePathSync() was corrected. The previous, insufficient check was replaced with a robust method that verifies that the path specified in the dir option does not resolve to a location outside of the base temporary directory.
During exploitation, a runtime profiler would capture a stack trace originating from a call to one of the public functions (file, dir, or tmpName) and proceeding through the internal helper functions (_assertOptionsBase, _getRelativePath, etc.) where the vulnerable logic resided. Therefore, both the public entry points and the internal functions with flawed logic are identified as key indicators of this vulnerability.