fix(loading): resolve absolute in-root paths under WithRoot#224
Merged
Conversation
os.Root only accepts paths relative to its root and rejects any absolute path,
even one that points inside the root. But callers that normalize references to
absolute paths before loading — notably github.com/go-openapi/spec, which
resolves every $ref to an absolute path against the spec's base — then had every
confined read rejected, making WithRoot unusable for its primary purpose:
safely resolving references in an untrusted document.
Rebase an absolute requested path onto the root (via filepath.Rel) before
handing it to os.Root:
- a relative path is passed through unchanged (os.Root confines it, including
".." traversal and symlink escapes);
- an absolute path inside the root is rebased to a root-relative path and read;
- an absolute path outside the root yields a "../" prefix and is rejected by
os.Root;
- a path that cannot be made relative to the root (e.g. a different volume on
Windows) makes filepath.Rel return an error, which is propagated as a
rejection rather than allowed to escape.
Symlink-escape protection is preserved: the rebase is purely lexical; the actual
open still goes through os.Root, which resolves every path component within the
root. Default behavior (no WithRoot) is unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Frederic BIDON <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #224 +/- ##
==========================================
- Coverage 92.29% 92.17% -0.12%
==========================================
Files 57 57
Lines 2674 2685 +11
==========================================
+ Hits 2468 2475 +7
- Misses 164 166 +2
- Partials 42 44 +2 ☔ View full report in Codecov by Harness. |
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.
os.Root only accepts paths relative to its root and rejects any absolute path, even one that points inside the root. But callers that normalize references to absolute paths before loading — notably github.com/go-openapi/spec, which resolves every $ref to an absolute path against the spec's base — then had every confined read rejected, making WithRoot unusable for its primary purpose: safely resolving references in an untrusted document.
Rebase an absolute requested path onto the root (via filepath.Rel) before handing it to os.Root:
Symlink-escape protection is preserved: the rebase is purely lexical; the actual open still goes through os.Root, which resolves every path component within the root. Default behavior (no WithRoot) is unchanged.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist