feat(loading): sandbox local loading with WithRoot (GHSA-v2xp-g8xf-22pf)#203
Merged
Conversation
Allow for optional secure sandboxing of the spec loader when reading local files. The defaults remain non-secure as it would introduce severe breaking changes downstream. Sandboxing therefore remains opt-in. LoadFromFileOrHTTP resolved file:// URIs and bare paths straight to os.ReadFile with no confinement, so a caller that forwards untrusted input could read any file the process can access (file:///etc/passwd, absolute paths, etc.). The advised mitigation, WithFS(os.DirFS(dir)), blocks ".." traversal but not symlinks that escape the root. Add WithRoot(dir): an opt-in, non-breaking option that confines local reads to dir via os.Root. It resolves every requested path relative to dir and rejects absolute paths, ".." traversal, and escaping symlinks -- including the symlink case os.DirFS misses. The root is opened per read and closed (no fd leak); the open error is wrapped with ErrLoader. WithRoot and WithFS are mutually exclusive (last applied wins). Also fix a latent bug in LoadStrategy: it applied native separators (filepath.FromSlash) to fs-backed loaders, breaking multi-segment paths on Windows for any fs.FS. fs-backed loaders now receive forward slashes on all platforms, preserving embed.FS's existing always-"/" rule; the native path is kept only for the default os.ReadFile loader. Document the default permissive behavior and steer untrusted-input callers to WithRoot in the package and function godoc. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #203 +/- ##
==========================================
+ Coverage 92.48% 92.53% +0.04%
==========================================
Files 54 54
Lines 2582 2598 +16
==========================================
+ Hits 2388 2404 +16
Misses 148 148
Partials 46 46 ☔ View full report in Codecov by Sentry. |
The remote loader is a thin wrapper over a standard net/http client and, like http.DefaultClient, follows redirects and applies no destination filtering. A caller that forwards untrusted URLs can thus trigger server-side request forgery (internal services, cloud metadata). Embedding a network policy (IP denylists, allowlists) in a general fetch utility is the wrong layer and invites false confidence: the existing WithHTTPClient option already lets a caller install a guarded transport that filters destinations at dial time, which is the only point that survives redirects and DNS rebinding. Document the exposure in the package godoc and add a runnable, self-validating example showing the WithHTTPClient + net.Dialer.Control recipe. Separately, fix a latent correctness bug: LoadStrategy routed any path with a bare "http" prefix to the remote loader, so a local file named e.g. "httpbin.json" was misrouted, and an uppercase "HTTP://" URL was sent to the local loader. Require a real, case-insensitive http:// or https:// scheme instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allow for optional secure sandboxing of the spec loader when reading local files.
The defaults remain non-secure as it would introduce severe breaking changes downstream. Sandboxing therefore remains opt-in.
LoadFromFileOrHTTP resolved file:// URIs and bare paths straight to os.ReadFile with no confinement, so a caller that forwards untrusted input could read any file the process can access (file:///etc/passwd, absolute paths, etc.). The advised mitigation, WithFS(os.DirFS(dir)), blocks ".." traversal but not symlinks that escape the root.
Add WithRoot(dir): an opt-in, non-breaking option that confines local reads to dir via os.Root. It resolves every requested path relative to dir and rejects absolute paths, ".." traversal, and escaping symlinks -- including the symlink case os.DirFS misses. The root is opened per read and closed (no fd leak); the open error is wrapped with ErrLoader. WithRoot and WithFS are mutually exclusive (last applied wins).
Also fix a latent bug in LoadStrategy: it applied native separators (filepath.FromSlash) to fs-backed loaders, breaking multi-segment paths on Windows for any fs.FS. fs-backed loaders now receive forward slashes on all platforms, preserving embed.FS's existing always-"/" rule; the native path is kept only for the default os.ReadFile loader.
Document the default permissive behavior and steer untrusted-input callers to WithRoot in the package and function godoc.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist