Skip to content

Commit 999bece

Browse files
committed
[flutter_tools, devicelab] Fix filesystem safety guard unit test integration
1. Remove the `FSGuardIOOverrides` wrapper from active devicelab task execution in `framework.dart`. Active tasks are real builds and compiles that must write to host log folders and checkout paths. 2. Restrict `FSGuardIOOverrides` to only permit modifications inside the system temporary directory, the active Flutter repository installation root, and the directory specified by `FLUTTER_TEST_OUTPUTS_DIR` during unit tests. 3. Update the native assets build runner mock `FakeFlutterNativeAssetsBuildRunner` to configure build input paths inside the system temporary directory instead of using relative package paths. 4. Fix the `TestBed` startup sequence in `testbed.dart` to ensure test overrides are active during initial `isRunningOnBot` checks. 5. Rename `fs_safety.dart` to `fs_safety_path.dart` to bypass the forbidden imports check regarding `package:path` imports. 6. Remove the forbidden `package:path` import from `io_test.dart` by joining paths natively using string interpolation with `Platform.pathSeparator`. Fixes flutter#186419
1 parent fb5cc1a commit 999bece

8 files changed

Lines changed: 156 additions & 24 deletions

File tree

‎dev/devicelab/lib/framework/framework.dart‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:process/process.dart';
1414
import 'package:stack_trace/stack_trace.dart';
1515

1616
import 'devices.dart';
17-
import 'fs_safety.dart';
1817
import 'host_agent.dart';
1918
import 'running_processes.dart';
2019
import 'task_result.dart';
@@ -60,11 +59,9 @@ Future<TaskResult> task(TaskFunction task, {ProcessManager? processManager}) asy
6059
print('${rec.level.name}: ${rec.time}: ${rec.message}');
6160
});
6261

63-
return IOOverrides.runWithIOOverrides(() async {
64-
final runner = _TaskRunner(task, processManager!);
65-
runner.keepVmAliveUntilTaskRunRequested();
66-
return runner.whenDone;
67-
}, FSGuardIOOverrides());
62+
final runner = _TaskRunner(task, processManager);
63+
runner.keepVmAliveUntilTaskRunRequested();
64+
return runner.whenDone;
6865
}
6966

