@@ -15,6 +15,7 @@ use deno_path_util::normalize_path;
15
15
use node_resolver:: NodeModuleKind ;
16
16
use node_resolver:: NodeResolutionMode ;
17
17
use node_resolver:: REQUIRE_CONDITIONS ;
18
+ use std:: borrow:: Cow ;
18
19
use std:: cell:: RefCell ;
19
20
use std:: path:: Path ;
20
21
use std:: path:: PathBuf ;
@@ -25,10 +26,11 @@ use crate::NodeRequireResolverRc;
25
26
use crate :: NodeResolverRc ;
26
27
use crate :: NpmResolverRc ;
27
28
28
- fn ensure_read_permission < P > (
29
+ #[ must_use = "the resolved return value to mitigate time-of-check to time-of-use issues" ]
30
+ fn ensure_read_permission < ' a , P > (
29
31
state : & mut OpState ,
30
- file_path : & Path ,
31
- ) -> Result < ( ) , AnyError >
32
+ file_path : & ' a Path ,
33
+ ) -> Result < Cow < ' a , Path > , AnyError >
32
34
where
33
35
P : NodePermissions + ' static ,
34
36
{
@@ -107,7 +109,7 @@ where
107
109
deno_path_util:: normalize_path ( current_dir. join ( from) )
108
110
} ;
109
111
110
- ensure_read_permission :: < P > ( state, & from) ?;
112
+ let from = ensure_read_permission :: < P > ( state, & from) ?;
111
113
112
114
if cfg ! ( windows) {
113
115
// return root node_modules when path is 'D:\\'.
@@ -129,7 +131,7 @@ where
129
131
}
130
132
131
133
let mut paths = Vec :: with_capacity ( from. components ( ) . count ( ) ) ;
132
- let mut current_path = from. as_path ( ) ;
134
+ let mut current_path = from. as_ref ( ) ;
133
135
let mut maybe_parent = Some ( current_path) ;
134
136
while let Some ( parent) = maybe_parent {
135
137
if !parent. ends_with ( "node_modules" ) {
@@ -267,7 +269,7 @@ where
267
269
P : NodePermissions + ' static ,
268
270
{
269
271
let path = PathBuf :: from ( path) ;
270
- ensure_read_permission :: < P > ( state, & path) ?;
272
+ let path = ensure_read_permission :: < P > ( state, & path) ?;
271
273
let fs = state. borrow :: < FileSystemRc > ( ) ;
272
274
if let Ok ( metadata) = fs. stat_sync ( & path) {
273
275
if metadata. is_file {
@@ -290,7 +292,7 @@ where
290
292
P : NodePermissions + ' static ,
291
293
{
292
294
let path = PathBuf :: from ( request) ;
293
- ensure_read_permission :: < P > ( state, & path) ?;
295
+ let path = ensure_read_permission :: < P > ( state, & path) ?;
294
296
let fs = state. borrow :: < FileSystemRc > ( ) ;
295
297
let canonicalized_path =
296
298
deno_core:: strip_unc_prefix ( fs. realpath_sync ( & path) ?) ;
@@ -362,7 +364,7 @@ where
362
364
if parent_id == "<repl>" || parent_id == "internal/preload" {
363
365
let fs = state. borrow :: < FileSystemRc > ( ) ;
364
366
if let Ok ( cwd) = fs. cwd ( ) {
365
- ensure_read_permission :: < P > ( state, & cwd) ?;
367
+ let cwd = ensure_read_permission :: < P > ( state, & cwd) ?;
366
368
return Ok ( Some ( cwd. to_string_lossy ( ) . into_owned ( ) ) ) ;
367
369
}
368
370
}
@@ -443,7 +445,7 @@ where
443
445
P : NodePermissions + ' static ,
444
446
{
445
447
let file_path = PathBuf :: from ( file_path) ;
446
- ensure_read_permission :: < P > ( state, & file_path) ?;
448
+ let file_path = ensure_read_permission :: < P > ( state, & file_path) ?;
447
449
let fs = state. borrow :: < FileSystemRc > ( ) ;
448
450
Ok ( fs. read_text_file_lossy_sync ( & file_path, None ) ?)
449
451
}
@@ -528,7 +530,7 @@ where
528
530
P : NodePermissions + ' static ,
529
531
{
530
532
let filename = PathBuf :: from ( filename) ;
531
- ensure_read_permission :: < P > ( state , filename . parent ( ) . unwrap ( ) ) ? ;
533
+ // permissions: allow reading the closest package.json files
532
534
let node_resolver = state. borrow :: < NodeResolverRc > ( ) . clone ( ) ;
533
535
node_resolver
534
536
. get_closest_package_json_from_path ( & filename)
@@ -567,7 +569,7 @@ where
567
569
P : NodePermissions + ' static ,
568
570
{
569
571
let referrer_path = PathBuf :: from ( & referrer_filename) ;
570
- ensure_read_permission :: < P > ( state, & referrer_path) ?;
572
+ let referrer_path = ensure_read_permission :: < P > ( state, & referrer_path) ?;
571
573
let node_resolver = state. borrow :: < NodeResolverRc > ( ) ;
572
574
let Some ( pkg) =
573
575
node_resolver. get_closest_package_json_from_path ( & referrer_path) ?
0 commit comments