9
9
"context"
10
10
"errors"
11
11
"fmt"
12
- "log/slog"
13
12
"path/filepath"
14
- "regexp "
13
+ "slices "
15
14
"strings"
16
15
"time"
17
16
@@ -356,10 +355,7 @@ func (col *Collector) Validate(allowedDirectories []string) error {
356
355
var err error
357
356
cleanedConfPath := filepath .Clean (col .ConfigPath )
358
357
359
- allowed , err := isAllowedDir (cleanedConfPath , allowedDirectories )
360
- if err != nil {
361
- return err
362
- }
358
+ allowed := isAllowedDir (cleanedConfPath , allowedDirectories )
363
359
if ! allowed {
364
360
err = errors .Join (err , fmt .Errorf ("collector path %s not allowed" , col .ConfigPath ))
365
361
}
@@ -378,10 +374,7 @@ func (nr *NginxReceiver) Validate(allowedDirectories []string) error {
378
374
}
379
375
380
376
for _ , al := range nr .AccessLogs {
381
- allowed , allowedError := isAllowedDir (al .FilePath , allowedDirectories )
382
- if allowedError != nil {
383
- err = errors .Join (err , fmt .Errorf ("invalid nginx receiver access log path: %s" , al .FilePath ))
384
- }
377
+ allowed := isAllowedDir (al .FilePath , allowedDirectories )
385
378
if ! allowed {
386
379
err = errors .Join (err , fmt .Errorf ("nginx receiver access log path %s not allowed" , al .FilePath ))
387
380
}
@@ -396,13 +389,9 @@ func (nr *NginxReceiver) Validate(allowedDirectories []string) error {
396
389
return err
397
390
}
398
391
399
- func (c * Config ) IsDirectoryAllowed (directory string ) bool {
400
- allow , err := isAllowedDir (directory , c .AllowedDirectories )
401
- if err != nil {
402
- slog .Warn ("Unable to determine if directory is allowed" , "error" , err )
403
- return false
404
- }
405
-
392
+ // IsAllowedDirectory checks if the given path is in the list of allowed directories.
393
+ func (c * Config ) IsDirectoryAllowed (path string ) bool {
394
+ allow := isAllowedDir (path , c .AllowedDirectories )
406
395
return allow
407
396
}
408
397
@@ -480,32 +469,19 @@ func (c *Config) IsCommandServerProxyConfigured() bool {
480
469
}
481
470
482
471
// isAllowedDir checks if the given path is in the list of allowed directories.
483
- // It returns true if the path is allowed, false otherwise.
484
- // If the path is allowed but does not exist, it also logs a warning.
485
- // It also checks if the path is a file, in which case it checks the parent directory of the file.
486
- func isAllowedDir (path string , allowedDirs []string ) (bool , error ) {
487
- if len (allowedDirs ) == 0 {
488
- return false , errors .New ("no allowed directories configured" )
489
- }
490
-
491
- directoryPath := path
492
- // Check if the path is a file, regex matches when end of string is /<filename>.<extension>
493
- isFilePath , err := regexp .MatchString (`/(\w+)\.(\w+)$` , directoryPath )
494
- if err != nil {
495
- return false , errors .New ("error matching path" + directoryPath )
496
- }
472
+ // It recursively checks the parent directories of the path, until it finds a match or reaches the root directory.
473
+ func isAllowedDir (path string , allowedDirs []string ) bool {
474
+ return checkDirIsAllowed (filepath .Clean (path ), allowedDirs )
475
+ }
497
476
498
- if isFilePath {
499
- directoryPath = filepath .Dir (directoryPath )
477
+ func checkDirIsAllowed (path string , allowedDirs []string ) bool {
478
+ if slices .Contains (allowedDirs , path ) {
479
+ return true
500
480
}
501
481
502
- for _ , allowedDirectory := range allowedDirs {
503
- // Check if the directoryPath starts with the allowedDirectory
504
- // This allows for subdirectories within the allowed directories.
505
- if strings .HasPrefix (directoryPath , allowedDirectory ) {
506
- return true , nil
507
- }
482
+ if path == "/" || ! strings .HasPrefix (path , "/" ) { // root directory reached with no match, path is not allowed
483
+ return false
508
484
}
509
485
510
- return false , nil
486
+ return checkDirIsAllowed ( filepath . Dir ( path ), allowedDirs )
511
487
}
0 commit comments