feat: inject a custom document loader into schema/spec validation#272
Merged
Conversation
Building a schema validator expands the schema's $ref, and validating a
specification resolves and expands $ref (schemas, parameters, responses, and the
whole-spec expansion). All of these resolved documents through the spec package's
global loader, with no way for a caller to supply their own.
Add WithPathLoader, an option-aware loader on SchemaValidatorOptions, consumed by
every path that loads documents during validation:
- newSchemaValidator uses spec.ExpandSchemaWithOptions (instead of the
default-settings ExpandSchema).
- resolveParam / expandResponseRef use spec.ExpandParameterWithOptions and
spec.ExpandResponseWithOptions.
- SpecValidator.resolveRef passes the loader through spec.ResolveRefWithBase.
- validateReferencesValid passes it to loads.Document.Expanded (no-op when unset:
loads falls back to the document's own loader).
- NewSpecValidator and the package-level Spec now accept ...Option, applied to
the internal schema options after the built-in defaults, so a caller can
inject the loader into spec validation. Backward compatible: existing
two-argument calls are unaffected.
The injected loader may carry any go-openapi/swag/loading options — a custom HTTP
client or timeout, authentication or custom headers, an embedded or rooted file
system, and so on. One important use is confining loading of untrusted input
(loading.WithRoot to confine local reads, loading.WithHTTPClient to restrict
remote fetches, or a restricted loader from go-openapi/loads), which closes the
arbitrary-file-read / SSRF surface that the unsandboxed default otherwise exposes.
Left unset, behavior is unchanged.
Bumps spec to v0.22.9 (for the option-aware expanders), analysis to v0.25.5,
loads to v0.25.0 and swag to v0.27.3.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi
force-pushed
the
feat/confined-ref-loading
branch
from
July 21, 2026 05:35
0f0d429 to
58f6939
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #272 +/- ##
==========================================
+ Coverage 92.35% 92.37% +0.01%
==========================================
Files 24 24
Lines 3272 3280 +8
==========================================
+ Hits 3022 3030 +8
Misses 165 165
Partials 85 85 ☔ 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.
Building a schema validator expands the schema's $ref, and validating a specification resolves and expands $ref (schemas, parameters, responses, and the whole-spec expansion). All of these resolved documents through the spec package's global loader, with no way for a caller to supply their own.
Add WithPathLoader, an option-aware loader on SchemaValidatorOptions, consumed by every path that loads documents during validation:
The injected loader may carry any go-openapi/swag/loading options — a custom HTTP client or timeout, authentication or custom headers, an embedded or rooted file system, and so on. One important use is confining loading of untrusted input (loading.WithRoot to confine local reads, loading.WithHTTPClient to restrict remote fetches, or a restricted loader from go-openapi/loads), which closes the arbitrary-file-read / SSRF surface that the unsandboxed default otherwise exposes. Left unset, behavior is unchanged.
Bumps spec to v0.22.9 (for the option-aware expanders), analysis to v0.25.5, loads to v0.25.0 and swag to v0.27.3.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist