|
11 | 11 | from typing import List, Tuple |
12 | 12 |
|
13 | 13 | import pytest |
| 14 | +from _pytest.monkeypatch import MonkeyPatch |
14 | 15 |
|
15 | 16 | from pip._internal.exceptions import InstallationError |
16 | 17 | from pip._internal.utils.unpacking import is_within_directory, untar_file, unzip_file |
@@ -171,6 +172,189 @@ def test_unpack_tar_success(self) -> None: |
171 | 172 | test_tar = self.make_tar_file("test_tar.tar", files) |
172 | 173 | untar_file(test_tar, self.tempdir) |
173 | 174 |
|
| 175 | + @pytest.mark.parametrize( |
| 176 | + "input_prefix, unpack_prefix", |
| 177 | + [ |
| 178 | + ("", ""), |
| 179 | + ("dir/", ""), # pip ignores a common leading directory |
| 180 | + ("dir/sub/", "sub/"), # pip ignores *one* common leading directory |
| 181 | + ], |
| 182 | + ) |
| 183 | + def test_unpack_tar_links(self, input_prefix: str, unpack_prefix: str) -> None: |
| 184 | + """ |
| 185 | + Test unpacking a *.tar with file containing hard & soft links |
| 186 | + """ |
| 187 | + test_tar = os.path.join(self.tempdir, "test_tar_links.tar") |
| 188 | + content = b"file content" |
| 189 | + with tarfile.open(test_tar, "w") as mytar: |
| 190 | + file_tarinfo = tarfile.TarInfo(input_prefix + "regular_file.txt") |
| 191 | + file_tarinfo.size = len(content) |
| 192 | + mytar.addfile(file_tarinfo, io.BytesIO(content)) |
| 193 | + |
| 194 | + hardlink_tarinfo = tarfile.TarInfo(input_prefix + "hardlink.txt") |
| 195 | + hardlink_tarinfo.type = tarfile.LNKTYPE |
| 196 | + hardlink_tarinfo.linkname = input_prefix + "regular_file.txt" |
| 197 | + mytar.addfile(hardlink_tarinfo) |
| 198 | + |
| 199 | + symlink_tarinfo = tarfile.TarInfo(input_prefix + "symlink.txt") |
| 200 | + symlink_tarinfo.type = tarfile.SYMTYPE |
| 201 | + symlink_tarinfo.linkname = "regular_file.txt" |
| 202 | + mytar.addfile(symlink_tarinfo) |
| 203 | + |
| 204 | + untar_file(test_tar, self.tempdir) |
| 205 | + |
| 206 | + unpack_dir = os.path.join(self.tempdir, unpack_prefix) |
| 207 | + with open(os.path.join(unpack_dir, "regular_file.txt"), "rb") as f: |
| 208 | + assert f.read() == content |
| 209 | + |
| 210 | + with open(os.path.join(unpack_dir, "hardlink.txt"), "rb") as f: |
| 211 | + assert f.read() == content |
| 212 | + |
| 213 | + with open(os.path.join(unpack_dir, "symlink.txt"), "rb") as f: |
| 214 | + assert f.read() == content |
| 215 | + |
| 216 | + def test_unpack_normal_tar_link1_no_data_filter( |
| 217 | + self, monkeypatch: MonkeyPatch |
| 218 | + ) -> None: |
| 219 | + """ |
| 220 | + Test unpacking a normal tar with file containing soft links, but no data_filter |
| 221 | + """ |
| 222 | + if hasattr(tarfile, "data_filter"): |
| 223 | + monkeypatch.delattr("tarfile.data_filter") |
| 224 | + |
| 225 | + tar_filename = "test_tar_links_no_data_filter.tar" |
| 226 | + tar_filepath = os.path.join(self.tempdir, tar_filename) |
| 227 | + |
| 228 | + extract_path = os.path.join(self.tempdir, "extract_path") |
| 229 | + |
| 230 | + with tarfile.open(tar_filepath, "w") as tar: |
| 231 | + file_data = io.BytesIO(b"normal\n") |
| 232 | + normal_file_tarinfo = tarfile.TarInfo(name="normal_file") |
| 233 | + normal_file_tarinfo.size = len(file_data.getbuffer()) |
| 234 | + tar.addfile(normal_file_tarinfo, fileobj=file_data) |
| 235 | + |
| 236 | + info = tarfile.TarInfo("normal_symlink") |
| 237 | + info.type = tarfile.SYMTYPE |
| 238 | + info.linkpath = "normal_file" |
| 239 | + tar.addfile(info) |
| 240 | + |
| 241 | + untar_file(tar_filepath, extract_path) |
| 242 | + |
| 243 | + assert os.path.islink(os.path.join(extract_path, "normal_symlink")) |
| 244 | + |
| 245 | + link_path = os.readlink(os.path.join(extract_path, "normal_symlink")) |
| 246 | + assert link_path == "normal_file" |
| 247 | + |
| 248 | + with open(os.path.join(extract_path, "normal_symlink"), "rb") as f: |
| 249 | + assert f.read() == b"normal\n" |
| 250 | + |
| 251 | + def test_unpack_normal_tar_link2_no_data_filter( |
| 252 | + self, monkeypatch: MonkeyPatch |
| 253 | + ) -> None: |
| 254 | + """ |
| 255 | + Test unpacking a normal tar with file containing soft links, but no data_filter |
| 256 | + """ |
| 257 | + if hasattr(tarfile, "data_filter"): |
| 258 | + monkeypatch.delattr("tarfile.data_filter") |
| 259 | + |
| 260 | + tar_filename = "test_tar_links_no_data_filter.tar" |
| 261 | + tar_filepath = os.path.join(self.tempdir, tar_filename) |
| 262 | + |
| 263 | + extract_path = os.path.join(self.tempdir, "extract_path") |
| 264 | + |
| 265 | + with tarfile.open(tar_filepath, "w") as tar: |
| 266 | + file_data = io.BytesIO(b"normal\n") |
| 267 | + normal_file_tarinfo = tarfile.TarInfo(name="normal_file") |
| 268 | + normal_file_tarinfo.size = len(file_data.getbuffer()) |
| 269 | + tar.addfile(normal_file_tarinfo, fileobj=file_data) |
| 270 | + |
| 271 | + info = tarfile.TarInfo("sub/normal_symlink") |
| 272 | + info.type = tarfile.SYMTYPE |
| 273 | + info.linkpath = ".." + os.path.sep + "normal_file" |
| 274 | + tar.addfile(info) |
| 275 | + |
| 276 | + untar_file(tar_filepath, extract_path) |
| 277 | + |
| 278 | + assert os.path.islink(os.path.join(extract_path, "sub", "normal_symlink")) |
| 279 | + |
| 280 | + link_path = os.readlink(os.path.join(extract_path, "sub", "normal_symlink")) |
| 281 | + assert link_path == ".." + os.path.sep + "normal_file" |
| 282 | + |
| 283 | + with open(os.path.join(extract_path, "sub", "normal_symlink"), "rb") as f: |
| 284 | + assert f.read() == b"normal\n" |
| 285 | + |
| 286 | + def test_unpack_evil_tar_link1_no_data_filter( |
| 287 | + self, monkeypatch: MonkeyPatch |
| 288 | + ) -> None: |
| 289 | + """ |
| 290 | + Test unpacking an evil tar with file containing soft links, but no data_filter |
| 291 | + """ |
| 292 | + if hasattr(tarfile, "data_filter"): |
| 293 | + monkeypatch.delattr("tarfile.data_filter") |
| 294 | + |
| 295 | + tar_filename = "test_tar_links_no_data_filter.tar" |
| 296 | + tar_filepath = os.path.join(self.tempdir, tar_filename) |
| 297 | + |
| 298 | + import_filename = "import_file" |
| 299 | + import_filepath = os.path.join(self.tempdir, import_filename) |
| 300 | + open(import_filepath, "w").close() |
| 301 | + |
| 302 | + extract_path = os.path.join(self.tempdir, "extract_path") |
| 303 | + |
| 304 | + with tarfile.open(tar_filepath, "w") as tar: |
| 305 | + info = tarfile.TarInfo("evil_symlink") |
| 306 | + info.type = tarfile.SYMTYPE |
| 307 | + info.linkpath = import_filepath |
| 308 | + tar.addfile(info) |
| 309 | + |
| 310 | + with pytest.raises(InstallationError) as e: |
| 311 | + untar_file(tar_filepath, extract_path) |
| 312 | + |
| 313 | + msg = ( |
| 314 | + "The tar file ({}) has a file ({}) trying to install outside " |
| 315 | + "target directory ({})" |
| 316 | + ) |
| 317 | + assert msg.format(tar_filepath, "evil_symlink", import_filepath) in str(e.value) |
| 318 | + |
| 319 | + assert not os.path.exists(os.path.join(extract_path, "evil_symlink")) |
| 320 | + |
| 321 | + def test_unpack_evil_tar_link2_no_data_filter( |
| 322 | + self, monkeypatch: MonkeyPatch |
| 323 | + ) -> None: |
| 324 | + """ |
| 325 | + Test unpacking an evil tar with file containing soft links, but no data_filter |
| 326 | + """ |
| 327 | + if hasattr(tarfile, "data_filter"): |
| 328 | + monkeypatch.delattr("tarfile.data_filter") |
| 329 | + |
| 330 | + tar_filename = "test_tar_links_no_data_filter.tar" |
| 331 | + tar_filepath = os.path.join(self.tempdir, tar_filename) |
| 332 | + |
| 333 | + import_filename = "import_file" |
| 334 | + import_filepath = os.path.join(self.tempdir, import_filename) |
| 335 | + open(import_filepath, "w").close() |
| 336 | + |
| 337 | + extract_path = os.path.join(self.tempdir, "extract_path") |
| 338 | + |
| 339 | + link_path = ".." + os.sep + import_filename |
| 340 | + |
| 341 | + with tarfile.open(tar_filepath, "w") as tar: |
| 342 | + info = tarfile.TarInfo("evil_symlink") |
| 343 | + info.type = tarfile.SYMTYPE |
| 344 | + info.linkpath = link_path |
| 345 | + tar.addfile(info) |
| 346 | + |
| 347 | + with pytest.raises(InstallationError) as e: |
| 348 | + untar_file(tar_filepath, extract_path) |
| 349 | + |
| 350 | + msg = ( |
| 351 | + "The tar file ({}) has a file ({}) trying to install outside " |
| 352 | + "target directory ({})" |
| 353 | + ) |
| 354 | + assert msg.format(tar_filepath, "evil_symlink", link_path) in str(e.value) |
| 355 | + |
| 356 | + assert not os.path.exists(os.path.join(extract_path, "evil_symlink")) |
| 357 | + |
174 | 358 |
|
175 | 359 | def test_unpack_tar_unicode(tmpdir: Path) -> None: |
176 | 360 | test_tar = tmpdir / "test.tar" |
|
0 commit comments