Repository navigation
Refuse remote $refs unless their host is in allow-remote-refs-from - #2622
Merged
Merged
Conversation
oapi-codegen resolved every external $ref in the input specification with kin-openapi's default reader, which fetches any http(s) URL the document names. Generating code from a specification written by someone else (a vendor's spec, a spec submitted in a pull request that CI processes) thus let that document make the generator issue requests to arbitrary hosts reachable from the build machine, before import-mapping or any other option was consulted. The loader now installs its own ReadFromURIFunc. References to local files are followed as before, so import-mapping across files is unaffected. References that name a host are checked against the new top-level configuration option allow-remote-refs-from, a list of host names (optionally with a port) or "*" for any host, and refused with a clear error otherwise. Nothing is fetched for a refused reference. When the specification itself is given as a URL, references to that same host are followed without listing it, since loading from there was the caller's choice. The default is an empty list, so remote references are refused unless opted in. util.LoadSwagger keeps its signature and the new default; LoadSwaggerWithOverlayOpts gains AllowRemoteRefsFrom, which the CLI fills from the configuration. The option is validated (host names, not URLs), added to configuration-schema.json and docs/configuration.md, and explained in the README's import-mapping section. The one fixture that references a remote document, internal/test/references/multipackage, lists its host; its generated output is unchanged. TestRemoteExternalReference allows the host it fetches from. Tests in pkg/util use a fake HTTP transport so they need no socket, and cover the refused default, host and host:port matching, the wildcard, the same-host rule for URL specs, and that local file references still load. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
[Medium impact] The PR appears safe to merge; the root URL fragment issue is fixed and no new actionable issue was found. SummaryThe PR adds
Reviews (3) · Last reviewed commit: "Record fragment-free URLs in the loader ..." · Reviewed by Greptile |
Review feedback on oapi-codegen#2622. Host-based matching was the wrong unit of trust: a shared host such as a code forge or a bucket serves documents from many unrelated people, and allowing the host allows all of them. It also invited matching rules (ports, IPv6 brackets, case) that each had edge cases, and the implicit allowance for the spec's own host had the same problem and was lost on the overlay re-load. An entry is now the URL of a referenced document exactly as the $ref names it, without the fragment, or "*". Nothing is normalised and nothing is inferred: a different path, port, scheme or spelling on the same host is refused until listed, and a relative $ref inside a remote document has to be listed as the absolute URL it resolves to. The only thing fetched without an entry is a spec that was itself passed to oapi-codegen as a URL, since kin-openapi fetches the top-level document through the same reader and the caller named that URL. The overlay path now builds its second loader with the same policy and the same location as the first, which also fixes relative references in a URL-loaded spec resolving as local paths once an overlay was applied. Configuration validation requires a URL with a scheme and host and no fragment. Docs, schema, the multipackage fixture config and the tests follow; the fixture's generated output is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]>
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Oct 23, 2026
Review feedback on oapi-codegen#2622. Host-based matching was the wrong unit of trust: a shared host such as a code forge or a bucket serves documents from many unrelated people, and allowing the host allows all of them. It also invited matching rules (ports, IPv6 brackets, case) that each had edge cases, and the implicit allowance for the spec's own host had the same problem and was lost on the overlay re-load. An entry is now the URL of a referenced document exactly as the $ref names it, without the fragment, or "*". Nothing is normalised and nothing is inferred: a different path, port, scheme or spelling on the same host is refused until listed, and a relative $ref inside a remote document has to be listed as the absolute URL it resolves to. The only thing fetched without an entry is a spec that was itself passed to oapi-codegen as a URL, since kin-openapi fetches the top-level document through the same reader and the caller named that URL. The overlay path now builds its second loader with the same policy and the same location as the first, which also fixes relative references in a URL-loaded spec resolving as local paths once an overlay was applied. Configuration validation requires a URL with a scheme and host and no fragment. Docs, schema, the multipackage fixture config and the tests follow; the fixture's generated output is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Review feedback on oapi-codegen#2622. The root document's URL was added to the allow list with its fragment, while the reader compares fragment-free document URLs, so a spec given as https://example.com/api.yaml#overview was refused before it was fetched. Add the root URL the way every other entry is kept. Co-Authored-By: Claude Fable 5.1 <[email protected]>
The previous commit's test asserted the request URL without the fragment, but kin-openapi passes the root URL to the reader with the fragment still on it, and the fake transport recorded it verbatim. Record the document URL instead, since the fragment never goes on the wire. Co-Authored-By: Claude Fable 5.1 <[email protected]>
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Oct 23, 2026
Review feedback on oapi-codegen#2622. The root document's URL was added to the allow list with its fragment, while the reader compares fragment-free document URLs, so a spec given as https://example.com/api.yaml#overview was refused before it was fetched. Add the root URL the way every other entry is kept. Co-Authored-By: Claude Fable 5.1 <[email protected]>
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Oct 23, 2026
…api-codegen#2622) * Refuse remote $refs unless their host is in allow-remote-refs-from oapi-codegen resolved every external $ref in the input specification with kin-openapi's default reader, which fetches any http(s) URL the document names. Generating code from a specification written by someone else (a vendor's spec, a spec submitted in a pull request that CI processes) thus let that document make the generator issue requests to arbitrary hosts reachable from the build machine, before import-mapping or any other option was consulted. The loader now installs its own ReadFromURIFunc. References to local files are followed as before, so import-mapping across files is unaffected. References that name a host are checked against the new top-level configuration option allow-remote-refs-from, a list of host names (optionally with a port) or "*" for any host, and refused with a clear error otherwise. Nothing is fetched for a refused reference. When the specification itself is given as a URL, references to that same host are followed without listing it, since loading from there was the caller's choice. The default is an empty list, so remote references are refused unless opted in. util.LoadSwagger keeps its signature and the new default; LoadSwaggerWithOverlayOpts gains AllowRemoteRefsFrom, which the CLI fills from the configuration. The option is validated (host names, not URLs), added to configuration-schema.json and docs/configuration.md, and explained in the README's import-mapping section. The one fixture that references a remote document, internal/test/references/multipackage, lists its host; its generated output is unchanged. TestRemoteExternalReference allows the host it fetches from. Tests in pkg/util use a fake HTTP transport so they need no socket, and cover the refused default, host and host:port matching, the wildcard, the same-host rule for URL specs, and that local file references still load. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Match allow-remote-refs-from entries against the whole document URL Review feedback on oapi-codegen#2622. Host-based matching was the wrong unit of trust: a shared host such as a code forge or a bucket serves documents from many unrelated people, and allowing the host allows all of them. It also invited matching rules (ports, IPv6 brackets, case) that each had edge cases, and the implicit allowance for the spec's own host had the same problem and was lost on the overlay re-load. An entry is now the URL of a referenced document exactly as the $ref names it, without the fragment, or "*". Nothing is normalised and nothing is inferred: a different path, port, scheme or spelling on the same host is refused until listed, and a relative $ref inside a remote document has to be listed as the absolute URL it resolves to. The only thing fetched without an entry is a spec that was itself passed to oapi-codegen as a URL, since kin-openapi fetches the top-level document through the same reader and the caller named that URL. The overlay path now builds its second loader with the same policy and the same location as the first, which also fixes relative references in a URL-loaded spec resolving as local paths once an overlay was applied. Configuration validation requires a URL with a scheme and host and no fragment. Docs, schema, the multipackage fixture config and the tests follow; the fixture's generated output is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Allow a root spec URL that carries a fragment Review feedback on oapi-codegen#2622. The root document's URL was added to the allow list with its fragment, while the reader compares fragment-free document URLs, so a spec given as https://example.com/api.yaml#overview was refused before it was fetched. Add the root URL the way every other entry is kept. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Record fragment-free URLs in the loader test transport The previous commit's test asserted the request URL without the fragment, but kin-openapi passes the root URL to the reader with the fragment still on it, and the fake transport recorded it verbatim. Record the document URL instead, since the fragment never goes on the wire. Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
Member
|
Sounds good, would we say this closed #1564? |
mromaszewicz
added a commit
that referenced
this pull request
Oct 23, 2026
….8.x backport, default "*") (#2623) * Refuse remote $refs unless their host is in allow-remote-refs-from (#2622) * Refuse remote $refs unless their host is in allow-remote-refs-from oapi-codegen resolved every external $ref in the input specification with kin-openapi's default reader, which fetches any http(s) URL the document names. Generating code from a specification written by someone else (a vendor's spec, a spec submitted in a pull request that CI processes) thus let that document make the generator issue requests to arbitrary hosts reachable from the build machine, before import-mapping or any other option was consulted. The loader now installs its own ReadFromURIFunc. References to local files are followed as before, so import-mapping across files is unaffected. References that name a host are checked against the new top-level configuration option allow-remote-refs-from, a list of host names (optionally with a port) or "*" for any host, and refused with a clear error otherwise. Nothing is fetched for a refused reference. When the specification itself is given as a URL, references to that same host are followed without listing it, since loading from there was the caller's choice. The default is an empty list, so remote references are refused unless opted in. util.LoadSwagger keeps its signature and the new default; LoadSwaggerWithOverlayOpts gains AllowRemoteRefsFrom, which the CLI fills from the configuration. The option is validated (host names, not URLs), added to configuration-schema.json and docs/configuration.md, and explained in the README's import-mapping section. The one fixture that references a remote document, internal/test/references/multipackage, lists its host; its generated output is unchanged. TestRemoteExternalReference allows the host it fetches from. Tests in pkg/util use a fake HTTP transport so they need no socket, and cover the refused default, host and host:port matching, the wildcard, the same-host rule for URL specs, and that local file references still load. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Match allow-remote-refs-from entries against the whole document URL Review feedback on #2622. Host-based matching was the wrong unit of trust: a shared host such as a code forge or a bucket serves documents from many unrelated people, and allowing the host allows all of them. It also invited matching rules (ports, IPv6 brackets, case) that each had edge cases, and the implicit allowance for the spec's own host had the same problem and was lost on the overlay re-load. An entry is now the URL of a referenced document exactly as the $ref names it, without the fragment, or "*". Nothing is normalised and nothing is inferred: a different path, port, scheme or spelling on the same host is refused until listed, and a relative $ref inside a remote document has to be listed as the absolute URL it resolves to. The only thing fetched without an entry is a spec that was itself passed to oapi-codegen as a URL, since kin-openapi fetches the top-level document through the same reader and the caller named that URL. The overlay path now builds its second loader with the same policy and the same location as the first, which also fixes relative references in a URL-loaded spec resolving as local paths once an overlay was applied. Configuration validation requires a URL with a scheme and host and no fragment. Docs, schema, the multipackage fixture config and the tests follow; the fixture's generated output is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Allow a root spec URL that carries a fragment Review feedback on #2622. The root document's URL was added to the allow list with its fragment, while the reader compares fragment-free document URLs, so a spec given as https://example.com/api.yaml#overview was refused before it was fetched. Add the root URL the way every other entry is kept. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Record fragment-free URLs in the loader test transport The previous commit's test asserted the request URL without the fragment, but kin-openapi passes the root URL to the reader with the fragment still on it, and the fake transport recorded it verbatim. Record the document URL instead, since the fragment never goes on the wire. Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]> * Default allow-remote-refs-from to "*" on the 2.8 release line Earlier 2.8 releases followed every remote $ref, and a patch release must not change that. When allow-remote-refs-from is omitted, the loader and Configuration.UpdateDefaults now treat it as ["*"], so existing specs and configs generate exactly as before; util.LoadSwagger keeps following remote references as well. An explicit empty list in the config refuses remote references, which is what the next minor release does by default. Documentation, the JSON schema and the loader tests say so; a new test pins the omitted / empty / listed distinction through YAML decoding. Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
Member
Author
Yes, it does. |
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.
Summary
oapi-codegenresolved every external$refin the input specification with kin-openapi's default reader, which fetches anyhttp(s)URL the document names. Generating code from a specification written by someone else (a vendor's spec, or a spec submitted in a pull request that CI processes) therefore let that document make the generator issue requests to arbitrary hosts reachable from the build machine, beforeimport-mappingor any other option was consulted.This adds a top-level configuration option,
allow-remote-refs-from, and refuses remote references that are not covered by it:"*"for any host. An entry without a port matches that host on any port; matching is case-insensitive.import-mappingacross files is unaffected."*"to keep patch-release behaviour unchanged.)Changes
pkg/util/loader.go: the loader installs its ownReadFromURIFunc(allow-listed HTTP reader, thenopenapi3.ReadFromFile).LoadSwaggerkeeps its signature with the new default;LoadSwaggerWithOverlayOptsgainsAllowRemoteRefsFrom.RemoteRefHostAllowedis exported for callers that build their own loader.Configuration.AllowRemoteRefsFrom, validated so that URLs or paths are rejected rather than silently allowing nothing. The CLI passes it to the loader.configuration-schema.json,docs/configuration.md, and an IMPORTANT note in the README's import-mapping section.internal/test/references/multipackageis the one fixture that references a remote document; its config now lists the host. Generated output is unchanged (make generateproduces no diff).TestRemoteExternalReferenceallows the host it fetches from.There is no CLI flag for the option; flags-only usage is deprecated and a config file is the supported way to configure the generator.
The local-file read oracle (a
$refto an absolute path reveals whether the file exists) is deliberately not addressed here, becauseimport-mappinglegitimately uses parent-directory references.Testing
pkg/utiluse a fake HTTP transport (no socket needed) and cover the refused default, host and host:port matching, the wildcard, the same-host rule for URL specs, and that local file references still load.Configuration.Validatetest for well-formed and malformed entries.go test ./pkg/...,./cmd/..., theinternal/testreferences and spec validation packages, lint andmake generatepass locally.🤖 Generated with Claude Code