Skip to content

Refuse remote $refs unless their host is in allow-remote-refs-from - #2622

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/remote-refs
Oct 23, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/remote-refs

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Summary

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, 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, before import-mapping or 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:

  • Entries are host names, optionally with a port, or "*" for any host. An entry without a port matches that host on any port; matching is case-insensitive.
  • A refused reference produces a clear error and nothing is fetched, so no request leaves the machine.
  • References to local files are followed exactly as before, so import-mapping across files is unaffected.
  • 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: remote references are refused unless opted in. (The 2.8.x backport will default to "*" to keep patch-release behaviour unchanged.)

Changes

  • pkg/util/loader.go: the loader installs its own ReadFromURIFunc (allow-listed HTTP reader, then openapi3.ReadFromFile). LoadSwagger keeps its signature with the new default; LoadSwaggerWithOverlayOpts gains AllowRemoteRefsFrom. RemoteRefHostAllowed is 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/multipackage is the one fixture that references a remote document; its config now lists the host. Generated output is unchanged (make generate produces no diff). TestRemoteExternalReference allows 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 $ref to an absolute path reveals whether the file exists) is deliberately not addressed here, because import-mapping legitimately uses parent-directory references.

Testing

  • New tests in pkg/util use 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.Validate test for well-formed and malformed entries.
  • go test ./pkg/..., ./cmd/..., the internal/test references and spec validation packages, lint and make generate pass locally.

🤖 Generated with Claude Code

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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 20:53
@mromaszewicz mromaszewicz added bug Something isn't working notable changes Used for release notes to highlight these more highly labels Oct 23, 2026
@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the root URL fragment issue is fixed and no new actionable issue was found.

Summary

The PR adds allow-remote-refs-from to permit specific document URLs or "*". Unlisted remote references are refused; local references still work.

  • The generator fetches remote $ref documents only when their URLs are allowed.

Reviews (3) · Last reviewed commit: "Record fragment-free URLs in the loader ..." · Reviewed by Greptile

Comment thread pkg/util/loader.go Resolved
Comment thread pkg/util/loader.go Resolved
Comment thread pkg/util/loader.go Resolved
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]>
Comment thread pkg/util/loader.go Resolved
mromaszewicz and others added 2 commits October 23, 2026 14:31
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
mromaszewicz merged commit 1f8de29 into oapi-codegen:main Oct 23, 2026
16 checks passed
@mromaszewicz
mromaszewicz deleted the fix/remote-refs branch October 23, 2026 21:35
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]>
@jamietanna

Copy link
Copy Markdown
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]>
@mromaszewicz

Copy link
Copy Markdown
Member Author

Sounds good, would we say this closed #1564?

Yes, it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants