Răsfoiți Sursa

refactor(ui): move CSP policy building into view package

cevdet 1 lună în urmă
părinte
comite
246f37d731

+ 0 - 36
internal/template/csp_header_test.go

@@ -1,36 +0,0 @@
-// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
-// SPDX-License-Identifier: Apache-2.0
-
-package template
-
-import (
-	"strings"
-	"testing"
-)
-
-// CSP must be delivered as an HTTP header, never as a meta tag.
-// See deadhead rule meta/http-equiv-content-security-policy (harmful):
-// meta policies drop frame-ancestors/report-uri/sandbox, stop the preload
-// scanner, and apply late. The template helper must therefore return a
-// header value, not a <meta> element.
-func TestCSPDoesNotReturnMetaTag(t *testing.T) {
-	got := CSPPolicy(nil, "1234")
-
-	if strings.Contains(strings.ToLower(got), "<meta") {
-		t.Fatalf("CSPPolicy() must not return a <meta> tag (use HTTP header instead), got %q", got)
-	}
-
-	if strings.Contains(strings.ToLower(got), "http-equiv") {
-		t.Fatalf("CSPPolicy() must not return http-equiv content (use HTTP header instead), got %q", got)
-	}
-
-	for _, want := range []string{
-		`default-src 'none';`,
-		`script-src 'nonce-1234'`,
-		`style-src 'nonce-1234';`,
-	} {
-		if !strings.Contains(got, want) {
-			t.Errorf("CSPPolicy() = %q, want it to contain %q", got, want)
-		}
-	}
-}

+ 0 - 35
internal/template/functions.go

@@ -171,41 +171,6 @@ func (f *funcMap) iconFunc() func(string) template.HTML {
 	}
 }
 
