| 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$', '! ^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", |
| 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 |
c.Assert(pc.HTTP.ProxyFromEnvironment, qt.IsFalse) |
| 160 |
|
| 161 |
c.Assert(pc.Exec.OsEnv.Accept("PATH"), qt.IsTrue) |
| 162 |
c.Assert(pc.Exec.OsEnv.Accept("GOROOT"), qt.IsTrue) |
| 163 |
c.Assert(pc.Exec.OsEnv.Accept("HOME"), qt.IsTrue) |
| 164 |
c.Assert(pc.Exec.OsEnv.Accept("SSH_AUTH_SOCK"), qt.IsTrue) |
| 165 |
c.Assert(pc.Exec.OsEnv.Accept("a"), qt.IsFalse) |
| 166 |
c.Assert(pc.Exec.OsEnv.Accept("e"), qt.IsFalse) |
| 167 |
c.Assert(pc.Exec.OsEnv.Accept("MYSECRET"), qt.IsFalse) |
| 168 |
|
| 169 |
c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue) |
| 170 |
c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"."}) |
| 171 |
c.Assert(pc.Node.Permissions.AllowWrite, qt.DeepEquals, []string{}) |
| 172 |
} |
| 173 |
|
| 174 |
func TestCheckAllowedHTTPURLHardenedDefaultsIssue14792(t *testing.T) { |
| 175 |
t.Parallel() |
| 176 |
c := qt.New(t) |
| 177 |
|
| 178 |
c.Run("Public URLs allowed by default", func(c *qt.C) { |
| 179 |
c.Parallel() |
| 180 |
pc, err := DecodeConfig(config.New()) |
| 181 |
c.Assert(err, qt.IsNil) |
| 182 |
for _, u := range []string{ |
| 183 |
"https://example.org/", |
| 184 |
"https://example.org:8443/foo", |
| 185 |
"https://sub.example.org/path", |
| 186 |
} { |
| 187 |
c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u)) |
| 188 |
} |
| 189 |
}) |
| 190 |
|
| 191 |
c.Run("Private/loopback URLs denied by default", func(c *qt.C) { |
| 192 |
c.Parallel() |
| 193 |
pc, err := DecodeConfig(config.New()) |
| 194 |
c.Assert(err, qt.IsNil) |
| 195 |
for _, u := range []string{ |
| 196 |
"http://localhost/", |
| 197 |
"http://LOCALHOST:8080/", |
| 198 |
"http://foo.localhost/", |
| 199 |
"http://127.0.0.1/", |
| 200 |
"HTTP://127.0.0.1/", // The deny rules must be case-insensitive. |
| 201 |
"HtTpS://169.254.169.254/", |
| 202 |
"http://127.1.2.3:8080/x", |
| 203 |
"http://user:[email protected]/", // userinfo must not sneak past the deny. |
| 204 |
"http://10.0.0.1/", |
| 205 |
"http://172.16.0.1/", |
| 206 |
"http://192.168.1.1/", |
| 207 |
"http://169.254.169.254/latest/meta-data/", // AWS/GCP metadata. |
| 208 |
"http://0.0.0.0/", |
| 209 |
"http://[::1]/", |
| 210 |
"http://[fe80::1]/", |
| 211 |
"http://[fc00::1]/", |
| 212 |
// Public IP literals are blocked as collateral; users can override. |
| 213 |
"http://93.184.216.34/", |
| 214 |
"https://[2001:db8::1]/", |
| 215 |
} { |
| 216 |
err := pc.CheckAllowedHTTPURL(u) |
| 217 |
c.Assert(err, qt.IsNotNil, qt.Commentf(u)) |
| 218 |
c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`, qt.Commentf(u)) |
| 219 |
} |
| 220 |
}) |
| 221 |
|
| 222 |
c.Run("Explicit user config bypasses hardening", func(c *qt.C) { |
| 223 |
c.Parallel() |
| 224 |
tomlConfig := ` |
| 225 |
[security.http] |
| 226 |
urls = ['http://127\.0\.0\.1.*', 'http://localhost.*'] |
| 227 |
` |
| 228 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 229 |
c.Assert(err, qt.IsNil) |
| 230 |
pc, err := DecodeConfig(cfg) |
| 231 |
c.Assert(err, qt.IsNil) |
| 232 |
c.Assert(pc.CheckAllowedHTTPURL("http://127.0.0.1:8080/foo"), qt.IsNil) |
| 233 |
c.Assert(pc.CheckAllowedHTTPURL("http://localhost:1313/"), qt.IsNil) |
| 234 |
}) |
| 235 |
|
| 236 |
c.Run("User can deny with the ! prefix", func(c *qt.C) { |
| 237 |
c.Parallel() |
| 238 |
tomlConfig := ` |
| 239 |
[security.http] |
| 240 |
urls = ['.*', '! ^https?://evil\.example\.com'] |
| 241 |
` |
| 242 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 243 |
c.Assert(err, qt.IsNil) |
| 244 |
pc, err := DecodeConfig(cfg) |
| 245 |
c.Assert(err, qt.IsNil) |
| 246 |
c.Assert(pc.CheckAllowedHTTPURL("https://good.example.com/"), qt.IsNil) |
| 247 |
err = pc.CheckAllowedHTTPURL("https://evil.example.com/x") |
| 248 |
c.Assert(err, qt.IsNotNil) |
| 249 |
c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`) |
| 250 |
}) |
| 251 |
} |
| 252 |
|
| 253 |
// A resolved destination address must be validated so a hostname that resolves |
| 254 |
// to an internal address cannot reach an internal endpoint via GetRemote. |
| 255 |
// See CVE-2026-10582. |
| 256 |
func TestCheckAllowedHTTPAddress(t *testing.T) { |
| 257 |
t.Parallel() |
| 258 |
c := qt.New(t) |
| 259 |
pc := DefaultConfig |
| 260 |
|
| 261 |
for _, addr := range []string{ |
| 262 |
"93.184.216.34:80", |
| 263 |
"[2606:4700::1]:443", |
| 264 |
"[64:ff9b::5db8:d822]:80", // NAT64-embedded 93.184.216.34. |
| 265 |
} { |
| 266 |
c.Assert(pc.CheckAllowedHTTPAddress("tcp", addr), qt.IsNil, qt.Commentf(addr)) |
| 267 |
} |
| 268 |
|
| 269 |
for _, addr := range []string{ |
| 270 |
"127.0.0.1:80", |
| 271 |
"[::1]:80", |
| 272 |
"10.0.0.1:8080", |
| 273 |
"172.16.0.1:80", |
| 274 |
"192.168.1.1:80", |
| 275 |
"169.254.169.254:80", // Cloud metadata. |
| 276 |
"[fe80::1]:80", |
| 277 |
"[fc00::1]:80", |
| 278 |
"0.0.0.0:80", |
| 279 |
"[::ffff:127.0.0.1]:80", // IPv4-mapped loopback. |
| 280 |
"100.64.0.1:80", // CGNAT. |
| 281 |
"[::ffff:100.64.0.1]:80", |
| 282 |
"192.0.0.9:80", |
| 283 |
"192.0.2.1:80", // TEST-NET-1. |
| 284 |
"198.18.0.1:80", |
| 285 |
"198.51.100.1:80", |
| 286 |
"203.0.113.1:80", |
| 287 |
"240.0.0.1:80", |
| 288 |
"[2001:db8::1]:443", |
| 289 |
"[3fff::1]:443", |
| 290 |
"[64:ff9b::7f00:1]:80", // NAT64-embedded 127.0.0.1. |
| 291 |
"[64:ff9b::a9fe:a9fe]:80", // NAT64-embedded 169.254.169.254. |
| 292 |
"[64:ff9b:1::a00:1]:80", // NAT64-embedded 10.0.0.1. |
| 293 |
} { |
| 294 |
err := pc.CheckAllowedHTTPAddress("tcp", addr) |
| 295 |
c.Assert(err, qt.IsNotNil, qt.Commentf(addr)) |
| 296 |
c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`, qt.Commentf(addr)) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func TestCheckAllowedHTTPURLAtInPathIssue14825(t *testing.T) { |
| 301 |
t.Parallel() |
| 302 |
c := qt.New(t) |
| 303 |
|
| 304 |
pc, err := DecodeConfig(config.New()) |
| 305 |
c.Assert(err, qt.IsNil) |
| 306 |
|
| 307 |
for _, u := range []string{ |
| 308 |
"https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.esm.min.mjs", |
| 309 |
"https://unpkg.com/react@18/umd/react.production.min.js", |
| 310 |
"https://example.org/foo@bar/baz", |
| 311 |
} { |
| 312 |
c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u)) |
| 313 |
} |
| 314 |
|
| 315 |
for _, u := range []string{ |
| 316 |
"http://[email protected]/", |
| 317 |
"http://user:[email protected]/", |
| 318 |
"https://[email protected]/foo@bar", |
| 319 |
} { |
| 320 |
err := pc.CheckAllowedHTTPURL(u) |
| 321 |
c.Assert(err, qt.IsNotNil, qt.Commentf(u)) |
| 322 |
c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`, qt.Commentf(u)) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
func TestCheckAllowedHTTPURLDigitHostnameIssue14837(t *testing.T) { |
| 327 |
t.Parallel() |
| 328 |
c := qt.New(t) |
| 329 |
|
| 330 |
pc, err := DecodeConfig(config.New()) |
| 331 |
c.Assert(err, qt.IsNil) |
| 332 |
|
| 333 |
for _, u := range []string{ |
| 334 |
"https://1password.com/", |
| 335 |
"https://37signals.com/foo", |
| 336 |
} { |
| 337 |
c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u)) |
| 338 |
} |
| 339 |
|
| 340 |
for _, u := range []string{ |
| 341 |
"http://127.0.0.1/", |
| 342 |
"http://10.0.0.1/", |
| 343 |
"http://192.168.1.1/", |
| 344 |
"http://0.0.0.0/", |
| 345 |
} { |
| 346 |
err := pc.CheckAllowedHTTPURL(u) |
| 347 |
c.Assert(err, qt.IsNotNil, qt.Commentf(u)) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// Integer/hex/octal IPv4 encodings must be denied just like their dotted-decimal |
| 352 |
// literals; digit-leading hostnames must still be allowed. See issue 14856. |
| 353 |
func TestCheckAllowedHTTPURLIntegerIPEncodings(t *testing.T) { |
| 354 |
t.Parallel() |
| 355 |
c := qt.New(t) |
| 356 |
|
| 357 |
pc, err := DecodeConfig(config.New()) |
| 358 |
c.Assert(err, qt.IsNil) |
| 359 |
|
| 360 |
for _, u := range []string{ |
| 361 |
"http://2130706433/", // 127.0.0.1 decimal |
| 362 |
"http://2130706433:9777/x", // 127.0.0.1 decimal, port |
| 363 |
"http://2852039166/", // 169.254.169.254 (cloud metadata) |
| 364 |
"http://0x7f000001/", // 127.0.0.1 hex |
| 365 |
"http://017700000001/", // 127.0.0.1 octal |
| 366 |
"http://0x7f.0.0.1/", // 127.0.0.1 dotted hex |
| 367 |
"http://0177.0.0.1/", // 127.0.0.1 dotted octal |
| 368 |
"http://127.1/", // 127.0.0.1 short form |
| 369 |
"http://0/", // 0.0.0.0 |
| 370 |
"http://0xa9fea9fe/", // 169.254.169.254 hex |
| 371 |
} { |
| 372 |
err := pc.CheckAllowedHTTPURL(u) |
| 373 |
c.Assert(err, qt.IsNotNil, qt.Commentf(u)) |
| 374 |
} |
| 375 |
|
| 376 |
for _, u := range []string{ |
| 377 |
"https://1password.com/", |
| 378 |
"https://37signals.com/foo", |
| 379 |
"https://3com.com/", |
| 380 |
"https://0x.tools/", |
| 381 |
} { |
| 382 |
c.Assert(pc.CheckAllowedHTTPURL(u), qt.IsNil, qt.Commentf(u)) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func TestCheckAllowedContent(t *testing.T) { |
| 387 |
t.Parallel() |
| 388 |
c := qt.New(t) |
| 389 |
|
| 390 |
c.Run("text/html denied by default", func(c *qt.C) { |
| 391 |
c.Parallel() |
| 392 |
pc, err := DecodeConfig(config.New()) |
| 393 |
c.Assert(err, qt.IsNil) |
| 394 |
err = pc.CheckAllowedContent("text/html") |
| 395 |
c.Assert(err, qt.IsNotNil) |
| 396 |
c.Assert(err, qt.ErrorMatches, `(?s).*"text/html" is not whitelisted in policy "security\.allowContent".*`) |
| 397 |
}) |
| 398 |
|
| 399 |
c.Run("text/org denied by default", func(c *qt.C) { |
| 400 |
c.Parallel() |
| 401 |
pc, err := DecodeConfig(config.New()) |
| 402 |
c.Assert(err, qt.IsNil) |
| 403 |
err = pc.CheckAllowedContent("text/org") |
| 404 |
c.Assert(err, qt.IsNotNil) |
| 405 |
c.Assert(err, qt.ErrorMatches, `(?s).*"text/org" is not whitelisted in policy "security\.allowContent".*`) |
| 406 |
}) |
| 407 |
|
| 408 |
c.Run("Other content types allowed by default", func(c *qt.C) { |
| 409 |
c.Parallel() |
| 410 |
pc, err := DecodeConfig(config.New()) |
| 411 |
c.Assert(err, qt.IsNil) |
| 412 |
for _, mt := range []string{ |
| 413 |
"text/markdown", |
| 414 |
"text/asciidoc", |
| 415 |
"text/rst", |
| 416 |
"text/pandoc", |
| 417 |
} { |
| 418 |
c.Assert(pc.CheckAllowedContent(mt), qt.IsNil, qt.Commentf(mt)) |
| 419 |
} |
| 420 |
}) |
| 421 |
|
| 422 |
c.Run("User can opt in to HTML", func(c *qt.C) { |
| 423 |
c.Parallel() |
| 424 |
tomlConfig := ` |
| 425 |
[security] |
| 426 |
allowContent = ['.*'] |
| 427 |
` |
| 428 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 429 |
c.Assert(err, qt.IsNil) |
| 430 |
pc, err := DecodeConfig(cfg) |
| 431 |
c.Assert(err, qt.IsNil) |
| 432 |
c.Assert(pc.CheckAllowedContent("text/html"), qt.IsNil) |
| 433 |
}) |
| 434 |
} |
| 435 |
|
| 436 |
func TestDecodeConfigNodePermissions(t *testing.T) { |
| 437 |
c := qt.New(t) |
| 438 |
|
| 439 |
c.Run("Custom paths", func(c *qt.C) { |
| 440 |
c.Parallel() |
| 441 |
tomlConfig := ` |
| 442 |
[security.node.permissions] |
| 443 |
allowRead = ["/tmp", "."] |
| 444 |
allowWrite = ["."] |
| 445 |
` |
| 446 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 447 |
c.Assert(err, qt.IsNil) |
| 448 |
pc, err := DecodeConfig(cfg) |
| 449 |
c.Assert(err, qt.IsNil) |
| 450 |
c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue) |
| 451 |
c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"/tmp", "."}) |
| 452 |
c.Assert(pc.Node.Permissions.AllowWrite, qt.DeepEquals, []string{"."}) |
| 453 |
}) |
| 454 |
|
| 455 |
c.Run("Disabled", func(c *qt.C) { |
| 456 |
c.Parallel() |
| 457 |
tomlConfig := ` |
| 458 |
[security.node.permissions] |
| 459 |
disable = true |
| 460 |
` |
| 461 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 462 |
c.Assert(err, qt.IsNil) |
| 463 |
pc, err := DecodeConfig(cfg) |
| 464 |
c.Assert(err, qt.IsNil) |
| 465 |
c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsFalse) |
| 466 |
}) |
| 467 |
|
| 468 |
c.Run("Wildcard", func(c *qt.C) { |
| 469 |
c.Parallel() |
| 470 |
tomlConfig := ` |
| 471 |
[security.node.permissions] |
| 472 |
allowRead = ["*"] |
| 473 |
allowWrite = ["*"] |
| 474 |
` |
| 475 |
cfg, err := config.FromConfigString(tomlConfig, "toml") |
| 476 |
c.Assert(err, qt.IsNil) |
| 477 |
pc, err := DecodeConfig(cfg) |
| 478 |
c.Assert(err, qt.IsNil) |
| 479 |
c.Assert(pc.Node.Permissions.IsEnabled(), qt.IsTrue) |
| 480 |
c.Assert(pc.Node.Permissions.AllowRead, qt.DeepEquals, []string{"*"}) |
| 481 |
}) |
| 482 |
} |