Debian Codegraph @

config/security/securityConfig_test.go

raw
1 // Copyright 2018 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 security
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/config"
21 )
22
23 func TestDecodeConfigFromTOML(t *testing.T) {
24 c := qt.New(t)
25
26 c.Run("Slice whitelist", func(c *qt.C) {
27 c.Parallel()
28 tomlConfig := `
29
30
31 someOtherValue = "bar"
32
33 [security]
34 enableInlineShortcodes=true
35 [security.exec]
36 allow=["a", "b"]
37 osEnv=["a", "b", "c"]
38 [security.funcs]
39 getEnv=["a", "b"]
40
41 `
42
43 cfg, err := config.FromConfigString(tomlConfig, "toml")
44 c.Assert(err, qt.IsNil)
45
46 pc, err := DecodeConfig(cfg)
47 c.Assert(err, qt.IsNil)
48 c.Assert(pc, qt.Not(qt.IsNil))
49 c.Assert(pc.EnableInlineShortcodes, qt.IsTrue)
50 c.Assert(pc.Exec.Allow.Accept("a"), qt.IsTrue)
51 c.Assert(pc.Exec.Allow.Accept("d"), qt.IsFalse)
52 c.Assert(pc.Exec.OsEnv.Accept("a"), qt.IsTrue)
53 c.Assert(pc.Exec.OsEnv.Accept("e"), qt.IsFalse)
54 c.Assert(pc.Funcs.Getenv.Accept("a"), qt.IsTrue)
55 c.Assert(pc.Funcs.Getenv.Accept("c"), qt.IsFalse)
56 })
57
58 c.Run("String whitelist", func(c *qt.C) {
59 c.Parallel()
60 tomlConfig := `
61
62
63 someOtherValue = "bar"
64
65 [security]
66 [security.exec]
67 allow="a"
68 osEnv="b"
69
70 `
71
72 cfg, err := config.FromConfigString(tomlConfig, "toml")
73 c.Assert(err, qt.IsNil)
74
75 pc, err := DecodeConfig(cfg)
76 c.Assert(err, qt.IsNil)
77 c.Assert(pc, qt.Not(qt.IsNil))
78 c.Assert(pc.Exec.Allow.Accept("a"), qt.IsTrue)
79 c.Assert(pc.Exec.Allow.Accept("d"), qt.IsFalse)
80 c.Assert(pc.Exec.OsEnv.Accept("b"), qt.IsTrue)
81 c.Assert(pc.Exec.OsEnv.Accept("e"), qt.IsFalse)
82 })
83
84 c.Run("Default exec.osEnv", func(c *qt.C) {
85 c.Parallel()
86 tomlConfig := `
87
88
89 someOtherValue = "bar"
90
91 [security]
92 [security.exec]
93 allow="a"
94
95 `
96
97 cfg, err := config.FromConfigString(tomlConfig, "toml")
98 c.Assert(err, qt.IsNil)
99
100 pc, err := DecodeConfig(cfg)
101 c.Assert(err, qt.IsNil)
102 c.Assert(pc, qt.Not(qt.IsNil))
103 c.Assert(pc.Exec.Allow.Accept("a"), qt.IsTrue)
104 c.Assert(pc.Exec.OsEnv.Accept("PATH"), qt.IsTrue)
105 c.Assert(pc.Exec.OsEnv.Accept("e"), qt.IsFalse)
106 })
107
108 c.Run("Enable inline shortcodes, legacy", func(c *qt.C) {
109 c.Parallel()
110 tomlConfig := `
111
112
113 someOtherValue = "bar"
114 enableInlineShortcodes=true
115
116 [security]
117 [security.exec]
118 allow="a"
119 osEnv="b"
120
121 `
122
123 cfg, err := config.FromConfigString(tomlConfig, "toml")
124 c.Assert(err, qt.IsNil)
125
126 pc, err := DecodeConfig(cfg)
127 c.Assert(err, qt.IsNil)
128 c.Assert(pc.EnableInlineShortcodes, qt.IsTrue)
129 })
130 }
131
132 func TestToTOML(t *testing.T) {
133 c := qt.New(t)
134
135 got := DefaultConfig.ToTOML()
136
137 c.Assert(got, qt.Equals,
138 "[security]\n allowContent = ['! ^text/html$']\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]', '! ^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",
139 )
140 }
141
142 func TestDecodeConfigDefault(t *testing.T) {
143 t.Parallel()
144 c := qt.New(t)
145
146 pc, err := DecodeConfig(config.New())
147 c.Assert(err, qt.IsNil)
148 c.Assert(pc, qt.Not(qt.IsNil))
149 c.Assert(pc.Exec.Allow.Accept("a"), qt.IsFalse)
150 c.Assert(pc.Exec.Allow.Accept("node"), qt.IsTrue)
151 c.Assert(pc.Exec.Allow.Accept("npx"), qt.IsFalse)
152
153 c.Assert(pc.HTTP.URLs.Accept("https://example.org"), qt.IsTrue)
154 c.Assert(pc.HTTP.Methods.Accept("POST"), qt.IsTrue)
155 c.Assert(pc.HTTP.Methods.Accept("GET"), qt.IsTrue)
156 c.Assert(pc.HTTP.Methods.Accept("get"), qt.IsTrue)
157 c.Assert(pc.HTTP.Methods.Accept("DELETE"), qt.IsFalse)
158 c.Assert(pc.HTTP.MediaTypes.Accept("application/msword"), qt.IsFalse)
159
160 c.Assert(pc.Exec.OsEnv.Accept("PATH"), qt.IsTrue)
161 c.Assert(pc.Exec.OsEnv.Accept("GOROOT"), qt.IsTrue)
162 c.Assert(pc.Exec.OsEnv.Accept("HOME"), qt.IsTrue)
163 c.Assert(pc.Exec.OsEnv.Accept("SSH_AUTH_SOCK"), qt.IsTrue)
164 c.Assert(pc.Exec.OsEnv.Accept("a"), qt.IsFalse)
165 c.Assert(pc.Exec.OsEnv.Accept("e"), qt.IsFalse)
166 c.Assert(pc.Exec.OsEnv.Accept("MYSECRET"), qt.IsFalse)
167
168 c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue)
169 c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"."})
170 c.Assert(pc.Node.Permissions.AllowWrite, qt.DeepEquals, []string{})
171 }
172
173 func TestCheckAllowedHTTPURLHardenedDefaultsIssue14792(t *testing.T) {
174 t.Parallel()
175 c := qt.New(t)
176
177 c.Run("Public URLs allowed by default", func(c *qt.C) {
178 c.Parallel()
179 pc, err := DecodeConfig(config.New())
180 c.Assert(err, qt.IsNil)
181 for _, u := range []string{
182 "https://example.org/",
183 "https://example.org:8443/foo",
184 "https://sub.example.org/path",
185 } {
186 c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u))
187 }
188 })
189
190 c.Run("Private/loopback URLs denied by default", func(c *qt.C) {
191 c.Parallel()
192 pc, err := DecodeConfig(config.New())
193 c.Assert(err, qt.IsNil)
194 for _, u := range []string{
195 "http://localhost/",
196 "http://LOCALHOST:8080/",
197 "http://foo.localhost/",
198 "http://127.0.0.1/",
199 "http://127.1.2.3:8080/x",
200 "http://user:[email protected]/", // userinfo must not sneak past the deny.
201 "http://10.0.0.1/",
202 "http://172.16.0.1/",
203 "http://192.168.1.1/",
204 "http://169.254.169.254/latest/meta-data/", // AWS/GCP metadata.
205 "http://0.0.0.0/",
206 "http://[::1]/",
207 "http://[fe80::1]/",
208 "http://[fc00::1]/",
209 // Public IP literals are blocked as collateral; users can override.
210 "http://93.184.216.34/",
211 "https://[2001:db8::1]/",
212 } {
213 err := pc.CheckAllowedHTTPURL(u)
214 c.Assert(err, qt.IsNotNil, qt.Commentf(u))
215 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`, qt.Commentf(u))
216 }
217 })
218
219 c.Run("Explicit user config bypasses hardening", func(c *qt.C) {
220 c.Parallel()
221 tomlConfig := `
222 [security.http]
223 urls = ['http://127\.0\.0\.1.*', 'http://localhost.*']
224 `
225 cfg, err := config.FromConfigString(tomlConfig, "toml")
226 c.Assert(err, qt.IsNil)
227 pc, err := DecodeConfig(cfg)
228 c.Assert(err, qt.IsNil)
229 c.Assert(pc.CheckAllowedHTTPURL("http://127.0.0.1:8080/foo"), qt.IsNil)
230 c.Assert(pc.CheckAllowedHTTPURL("http://localhost:1313/"), qt.IsNil)
231 })
232
233 c.Run("User can deny with the ! prefix", func(c *qt.C) {
234 c.Parallel()
235 tomlConfig := `
236 [security.http]
237 urls = ['.*', '! ^https?://evil\.example\.com']
238 `
239 cfg, err := config.FromConfigString(tomlConfig, "toml")
240 c.Assert(err, qt.IsNil)
241 pc, err := DecodeConfig(cfg)
242 c.Assert(err, qt.IsNil)
243 c.Assert(pc.CheckAllowedHTTPURL("https://good.example.com/"), qt.IsNil)
244 err = pc.CheckAllowedHTTPURL("https://evil.example.com/x")
245 c.Assert(err, qt.IsNotNil)
246 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`)
247 })
248 }
249
250 func TestCheckAllowedHTTPURLAtInPathIssue14825(t *testing.T) {
251 t.Parallel()
252 c := qt.New(t)
253
254 pc, err := DecodeConfig(config.New())
255 c.Assert(err, qt.IsNil)
256
257 for _, u := range []string{
258 "https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.esm.min.mjs",
259 "https://unpkg.com/react@18/umd/react.production.min.js",
260 "https://example.org/foo@bar/baz",
261 } {
262 c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u))
263 }
264
265 for _, u := range []string{
266 "http://[email protected]/",
267 "http://user:[email protected]/",
268 "https://[email protected]/foo@bar",
269 } {
270 err := pc.CheckAllowedHTTPURL(u)
271 c.Assert(err, qt.IsNotNil, qt.Commentf(u))
272 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`, qt.Commentf(u))
273 }
274 }
275
276 func TestCheckAllowedHTTPURLDigitHostnameIssue14837(t *testing.T) {
277 t.Parallel()
278 c := qt.New(t)
279
280 pc, err := DecodeConfig(config.New())
281 c.Assert(err, qt.IsNil)
282
283 for _, u := range []string{
284 "https://1password.com/",
285 "https://37signals.com/foo",
286 } {
287 c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u))
288 }
289
290 for _, u := range []string{
291 "http://127.0.0.1/",
292 "http://10.0.0.1/",
293 "http://192.168.1.1/",
294 "http://0.0.0.0/",
295 } {
296 err := pc.CheckAllowedHTTPURL(u)
297 c.Assert(err, qt.IsNotNil, qt.Commentf(u))
298 }
299 }
300
301 // Integer/hex/octal IPv4 encodings must be denied just like their dotted-decimal
302 // literals; digit-leading hostnames must still be allowed. See issue 14856.
303 func TestCheckAllowedHTTPURLIntegerIPEncodings(t *testing.T) {
304 t.Parallel()
305 c := qt.New(t)
306
307 pc, err := DecodeConfig(config.New())
308 c.Assert(err, qt.IsNil)
309
310 for _, u := range []string{
311 "http://2130706433/", // 127.0.0.1 decimal
312 "http://2130706433:9777/x", // 127.0.0.1 decimal, port
313 "http://2852039166/", // 169.254.169.254 (cloud metadata)
314 "http://0x7f000001/", // 127.0.0.1 hex
315 "http://017700000001/", // 127.0.0.1 octal
316 "http://0x7f.0.0.1/", // 127.0.0.1 dotted hex
317 "http://0177.0.0.1/", // 127.0.0.1 dotted octal
318 "http://127.1/", // 127.0.0.1 short form
319 "http://0/", // 0.0.0.0
320 "http://0xa9fea9fe/", // 169.254.169.254 hex
321 } {
322 err := pc.CheckAllowedHTTPURL(u)
323 c.Assert(err, qt.IsNotNil, qt.Commentf(u))
324 }
325
326 for _, u := range []string{
327 "https://1password.com/",
328 "https://37signals.com/foo",
329 "https://3com.com/",
330 "https://0x.tools/",
331 } {
332 c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u))
333 }
334 }
335
336 func TestCheckAllowedContent(t *testing.T) {
337 t.Parallel()
338 c := qt.New(t)
339
340 c.Run("text/html denied by default", func(c *qt.C) {
341 c.Parallel()
342 pc, err := DecodeConfig(config.New())
343 c.Assert(err, qt.IsNil)
344 err = pc.CheckAllowedContent("text/html")
345 c.Assert(err, qt.IsNotNil)
346 c.Assert(err, qt.ErrorMatches, `(?s).*"text/html" is not whitelisted in policy "security\.allowContent".*`)
347 })
348
349 c.Run("Other content types allowed by default", func(c *qt.C) {
350 c.Parallel()
351 pc, err := DecodeConfig(config.New())
352 c.Assert(err, qt.IsNil)
353 for _, mt := range []string{
354 "text/markdown",
355 "text/asciidoc",
356 "text/x-org",
357 "text/rst",
358 "text/pandoc",
359 } {
360 c.Assert(pc.CheckAllowedContent(mt), qt.IsNil, qt.Commentf(mt))
361 }
362 })
363
364 c.Run("User can opt in to HTML", func(c *qt.C) {
365 c.Parallel()
366 tomlConfig := `
367 [security]
368 allowContent = ['.*']
369 `
370 cfg, err := config.FromConfigString(tomlConfig, "toml")
371 c.Assert(err, qt.IsNil)
372 pc, err := DecodeConfig(cfg)
373 c.Assert(err, qt.IsNil)
374 c.Assert(pc.CheckAllowedContent("text/html"), qt.IsNil)
375 })
376 }
377
378 func TestDecodeConfigNodePermissions(t *testing.T) {
379 c := qt.New(t)
380
381 c.Run("Custom paths", func(c *qt.C) {
382 c.Parallel()
383 tomlConfig := `
384 [security.node.permissions]
385 allowRead = ["/tmp", "."]
386 allowWrite = ["."]
387 `
388 cfg, err := config.FromConfigString(tomlConfig, "toml")
389 c.Assert(err, qt.IsNil)
390 pc, err := DecodeConfig(cfg)
391 c.Assert(err, qt.IsNil)
392 c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue)
393 c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"/tmp", "."})
394 c.Assert(pc.Node.Permissions.AllowWrite, qt.DeepEquals, []string{"."})
395 })
396
397 c.Run("Disabled", func(c *qt.C) {
398 c.Parallel()
399 tomlConfig := `
400 [security.node.permissions]
401 disable = true
402 `
403 cfg, err := config.FromConfigString(tomlConfig, "toml")
404 c.Assert(err, qt.IsNil)
405 pc, err := DecodeConfig(cfg)
406 c.Assert(err, qt.IsNil)
407 c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsFalse)
408 })
409
410 c.Run("Wildcard", func(c *qt.C) {
411 c.Parallel()
412 tomlConfig := `
413 [security.node.permissions]
414 allowRead = ["*"]
415 allowWrite = ["*"]
416 `
417 cfg, err := config.FromConfigString(tomlConfig, "toml")
418 c.Assert(err, qt.IsNil)
419 pc, err := DecodeConfig(cfg)
420 c.Assert(err, qt.IsNil)
421 c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue)
422 c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"*"})
423 })
424 }