-func CSPPolicy(user *model.User, nonce string) string {
-	policies := map[string]string{
-		"default-src":               "'none'",
-		"frame-src":                 "*",
-		"img-src":                   "* data:",
-		"manifest-src":              "'self'",
-		"media-src":                 "*",
-		"require-trusted-types-for": "'script'",
-		"script-src":                "'nonce-" + nonce + "' 'strict-dynamic'",
-		"style-src":                 "'nonce-" + nonce + "'",
-		"trusted-types":             "html url",
-		"connect-src":               "'self'",
-	}
-
-	if user != nil {
-		if user.ExternalFontHosts != "" {
-			policies["font-src"] = user.ExternalFontHosts
-			if user.Stylesheet != "" {
-				policies["style-src"] += " " + user.ExternalFontHosts
-			}
-		}
-	}
-
-	var policy strings.Builder
-	policy.Grow(350)
-	for key, value := range policies {
-		policy.WriteString(key)
-		policy.WriteString(" ")
-		policy.WriteString(value)
-		policy.WriteString("; ")
-	}
-
-	return policy.String()
-}
-
 func dict(values ...any) (map[string]any, error) {
 	if len(values)%2 != 0 {
 		return nil, errors.New("dict expects an even number of arguments")

+ 0 - 90
internal/template/functions_test.go

@@ -10,7 +10,6 @@ import (
 	"time"
 
 	"miniflux.app/v2/internal/locale"
-	"miniflux.app/v2/internal/model"
 )
 
 func TestDict(t *testing.T) {
@@ -271,92 +270,3 @@ func TestQueryString(t *testing.T) {
 		t.Fatalf(`Expected empty query string, got %q`, got)
 	}
 }
-
-func TestCSPExternalFont(t *testing.T) {
-	want := []string{
-		`default-src 'none';`,
-		`img-src * data:;`,
-		`media-src *;`,
-		`frame-src *;`,
-		`style-src 'nonce-1234';`,
-		`script-src 'nonce-1234'`,
-		`'strict-dynamic';`,
-		`font-src test.com;`,
-		`require-trusted-types-for 'script';`,
-		`trusted-types html url;`,
-		`manifest-src 'self';`,
-	}
-	got := CSPPolicy(&model.User{ExternalFontHosts: "test.com"}, "1234")
-
-	for _, value := range want {
-		if !strings.Contains(got, value) {
-			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
-		}
-	}
-}
-
-func TestCSPNoUser(t *testing.T) {
-	want := []string{
-		`default-src 'none';`,
-		`img-src * data:;`,
-		`media-src *;`,
-		`frame-src *;`,
-		`style-src 'nonce-1234';`,
-		`script-src 'nonce-1234'`,
-		`'strict-dynamic';`,
-		`require-trusted-types-for 'script';`,
-		`trusted-types html url;`,
-		`manifest-src 'self';`,
-	}
-	got := CSPPolicy(nil, "1234")
-
-	for _, value := range want {
-		if !strings.Contains(got, value) {
-			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
-		}
-	}
-}
-
-func TestCSPCustomJSExternalFont(t *testing.T) {
-	want := []string{
-		`default-src 'none';`,
-		`img-src * data:;`,
-		`media-src *;`,
-		`frame-src *;`,
-		`style-src 'nonce-1234';`,
-		`script-src 'nonce-1234'`,
-		`'strict-dynamic';`,
-		`require-trusted-types-for 'script';`,
-		`trusted-types html url;`,
-		`manifest-src 'self';`,
-	}
-	got := CSPPolicy(&model.User{ExternalFontHosts: "test.com", CustomJS: "alert(1)"}, "1234")
-
-	for _, value := range want {
-		if !strings.Contains(got, value) {
-			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
-		}
-	}
-}
-
-func TestCSPExternalFontStylesheet(t *testing.T) {
-	want := []string{
-		`default-src 'none';`,
-		`img-src * data:;`,
-		`media-src *;`,
-		`frame-src *;`,
-		`style-src 'nonce-1234' test.com;`,
-		`script-src 'nonce-1234'`,
-		`'strict-dynamic';`,
-		`require-trusted-types-for 'script';`,
-		`trusted-types html url;`,
-		`manifest-src 'self';`,
-	}
-	got := CSPPolicy(&model.User{ExternalFontHosts: "test.com", Stylesheet: "a {color: red;}"}, "1234")
-
-	for _, value := range want {
-		if !strings.Contains(got, value) {
-			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
-		}
-	}
-}

+ 111 - 0
internal/ui/view/csp_test.go

@@ -0,0 +1,111 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package view
+
+import (
+	"strings"
+	"testing"
+
+	"miniflux.app/v2/internal/model"
+)
+
+func TestCSPExternalFont(t *testing.T) {
+	want := []string{
+		`default-src 'none';`,
+		`img-src * data:;`,
+		`media-src *;`,
+		`frame-src *;`,
+		`style-src 'nonce-1234';`,
+		`script-src 'nonce-1234'`,
+		`'strict-dynamic';`,
+		`font-src test.com;`,
+		`require-trusted-types-for 'script';`,
+		`trusted-types html url;`,
+		`manifest-src 'self';`,
+	}
+	got := csp(&model.User{ExternalFontHosts: "test.com"}, "1234")
+
+	for _, value := range want {
+		if !strings.Contains(got, value) {
+			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
+		}
+	}
+}
+
+func TestCSPNoUser(t *testing.T) {
+	want := []string{
+		`default-src 'none';`,
+		`img-src * data:;`,
+		`media-src *;`,
+		`frame-src *;`,
+		`style-src 'nonce-1234';`,
+		`script-src 'nonce-1234'`,
+		`'strict-dynamic';`,
+		`require-trusted-types-for 'script';`,
+		`trusted-types html url;`,
+		`manifest-src 'self';`,
+	}
+	got := csp(nil, "1234")
+
+	for _, value := range want {
+		if !strings.Contains(got, value) {
+			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
+		}
+	}
+}
+
+func TestCSPCustomJSExternalFont(t *testing.T) {
+	want := []string{
+		`default-src 'none';`,
+		`img-src * data:;`,
+		`media-src *;`,
+		`frame-src *;`,
+		`style-src 'nonce-1234';`,
+		`script-src 'nonce-1234'`,
+		`'strict-dynamic';`,
+		`require-trusted-types-for 'script';`,
+		`trusted-types html url;`,
+		`manifest-src 'self';`,
+	}
+	got := csp(&model.User{ExternalFontHosts: "test.com", CustomJS: "alert(1)"}, "1234")
+
+	for _, value := range want {
+		if !strings.Contains(got, value) {
+			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
+		}
+	}
+}
+
+func TestCSPExternalFontStylesheet(t *testing.T) {
+	want := []string{
+		`default-src 'none';`,
+		`img-src * data:;`,
+		`media-src *;`,
+		`frame-src *;`,
+		`style-src 'nonce-1234' test.com;`,
+		`script-src 'nonce-1234'`,
+		`'strict-dynamic';`,
+		`require-trusted-types-for 'script';`,
+		`trusted-types html url;`,
+		`manifest-src 'self';`,
+	}
+	got := csp(&model.User{ExternalFontHosts: "test.com", Stylesheet: "a {color: red;}"}, "1234")
+
+	for _, value := range want {
+		if !strings.Contains(got, value) {
+			t.Errorf(`Unexpected result, didn't find %q in %q`, value, got)
+		}
+	}
+}
+
+// CSP must not come back as a meta tag: it ships as a response header.
+// See deadhead rule meta/http-equiv-content-security-policy (harmful).
+func TestCSPDoesNotReturnMetaTag(t *testing.T) {
+	got := csp(nil, "1234")
+
+	lower := strings.ToLower(got)
+	if strings.Contains(lower, "<meta") || strings.Contains(lower, "http-equiv") {
+		t.Fatalf("csp() must return a header value, not a meta tag, got %q", got)
+	}
+}

+ 43 - 1
internal/ui/view/view.go

@@ -5,6 +5,7 @@ package view // import "miniflux.app/v2/internal/ui/view"
 
 import (
 	"net/http"
+	"strings"
 
 	"miniflux.app/v2/internal/config"
 	"miniflux.app/v2/internal/crypto"
@@ -44,7 +45,48 @@ func (v *view) CSP() string {
 		user = u
 	}
 	nonce, _ := v.params["cspNonce"].(string)
-	return template.CSPPolicy(user, nonce)
+	return csp(user, nonce)
+}
+
+// csp builds the Content-Security-Policy header value for templated pages.
+// It lives in the view layer because every templated page ships the policy
+// as a response header (never as a meta tag, see deadhead rule
+// meta/http-equiv-content-security-policy): meta policies drop
+// frame-ancestors/report-uri/sandbox, stop the preload scanner, and apply
+// late.
+func csp(user *model.User, nonce string) string {
+	policies := map[string]string{
+		"default-src":               "'none'",
+		"frame-src":                 "*",
+		"img-src":                   "* data:",
+		"manifest-src":              "'self'",
+		"media-src":                 "*",
+		"require-trusted-types-for": "'script'",
+		"script-src":                "'nonce-" + nonce + "' 'strict-dynamic'",
+		"style-src":                 "'nonce-" + nonce + "'",
+		"trusted-types":             "html url",
+		"connect-src":               "'self'",
+	}
+
+	if user != nil {
+		if user.ExternalFontHosts != "" {
+			policies["font-src"] = user.ExternalFontHosts
+			if user.Stylesheet != "" {
+				policies["style-src"] += " " + user.ExternalFontHosts
+			}
+		}
+	}
+
+	var policy strings.Builder
+	policy.Grow(350)
+	for key, value := range policies {
+		policy.WriteString(key)
+		policy.WriteString(" ")
+		policy.WriteString(value)
+		policy.WriteString("; ")
+	}
+
+	return policy.String()
 }
 
 // HTML renders the given template and writes it as an HTML response with