Skip to content

Commit b138043

Browse files
bkonyimattkae
andauthored
[flutter_tools] Extract Windows archives using native tar with PowerShell fallback (#192298)
## Description Today, `flutter_tools` extracts archives on Windows using `package:archive` in memory because historically Windows lacked a built-in unzipping CLI tool. This causes high memory consumption, slow extraction speeds, and out-of-memory crashes on large archives (#133371). Furthermore, `package:archive` 4.x introduced a dependency on `package:posix` which relies on native FFI and cannot execute on Windows (#115660). This PR: 1. Updates `_WindowsUtils.unzip` and `_WindowsUtils.unpack` in `os.dart` to use native `%WINDIR%\System32\tar.exe` (available on Windows 10 build 17063+ / version 1803 and Windows 11). This streams extraction directly from disk to disk, eliminating OOM crashes and providing significantly faster throughput. 2. Adds a backwards-compatible fallback to PowerShell `Expand-Archive` (checking `powershell` then `pwsh`) with verbatim single-quote escaping and `$ErrorActionPreference = "Stop"` for older Windows 10 versions. 3. Automatically creates missing destination directories prior to extraction. 4. Decouples `WindowsApp.fromPrebuiltApp` in `application_package.dart` from `package:archive` by handling general `Exception`. 5. Removes `package:archive` from `os.dart` and `os_test.dart`. 6. Adds unit tests covering Windows `tar` unzipping, target directory creation, PowerShell fallback, PowerShell Core (`pwsh`) fallback, and tool-missing error exits. ## Related Issues - Fixes #133371 - Part of #115660 ## Tests - Added unit tests in `packages/flutter_tools/test/general.shard/base/os_test.dart` - Added unit tests in `packages/flutter_tools/test/general.shard/windows/application_package_test.dart` - Verified with `flutter test` and clean `dart analyze --fatal-infos` --------- Co-authored-by: Matthew Kosarek <[email protected]>
1 parent 9d06bde commit b138043

4 files changed

Lines changed: 245 additions & 109 deletions

File tree

‎packages/flutter_tools/lib/src/base/os.dart‎

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:ffi' show Abi;
66

7-
import 'package:archive/archive.dart';
87
import 'package:file/file.dart';
98
import 'package:meta/meta.dart';
109
import 'package:process/process.dart';
@@ -531,58 +530,63 @@ class _WindowsUtils extends OperatingSystemUtils {
531530
return <File>[_fileSystem.file(lines.first.trim())];
532531
}
533532

534-
@override
535-
void unzip(File file, Directory targetDirectory) {
536-
final Archive archive = ZipDecoder().decodeBytes(file.readAsBytesSync());
537-
_unpackArchive(archive, targetDirectory);
533+
void _unpackWithTar(File file, Directory targetDirectory) {
534+
_processUtils.runSync(
535+
<String>['tar', '-xf', file.path, '-C', targetDirectory.path],
536+
throwOnError: true,
537+
verboseExceptions: true,
538+
);
538539
}
539540

540541
@override
541-
void unpack(File gzippedTarFile, Directory targetDirectory) {
542-
final Archive archive = TarDecoder().decodeBytes(
543-
GZipDecoder().decodeBytes(gzippedTarFile.readAsBytesSync()),
542+
void unzip(File file, Directory targetDirectory) {
543+
if (!targetDirectory.existsSync()) {
544+
targetDirectory.createSync(recursive: true);
545+
}
546+
// Windows 10 build 17063+ includes bsdtar in System32, which can unpack both
547+
// zip and tar archives significantly faster than starting a PowerShell host.
548+
if (_processManager.canRun('tar')) {
549+
_unpackWithTar(file, targetDirectory);
550+
return;
551+
}
552+
// Fall back to PowerShell's Expand-Archive on older Windows versions.
553+
// Check for both Windows PowerShell ('powershell') and PowerShell Core ('pwsh').
554+
final String? powershellExec = switch (true) {
555+
_ when _processManager.canRun('powershell') => 'powershell',
556+
_ when _processManager.canRun('pwsh') => 'pwsh',
557+
_ => null,
558+
};
559+
if (powershellExec != null) {
560+
final String escapedFilePath = file.path.replaceAll("'", "''");
561+
final String escapedTargetPath = targetDirectory.path.replaceAll("'", "''");
562+
final script =
563+
r"$ErrorActionPreference = 'Stop'; "
564+
"Expand-Archive -LiteralPath '$escapedFilePath' -DestinationPath '$escapedTargetPath' -Force";
565+
_processUtils.runSync(
566+
<String>[powershellExec, '-NoProfile', '-NonInteractive', '-Command', script],
567+
throwOnError: true,
568+
verboseExceptions: true,
569+
);
570+
return;
571+
}
572+
throwToolExit(
573+
'Missing "tar" or "powershell" tool. Unable to extract ${file.path}.\n'
574+
'Ensure System32 and PowerShell are on the PATH.',
544575
);
545-
_unpackArchive(archive, targetDirectory);
546576
}
547577

548-
void _unpackArchive(Archive archive, Directory targetDirectory) {
549-
// The target directory does not change across entries, so compute its
550-
// canonical form once instead of per file.
551-
final String targetDirectoryCanonicalPath = _fileSystem.path.canonicalize(targetDirectory.path);
552-
for (final ArchiveFile archiveFile in archive.files) {
553-
// The archive package doesn't correctly set isFile.
554-
if (!archiveFile.isFile || archiveFile.name.endsWith('/')) {
555-
continue;
556-
}
557-
558-
final File destFile = _fileSystem.file(
559-
_fileSystem.path.canonicalize(
560-
_fileSystem.path.join(targetDirectory.path, archiveFile.name),
561-
),
562-
);
563-
564-
// Validate that the destFile is within the targetDirectory we want to
565-
// extract to.
566-
//
567-
// See https://snyk.io/research/zip-slip-vulnerability for more context.
568-
final String destinationFileCanonicalPath = _fileSystem.path.canonicalize(destFile.path);
569-
final bool isAtRoot = _fileSystem.path.equals(
570-
targetDirectoryCanonicalPath,
571-
destinationFileCanonicalPath,
578+
@override
579+
void unpack(File gzippedTarFile, Directory targetDirectory) {
580+
if (!_processManager.canRun('tar')) {
581+
throwToolExit(
582+
'Missing "tar" tool. Unable to extract ${gzippedTarFile.path}.\n'
583+
'Ensure System32 is on the PATH.',
572584
);
573-
if (!isAtRoot &&
574-
!_fileSystem.path.isWithin(targetDirectoryCanonicalPath, destinationFileCanonicalPath)) {
575-
throw StateError(
576-
'Tried to extract the file $destinationFileCanonicalPath outside of the '
577-
'target directory $targetDirectoryCanonicalPath',
578-
);
579-
}
580-
581-
if (!destFile.parent.existsSync()) {
582-
destFile.parent.createSync(recursive: true);
583-
}
584-
destFile.writeAsBytesSync(archiveFile.content as List<int>);
585585
}
586+
if (!targetDirectory.existsSync()) {
587+
targetDirectory.createSync(recursive: true);
588+
}
589+
_unpackWithTar(gzippedTarFile, targetDirectory);
586590
}
587591

588592
@override

‎packages/flutter_tools/lib/src/windows/application_package.dart‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:archive/archive.dart';
6-
75
import '../application_package.dart';
6+
import '../base/common.dart';
87
import '../base/file_system.dart';
98
import '../build_info.dart';
109
import '../cmake.dart';
@@ -45,7 +44,9 @@ abstract class WindowsApp extends ApplicationPackage {
4544
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_app.');
4645
try {
4746
globals.os.unzip(globals.fs.file(applicationBinary), tempDir);
48-
} on ArchiveException {
47+
} on ToolExit {
48+
rethrow;
49+
} on Exception {
4950
globals.printError('Invalid prebuilt Windows app. Unable to extract from archive.');
5051
return null;
5152
}

‎packages/flutter_tools/test/general.shard/base/os_test.dart‎

Lines changed: 165 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:ffi' show Abi;
66

7-
import 'package:archive/archive.dart';
87
import 'package:file/file.dart';
98
import 'package:file/memory.dart';
109
import 'package:file_testing/file_testing.dart';
@@ -38,9 +37,9 @@ void main() {
3837
fakeProcessManager = FakeProcessManager.empty();
3938
});
4039

41-
OperatingSystemUtils createOSUtils(Platform platform, {Abi? currentAbi}) {
40+
OperatingSystemUtils createOSUtils(Platform platform, {Abi? currentAbi, FileSystem? fileSystem}) {
4241
return OperatingSystemUtils(
43-
fileSystem: MemoryFileSystem.test(),
42+
fileSystem: fileSystem ?? MemoryFileSystem.test(),
4443
logger: BufferLogger.test(),
4544
platform: platform,
4645
processManager: fakeProcessManager,
@@ -409,73 +408,178 @@ void main() {
409408
expect(utils.name, 'Pretty Name');
410409
});
411410

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+
),
425421
);
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],
435439
),
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+
},
437519
);
438520
});
439521

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')
457526
..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+
);
465573
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.',
473579
),
474580
);
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+
});
479583
});
480584

481585
group('HostPlatform.fromOsAndArch', () {

0 commit comments

Comments
 (0)