Repository navigation
Refuse remote $refs unless their host is in allow-remote-refs-from (2.8.x backport, default "*") - #2623
Conversation
[Medium impact] The change affects remote document loading across the code-generation workflow. The PR should not merge while redirects bypass the allow list, saved settings lose the restriction, and overlays reject remote references allowed by the default. FindingsFix with agent prompt### Issue 1
pkg/util/loader.go:70-73
The host check only covers the first URL. If an allowed host redirects a `$ref` request to an unlisted host, `http.DefaultClient` follows it without checking `allow-remote-refs-from` again. An untrusted spec can use an allowed host's redirect endpoint to reach an internal address despite a restrictive list. Check every redirect target before sending the next request.
**How this was verified:** `remoteRefReader` checks the original URL, then delegates to `ReadFromHTTP` with `http.DefaultClient`, whose default redirect policy follows redirects without checking the allowed hosts.
### Issue 2
pkg/codegen/configuration.go:undefined-45
`omitempty` drops an explicit empty `AllowRemoteRefsFrom` slice when `--output-config` writes the configuration. Reusing that output turns the missing field into `["*"]` through `UpdateDefaults`, silently changing “refuse remote references” into “allow every host.” Preserve the empty list when writing the configuration and add a round-trip test.
**How this was verified:** `--output-config` YAML-encodes `Configuration`, the new slice has `omitempty`, and `UpdateDefaults` changes an omitted list to `["*"]`.
```suggestion
AllowRemoteRefsFrom []string `yaml:"allow-remote-refs-from"`
```
### Issue 3
pkg/util/loader.go:undefined-165
The loader used after applying an overlay receives the original `opts.AllowRemoteRefsFrom`, not the effective list used by `loadSwagger`. A library call with only `Path` set allows remote references during the first load but refuses an absolute remote `$ref` added by the overlay: the second loader receives `nil` and matches no hosts. It also loses the automatic permission for the input URL's own host. Build the effective list once and use it for both loads.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryAdds
Reviews (4) · Last reviewed commit: "Default allow-remote-refs-from to "*" on..." · Reviewed by Greptile |
| if !RemoteRefHostAllowed(allowed, location) { | ||
| return nil, fmt.Errorf("refusing to resolve remote reference %q: host %q is not listed in allow-remote-refs-from", location.String(), location.Host) | ||
| } | ||
| return readHTTP(loader, location) |
There was a problem hiding this comment.
Redirects bypass allowed hosts
The host check only covers the first URL. If an allowed host redirects a $ref request to an unlisted host, http.DefaultClient follows it without checking allow-remote-refs-from again. An untrusted spec can use an allowed host's redirect endpoint to reach an internal address despite a restrictive list. Check every redirect target before sending the next request.
How this was verified: remoteRefReader checks the original URL, then delegates to ReadFromHTTP with http.DefaultClient, whose default redirect policy follows redirects without checking the allowed hosts.
Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/util/loader.go
Line: 70-73
Comment:
**Redirects bypass allowed hosts**
The host check only covers the first URL. If an allowed host redirects a `$ref` request to an unlisted host, `http.DefaultClient` follows it without checking `allow-remote-refs-from` again. An untrusted spec can use an allowed host's redirect endpoint to reach an internal address despite a restrictive list. Check every redirect target before sending the next request.
**How this was verified:** `remoteRefReader` checks the original URL, then delegates to `ReadFromHTTP` with `http.DefaultClient`, whose default redirect policy follows redirects without checking the allowed hosts.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
This is intentional. A user has to whitelist based on the $ref host, it's up to them to check if it's a valid ref.
There was a problem hiding this comment.
Understood. The allowlist is intended to authorize the host named by the $ref; users are responsible for trusting that host, including its redirect behavior. My comment assumed the policy was meant to constrain every network destination, which isn't the intended contract here. I'll withdraw the finding.
| // remote reference, so that only references to local files (and to the | ||
| // host the specification itself was loaded from, when it was given as a | ||
| // URL) are followed. | ||
| AllowRemoteRefsFrom []string `yaml:"allow-remote-refs-from,omitempty"` |
There was a problem hiding this comment.
Saved settings allow every host
omitempty drops an explicit empty AllowRemoteRefsFrom slice when --output-config writes the configuration. Reusing that output turns the missing field into ["*"] through UpdateDefaults, silently changing “refuse remote references” into “allow every host.” Preserve the empty list when writing the configuration and add a round-trip test.
How this was verified: --output-config YAML-encodes Configuration, the new slice has omitempty, and UpdateDefaults changes an omitted list to ["*"].
| AllowRemoteRefsFrom []string `yaml:"allow-remote-refs-from,omitempty"` | |
| AllowRemoteRefsFrom []string `yaml:"allow-remote-refs-from"` |
Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/codegen/configuration.go
Line: 45
Comment:
**Saved settings allow every host**
`omitempty` drops an explicit empty `AllowRemoteRefsFrom` slice when `--output-config` writes the configuration. Reusing that output turns the missing field into `["*"]` through `UpdateDefaults`, silently changing “refuse remote references” into “allow every host.” Preserve the empty list when writing the configuration and add a round-trip test.
**How this was verified:** `--output-config` YAML-encodes `Configuration`, the new slice has `omitempty`, and `UpdateDefaults` changes an omitted list to `["*"]`.
```suggestion
AllowRemoteRefsFrom []string `yaml:"allow-remote-refs-from"`
```
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
We can't regress v2.8.0 behavior in v2.8.1. We will add this feature for people who care, and document the new option, but we can't break existing behavior per semver in a pach release.
There was a problem hiding this comment.
Agreed—the omitted option must continue to allow all remote refs on the 2.8.x patch line. My comment isn’t proposing a default change: it’s about preserving an explicit allow-remote-refs-from: [] opt-out when --output-config serializes the config. With omitempty, that empty list is omitted, and loading the output applies the [*] default, changing the user’s explicit setting. The compatibility default and preserving an explicitly empty list are both needed.
| loader := openapi3.NewLoader() | ||
| loader.IsExternalRefsAllowed = true | ||
| loader.IncludeOrigin = true | ||
| loader := newLoader(opts.AllowRemoteRefsFrom, http.DefaultClient) |
There was a problem hiding this comment.
The loader used after applying an overlay receives the original opts.AllowRemoteRefsFrom, not the effective list used by loadSwagger. A library call with only Path set allows remote references during the first load but refuses an absolute remote $ref added by the overlay: the second loader receives nil and matches no hosts. It also loses the automatic permission for the input URL's own host. Build the effective list once and use it for both loads.
Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/util/loader.go
Line: 165
Comment:
**Overlays lose allowed hosts**
The loader used after applying an overlay receives the original `opts.AllowRemoteRefsFrom`, not the effective list used by `loadSwagger`. A library call with only `Path` set allows remote references during the first load but refuses an absolute remote `$ref` added by the overlay: the second loader receives `nil` and matches no hosts. It also loses the automatic permission for the input URL's own host. Build the effective list once and use it for both loads.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.…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]>
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]>
52e522e to
0af7765
Compare
Summary
Backport of #2622 to
release/v2.8.x, as two commits:ReadFromURIFunc; a remote$refis followed only when its document URL (the$refwithout its fragment) is listed in the new top-levelallow-remote-refs-fromoption, or the option contains"*"; local file references are untouched; a spec passed tooapi-codegenas a URL is fetched without listing itself; the overlay re-load applies the same policy at the same location. Schema,docs/configuration.md, README note, tests and the one fixture that uses a remote reference come along.allow-remote-refs-fromis omitted it is treated as["*"], both inConfiguration.UpdateDefaultsand in the loader (soutil.LoadSwaggerkeeps following remote references too). Existing specs and configs therefore generate exactly as they did in 2.8.0. An explicit empty list refuses remote references, which is what the next minor release does by default. Docs, schema description and tests are adjusted, and a test pins the omitted / empty / listed distinction through YAML decoding.Why default to
"*"here2.8.0 followed every remote reference. A patch release should let users opt into the restriction without breaking anyone's build; main flips the default instead.
Testing
go test ./pkg/util/ ./pkg/codegen/ ./cmd/...and root lint pass locally.🤖 Generated with Claude Code