Provides a secure wrapper around filesystem operations to prevent path traversal attacks and
unauthorized file access.
Security features:
- Restricts operations to specified allowed paths
- Prevents path traversal attacks by normalizing all paths
- Blocks access to symlinks to avoid arbitrary file access
- Requires absolute paths to avoid relative path confusion
- Validates the entire path hierarchy for each operation
This class should be used whenever performing filesystem operations on
user-provided or untrusted input paths to ensure proper access boundaries.
allowed_paths
type ReadonlyArray<ScopedFsPath>
readonly
constructor
Create a new ScopedFs instance with the specified allowed paths.
type new (allowed_paths: readonly string[] | string[]): ScopedFs
allowed_paths
Array of absolute paths that operations will be restricted to
type readonly string[] | string[]
is_path_allowed
Checks if the given path is allowed based on the paths provided during instantiation.
type (path_to_check: string): boolean
path_to_check
type string
returns boolean
is_path_safe
Performs a complete security check on a path, including symlink validation
type (path_to_check: string): Promise<boolean>
path_to_check
type string
returns Promise<boolean>
read_file
type (file_path: string, options?: (ObjectEncodingOptions & Abortable & { flag?: OpenMode | undefined; }) | BufferEncoding | null | undefined): Promise<...>
options
type (ObjectEncodingOptions & Abortable & { flag?: OpenMode | undefined; }) | BufferEncoding | null | undefined
default 'utf8'
returns Promise<string | Buffer<ArrayBufferLike>>
write_file
type (file_path: string, data: string | ArrayBufferView<ArrayBufferLike> | Iterable<string | ArrayBufferView<ArrayBufferLike>> | AsyncIterable<...> | Stream, options?: BufferEncoding | ... 2 more ... | undefined): Promise<...>
data
type string | ArrayBufferView<ArrayBufferLike> | Iterable<string | ArrayBufferView<ArrayBufferLike>> | AsyncIterable<...> | Stream
options
type BufferEncoding | (ObjectEncodingOptions & { mode?: Mode | undefined; flag?: OpenMode | undefined; flush?: boolean | undefined; } & Abortable) | null | undefined
default 'utf8'
returns Promise<void>
rm
type (path_to_remove: string, options?: RmOptions | undefined): Promise<void>
path_to_remove
type string
options?
type RmOptions | undefined
optional
returns Promise<void>
mkdir
type (dir_path: string, options?: MakeDirectoryOptions | undefined): Promise<string | undefined>
options?
type MakeDirectoryOptions | undefined
optional
returns Promise<string | undefined>
readdir
type (path: PathLike, options?: BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined): Promise<...>
options?
type BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined
optional
returns Promise<string[]>
readdir
type (path: PathLike, options?: BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined): Promise<...>
options?
type BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined
optional
returns Promise<string[]>
readdir
type (path: PathLike, options?: BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined): Promise<...>
options?
type BufferEncoding | (ObjectEncodingOptions & { withFileTypes?: false | undefined; recursive?: boolean | undefined; }) | null | undefined
optional
returns Promise<string[]>
stat
type (path_to_stat: string, options?: StatOptions | undefined): Promise<Stats>
options?
type StatOptions | undefined
optional
returns Promise<Stats>
stat
type (path_to_stat: string, options?: StatOptions | undefined): Promise<Stats>
options?
type StatOptions | undefined
optional
returns Promise<Stats>
stat
type (path_to_stat: string, options?: StatOptions | undefined): Promise<Stats>
options?
type StatOptions | undefined
optional
returns Promise<Stats>
copy_file
type (source: string, destination: string, mode?: number | undefined): Promise<void>
mode?
type number | undefined
optional
returns Promise<void>
exists
type (path_to_check: string): Promise<boolean>
path_to_check
type string
returns Promise<boolean>