-
-
Notifications
You must be signed in to change notification settings - Fork 39
Use os.Root for restricted filesystem operations #22081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use os.Root for restricted filesystem operations #22081
Conversation
This change replaces direct file system operations with the SafeFS utility in various test files. This enhances security by restricting file access to a defined root directory, preventing unintended side effects and improving test isolation. Co-authored-by: batazor111 <batazor111@gmail.com>
Cursor Agent can help with this pull request. Just |
Reviewer's GuideThis PR introduces a new pkg/fsroot package that wraps Go 1.25’s os.Root into a SafeFS utility for confined filesystem operations, then systematically migrates existing file and directory access across various modules and tests to use this secure API, eliminating direct os.* calls to prevent path traversal. Sequence diagram for secure rule loading in loadRules (poc/cel/rules.go)sequenceDiagram
participant Caller
participant SafeFS
participant Directory
participant File
Caller->>SafeFS: OpenRoot(path)
SafeFS->>Directory: Open(".")
Directory->>SafeFS: ReadDir(-1)
SafeFS->>Caller: files
loop For each file
Caller->>SafeFS: ReadFile(file.Name())
SafeFS->>File: ReadFile(file.Name())
File->>SafeFS: content
SafeFS->>Caller: content
end
Class diagram for the new SafeFS type in pkg/fsrootclassDiagram
class SafeFS {
- root: os.Root
+ ReadFile(name string) (data []byte, err error)
+ ReadDir(name string) ([]os.DirEntry, error)
+ Create(name string) (*os.File, error)
+ Open(name string) (*os.File, error)
+ Remove(name string) error
+ Stat(name string) (os.FileInfo, error)
+ WriteFile(name string, data []byte, perm os.FileMode) error
}
SafeFS o-- "1" os.Root: wraps
class os.Root {
+ Open(name string) (*os.File, error)
+ ReadFile(name string) ([]byte, error)
+ ...
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Co-authored-by: batazor111 <batazor111@gmail.com>
Co-authored-by: batazor111 <batazor111@gmail.com>
boundaries/link/internal/infrastructure/repository/crud/sqlite/sqlite_test.go
Outdated
Show resolved
Hide resolved
Co-authored-by: batazor111 <batazor111@gmail.com>
Co-authored-by: batazor111 <batazor111@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider using your new fsroot.SafeFS abstraction instead of raw os.OpenRoot calls everywhere to reduce boilerplate and centralize error handling.
- You have repeated os.OpenRoot(...)/defer root.Close() blocks in multiple tests—extract a small test helper to initialize and clean up the root FS to avoid copy-pasted code.
- In loadRules, you could wrap the directory iteration and file reading into a helper method on SafeFS to simplify the function and improve readability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using your new fsroot.SafeFS abstraction instead of raw os.OpenRoot calls everywhere to reduce boilerplate and centralize error handling.
- You have repeated os.OpenRoot(...)/defer root.Close() blocks in multiple tests—extract a small test helper to initialize and clean up the root FS to avoid copy-pasted code.
- In loadRules, you could wrap the directory iteration and file reading into a helper method on SafeFS to simplify the function and improve readability.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Pull request was closed
Summary
Introduces
pkg/fsroot
to leverage Go 1.25'sos.Root
for secure filesystem operations, preventing path traversal vulnerabilities. Existing file and directory operations across the codebase have been migrated to use this newSafeFS
utility.Details
This PR implements the
os.Root
type to restrict filesystem operations to specific directories, enhancing security by preventing path traversal attacks.Key Changes:
New
pkg/fsroot
Package:fsroot.go
: DefinesSafeFS
, a wrapper aroundos.Root
, providing secure methods forReadFile
,ReadDir
,Create
,Open
,Remove
,Stat
, andWriteFile
.fsroot_test.go
: Comprehensive unit tests forSafeFS
functionality.example_test.go
: Demonstrates basic usage and security features ofSafeFS
.README.md
: Detailed documentation, API reference, and best practices forfsroot
.Migration to
SafeFS
:poc/cel/rules.go
:loadRules
now usesSafeFS
to safely read rule files from a designated directory.pkg/protoc/protoc-gen-rich-model/rich_model_test.go
,pkg/protoc/protoc-gen-go-orm/ram_orm_test.go
,postgres_orm_test.go
,mongo_orm_test.go
): Updated to useSafeFS
for securely reading generated output files within their respective output directories.pkg/s3/s3_test.go
: File operations for test fixtures (reading and removing) now utilizeSafeFS
.boundaries/link/internal/infrastructure/repository/crud/sqlite/sqlite_test.go
: The removal of SQLite database files is now handled securely bySafeFS
.This change ensures that all updated filesystem interactions are confined to their intended directories, significantly reducing the risk of unauthorized file access.
Summary by Sourcery
Leverage Go 1.25 os.Root by introducing SafeFS to restrict filesystem interactions to designated directories and update existing code and tests to use this secure interface.
New Features:
Enhancements:
Documentation:
Tests: