Backtestshugorun-20260922-141813#15302
Add config option security.http.proxyFromEnvironment = false
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both add the `proxyFromEnvironment` flag defaulting off and both correctly disable the environment proxy in the default case, so the default hardening is equivalent. The decisive difference is the opt-in path. Patch A, when the flag is enabled, leaves the cloned transport's `base.Proxy = http.ProxyFromEnvironment` but keeps the same `Control`/`CheckAllowedHTTPAddress` dialer for every dial. Since a proxied request is dialed to the proxy address, the dial-time address check then runs against the *proxy*, so a proxy on a loopback or private address is rejected. That is exactly the scenario the issue names ('HTTPS_PROXY set on users machine or in CI') — a localhost/internal proxy — so Patch A's opt-in fails to actually let those users through under the default allowlist; it re-enables the proxy but re-blocks it. Patch B builds a per-request proxy resolution (proxyTransport + context-carried proxy URL + canonicalAddr) so the dial to the proxy bypasses the address check (the user has opted into trusting the proxy), while direct/NO_PROXY dials — and even a direct dial to the proxy's own address — remain validated, closing the obvious hole. This resolves the root cause for the described case. Tests: Patch A only asserts `tr.Proxy == nil`/`!= nil`, a near-tautological field check that would not catch the loopback-proxy failure; Patch B stands up real httptest proxy and target servers and asserts default-ignored, enabled-via-loopback-proxy, and still-denied direct loopback dials — tests that genuinely fail before and pass after and exercise the security boundary. Patch B is larger and adds user-facing docs (security.md, docs.yaml) and an httpproxy dependency, but the docs are appropriate for a new security config option and the complexity is proportionate to correctly handling internal proxies. Patch A is more surgical and lacks docs, but its incompleteness on the core scenario outweighs its smaller footprint. B wins on correctness and tests.
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
33m 11s13 turns
Wall-clock time in the container
Full test suite
No new failures
3,752 passed · 100 skipped · 3,858 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
85vs 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..5b06a0825 100644@@ -133,6 +133,13 @@ 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 lets remote HTTP requests use the proxy settings+ // from the environment (HTTP_PROXY, HTTPS_PROXY, NO_PROXY). Disabled by+ // default: a proxy hides the resolved destination address from the+ // dial-time address check (see CheckAllowedHTTPAddress), so honoring the+ // environment proxy would silently bypass that protection.+ ProxyFromEnvironment bool `json:"proxyFromEnvironment"` } // Node holds Node.js security settings.config/security/securityConfig_test.goindex 4ac059a67..cc1ea997a 100644@@ -135,10 +135,31 @@ 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", ) } +func TestDecodeConfigHTTPProxyFromEnvironment(t *testing.T) {+ t.Parallel()+ c := qt.New(t)++ // Default: the environment proxy is not honored.+ pc, err := DecodeConfig(config.New())+ c.Assert(err, qt.IsNil)+ c.Assert(pc.HTTP.ProxyFromEnvironment, qt.IsFalse)++ // Opt-in via config.+ tomlConfig := `+[security.http]+proxyFromEnvironment = true+`+ cfg, err := config.FromConfigString(tomlConfig, "toml")+ c.Assert(err, qt.IsNil)+ pc, err = DecodeConfig(cfg)+ c.Assert(err, qt.IsNil)+ c.Assert(pc.HTTP.ProxyFromEnvironment, qt.IsTrue)+}+ func TestDecodeConfigDefault(t *testing.T) { t.Parallel() c := qt.New(t)resources/resource_factories/create/create.goindex a34f9fc7b..576ad1fce 100644@@ -156,6 +156,13 @@ 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 proxy terminates the connection at the proxy address, so the+ // dial-time address check below would only ever see the proxy and not+ // the real destination. Ignore any environment proxy unless the user+ // has explicitly opted in.+ base.Proxy = nil+ } d := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second,resources/resource_factories/create/proxy_test.gonew file mode 100644index 000000000..65b092ecc@@ -0,0 +1,48 @@+// Copyright 2021 The Hugo Authors. All rights reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package create++import (+ "net/http"+ "testing"++ qt "github.com/frankban/quicktest"++ "github.com/gohugoio/hugo/config/security"+)++// TestNewSecureBaseTransportProxyFromEnvironment guards the default value of the+// security.http.proxyFromEnvironment option: by default the secure base+// transport must ignore any environment proxy (Proxy is nil), and setting the+// option to true must re-enable it (Proxy is non-nil).+func TestNewSecureBaseTransportProxyFromEnvironment(t *testing.T) {+ t.Parallel()++ c := qt.New(t)++ // Default hardened behavior: the environment proxy is ignored.+ sec := security.DefaultConfig+ c.Assert(sec.HTTP.ProxyFromEnvironment, qt.IsFalse)++ tr, ok := newSecureBaseTransport(sec).(*http.Transport)+ c.Assert(ok, qt.IsTrue)+ c.Assert(tr.Proxy == nil, qt.IsTrue)++ // Opt-in: the environment proxy is honored again.+ sec.HTTP.ProxyFromEnvironment = true++ tr, ok = newSecureBaseTransport(sec).(*http.Transport)+ c.Assert(ok, qt.IsTrue)+ c.Assert(tr.Proxy != nil, qt.IsTrue)+}
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
The fix is complete, reviewed, and the gate passes. Here is the final report.
What was wrong
Commit d6e6f9e5 (unreleased) added a dial-time destination-address check (CheckAllowedHTTPAddress, installed as a net.Dialer Control hook in newSecureBaseTransport) so that a hostname resolving to an internal address can't reach internal endpoints via resources.GetRemote. But Hugo's HTTP transport is a clone of http.DefaultTransport, whose Proxy is http.ProxyFromEnvironment. When a proxy is configured in the environment (HTTPS_PROXY, etc., common in CI), the dialer connects to the proxy address, so the dial-time check only ever validates the proxy — silently bypassing the protection with no workaround.
What I changed
config/security/securityConfig.go— Added a new field to theHTTPstruct:go ProxyFromEnvironment bool `json:"proxyFromEnvironment"`with a doc comment. Default is the zero valuefalse(DefaultConfigunchanged), so the environment proxy is disabled by default andDecodeConfigpicks it up automatically.
resources/resource_factories/create/create.go— InnewSecureBaseTransport, gated the cloned transport's proxy:go if !sec.HTTP.ProxyFromEnvironment { base.Proxy = nil }By default this forces direct dials, so the dial-time address check sees the real destination. Setting the flag re-enables the environment proxy for users who need it.
Test evidence
resources/resource_factories/create/proxy_test.go(new) —TestNewSecureBaseTransportProxyFromEnvironment: with default config assertstr.Proxy == nil; with the flag set assertstr.Proxy != nil. The default-case assertion fails on pre-change code (whereProxywashttp.ProxyFromEnvironment, non-nil), so it is a genuine regression guard for the exact reported scenario.config/security/securityConfig_test.go— addedTestDecodeConfigHTTPProxyFromEnvironment(default false; true when set via TOML); updated theTestToTOMLgolden string to includeproxyFromEnvironment = falsein alphabetical order (mechanical, no existing assertion weakened).- Gate:
./bt-test ./config/security/... ./resources/resource_factories/create/...→ exit 0. All new and pre-existing tests pass. - Reviewer confirmed correct implementation, default-safe posture, meaningful test, no scope creep or scratch files.
Requirements disposition
- Add
security.http.proxyFromEnvironmentbool config flag — ✅ Implemented (securityConfig.go), verified byTestDecodeConfigHTTPProxyFromEnvironmentandTestToTOML. - Default disabled (false) — ✅ Implemented (zero value; not present in
DefaultConfig), verified by default-case assertions in both new tests. - When disabled, the environment proxy must not be honored (so the dial-time check isn't bypassed) — ✅ Implemented (
create.gosetsbase.Proxy = nil), verified byTestNewSecureBaseTransportProxyFromEnvironment(defaultProxy == nil; opt-inProxy != nil).
Note: AGENTS.md/CLAUDE.md show as deleted in git status — pre-existing environment state, not part of this change. All changes are left uncommitted in the working tree.