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("os.GetEnv, denied", func(c *qt.C) {
102 c.Parallel()
103 files := `
104 -- hugo.toml --
105 baseURL = "https://example.org"
106 -- layouts/home.html --
107 {{ os.Getenv "FOOBAR" }}
108 `
109 _, err := TestE(c, files)
110 c.Assert(err, qt.IsNotNil)
111 c.Assert(err, qt.ErrorMatches, `(?s).*"FOOBAR" is not whitelisted in policy "security\.funcs\.getenv".*`)
112 })
113
114 c.Run("os.GetEnv, OK", func(c *qt.C) {
115 c.Parallel()
116 files := `
117 -- hugo.toml --
118 baseURL = "https://example.org"
119 -- layouts/home.html --
120 {{ os.Getenv "HUGO_FOO" }}
121 `
122 Test(c, files)
123 })
124
125 c.Run("AsciiDoc, denied", func(c *qt.C) {
126 c.Parallel()
127 if ok, err := asciidocext.Supports(); !ok {
128 c.Skip(err)
129 }
130
131 files := `
132 -- hugo.toml --
133 baseURL = "https://example.org"
134 -- content/page.ad --
135 foo
136 `
137 _, err := TestE(c, files)
138 c.Assert(err, qt.IsNotNil)
139 c.Assert(err, qt.ErrorMatches, `(?s).*"asciidoctor" is not whitelisted in policy "security\.exec\.allow".*`)
140 })
141
142 c.Run("RST, denied", func(c *qt.C) {
143 c.Parallel()
144 if !rst.Supports() {
145 c.Skip()
146 }
147
148 files := `
149 -- hugo.toml --
150 baseURL = "https://example.org"
151 -- content/page.rst --
152 foo
153 `
154 _, err := TestE(c, files)
155 c.Assert(err, qt.IsNotNil)
156 if runtime.GOOS == "windows" {
157 c.Assert(err, qt.ErrorMatches, `(?s).*python(\.exe)?" is not whitelisted in policy "security\.exec\.allow".*`)
158 } else {
159 c.Assert(err, qt.ErrorMatches, `(?s).*"rst2html(\.py)?" is not whitelisted in policy "security\.exec\.allow".*`)
160 }
161 })
162
163 c.Run("Pandoc, denied", func(c *qt.C) {
164 c.Parallel()
165 if !pandoc.Supports() {
166 c.Skip()
167 }
168
169 files := `
170 -- hugo.toml --
171 baseURL = "https://example.org"
172 -- content/page.pdc --
173 foo
174 `
175 _, err := TestE(c, files)
176 c.Assert(err, qt.IsNotNil)
177 c.Assert(err, qt.ErrorMatches, `(?s).*pandoc" is not whitelisted in policy "security\.exec\.allow".*`)
178 })
179
180 c.Run("Dart SASS, OK", func(c *qt.C) {
181 c.Parallel()
182 if !dartsass.Supports() {
183 c.Skip()
184 }
185 files := `
186 -- hugo.toml --
187 baseURL = "https://example.org"
188 -- layouts/home.html --
189 {{ $scss := "body { color: #333; }" | resources.FromString "foo.scss" | css.Sass (dict "transpiler" "dartsass") }}
190 `
191 Test(c, files)
192 })
193
194 c.Run("Dart SASS, denied", func(c *qt.C) {
195 c.Parallel()
196 if !dartsass.Supports() {
197 c.Skip()
198 }
199 files := `
200 -- hugo.toml --
201 baseURL = "https://example.org"
202 [security]
203 [security.exec]
204 allow="none"
205 -- layouts/home.html --
206 {{ $scss := "body { color: #333; }" | resources.FromString "foo.scss" | css.Sass (dict "transpiler" "dartsass") }}
207 `
208 _, err := TestE(c, files)
209 c.Assert(err, qt.IsNotNil)
210 c.Assert(err, qt.ErrorMatches, `(?s).*sass(-embedded)?" is not whitelisted in policy "security\.exec\.allow".*`)
211 })
212
213 c.Run("resources.GetRemote, OK", func(c *qt.C) {
214 c.Parallel()
215 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
216 c.Cleanup(func() {
217 ts.Close()
218 })
219 files := fmt.Sprintf(`
220 -- hugo.toml --
221 baseURL = "https://example.org"
222 [security]
223 [security.http]
224 urls = ['.*']
225 -- layouts/home.html --
226 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
227 `, ts.URL)
228 Test(c, files)
229 })
230
231 c.Run("resources.GetRemote, denied method", func(c *qt.C) {
232 c.Parallel()
233 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
234 c.Cleanup(func() {
235 ts.Close()
236 })
237 files := fmt.Sprintf(`
238 -- hugo.toml --
239 baseURL = "https://example.org"
240 [security]
241 [security.http]
242 urls = ['.*']
243 -- layouts/home.html --
244 {{ $json := resources.GetRemote "%s/fruits.json" (dict "method" "DELETE" ) }}{{ $json.Content }}
245 `, ts.URL)
246 _, err := TestE(c, files)
247 c.Assert(err, qt.IsNotNil)
248 c.Assert(err, qt.ErrorMatches, `(?s).*"DELETE" is not whitelisted in policy "security\.http\.method".*`)
249 })
250
251 c.Run("resources.GetRemote, denied URL", func(c *qt.C) {
252 c.Parallel()
253 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
254 c.Cleanup(func() {
255 ts.Close()
256 })
257 files := fmt.Sprintf(`
258 -- hugo.toml --
259 baseURL = "https://example.org"
260 [security]
261 [security.http]
262 urls="none"
263 -- layouts/home.html --
264 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
265 `, ts.URL)
266 _, err := TestE(c, files)
267 c.Assert(err, qt.IsNotNil)
268 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`)
269 })
270
271 c.Run("resources.GetRemote, denied loopback URL by default", func(c *qt.C) {
272 c.Parallel()
273 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
274 c.Cleanup(func() {
275 ts.Close()
276 })
277 files := fmt.Sprintf(`
278 -- hugo.toml --
279 baseURL = "https://example.org"
280 -- layouts/home.html --
281 {{ $json := resources.GetRemote "%s/fruits.json" }}{{ $json.Content }}
282 `, ts.URL)
283 _, err := TestE(c, files)
284 c.Assert(err, qt.IsNotNil)
285 c.Assert(err, qt.ErrorMatches, `(?s).*is not whitelisted in policy "security\.http\.urls".*`)
286 })
287
288 c.Run("resources.GetRemote, fake JSON", func(c *qt.C) {
289 c.Parallel()
290 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
291 c.Cleanup(func() {
292 ts.Close()
293 })
294 files := fmt.Sprintf(`
295 -- hugo.toml --
296 baseURL = "https://example.org"
297 [security]
298 [security.http]
299 urls = ['.*']
300 -- layouts/home.html --
301 {{ $json := resources.GetRemote "%s/fakejson.json" }}{{ $json.Content }}
302 `, ts.URL)
303 _, err := TestE(c, files)
304 c.Assert(err, qt.IsNotNil)
305 c.Assert(err, qt.ErrorMatches, `(?s).*failed to resolve media type.*`)
306 })
307
308 c.Run("resources.GetRemote, fake JSON whitelisted", func(c *qt.C) {
309 c.Parallel()
310 ts := httptest.NewServer(http.FileServer(http.Dir("testdata/")))
311 c.Cleanup(func() {
312 ts.Close()
313 })
314 files := fmt.Sprintf(`
315 -- hugo.toml --
316 baseURL = "https://example.org"
317 [security]
318 [security.http]
319 urls = ['.*']
320 mediaTypes=["application/json"]
321 -- layouts/home.html --
322 {{ $json := resources.GetRemote "%s/fakejson.json" }}{{ $json.Content }}
323 `, ts.URL)
324 Test(c, files)
325 })
326 }