@@ -6,6 +6,7 @@ import 'dart:convert';
66
77import 'package:devtools_shared/src/server/file_system.dart' hide fileSystem;
88import 'package:file/memory.dart' ;
9+ import 'package:path/path.dart' as path;
910import 'package:test/test.dart' ;
1011
1112void main () {
@@ -25,31 +26,50 @@ void main() {
2526 // stay confined to the ~/.flutter-devtools/ directory.
2627
2728 test ('rejects absolute paths' , () {
28- // path.join() discards the base directory when its second argument is
29+ final root = path.style == .windows ? 'C:\\ ' : '/' ;
30+ // `path.join()` discards the base directory when its second argument is
2931 // absolute, so an absolute path would otherwise escape the DevTools
3032 // directory and read an arbitrary file on disk.
31- expect (fs.devToolsFileFromPath ('/etc/passwd' ), isNull);
32- expect (fs.devToolsFileFromPath ('/absolute/path/to/file.json' ), isNull);
33+ expect (
34+ fs.devToolsFileFromPath (path.join ('${root }etc' , 'passwd' )),
35+ isNull,
36+ );
37+ expect (
38+ fs.devToolsFileFromPath (
39+ path.join ('${root }absolute' , 'path' , 'to' , 'file.json' ),
40+ ),
41+ isNull,
42+ );
3343 });
3444
3545 test ('rejects paths containing ".."' , () {
3646 expect (fs.devToolsFileFromPath ('..' ), isNull);
37- expect (fs.devToolsFileFromPath ('../../../etc/passwd' ), isNull);
38- expect (fs.devToolsFileFromPath ('subdir/../../escape.json' ), isNull);
47+ expect (
48+ fs.devToolsFileFromPath (path.join ('..' , '..' , '..' , 'etc' , 'passwd' )),
49+ isNull,
50+ );
51+ expect (
52+ fs.devToolsFileFromPath (
53+ path.join ('subdir' , '..' , '..' , 'escape.json' ),
54+ ),
55+ isNull,
56+ );
3957 });
4058 });
4159
4260 test ('returns file when path is valid and file exists' , () {
43- final devToolsDir = FileSystemExtension .devToolsDir;
44- final testFile = fs. file ( '$ devToolsDir / sub/ file.json' )
45- ..createSync (recursive: true );
61+ final testFile = fs. file (
62+ path. join ( FileSystemExtension . devToolsDir, ' sub' , ' file.json' ),
63+ ) ..createSync (recursive: true );
4664
47- final file = fs.devToolsFileFromPath ('sub/ file.json' )! ;
65+ final file = fs.devToolsFileFromPath (path. join ( 'sub' , ' file.json') )! ;
4866 expect (file.path, testFile.path);
4967 });
5068
5169 test ('returns null when path is valid but file does not exist' , () {
52- final file = fs.devToolsFileFromPath ('sub/non_existent.json' );
70+ final file = fs.devToolsFileFromPath (
71+ path.join ('sub' , 'non_existent.json' ),
72+ );
5373 expect (file, isNull);
5474 });
5575 });
@@ -70,17 +90,16 @@ void main() {
7090 });
7191
7292 test ('returns null if file is not json' , () {
73- final devToolsDir = FileSystemExtension .devToolsDir;
74- fs.file ('$devToolsDir /test.txt' )
93+ fs.file (path.join (FileSystemExtension .devToolsDir, 'test.txt' ))
7594 ..createSync (recursive: true )
7695 ..writeAsStringSync ('hello' );
7796 expect (fs.devToolsFileAsJson ('test.txt' ), isNull);
7897 });
7998
8099 test ('returns json content with lastModifiedTime' , () {
81- final devToolsDir = FileSystemExtension .devToolsDir;
82- final file = fs. file ( '$ devToolsDir / test.json' )
83- ..createSync (recursive: true );
100+ final file = fs. file (
101+ path. join ( FileSystemExtension . devToolsDir, ' test.json' ),
102+ ) ..createSync (recursive: true );
84103 file.writeAsStringSync ('{"key": "value"}' );
85104
86105 final jsonStr = fs.devToolsFileAsJson ('test.json' )! ;
0 commit comments