@@ -226,6 +226,38 @@ pub struct ReadDenyMatcher {
226226}
227227
228228impl ReadDenyMatcher {
229+ /// Fallible constructor for callers that need to reject malformed deny
230+ /// glob patterns before using the matcher.
231+ pub fn try_new (
232+ file_system_sandbox_policy : & FileSystemSandboxPolicy ,
233+ cwd : & Path ,
234+ ) -> Result < Option < Self > , String > {
235+ if !file_system_sandbox_policy. has_denied_read_restrictions ( ) {
236+ return Ok ( None ) ;
237+ }
238+
239+ let denied_candidates = file_system_sandbox_policy
240+ . get_unreadable_roots_with_cwd ( cwd)
241+ . into_iter ( )
242+ . map ( |path| normalized_and_canonical_candidates ( path. as_path ( ) ) )
243+ . collect ( ) ;
244+
245+ let deny_read_matchers = file_system_sandbox_policy
246+ . get_unreadable_globs_with_cwd ( cwd)
247+ . into_iter ( )
248+ . map ( |pattern| {
249+ build_glob_matcher_result ( & pattern)
250+ . map_err ( |err| format ! ( "invalid deny-read glob pattern {pattern:?}: {err}" ) )
251+ } )
252+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
253+
254+ Ok ( Some ( Self {
255+ denied_candidates,
256+ deny_read_matchers,
257+ invalid_pattern : false ,
258+ } ) )
259+ }
260+
229261 /// Builds a matcher from exact deny-read roots and deny-read glob entries.
230262 ///
231263 /// Returns `None` when the policy has no deny-read restrictions, so callers
@@ -1292,13 +1324,16 @@ fn push_unique(candidates: &mut Vec<PathBuf>, candidate: PathBuf) {
12921324}
12931325
12941326fn build_glob_matcher ( pattern : & str ) -> Option < GlobMatcher > {
1327+ build_glob_matcher_result ( pattern) . ok ( )
1328+ }
1329+
1330+ fn build_glob_matcher_result ( pattern : & str ) -> Result < GlobMatcher , globset:: Error > {
12951331 // Keep `*` and `?` within a single path component and preserve an unclosed
12961332 // `[` as a literal so matcher behavior stays aligned with config parsing.
12971333 GlobBuilder :: new ( pattern)
12981334 . literal_separator ( true )
12991335 . allow_unclosed_class ( true )
13001336 . build ( )
1301- . ok ( )
13021337 . map ( |glob| glob. compile_matcher ( ) )
13031338}
13041339
0 commit comments