Backtestshugorun-20260930-140149#15302
Add config option security.http.proxyFromEnvironment = false
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches add `security.http.proxyFromEnvironment`, defaulting to false. With the flag off, both clear the transport's Proxy, so the dial-time address check sees the real destination. That fixes the core issue in both. The difference is what happens when the flag is enabled. - **Patch B** keeps the default `ProxyFromEnvironment`, and the dial check then validates the proxy address. Its own docs admit that a typical local or corporate proxy on a loopback or private address is refused under the default policy. Users must loosen `http.urls`, which also disables the resolved-address hardening entirely. That makes the opt-in awkward and pushes users toward weaker settings. - **Patch A** resolves the proxy per request and passes it through the request context. It skips the address check only for the dial to the chosen proxy, and still validates every direct dial, including a direct request to the proxy's own address. This is a more careful design that keeps the hardening intact when the flag is on. It is more complex, but the logic is well commented, and `canonicalAddr` mirrors `net/http`. **Tests:** Patch A adds a real transport test with httptest servers. It checks that the proxy is ignored by default, used when enabled even though it is on loopback, and that direct loopback dials, including to the proxy address, are still denied. It also updates the TOML and default-decode tests. Patch B adds no tests. **Craft:** Patch A updates `docs.yaml`, documents the resolved-address validation, and adds a newIn marker. Patch B uses `omitempty`, which hides the option from the default config dump. Overall, Patch A is more usable and safer when enabled, and it is well tested.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
22m 28s15 turns
Wall-clock time in the container
Full test suite
No new failures
3,754 passed · 100 skipped · 3,856 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
14vs 167 merged
Changed lines, ours vs merged
The issue
View on GitHubThis is a security hardening issue.
In d6e6f9e500eebdeae8e28de830fff2e3bfc7d534 (unreleased) we added a dial-time URL validation.
If proxied (e.g. HTTPS_PROXY set on users machine or in CI) the above validation will only see the proxy address. There's no workaround for that, but I suggest by adding a new security.http.proxyFromEnvironment bool config flag (default disabled).
Thanks to @Reload3d for reporting this issue.
The two patches
config/security/securityConfig.goindex f7cc157b8..37ff02135 100644@@ -133,6 +133,12 @@ type HTTP struct { // Media types where the Content-Type in the response is used instead of resolving from the file content. MediaTypes Whitelist `json:"mediaTypes"`++ // ProxyFromEnvironment enables use of the HTTP_PROXY, HTTPS_PROXY and+ // NO_PROXY environment variables for remote HTTP requests.+ // This is disabled by default because a proxied request is dialed to the+ // proxy, so the dial-time address check would only see the proxy address.+ ProxyFromEnvironment bool `json:"proxyFromEnvironment,omitempty"` } // Node holds Node.js security settings.docs/content/en/configuration/security.mdindex 9efbc0148..141aa4f87 100644@@ -34,6 +34,9 @@ This is the default security configuration: `http.mediaTypes` : (`[]string`) Applicable to the `resources.GetRemote` function, a slice of [regular expressions](g) matching the `Content-Type` in HTTP responses that Hugo trusts, bypassing file content analysis for media type detection. +`http.proxyFromEnvironment`+: (`bool`) Whether the `resources.GetRemote` function should use the proxy configured in the `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables. When enabled, Hugo validates the address of the proxy instead of the address of the remote host. With the default `http.urls` setting, a proxy on a loopback or private address is refused, so you must also customize `http.urls`. Default is `false`.+ `http.urls` : (`[]string`) A slice of [regular expressions](g) matching the URLs that the `resources.GetRemote` function is allowed to access. resources/resource_factories/create/create.goindex a34f9fc7b..f8f7de869 100644@@ -156,6 +156,11 @@ func New(rs *resources.Spec) *Client { // reach internal endpoints. See CheckAllowedHTTPAddress. func newSecureBaseTransport(sec security.Config) http.RoundTripper { base := http.DefaultTransport.(*http.Transport).Clone()+ if !sec.HTTP.ProxyFromEnvironment {+ // A proxied request is dialed to the proxy, so the address check+ // below would only see the proxy address.+ base.Proxy = nil+ } d := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second,
config/security/securityConfig.goindex f7cc157b8..a2708288a 100644@@ -128,6 +128,12 @@ type HTTP struct { // URLs to allow in remote HTTP (resources.Get, getJSON, getCSV). URLs Whitelist `json:"urls"` + // Whether to honor the HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment+ // variables for remote HTTP. Off by default, as a proxy hides the+ // resolved destination address from the security policy.+ // <docsmeta>{ "newIn": "0.166.0" }</docsmeta>+ ProxyFromEnvironment bool `json:"proxyFromEnvironment"`+ // HTTP methods to allow. Methods Whitelist `json:"methods"` config/security/securityConfig_test.goindex 4ac059a67..e6f8c68e3 100644@@ -135,7 +135,7 @@ func TestToTOML(t *testing.T) { got := DefaultConfig.ToTOML() c.Assert(got, qt.Equals,- "[security]\n allowContent = ['! ^text/html$', '! ^text/org$']\n enableInlineShortcodes = false\n\n [security.exec]\n allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$']\n osEnv = ['(?i)^((HTTPS?|NO)_PROXY|PATH(EXT)?|APPDATA|TE?MP|TERM|GO\\w+|(XDG_CONFIG_)?HOME|USERPROFILE|SSH_AUTH_SOCK|DISPLAY|LANG|SYSTEMDRIVE|PROGRAMDATA)$']\n\n [security.funcs]\n getenv = ['^HUGO_', '^CI$']\n\n [security.http]\n methods = ['(?i)GET|POST']\n urls = ['(?i)^https?://[a-z0-9]', '! (?i)^https?://\\d+\\.', '! (?i)localhost', '! (?i)^https?://[^/?#]*@']\n\n [security.node]\n [security.node.permissions]\n allowAddons = ['tailwindcss']\n allowChildProcess = ['tailwindcss']\n allowRead = ['.']\n allowWorker = ['tailwindcss']\n allowWrite = []\n disable = false",+ "[security]\n allowContent = ['! ^text/html$', '! ^text/org$']\n enableInlineShortcodes = false\n\n [security.exec]\n allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$']\n osEnv = ['(?i)^((HTTPS?|NO)_PROXY|PATH(EXT)?|APPDATA|TE?MP|TERM|GO\\w+|(XDG_CONFIG_)?HOME|USERPROFILE|SSH_AUTH_SOCK|DISPLAY|LANG|SYSTEMDRIVE|PROGRAMDATA)$']\n\n [security.funcs]\n getenv = ['^HUGO_', '^CI$']\n\n [security.http]\n methods = ['(?i)GET|POST']\n proxyFromEnvironment = false\n urls = ['(?i)^https?://[a-z0-9]', '! (?i)^https?://\\d+\\.', '! (?i)localhost', '! (?i)^https?://[^/?#]*@']\n\n [security.node]\n [security.node.permissions]\n allowAddons = ['tailwindcss']\n allowChildProcess = ['tailwindcss']\n allowRead = ['.']\n allowWorker = ['tailwindcss']\n allowWrite = []\n disable = false", ) } @@ -156,6 +156,7 @@ func TestDecodeConfigDefault(t *testing.T) { c.Assert(pc.HTTP.Methods.Accept("get"), qt.IsTrue) c.Assert(pc.HTTP.Methods.Accept("DELETE"), qt.IsFalse) c.Assert(pc.HTTP.MediaTypes.Accept("application/msword"), qt.IsFalse)+ c.Assert(pc.HTTP.ProxyFromEnvironment, qt.IsFalse) c.Assert(pc.Exec.OsEnv.Accept("PATH"), qt.IsTrue) c.Assert(pc.Exec.OsEnv.Accept("GOROOT"), qt.IsTrue)docs/content/en/configuration/security.mdindex 9efbc0148..54ebb4521 100644@@ -34,9 +34,15 @@ This is the default security configuration: `http.mediaTypes` : (`[]string`) Applicable to the `resources.GetRemote` function, a slice of [regular expressions](g) matching the `Content-Type` in HTTP responses that Hugo trusts, bypassing file content analysis for media type detection. +`http.proxyFromEnvironment`+: {{< new-in 0.166.0 />}}+: (`bool`) Whether the `resources.GetRemote` function honors the `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variables. Default is `false`. When a proxy is used, Hugo connects to the proxy rather than the destination, so the resolved address validation described under `http.urls` does not apply.+ `http.urls` : (`[]string`) A slice of [regular expressions](g) matching the URLs that the `resources.GetRemote` function is allowed to access. + The default allowlist denies URLs with an IP address or `localhost` as the host name. In addition, with the default allowlist, Hugo validates the resolved address when connecting and rejects connections to loopback, private, link-local, and other non-public addresses, e.g. a host name that resolves to a cloud metadata endpoint. This validation is disabled if you override `http.urls`, as the override may intentionally allow access to hosts on your local network, such as a development server.+ `node.permissions.disable` : {{< new-in 0.161.0 />}} : (`bool`) Whether to disable the Node.js [permission model][]. When `false`, Hugo runs Node.js tools with the `--permission` flag, restricting their file system and resource access to what is explicitly allowed below. Default is `false`.docs/data/docs.yamlindex e51915c49..6d2578339 100644@@ -2253,6 +2253,7 @@ config: mediaTypes: null methods: - (?i)GET|POST+ proxyFromEnvironment: false urls: - (?i)^https?://[a-z0-9] - '! (?i)^https?://\d+\.'resources/resource_factories/create/create.goindex a34f9fc7b..1570c2648 100644@@ -16,9 +16,11 @@ package create import (+ "context" "errors" "net" "net/http"+ "net/url" "os" "path" "path/filepath"@@ -26,6 +28,8 @@ import ( "syscall" "time" + "golang.org/x/net/http/httpproxy"+ "github.com/bep/helpers/contexthelpers" "github.com/bep/logg"@@ -65,6 +69,7 @@ type contextKey uint8 const ( contextKeyResourceID contextKey = iota+ contextKeyProxyURL ) // New creates a new Client with the given specification.@@ -154,17 +159,80 @@ func New(rs *resources.Spec) *Client { // hook that validates the resolved destination address against the security // policy, so a hostname resolving to an internal address cannot be used to // reach internal endpoints. See CheckAllowedHTTPAddress.+//+// Proxies are only used when security.http.proxyFromEnvironment is enabled.+// A proxied connection is dialed to the proxy, not the destination, so the+// address check is skipped for that dial; the user has opted into trusting+// the proxy with the destination. The proxy decision is carried per request+// in its context, so a direct dial is always validated, even to the proxy's+// own address. func newSecureBaseTransport(sec security.Config) http.RoundTripper { base := http.DefaultTransport.(*http.Transport).Clone()- d := &net.Dialer{+ secureDialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, Control: func(network, address string, _ syscall.RawConn) error { return sec.CheckAllowedHTTPAddress(network, address) }, }- base.DialContext = d.DialContext- return base+ base.Proxy = nil+ base.DialContext = secureDialer.DialContext++ if !sec.HTTP.ProxyFromEnvironment {+ return base+ }++ proxyFromCtx := func(ctx context.Context) *url.URL {+ u, _ := ctx.Value(contextKeyProxyURL).(*url.URL)+ return u+ }+ base.Proxy = func(req *http.Request) (*url.URL, error) {+ return proxyFromCtx(req.Context()), nil+ }+ proxyDialer := &net.Dialer{Timeout: secureDialer.Timeout, KeepAlive: secureDialer.KeepAlive}+ base.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {+ if u := proxyFromCtx(ctx); u != nil && canonicalAddr(u) == addr {+ return proxyDialer.DialContext(ctx, network, addr)+ }+ return secureDialer.DialContext(ctx, network, addr)+ }+ return &proxyTransport{base: base, proxyFunc: httpproxy.FromEnvironment().ProxyFunc()}+}++// proxyTransport resolves the proxy for each request up front and stores it+// in the request context, where both the Proxy and DialContext hooks of base+// can see it.+type proxyTransport struct {+ base *http.Transport+ proxyFunc func(*url.URL) (*url.URL, error)+}++func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {+ u, err := t.proxyFunc(req.URL)+ if err != nil {+ return nil, err+ }+ if u != nil {+ req = req.WithContext(context.WithValue(req.Context(), contextKeyProxyURL, u))+ }+ return t.base.RoundTrip(req)+}++// canonicalAddr mirrors net/http's canonicalAddr: the host:port the transport+// dials for u, with the port defaulted from the scheme.+func canonicalAddr(u *url.URL) string {+ port := u.Port()+ if port == "" {+ switch u.Scheme {+ case "http":+ port = "80"+ case "https":+ port = "443"+ case "socks5", "socks5h":+ port = "1080"+ }+ }+ return net.JoinHostPort(u.Hostname(), port) } // Copy copies r to the new targetPath.resources/resource_factories/create/remote_test.goindex f9830c842..d7bb98361 100644@@ -14,8 +14,10 @@ package create import (+ "io" "net/http" "net/http/httptest"+ "sync/atomic" "testing" qt "github.com/frankban/quicktest"@@ -173,3 +175,78 @@ urls = ['.*'] c.Assert(err, qt.IsNil) resp.Body.Close() }++// Proxies from the environment are ignored unless+// security.http.proxyFromEnvironment is enabled. When enabled, the dial-time+// address check does not apply to the proxy itself. See issue 15302.+func TestSecureBaseTransportProxyFromEnvironment(t *testing.T) {+ c := qt.New(t)++ var proxyHits, targetHits atomic.Int32+ proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ proxyHits.Add(1)+ w.Write([]byte("via proxy"))+ }))+ t.Cleanup(proxy.Close)+ target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ targetHits.Add(1)+ w.Write([]byte("direct"))+ }))+ t.Cleanup(target.Close)++ t.Setenv("HTTP_PROXY", proxy.URL)+ t.Setenv("NO_PROXY", "")+ t.Setenv("no_proxy", "")++ decode := func(toml string) security.Config {+ sec, err := security.DecodeConfig(config.FromTOMLConfigString(toml))+ c.Assert(err, qt.IsNil)+ return sec+ }+ fetch := func(sec security.Config, u string) (string, error) {+ req, err := http.NewRequest("GET", u, nil)+ c.Assert(err, qt.IsNil)+ resp, err := newSecureBaseTransport(sec).RoundTrip(req)+ if err != nil {+ return "", err+ }+ defer resp.Body.Close()+ b, err := io.ReadAll(resp.Body)+ c.Assert(err, qt.IsNil)+ return string(b), nil+ }++ // Default: proxy ignored, fetched directly.+ body, err := fetch(decode(`+[security.http]+urls = ['.*']+`), target.URL)+ c.Assert(err, qt.IsNil)+ c.Assert(body, qt.Equals, "direct")+ c.Assert(proxyHits.Load(), qt.Equals, int32(0))+ c.Assert(targetHits.Load(), qt.Equals, int32(1))++ // Enabled: the loopback proxy is dialed even under the default hardened+ // policy, and the destination is never dialed directly.+ body, err = fetch(decode(`+[security.http]+proxyFromEnvironment = true+`), "http://example.com/")+ c.Assert(err, qt.IsNil)+ c.Assert(body, qt.Equals, "via proxy")+ c.Assert(proxyHits.Load(), qt.Equals, int32(1))+ c.Assert(targetHits.Load(), qt.Equals, int32(1))++ // Enabled, but loopback destinations are never proxied: the address+ // check still applies to the direct dial, also when the destination is+ // the proxy's own address.+ for _, u := range []string{target.URL, proxy.URL} {+ _, err = fetch(decode(`+[security.http]+proxyFromEnvironment = true+`), u)+ c.Assert(security.IsAccessDenied(err), qt.IsTrue, qt.Commentf(u))+ }+ c.Assert(proxyHits.Load(), qt.Equals, int32(1))+ c.Assert(targetHits.Load(), qt.Equals, int32(1))+}
What the agent reported
I added the security.http.proxyFromEnvironment option, off by default, but none of it has been tested: this sandbox has no network and no Go module cache, so the affected packages don't build here. I wrote regression tests, but since I never saw them compile or pass, I removed them as the task rules require. The final diff therefore contains no test, even though the rules require one; I couldn't meet both rules here.
What was wrong: the remote-fetch transport (newSecureBaseTransport in resources/resource_factories/create/create.go) is a copy of Go's default HTTP transport, which uses whatever proxy HTTP_PROXY/HTTPS_PROXY/NO_PROXY point to. When one of those is set, Hugo connects to the proxy, so the new check on the connection address saw only the proxy's address and never the real remote host. That let a proxied request get past the check for internal addresses.
What I changed:
config/security/securityConfig.go: new fieldProxyFromEnvironment bool \json:"proxyFromEnvironment,omitempty"\`inHTTP, defaulting tofalse. It is set with[security.http] proxyFromEnvironment = trueorHUGO_SECURITY_HTTP_PROXYFROMENVIRONMENT=true. Theomitemptykeeps the default printed security config unchanged, soTestToTOML` keeps its current expected output.create.go:newSecureBaseTransportturns off the environment proxy (base.Proxy = nil) unless the option is set. Without a proxy, the address check sees the real destination.docs/content/en/configuration/security.md: documentedhttp.proxyFromEnvironment, including the limit below.
Test evidence: ./bt-test ./config/security/ ./resources/resource_factories/create/ reported [setup failed] for both packages because Go couldn't download modules (lookup proxy.golang.org ... server misbehaving). No test compiled, old or new. I considered restoring the tests to the diff, but the rules say to drop tests you haven't seen pass, so they're gone. They were:
- Transport: with the default config the transport has no proxy; with the option on, it uses
http.ProxyFromEnvironment. - Config: decoding
proxyFromEnvironment = trueworks, and the default isfalse.
These need to be re-added and run once dependencies are available. A reviewer subagent read the change and found it correct: the proxy is dropped by default, the setting decodes from config and from the env var, and no other remote-fetch code was missed.
Things to know:
- Breaking change: anyone using
resources.GetRemotebehindHTTPS_PROXYtoday will bypass the proxy after upgrading until they turn the option on. The issue asks for this, but it needs a release note. - Proxies on private addresses: with the option on, the address check still runs against the proxy. Under the default
security.http.urls, a proxy on a loopback or private address (for example 127.0.0.1:3128 or 10.x) is refused. I kept this strict behaviour rather than weaken the check, and the docs tell users they must then also customizesecurity.http.urls. Skipping the check whenever a proxy is used would be the alternative.
Requirements:
- Add
security.http.proxyFromEnvironmentas a bool: implemented insecurityConfig.go; the reviewer confirmed it decodes, but no test has run. - Default disabled: implemented (the default is
false); the test for this was removed because it couldn't run. - Stop the environment proxy from hiding the real address from the check: implemented in
newSecureBaseTransport; the reviewer confirmed the logic, but no test has run. - Document the option: done.
AGENTS.md and CLAUDE.md show as deleted, but they were already deleted before this session and I left them alone. Nothing was committed.