|
4 | 4 |
|
5 | 5 | import 'dart:ffi' show Abi; |
6 | 6 |
|
7 | | -import 'package:archive/archive.dart'; |
8 | 7 | import 'package:file/file.dart'; |
9 | 8 | import 'package:file/memory.dart'; |
10 | 9 | import 'package:file_testing/file_testing.dart'; |
@@ -38,9 +37,9 @@ void main() { |
38 | 37 | fakeProcessManager = FakeProcessManager.empty(); |
39 | 38 | }); |
40 | 39 |
|
41 | | - OperatingSystemUtils createOSUtils(Platform platform, {Abi? currentAbi}) { |
| 40 | + OperatingSystemUtils createOSUtils(Platform platform, {Abi? currentAbi, FileSystem? fileSystem}) { |
42 | 41 | return OperatingSystemUtils( |
43 | | - fileSystem: MemoryFileSystem.test(), |
| 42 | + fileSystem: fileSystem ?? MemoryFileSystem.test(), |
44 | 43 | logger: BufferLogger.test(), |
45 | 44 | platform: platform, |
46 | 45 | processManager: fakeProcessManager, |
@@ -409,73 +408,178 @@ void main() { |
409 | 408 | expect(utils.name, 'Pretty Name'); |
410 | 409 | }); |
411 | 410 |
|
412 | | - // See https://snyk.io/research/zip-slip-vulnerability for more context |
413 | | - testWithoutContext('Windows validates paths when unzipping', () { |
414 | | - // on POSIX systems we use the `unzip` binary, which will fail to extract |
415 | | - // files with paths outside the target directory |
416 | | - final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows')); |
417 | | - final fs = MemoryFileSystem.test(); |
418 | | - final File fakeZipFile = fs.file('archive.zip'); |
419 | | - final Directory targetDirectory = fs.directory('output')..createSync(recursive: true); |
420 | | - const content = 'hello, world!'; |
421 | | - final archive = Archive() |
422 | | - ..addFile( |
423 | | - // This file would be extracted outside of the target extraction dir |
424 | | - ArchiveFile(r'..\..\..\Target File.txt', content.length, content.codeUnits), |
| 411 | + group('unzip on Windows', () { |
| 412 | + testWithoutContext('unzips with tar when tar is available', () { |
| 413 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 414 | + final Directory targetDirectory = fileSystem.directory('output') |
| 415 | + ..createSync(recursive: true); |
| 416 | + final File fakeZipFile = fileSystem.file('archive.zip')..createSync(); |
| 417 | + fakeProcessManager.addCommand( |
| 418 | + FakeCommand( |
| 419 | + command: <String>['tar', '-xf', fakeZipFile.path, '-C', targetDirectory.path], |
| 420 | + ), |
425 | 421 | ); |
426 | | - final List<int> zipData = ZipEncoder().encode(archive)!; |
427 | | - fakeZipFile.writeAsBytesSync(zipData); |
428 | | - expect( |
429 | | - () => utils.unzip(fakeZipFile, targetDirectory), |
430 | | - throwsA( |
431 | | - isA<StateError>().having( |
432 | | - (StateError error) => error.message, |
433 | | - 'correct error message', |
434 | | - contains('Tried to extract the file '), |
| 422 | + |
| 423 | + final OperatingSystemUtils utils = createOSUtils( |
| 424 | + FakePlatform(operatingSystem: 'windows'), |
| 425 | + fileSystem: fileSystem, |
| 426 | + ); |
| 427 | + utils.unzip(fakeZipFile, targetDirectory); |
| 428 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 429 | + }); |
| 430 | + |
| 431 | + testWithoutContext('creates target directory if it does not exist', () { |
| 432 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 433 | + final Directory targetDirectory = fileSystem.directory('nonexistent_output'); |
| 434 | + expect(targetDirectory.existsSync(), isFalse); |
| 435 | + final File fakeZipFile = fileSystem.file('archive.zip')..createSync(); |
| 436 | + fakeProcessManager.addCommand( |
| 437 | + FakeCommand( |
| 438 | + command: <String>['tar', '-xf', fakeZipFile.path, '-C', targetDirectory.path], |
435 | 439 | ), |
436 | | - ), |
| 440 | + ); |
| 441 | + |
| 442 | + final OperatingSystemUtils utils = createOSUtils( |
| 443 | + FakePlatform(operatingSystem: 'windows'), |
| 444 | + fileSystem: fileSystem, |
| 445 | + ); |
| 446 | + utils.unzip(fakeZipFile, targetDirectory); |
| 447 | + expect(targetDirectory.existsSync(), isTrue); |
| 448 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 449 | + }); |
| 450 | + |
| 451 | + testWithoutContext('falls back to powershell when tar is not available', () { |
| 452 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 453 | + final Directory targetDirectory = fileSystem.directory('output') |
| 454 | + ..createSync(recursive: true); |
| 455 | + final File fakeZipFile = fileSystem.file("archive's.zip")..createSync(); |
| 456 | + fakeProcessManager.excludedExecutables.add('tar'); |
| 457 | + const command = |
| 458 | + r"$ErrorActionPreference = 'Stop'; " |
| 459 | + "Expand-Archive -LiteralPath 'archive''s.zip' -DestinationPath 'output' -Force"; |
| 460 | + fakeProcessManager.addCommand( |
| 461 | + const FakeCommand( |
| 462 | + command: <String>['powershell', '-NoProfile', '-NonInteractive', '-Command', command], |
| 463 | + ), |
| 464 | + ); |
| 465 | + |
| 466 | + final OperatingSystemUtils utils = createOSUtils( |
| 467 | + FakePlatform(operatingSystem: 'windows'), |
| 468 | + fileSystem: fileSystem, |
| 469 | + ); |
| 470 | + utils.unzip(fakeZipFile, targetDirectory); |
| 471 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 472 | + }); |
| 473 | + |
| 474 | + testWithoutContext('falls back to pwsh when tar and powershell are not available', () { |
| 475 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 476 | + final Directory targetDirectory = fileSystem.directory('output') |
| 477 | + ..createSync(recursive: true); |
| 478 | + final File fakeZipFile = fileSystem.file('archive.zip')..createSync(); |
| 479 | + fakeProcessManager.excludedExecutables.addAll(<String>['tar', 'powershell']); |
| 480 | + const command = |
| 481 | + r"$ErrorActionPreference = 'Stop'; " |
| 482 | + "Expand-Archive -LiteralPath 'archive.zip' -DestinationPath 'output' -Force"; |
| 483 | + fakeProcessManager.addCommand( |
| 484 | + const FakeCommand( |
| 485 | + command: <String>['pwsh', '-NoProfile', '-NonInteractive', '-Command', command], |
| 486 | + ), |
| 487 | + ); |
| 488 | + |
| 489 | + final OperatingSystemUtils utils = createOSUtils( |
| 490 | + FakePlatform(operatingSystem: 'windows'), |
| 491 | + fileSystem: fileSystem, |
| 492 | + ); |
| 493 | + utils.unzip(fakeZipFile, targetDirectory); |
| 494 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 495 | + }); |
| 496 | + |
| 497 | + testWithoutContext( |
| 498 | + 'throws ToolExit when neither tar nor powershell nor pwsh is available', |
| 499 | + () { |
| 500 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 501 | + final Directory targetDirectory = fileSystem.directory('output') |
| 502 | + ..createSync(recursive: true); |
| 503 | + final File fakeZipFile = fileSystem.file('archive.zip')..createSync(); |
| 504 | + fakeProcessManager.excludedExecutables.addAll(<String>['tar', 'powershell', 'pwsh']); |
| 505 | + |
| 506 | + final OperatingSystemUtils utils = createOSUtils( |
| 507 | + FakePlatform(operatingSystem: 'windows'), |
| 508 | + fileSystem: fileSystem, |
| 509 | + ); |
| 510 | + expect( |
| 511 | + () => utils.unzip(fakeZipFile, targetDirectory), |
| 512 | + throwsToolExit( |
| 513 | + message: |
| 514 | + 'Missing "tar" or "powershell" tool. Unable to extract ${fakeZipFile.path}.\n' |
| 515 | + 'Ensure System32 and PowerShell are on the PATH.', |
| 516 | + ), |
| 517 | + ); |
| 518 | + }, |
437 | 519 | ); |
438 | 520 | }); |
439 | 521 |
|
440 | | - // Regression test for https://github.com/flutter/flutter/issues/185794. |
441 | | - // A canonical archive entry that resolves to a sibling directory sharing |
442 | | - // a name prefix with the extraction root (e.g. `<target>-sibling/x.txt`) |
443 | | - // must NOT be accepted: the previous `startsWith` check on canonical |
444 | | - // paths returned true for sibling directories with a name that started |
445 | | - // with the target's name. |
446 | | - testWithoutContext( |
447 | | - 'Windows rejects archive entries that escape into a sibling directory with a name prefix', |
448 | | - () { |
449 | | - final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows')); |
450 | | - final fs = MemoryFileSystem.test(); |
451 | | - final File fakeZipFile = fs.file('archive.zip'); |
452 | | - // Extract into `<cwd>/cache/windows-x64`. A crafted archive entry of |
453 | | - // `../windows-x64-profile/poc_marker.txt` resolves to |
454 | | - // `<cwd>/cache/windows-x64-profile/poc_marker.txt`, a sibling of the |
455 | | - // target whose canonical path shares the `windows-x64` prefix. |
456 | | - final Directory targetDirectory = fs.directory('cache/windows-x64') |
| 522 | + group('unpack on Windows', () { |
| 523 | + testWithoutContext('unpacks with tar when tar is available', () { |
| 524 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 525 | + final Directory targetDirectory = fileSystem.directory('output') |
457 | 526 | ..createSync(recursive: true); |
458 | | - const content = 'malicious'; |
459 | | - final archive = Archive() |
460 | | - ..addFile( |
461 | | - ArchiveFile('../windows-x64-profile/poc_marker.txt', content.length, content.codeUnits), |
462 | | - ); |
463 | | - final List<int> zipData = ZipEncoder().encode(archive)!; |
464 | | - fakeZipFile.writeAsBytesSync(zipData); |
| 527 | + final File fakeTarFile = fileSystem.file('archive.tar.gz')..createSync(); |
| 528 | + fakeProcessManager.addCommand( |
| 529 | + FakeCommand( |
| 530 | + command: <String>['tar', '-xf', fakeTarFile.path, '-C', targetDirectory.path], |
| 531 | + ), |
| 532 | + ); |
| 533 | + |
| 534 | + final OperatingSystemUtils utils = createOSUtils( |
| 535 | + FakePlatform(operatingSystem: 'windows'), |
| 536 | + fileSystem: fileSystem, |
| 537 | + ); |
| 538 | + utils.unpack(fakeTarFile, targetDirectory); |
| 539 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 540 | + }); |
| 541 | + |
| 542 | + testWithoutContext('creates target directory if it does not exist', () { |
| 543 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 544 | + final Directory targetDirectory = fileSystem.directory('nonexistent_output'); |
| 545 | + expect(targetDirectory.existsSync(), isFalse); |
| 546 | + final File fakeTarFile = fileSystem.file('archive.tar.gz')..createSync(); |
| 547 | + fakeProcessManager.addCommand( |
| 548 | + FakeCommand( |
| 549 | + command: <String>['tar', '-xf', fakeTarFile.path, '-C', targetDirectory.path], |
| 550 | + ), |
| 551 | + ); |
| 552 | + |
| 553 | + final OperatingSystemUtils utils = createOSUtils( |
| 554 | + FakePlatform(operatingSystem: 'windows'), |
| 555 | + fileSystem: fileSystem, |
| 556 | + ); |
| 557 | + utils.unpack(fakeTarFile, targetDirectory); |
| 558 | + expect(targetDirectory.existsSync(), isTrue); |
| 559 | + expect(fakeProcessManager, hasNoRemainingExpectations); |
| 560 | + }); |
| 561 | + |
| 562 | + testWithoutContext('throws ToolExit when tar is not available', () { |
| 563 | + final FileSystem fileSystem = MemoryFileSystem.test(); |
| 564 | + final Directory targetDirectory = fileSystem.directory('output') |
| 565 | + ..createSync(recursive: true); |
| 566 | + final File fakeTarFile = fileSystem.file('archive.tar.gz')..createSync(); |
| 567 | + fakeProcessManager.excludedExecutables.add('tar'); |
| 568 | + |
| 569 | + final OperatingSystemUtils utils = createOSUtils( |
| 570 | + FakePlatform(operatingSystem: 'windows'), |
| 571 | + fileSystem: fileSystem, |
| 572 | + ); |
465 | 573 | expect( |
466 | | - () => utils.unzip(fakeZipFile, targetDirectory), |
467 | | - throwsA( |
468 | | - isA<StateError>().having( |
469 | | - (StateError error) => error.message, |
470 | | - 'correct error message', |
471 | | - contains('Tried to extract the file '), |
472 | | - ), |
| 574 | + () => utils.unpack(fakeTarFile, targetDirectory), |
| 575 | + throwsToolExit( |
| 576 | + message: |
| 577 | + 'Missing "tar" tool. Unable to extract ${fakeTarFile.path}.\n' |
| 578 | + 'Ensure System32 is on the PATH.', |
473 | 579 | ), |
474 | 580 | ); |
475 | | - // The sibling file must not have been written. |
476 | | - expect(fs.file('cache/windows-x64-profile/poc_marker.txt').existsSync(), isFalse); |
477 | | - }, |
478 | | - ); |
| 581 | + }); |
| 582 | + }); |
479 | 583 | }); |
480 | 584 |
|
481 | 585 | group('HostPlatform.fromOsAndArch', () { |
|
0 commit comments