Debian Codegraph @

hugolib/securitypolicies_test.go

raw
1 // Copyright 2025 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 hugolib
15
16 import (
17 "fmt"
18 "net/http"
19 "net/http/httptest"
20 "runtime"
21 "testing"
22
23 qt "github.com/frankban/quicktest"
24 "github.com/gohugoio/hugo/markup/asciidocext"
25 "github.com/gohugoio/hugo/markup/pandoc"
26 "github.com/gohugoio/hugo/markup/rst"
27 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
28 )
29
30 func TestSecurityPolicies(t *testing.T) {
31 c := qt.New(t)
32
33 c.Run("HTML content, denied by default", func(c *qt.C) {
34 c.Parallel()
35 files := `
36 -- hugo.toml --
37 baseURL = "https://example.org"
38 -- content/page.html --
39 ---
40 title: "Untrusted"
41 ---
42 <script>alert(1)</script>
43 -- layouts/single.html --
44 {{ .Content }}
45 `
46 _, err := TestE(c, files)
47 c.Assert(err, qt.IsNotNil)
48 c.Assert(err, qt.ErrorMatches, `(?s).*"text/html" is not whitelisted in policy "security\.allowContent".*`)
49 })
50
51 c.Run("HTML content, allowed via override", func(c *qt.C) {
52 c.Parallel()
53 files := `
54 -- hugo.toml --
55 baseURL = "https://example.org"
56 [security]
57 allowContent = ['.*']
58 -- content/page.html --
59 ---
60 title: "Trusted"
61 ---
62 <p>hello</p>
63 -- layouts/single.html --
64 {{ .Content }}
65 `
66 b := Test(c, files)
67 b.AssertFileContent("public/page/index.html", "<p>hello</p>")
68 })
69
70 c.Run("HTML content from content adapter, denied by default", func(c *qt.C) {
71 c.Parallel()
72 files := `
73 -- hugo.toml --
74 baseURL = "https://example.org"
75 -- content/_content.gotmpl --
76 {{ .AddPage (dict "path" "p1" "title" "Untrusted" "content" (dict "value" "<script>alert(1)</script>" "mediaType" "text/html")) }}
77 -- layouts/single.html --
78 {{ .Content }}
79 `
80 _, err := TestE(c, files)
81 c.Assert(err, qt.IsNotNil)
82 c.Assert(err, qt.ErrorMatches, `(?s).*"text/html" is not whitelisted in policy "security\.allowContent".*`)
83 })
84
85 c.Run("HTML content from content adapter, allowed via override", func(c *qt.C) {
86 c.Parallel()
87 files := `
88 -- hugo.toml --
89 baseURL = "https://example.org"
90 [security]
91 allowContent = ['.*']
92 -- content/_content.gotmpl --
93 {{ .AddPage (dict "path" "p1" "title" "Trusted" "content" (dict "value" "<p>hello</p>" "mediaType" "text/html")) }}
94 -- layouts/single.html --
95 {{ .Content }}
96 `
97 b := Test(c, files)
98 b.AssertFileContent("public/p1/index.html", "<p>hello</p>")
99 })
100
101 c.Run("Org content, denied by default", func(c *qt.C) {
102 c.Parallel()
103 files := `
104 -- hugo.toml --
105 baseURL = "https://example.org"
106 -- content/page.org --
107 ---
108 title: "Untrusted"
109 ---
110 @@html:<script>alert(1)</script>@@
111 -- layouts/single.html --
112 {{ .Content }}
113 `
114 _, err := TestE(c, files)
115 c.Assert(err, qt.IsNotNil)
116 c.Assert(err, qt.ErrorMatches, `(?s).*"text/org" is not whitelisted in policy "security\.allowContent".*`)
117 })
118
119 c.Run("Org content, allowed via override", func(c *qt.C) {
120 c.Parallel()
121 files := `
122 -- hugo.toml --
123 baseURL = "https://example.org"
124 [security]
125 allowContent = ['.*']
126 -- content/page.org --
127 ---
128 title: "Trusted"
129 ---
130 hello
131 -- layouts/single.html --
132 {{ .Content }}
133 `
134 b := Test(c, files)
135 b.AssertFileContent("public/page/index.html", "hello")
136 })
137
138 c.Run("Org content from content adapter, denied by default", func(c *qt.C) {
139 c.Parallel()
140 files := `
141 -- hugo.toml --
142 baseURL = "https://example.org"
143 -- content/_content.gotmpl --
144 {{ .AddPage (dict "path" "p1" "title" "Untrusted" "content" (dict "value" "@@html:<script>alert(1)</script>@@" "mediaType" "text/org")) }}
145 -- layouts/single.html --
146 {{ .Content }}
147 `
148 _, err := TestE(c, files)
149 c.Assert(err, qt.IsNotNil)
150 c.Assert(err, qt.ErrorMatches, `(?s).*"text/org" is not whitelisted in policy "security\.allowContent".*`)
151 })
152
153 c.Run("os.GetEnv, denied", func(c *qt.C) {
154 c.Parallel()
155 files := `
156 -- hugo.toml --
157 baseURL = "https://example.org"
158 -- layouts/home.html --
159 {{ os.Getenv "FOOBAR" }}
160 `
161 _, err := TestE(c, files)
162 c.Assert(err, qt.IsNotNil)
163 c.Assert(err, qt.ErrorMatches, `(?s).*"FOOBAR" is not whitelisted in policy "security\.funcs\.getenv".*`)
164 })
165
166 c.Run("os.GetEnv, OK", func(c *qt.C) {
167 c.Parallel()
168 files := `
169 -- hugo.toml --
170 baseURL = "https://example.org"
171 -- layouts/home.html --
172 {{ os.Getenv "HUGO_FOO" }}
173 `
174 Test(c, files)
175 })
176
177 c.Run("AsciiDoc, denied", func(c *qt.C) {
178 c.Parallel()
179 if ok, err := asciidocext.Supports(); !ok {
180 c.Skip(err)
181 }
182
183 files := `
184 -- hugo.toml --
185 baseURL = "https://example.org"
186 -- content/page.ad --
187 foo
188 `
189 _, err := TestE(c, files)
190 c.Assert(err, qt.IsNotNil)
191 c.Assert(err, qt.ErrorMatches, `(?s).*"asciidoctor" is not whitelisted in policy "security\.exec\.allow".*`)
192 })
193
194 c.Run("RST, denied", func(c *qt.C) {
195 c.Parallel()
196 if !rst.Supports() {
197 c.Skip()
198 }
199
200 files := `
201 -- hugo.toml --
202 baseURL = "https://example.org"
203 -- content/page.rst --
204 foo
205 `
206 _, err := TestE(c, files)
207 c.Assert(err, qt.IsNotNil)
208 if runtime.GOOS == "windows" {
209 c.Assert(err, qt.ErrorMatches, `(?s).*python(\.exe)?" is not whitelisted in policy "security\.exec\.allow".*`)
210 } else {
211 c.Assert(err, qt.ErrorMatches, `(?s).*"rst2html(\.py)?" is not whitelisted in policy "security\.exec\.allow".*`)
212 }
213 })
214
215 c.Run("Pandoc, denied", func(c *qt.C) {
216 c.Parallel()
217 if !pandoc.Supports() {
218 c.Skip()
219 }
220
221 files := `
222 -- hugo.toml --
223 baseURL = "https://example.org"
224 -- content/page.pdc --
225 foo
226 `
227 _, err := TestE(c, files)
228 c.Assert(err, qt.IsNotNil)
229 c.Assert(err, qt.ErrorMatches, `(?s).*pandoc" is not whitelisted in policy "security\.exec\.allow".*`)
230 })
231
232 c.Run("Dart SASS, OK", func(c *qt.C) {
233 c.Parallel()
234 if !dartsass.Supports() {
235 c.Skip()
236 }
237 files := `
238 -- hugo.toml --
239 baseURL = "https://example.org"
240 -- layouts/home.html --
241 {{ $scss := "body { color: #333; }" | resources.FromString "foo.scss" | css.Sass (dict "transpiler" "dartsass") }}
242 `
243 Test(c, files)
244 })
245
246 c.Run("Dart SASS, denied", func(c *qt.C) {
247 c.Parallel()
248 if !dartsass.Supports() {
249 c.Skip()
250 }
251 files := `
252 -- hugo.toml --
253 baseURL = "https://example.org"
254 [security]
255 [security.exec]
256 allow="none"
257 -- layouts/home.html --
258 {{ $scss := "body { color: #333; }" | resources.FromString "foo.scss" | css.Sass (dict "transpiler" "dartsass") }}
259 `
260 _, err := TestE(c, files)
261 c.Assert(err, qt.IsNotNil)
262 c.Assert(err, qt.ErrorMatches, `(?s).*sass(-embedded)?" is not whitelisted in policy "security\.exec\.allow".*`)
263 })
264
265 c.Run("resources.GetRemote, OK", func(c *qt.C) {
266 c.Parallel()
267 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
268 c.Cleanup(func() {
269 ts.Close()
270 })
271 files := fmt.Sprintf(`
272 -- hugo.toml --
273 baseURL = "https://example.org"
274 [security]
275 [security.http]
276 urls = ['.*']
277 -- layouts/home.html --
278 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
279 `, ts.URL)
280 Test(c, files)
281 })
282
283 c.Run("resources.GetRemote, denied method", func(c *qt.C) {
284 c.Parallel()
285 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
286 c.Cleanup(func() {
287 ts.Close()
288 })
289 files := fmt.Sprintf(`
290 -- hugo.toml --
291 baseURL = "https://example.org"
292 [security]
293 [security.http]
294 urls = ['.*']
295 -- layouts/home.html --
296 {{ $json := resources.GetRemote "%s/fruits.json" (dict "method" "DELETE" ) }}{{ $json.Content }}
297 `, ts.URL)
298 _, err := TestE(c, files)
299 c.Assert(err, qt.IsNotNil)
300 c.Assert(err, qt.ErrorMatches, `(?s).*"DELETE" is not whitelisted in policy "security\.http\.method".*`)
301 })
302
303 c.Run("resources.GetRemote, denied URL", func(c *qt.C) {
304 c.Parallel()
305 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
306 c.Cleanup(func() {
307 ts.Close()
308 })
309 files := fmt.Sprintf(`
310 -- hugo.toml --
311 baseURL = "https://example.org"
312 [security]
313 [security.http]
314 urls="none"
315 -- layouts/home.html --
316 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
317 `, ts.URL)
318 _, err := TestE(c, files)
319 c.Assert(err, qt.IsNotNil)
320 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`)
321 })
322
323 c.Run("resources.GetRemote, denied loopback URL by default", func(c *qt.C) {
324 c.Parallel()
325 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
326 c.Cleanup(func() {
327 ts.Close()
328 })
329 files := fmt.Sprintf(`
330 -- hugo.toml --
331 baseURL = "https://example.org"
332 -- layouts/home.html --
333 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
334 `, ts.URL)
335 _, err := TestE(c, files)
336 c.Assert(err, qt.IsNotNil)
337 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`)
338 })
339
340 c.Run("resources.GetRemote, fake JSON", func(c *qt.C) {
341 c.Parallel()
342 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
343 c.Cleanup(func() {
344 ts.Close()
345 })
346 files := fmt.Sprintf(`
347 -- hugo.toml --
348 baseURL = "https://example.org"
349 [security]
350 [security.http]
351 urls = ['.*']
352 -- layouts/home.html --
353 {{ $json := resources.GetRemote "%s/fakejson.json" }}{{ $json.Content }}
354 `, ts.URL)
355 _, err := TestE(c, files)
356 c.Assert(err, qt.IsNotNil)
357 c.Assert(err, qt.ErrorMatches, `(?s).*failed to resolve media type.*`)
358 })
359
360 c.Run("resources.GetRemote, fake JSON whitelisted", func(c *qt.C) {
361 c.Parallel()
362 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
363 c.Cleanup(func() {
364 ts.Close()
365 })
366 files := fmt.Sprintf(`
367 -- hugo.toml --
368 baseURL = "https://example.org"
369 [security]
370 [security.http]
371 urls = ['.*']
372 mediaTypes=["application/json"]
373 -- layouts/home.html --
374 {{ $json := resources.GetRemote "%s/fakejson.json" }}{{ $json.Content }}
375 `, ts.URL)
376 Test(c, files)
377 })
378 }