Skip to content

Commit 2110aa6

Browse files
committed
[flutter_tools, devicelab] Address review comments for filesystem guard
- Restrict the dangerous directory length check to Windows to prevent banning short paths (like /x or /fl) on POSIX. - Explicitly wrap the FSGuardIOOverrides test in io_test.dart with IOOverrides.runWithIOOverrides. - Make unsafe test paths in io_test.dart and utils_test.dart platform-agnostic using package:path. Fixes flutter#186419
1 parent bc0a06c commit 2110aa6

4 files changed

Lines changed: 31 additions & 27 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bool _isDangerousDirectory(String dirPath) {
1414
final String canonical = path.canonicalize(dirPath);
1515

1616
// Check if it is root
17-
if (canonical == '/' || canonical == r'C:\' || canonical.length <= 3) {
17+
if (canonical == '/' || canonical == r'C:\' || (io.Platform.isWindows && canonical.length <= 3)) {
1818
return true;
1919
}
2020

‎dev/devicelab/test/utils_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void main() {
5959
expect(tempFile.readAsStringSync(), 'safe-devicelab-content');
6060

6161
// Modifying outside system temp should fail and throw our guarded exception
62-
final unsafeFile = io.File('/tmp_unsafe_devicelab.txt');
62+
final String root = path.rootPrefix(io.Directory.current.absolute.path);
63+
final unsafeFile = io.File(path.join(root, 'tmp_unsafe_devicelab.txt'));
6364
expect(unsafeFile.existsSync(), false);
6465
expect(
6566
() => unsafeFile.writeAsStringSync('unsafe-content'),

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ 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; // flutter_ignore: package_path_import
1213
import 'package:test/fake.dart';
1314

1415
import '../../src/common.dart';
16+
import '../../src/fs_safety.dart';
1517
import '../../src/io.dart';
1618

1719
void main() {
@@ -119,31 +121,32 @@ void main() {
119121
testWithoutContext(
120122
'FSGuardIOOverrides isolates filesystem modifications to system temp directory',
121123
() {
122-
final tempFile = io.File(
123-
'${io.Directory.systemTemp.path}${io.Platform.pathSeparator}fs_guard_test_safe.txt',
124-
);
125-
addTearDown(() {
126-
if (tempFile.existsSync()) {
127-
tempFile.deleteSync();
128-
}
129-
});
130-
// Writing under system temp should succeed
131-
tempFile.writeAsStringSync('safe-content');
132-
expect(tempFile.readAsStringSync(), 'safe-content');
133-
134-
// Modifying outside system temp should fail and throw our guarded exception
135-
final unsafeFile = io.File('/tmp_unsafe_outside_temp.txt');
136-
expect(unsafeFile.existsSync(), false);
137-
expect(
138-
() => unsafeFile.writeAsStringSync('unsafe-content'),
139-
throwsA(
140-
isA<io.FileSystemException>().having(
141-
(e) => e.message,
142-
'message',
143-
contains('Test attempted to modify file outside of temp directory'),
124+
io.IOOverrides.runWithIOOverrides(() {
125+
final tempFile = io.File(path.join(io.Directory.systemTemp.path, 'fs_guard_test_safe.txt'));
126+
addTearDown(() {
127+
if (tempFile.existsSync()) {
128+
tempFile.deleteSync();
129+
}
130+
});
131+
// Writing under system temp should succeed
132+
tempFile.writeAsStringSync('safe-content');
133+
expect(tempFile.readAsStringSync(), 'safe-content');
134+
135+
// Modifying outside system temp should fail and throw our guarded exception
136+
final String root = path.rootPrefix(io.Directory.current.absolute.path);
137+
final unsafeFile = io.File(path.join(root, 'tmp_unsafe_outside_temp.txt'));
138+
expect(unsafeFile.existsSync(), false);
139+
expect(
140+
() => unsafeFile.writeAsStringSync('unsafe-content'),
141+
throwsA(
142+
isA<io.FileSystemException>().having(
143+
(e) => e.message,
144+
'message',
145+
contains('Test attempted to modify file outside of temp directory'),
146+
),
144147
),
145-
),
146-
);
148+
);
149+
}, FSGuardIOOverrides());
147150
},
148151
);
149152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bool _isDangerousDirectory(String dirPath) {
1414
final String canonical = path.canonicalize(dirPath);
1515

1616
// Check if it is root
17-
if (canonical == '/' || canonical == r'C:\' || canonical.length <= 3) {
17+
if (canonical == '/' || canonical == r'C:\' || (io.Platform.isWindows && canonical.length <= 3)) {
1818
return true;
1919
}
2020

0 commit comments

Comments
 (0)