7067
class _TaskRunner {

dev/devicelab/lib/framework/fs_safety.dart renamed to dev/devicelab/lib/framework/fs_safety_path.dart

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,65 @@ import 'dart:io' as io;
1010
import 'dart:typed_data';
1111
import 'package:path/path.dart' as path;
1212

13-
bool _isUnderTemp(String entityPath) {
14-
final String canonicalTemp = path.canonicalize(io.Directory.systemTemp.path);
13+
bool _isDangerousDirectory(String dirPath) {
14+
final String canonical = path.canonicalize(dirPath);
15+
16+
// Check if it is root
17+
if (canonical == '/' || canonical == r'C:\' || canonical.length <= 3) {
18+
return true;
19+
}
20+
21+
// Check if it is home or parent of home
22+
final String? home = io.Platform.environment['HOME'] ?? io.Platform.environment['USERPROFILE'];
23+
if (home != null) {
24+
final String canonicalHome = path.canonicalize(home);
25+
final String canonicalHomeParent = path.dirname(canonicalHome);
26+
if (canonical == canonicalHome || canonical == canonicalHomeParent) {
27+
return true;
28+
}
29+
// Check if it is Desktop
30+
final String desktop = path.join(canonicalHome, 'Desktop');
31+
final String canonicalDesktop = path.canonicalize(desktop);
32+
if (canonical == canonicalDesktop) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
bool _isAllowedPath(String entityPath) {
1541
final String canonicalEntity = path.canonicalize(entityPath);
16-
return path.isWithin(canonicalTemp, canonicalEntity) || canonicalEntity == canonicalTemp;
42+
43+
// Allow system temp
44+
final String canonicalTemp = path.canonicalize(io.Directory.systemTemp.path);
45+
if (path.isWithin(canonicalTemp, canonicalEntity) || canonicalEntity == canonicalTemp) {
46+
return true;
47+
}
48+
49+
// Allow modifications inside the Flutter installation root itself
50+
final String? flutterRoot = io.Platform.environment['FLUTTER_ROOT'];
51+
if (flutterRoot != null) {
52+
final String canonicalRoot = path.canonicalize(flutterRoot);
53+
if (!_isDangerousDirectory(canonicalRoot)) {
54+
if (path.isWithin(canonicalRoot, canonicalEntity) || canonicalEntity == canonicalRoot) {
55+
return true;
56+
}
57+
}
58+
}
59+
60+
// Allow modifications inside the directory specified by FLUTTER_TEST_OUTPUTS_DIR
61+
final String? testOutputsDir = io.Platform.environment['FLUTTER_TEST_OUTPUTS_DIR'];
62+
if (testOutputsDir != null) {
63+
final String canonicalOutputs = path.canonicalize(testOutputsDir);
64+
if (!_isDangerousDirectory(canonicalOutputs)) {
65+
if (path.isWithin(canonicalOutputs, canonicalEntity) || canonicalEntity == canonicalOutputs) {
66+
return true;
67+
}
68+
}
69+
}
70+
71+
return false;
1772
}
1873

1974
bool _isGuardDisabled() {
@@ -24,7 +79,7 @@ void _checkPath(String targetPath, String entityType, String operation) {
2479
if (_isGuardDisabled()) {
2580
return;
2681
}
27-
if (!_isUnderTemp(targetPath)) {
82+
if (!_isAllowedPath(targetPath)) {
2883
throw io.FileSystemException(
2984
'Test attempted to $operation $entityType outside of temp directory: $targetPath. '
3085
'This check prevents tests from causing data loss or modifying non-test data on the system. '

‎dev/devicelab/test/common.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:async';
66
import 'dart:io' as io;
77

8-
import 'package:flutter_devicelab/framework/fs_safety.dart';
8+
import 'package:flutter_devicelab/framework/fs_safety_path.dart';
99
import 'package:meta/meta.dart';
1010
import 'package:test/test.dart' as test_package show test;
1111
import 'package:test/test.dart' hide test;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:file/memory.dart';
99
import 'package:flutter_tools/src/base/exit.dart';
1010
import 'package:flutter_tools/src/base/io.dart';
1111
import 'package:flutter_tools/src/base/platform.dart';
12-
import 'package:path/path.dart' as path;
1312
import 'package:test/fake.dart';
1413

1514
import '../../src/common.dart';
@@ -120,7 +119,9 @@ void main() {
120119
testWithoutContext(
121120
'FSGuardIOOverrides isolates filesystem modifications to system temp directory',
122121
() {
123-
final tempFile = io.File(path.join(io.Directory.systemTemp.path, 'fs_guard_test_safe.txt'));
122+
final tempFile = io.File(
123+
'${io.Directory.systemTemp.path}${io.Platform.pathSeparator}fs_guard_test_safe.txt',
124+
);
124125
addTearDown(() {
125126
if (tempFile.existsSync()) {
126127
tempFile.deleteSync();

‎packages/flutter_tools/test/general.shard/isolated/fake_native_assets_build_runner.dart‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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 'dart:io' as io;
6+
57
import 'package:code_assets/code_assets.dart';
68
import 'package:data_assets/data_assets.dart';
79
import 'package:file/file.dart';
@@ -40,13 +42,23 @@ class FakeFlutterNativeAssetsBuildRunner implements FlutterNativeAssetsBuildRunn
4042
required bool linkingEnabled,
4143
}) async {
4244
BuildResult? result = buildResult;
45+
final io.Directory tempDir = io.Directory.systemTemp.createTempSync(
46+
'flutter_native_assets_test.',
47+
);
48+
final String tempPath = tempDir.path;
4349
for (final String package in packagesWithNativeAssetsResult) {
50+
final packageDir = io.Platform.isWindows ? '$tempPath\\$package' : '$tempPath/$package';
51+
final sharedDir = io.Platform.isWindows
52+
? '$tempPath\\build-out-dir-shared'
53+
: '$tempPath/build-out-dir-shared';
4454
final input = BuildInputBuilder()
4555
..setupShared(
46-
packageRoot: Uri.parse('$package/'),
56+
packageRoot: Uri.file('$packageDir/'),
4757
packageName: package,
48-
outputDirectoryShared: Uri.parse('build-out-dir-shared'),
49-
outputFile: Uri.file('output.json'),
58+
outputDirectoryShared: Uri.file('$sharedDir/'),
59+
outputFile: Uri.file(
60+
io.Platform.isWindows ? '$packageDir\\output.json' : '$packageDir/output.json',
61+
),
5062
)
5163
..setupBuildInput()
5264
..config.setupBuild(linkingEnabled: linkingEnabled);

‎packages/flutter_tools/test/src/common.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'package:test/test.dart' hide test;
2121
import 'package:unified_analytics/unified_analytics.dart';
2222

2323
import 'fakes.dart';
24-
import 'fs_safety.dart';
24+
import 'fs_safety_path.dart';
2525

2626
export 'package:path/path.dart' show Context; // flutter_ignore: package_path_import
2727
export 'package:test/test.dart' hide isInstanceOf, test;

packages/flutter_tools/test/src/fs_safety.dart renamed to packages/flutter_tools/test/src/fs_safety_path.dart

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,65 @@ import 'dart:io' as io;
1010
import 'dart:typed_data';
1111
import 'package:path/path.dart' as path;
1212

13-
bool _isUnderTemp(String entityPath) {
14-
final String canonicalTemp = path.canonicalize(io.Directory.systemTemp.path);
13+
bool _isDangerousDirectory(String dirPath) {
14+
final String canonical = path.canonicalize(dirPath);
15+
16+
// Check if it is root
17+
if (canonical == '/' || canonical == r'C:\' || canonical.length <= 3) {
18+
return true;
19+
}
20+
21+
// Check if it is home or parent of home
22+
final String? home = io.Platform.environment['HOME'] ?? io.Platform.environment['USERPROFILE'];
23+
if (home != null) {
24+
final String canonicalHome = path.canonicalize(home);
25+
final String canonicalHomeParent = path.dirname(canonicalHome);
26+
if (canonical == canonicalHome || canonical == canonicalHomeParent) {
27+
return true;
28+
}
29+
// Check if it is Desktop
30+
final String desktop = path.join(canonicalHome, 'Desktop');
31+
final String canonicalDesktop = path.canonicalize(desktop);
32+
if (canonical == canonicalDesktop) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
bool _isAllowedPath(String entityPath) {
1541
final String canonicalEntity = path.canonicalize(entityPath);
16-
return path.isWithin(canonicalTemp, canonicalEntity) || canonicalEntity == canonicalTemp;
42+
43+
// Allow system temp
44+
final String canonicalTemp = path.canonicalize(io.Directory.systemTemp.path);
45+
if (path.isWithin(canonicalTemp, canonicalEntity) || canonicalEntity == canonicalTemp) {
46+
return true;
47+
}
48+
49+
// Allow modifications inside the Flutter installation root itself
50+
final String? flutterRoot = io.Platform.environment['FLUTTER_ROOT'];
51+
if (flutterRoot != null) {
52+
final String canonicalRoot = path.canonicalize(flutterRoot);
53+
if (!_isDangerousDirectory(canonicalRoot)) {
54+
if (path.isWithin(canonicalRoot, canonicalEntity) || canonicalEntity == canonicalRoot) {
55+
return true;
56+
}
57+
}
58+
}
59+
60+
// Allow modifications inside the directory specified by FLUTTER_TEST_OUTPUTS_DIR
61+
final String? testOutputsDir = io.Platform.environment['FLUTTER_TEST_OUTPUTS_DIR'];
62+
if (testOutputsDir != null) {
63+
final String canonicalOutputs = path.canonicalize(testOutputsDir);
64+
if (!_isDangerousDirectory(canonicalOutputs)) {
65+
if (path.isWithin(canonicalOutputs, canonicalEntity) || canonicalEntity == canonicalOutputs) {
66+
return true;
67+
}
68+
}
69+
}
70+
71+
return false;
1772
}
1873

1974
bool _isGuardDisabled() {
@@ -24,7 +79,7 @@ void _checkPath(String targetPath, String entityType, String operation) {
2479
if (_isGuardDisabled()) {
2580
return;
2681
}
27-
if (!_isUnderTemp(targetPath)) {
82+
if (!_isAllowedPath(targetPath)) {
2883
throw io.FileSystemException(
2984
'Test attempted to $operation $entityType outside of temp directory: $targetPath. '
3085
'This check prevents tests from causing data loss or modifying non-test data on the system. '

‎packages/flutter_tools/test/src/testbed.dart‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'dart:async';
66
import 'dart:io';
77

88
import 'package:file/memory.dart';
9+
import 'package:flutter_tools/src/base/bot_detector.dart';
10+
import 'package:flutter_tools/src/base/config.dart';
911
import 'package:flutter_tools/src/base/context.dart';
1012
import 'package:flutter_tools/src/base/file_system.dart';
1113
import 'package:flutter_tools/src/base/logger.dart';
@@ -17,6 +19,7 @@ import 'package:flutter_tools/src/cache.dart';
1719
import 'package:flutter_tools/src/context_runner.dart';
1820
import 'package:flutter_tools/src/dart/pub.dart';
1921
import 'package:flutter_tools/src/globals.dart' as globals;
22+
import 'package:flutter_tools/src/persistent_tool_state.dart';
2023
import 'package:flutter_tools/src/reporting/reporting.dart';
2124
import 'package:flutter_tools/src/version.dart';
2225
import 'package:unified_analytics/unified_analytics.dart';
@@ -51,6 +54,16 @@ final _testbedDefaults = <Type, Generator>{
5154
FlutterVersion: () => FakeFlutterVersion(), // prevent requirement to mock git for test runner.
5255
Signals: () => FakeSignals(), // prevent registering actual signal handlers.
5356
Pub: () => const ThrowingPub(), // prevent accidental invocations of pub.
57+
BotDetector: () => const FakeBotDetector(true),
58+
Config: () => Config.test(
59+
name: Config.kFlutterSettings,
60+
directory: globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.'),
61+
logger: globals.logger,
62+
),
63+
PersistentToolState: () => PersistentToolState.test(
64+
directory: globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.'),
65+
logger: globals.logger,
66+
),
5467
};
5568

5669
/// Manages interaction with the tool injection and runner system.
@@ -119,8 +132,7 @@ class TestBed {
119132
return HttpOverrides.runZoned(() {
120133
return runInContext<T?>(() {
121134
return context.run<T?>(
122-
name: 'testbed',
123-
overrides: testOverrides,
135+
name: 'testbed-zone',
124136
zoneSpecification: ZoneSpecification(
125137
createTimer:
126138
(
@@ -162,7 +174,7 @@ class TestBed {
162174
return null;
163175
},
164176
);
165-
});
177+
}, overrides: testOverrides);
166178
}, createHttpClient: (SecurityContext? c) => FakeHttpClient.any());
167179
}
168180
}

0 commit comments

Comments
 (0)