package_control icon indicating copy to clipboard operation
package_control copied to clipboard

Zip slip: _extract_zip writes archive members with '..' outside the destination directory

Open dpc00 opened this issue 2 months ago • 1 comments

Path traversal ("zip slip") in PackageManager._extract_zip: archive members containing .. are written outside the destination directory.

_extract_zip computes dest = sys_path.longpath(os.path.join(dest_dir, source[len(src_dir):])) and writes to it without checking that dest is inside dest_dir. On Windows only members containing :*?"<>| are skipped; .. segments and backslashes are not. Used for both packages (install_package) and libraries (PyPI wheels).

Reproduced in Sublime Text 4 (Package Control 4.2.8; master 1c52ef1 has the identical line) by calling the method directly on a sandbox directory (<sandbox>\Packages\Evil) with an in-memory zip:

z.writestr('plugin.py', '# ok')
z.writestr('../escaped_one_up.txt', ...)
z.writestr('sub/../../escaped_via_sub.txt', ...)
z.writestr('..\escaped_backslash.txt', ...)
z.writestr('../../escaped_two_up.txt', ...)

Result (os.walk of the sandbox):

escaped_two_up.txt              <- two levels above the package dir
Packages\escaped_backslash.txt  <- one level above (in Packages)
Packages\escaped_one_up.txt
Packages\escaped_via_sub.txt
Packages\Evil\plugin.py         <- only this one is where it belongs

Impact and honest scope: the archive comes from a repository/channel release URL (or a PyPI wheel), and an installed package already executes code, so this is defence in depth, not a new privilege. But it lets a package overwrite files it doesn't own (other packages' files, Packages/User/*, anything under the ST data directory) at install time, before any user review, and libraries are installed automatically as dependencies. It also combines with the unvalidated package names in #1768.

Fix: after computing dest, resolve both paths (os.path.realpath/os.path.abspath + normcase) and skip (log, like the existing invalid-filename case) any member whose destination isn't inside dest_dir (e.g. os.path.commonpath([dest_dir, dest]) == dest_dir); also reject absolute member names and normalise \ to / first.

dpc00 avatar Sep 24 '26 17:09 dpc00

Withdrawing this, it was wrong for the real install path.

My reproduction called PackageManager._extract_zip() directly, which bypasses PackageManager._common_folder(). Both real call sites (install_package ~line 1141 and the library install ~line 877) call _common_folder() first and abort when it returns False, and _common_folder() rejects names that start with / or contain ../, ..\, :/ or :\ ("The archive for ... contains files that may traverse outside of package root and cannot be safely installed, aborting"). I checked afterwards: for entries ../x, sub/../../x, ..\x, ../../x, /abs/x, \abs\x and \srv\sh\x it returns False, so none of them get as far as extraction. _extract_zip itself has no containment check, but nothing reaches it unvalidated, so there's no exploitable path here and this issue's title and impact were incorrect. Sorry for the noise; closing.

dpc00 avatar Sep 24 '26 17:09 dpc00