| 1 |
// Copyright 2021 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package create |
| 15 |
|
| 16 |
import ( |
| 17 |
"io" |
| 18 |
"net/http" |
| 19 |
"net/http/httptest" |
| 20 |
"sync/atomic" |
| 21 |
"testing" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
|
| 25 |
"github.com/gohugoio/hugo/config" |
| 26 |
"github.com/gohugoio/hugo/config/security" |
| 27 |
) |
| 28 |
|
| 29 |
func TestDecodeRemoteOptions(t *testing.T) { |
| 30 |
t.Parallel() |
| 31 |
|
| 32 |
c := qt.New(t) |
| 33 |
|
| 34 |
for _, test := range []struct { |
| 35 |
name string |
| 36 |
args map[string]any |
| 37 |
want fromRemoteOptions |
| 38 |
wantErr bool |
| 39 |
}{ |
| 40 |
{ |
| 41 |
"POST", |
| 42 |
map[string]any{ |
| 43 |
"meThod": "PoST", |
| 44 |
"headers": map[string]any{ |
| 45 |
"foo": "bar", |
| 46 |
}, |
| 47 |
}, |
| 48 |
fromRemoteOptions{ |
| 49 |
Method: "POST", |
| 50 |
Headers: map[string]any{ |
| 51 |
"foo": "bar", |
| 52 |
}, |
| 53 |
}, |
| 54 |
false, |
| 55 |
}, |
| 56 |
{ |
| 57 |
"Body", |
| 58 |
map[string]any{ |
| 59 |
"meThod": "POST", |
| 60 |
"body": []byte("foo"), |
| 61 |
}, |
| 62 |
fromRemoteOptions{ |
| 63 |
Method: "POST", |
| 64 |
Body: []byte("foo"), |
| 65 |
}, |
| 66 |
false, |
| 67 |
}, |
| 68 |
{ |
| 69 |
"Body, string", |
| 70 |
map[string]any{ |
| 71 |
"meThod": "POST", |
| 72 |
"body": "foo", |
| 73 |
}, |
| 74 |
fromRemoteOptions{ |
| 75 |
Method: "POST", |
| 76 |
Body: []byte("foo"), |
| 77 |
}, |
| 78 |
false, |
| 79 |
}, |
| 80 |
} { |
| 81 |
c.Run(test.name, func(c *qt.C) { |
| 82 |
got, err := decodeRemoteOptions(test.args) |
| 83 |
isErr := qt.IsNil |
| 84 |
if test.wantErr { |
| 85 |
isErr = qt.IsNotNil |
| 86 |
} |
| 87 |
|
| 88 |
c.Assert(err, isErr) |
| 89 |
c.Assert(got, qt.DeepEquals, test.want) |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestOptionsNewRequest(t *testing.T) { |
| 95 |
t.Parallel() |
| 96 |
|
| 97 |
c := qt.New(t) |
| 98 |
|
| 99 |
opts := fromRemoteOptions{ |
| 100 |
Method: "GET", |
| 101 |
Body: []byte("foo"), |
| 102 |
} |
| 103 |
|
| 104 |
req, err := opts.NewRequest("https://example.com/api") |
| 105 |
|
| 106 |
c.Assert(err, qt.IsNil) |
| 107 |
c.Assert(req.Method, qt.Equals, "GET") |
| 108 |
c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"Hugo Static Site Generator"}) |
| 109 |
|
| 110 |
opts = fromRemoteOptions{ |
| 111 |
Method: "GET", |
| 112 |
Body: []byte("foo"), |
| 113 |
Headers: map[string]any{ |
| 114 |
"User-Agent": "foo", |
| 115 |
}, |
| 116 |
} |
| 117 |
|
| 118 |
req, err = opts.NewRequest("https://example.com/api") |
| 119 |
|
| 120 |
c.Assert(err, qt.IsNil) |
| 121 |
c.Assert(req.Method, qt.Equals, "GET") |
| 122 |
c.Assert(req.Header["User-Agent"], qt.DeepEquals, []string{"foo"}) |
| 123 |
} |
| 124 |
|
| 125 |
func TestRemoteResourceKeys(t *testing.T) { |
| 126 |
t.Parallel() |
| 127 |
|
| 128 |
c := qt.New(t) |
| 129 |
|
| 130 |
check := func(uri string, optionsm map[string]any, expect1, expect2 string) { |
| 131 |
c.Helper() |
| 132 |
got1, got2 := remoteResourceKeys(uri, optionsm) |
| 133 |
c.Assert(got1, qt.Equals, expect1) |
| 134 |
c.Assert(got2, qt.Equals, expect2) |
| 135 |
} |
| 136 |
|
| 137 |
check("foo", nil, "7763396052142361238", "7763396052142361238") |
| 138 |
check("foo", map[string]any{"bar": "baz"}, "5783339285578751849", "5783339285578751849") |
| 139 |
check("foo", map[string]any{"key": "1234", "bar": "baz"}, "15578353952571222948", "5783339285578751849") |
| 140 |
check("foo", map[string]any{"key": "12345", "bar": "baz"}, "14335752410685132726", "5783339285578751849") |
| 141 |
check("asdf", map[string]any{"key": "1234", "bar": "asdf"}, "15578353952571222948", "15615023578599429261") |
| 142 |
check("asdf", map[string]any{"key": "12345", "bar": "asdf"}, "14335752410685132726", "15615023578599429261") |
| 143 |
} |
| 144 |
|
| 145 |
// The transport used for remote fetches must refuse to connect to an internal |
| 146 |
// (here loopback) address under the default security policy, even though the |
| 147 |
// URL text check happens elsewhere. See CVE-2026-10582. |
| 148 |
func TestSecureBaseTransportBlocksInternalAddress(t *testing.T) { |
| 149 |
t.Parallel() |
| 150 |
|
| 151 |
c := qt.New(t) |
| 152 |
|
| 153 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 154 |
w.Write([]byte("secret")) |
| 155 |
})) |
| 156 |
t.Cleanup(srv.Close) |
| 157 |
|
| 158 |
req, err := http.NewRequest("GET", srv.URL, nil) |
| 159 |
c.Assert(err, qt.IsNil) |
| 160 |
|
| 161 |
// Default policy: the loopback address the httptest server listens on is |
| 162 |
// refused at dial time. |
| 163 |
_, err = newSecureBaseTransport(security.DefaultConfig).RoundTrip(req) |
| 164 |
c.Assert(err, qt.IsNotNil) |
| 165 |
c.Assert(security.IsAccessDenied(err), qt.IsTrue) |
| 166 |
|
| 167 |
// Customized policy: the user has opted into their own hosts, so the dial |
| 168 |
// check stands down and the fetch succeeds. |
| 169 |
sec, err := security.DecodeConfig(config.FromTOMLConfigString(` |
| 170 |
[security.http] |
| 171 |
urls = ['.*'] |
| 172 |
`)) |
| 173 |
c.Assert(err, qt.IsNil) |
| 174 |
resp, err := newSecureBaseTransport(sec).RoundTrip(req) |
| 175 |
c.Assert(err, qt.IsNil) |
| 176 |
resp.Body.Close() |
| 177 |
} |
| 178 |
|
| 179 |
// Proxies from the environment are ignored unless |
| 180 |
// security.http.proxyFromEnvironment is enabled. When enabled, the dial-time |
| 181 |
// address check does not apply to the proxy itself. See issue 15302. |
| 182 |
func TestSecureBaseTransportProxyFromEnvironment(t *testing.T) { |
| 183 |
c := qt.New(t) |
| 184 |
|
| 185 |
var proxyHits, targetHits atomic.Int32 |
| 186 |
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 187 |
proxyHits.Add(1) |
| 188 |
w.Write([]byte("via proxy")) |
| 189 |
})) |
| 190 |
t.Cleanup(proxy.Close) |
| 191 |
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 192 |
targetHits.Add(1) |
| 193 |
w.Write([]byte("direct")) |
| 194 |
})) |
| 195 |
t.Cleanup(target.Close) |
| 196 |
|
| 197 |
t.Setenv("HTTP_PROXY", proxy.URL) |
| 198 |
t.Setenv("NO_PROXY", "") |
| 199 |
t.Setenv("no_proxy", "") |
| 200 |
|
| 201 |
decode := func(toml string) security.Config { |
| 202 |
sec, err := security.DecodeConfig(config.FromTOMLConfigString(toml)) |
| 203 |
c.Assert(err, qt.IsNil) |
| 204 |
return sec |
| 205 |
} |
| 206 |
fetch := func(sec security.Config, u string) (string, error) { |
| 207 |
req, err := http.NewRequest("GET", u, nil) |
| 208 |
c.Assert(err, qt.IsNil) |
| 209 |
resp, err := newSecureBaseTransport(sec).RoundTrip(req) |
| 210 |
if err != nil { |
| 211 |
return "", err |
| 212 |
} |
| 213 |
defer resp.Body.Close() |
| 214 |
b, err := io.ReadAll(resp.Body) |
| 215 |
c.Assert(err, qt.IsNil) |
| 216 |
return string(b), nil |
| 217 |
} |
| 218 |
|
| 219 |
// Default: proxy ignored, fetched directly. |
| 220 |
body, err := fetch(decode(` |
| 221 |
[security.http] |
| 222 |
urls = ['.*'] |
| 223 |
`), target.URL) |
| 224 |
c.Assert(err, qt.IsNil) |
| 225 |
c.Assert(body, qt.Equals, "direct") |
| 226 |
c.Assert(proxyHits.Load(), qt.Equals, int32(0)) |
| 227 |
c.Assert(targetHits.Load(), qt.Equals, int32(1)) |
| 228 |
|
| 229 |
// Enabled: the loopback proxy is dialed even under the default hardened |
| 230 |
// policy, and the destination is never dialed directly. |
| 231 |
body, err = fetch(decode(` |
| 232 |
[security.http] |
| 233 |
proxyFromEnvironment = true |
| 234 |
`), "http://example.com/") |
| 235 |
c.Assert(err, qt.IsNil) |
| 236 |
c.Assert(body, qt.Equals, "via proxy") |
| 237 |
c.Assert(proxyHits.Load(), qt.Equals, int32(1)) |
| 238 |
c.Assert(targetHits.Load(), qt.Equals, int32(1)) |
| 239 |
|
| 240 |
// Enabled, but loopback destinations are never proxied: the address |
| 241 |
// check still applies to the direct dial, also when the destination is |
| 242 |
// the proxy's own address. |
| 243 |
for _, u := range []string{target.URL, proxy.URL} { |
| 244 |
_, err = fetch(decode(` |
| 245 |
[security.http] |
| 246 |
proxyFromEnvironment = true |
| 247 |
`), u) |
| 248 |
c.Assert(security.IsAccessDenied(err), qt.IsTrue, qt.Commentf(u)) |
| 249 |
} |
| 250 |
c.Assert(proxyHits.Load(), qt.Equals, int32(1)) |
| 251 |
c.Assert(targetHits.Load(), qt.Equals, int32(1)) |
| 252 |
} |