|
|
@@ -7,11 +7,13 @@ import (
|
|
|
"context"
|
|
|
"net/http"
|
|
|
"net/http/httptest"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
+ "miniflux.app/v2/internal/template"
|
|
|
)
|
|
|
|
|
|
func configureTestOptions(t *testing.T) {
|
|
|
@@ -29,13 +31,18 @@ func configureTestOptions(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+func webSessionRequest(path string) (*http.Request, *model.WebSession) {
|
|
|
+ session, _ := model.NewWebSession("test", "127.0.0.1")
|
|
|
+ r := httptest.NewRequest(http.MethodGet, path, nil)
|
|
|
+ return r.WithContext(context.WithValue(r.Context(), request.WebSessionContextKey, session)), session
|
|
|
+}
|
|
|
+
|
|
|
func TestNewPublicOmitsSessionSecrets(t *testing.T) {
|
|
|
configureTestOptions(t)
|
|
|
session, _ := model.NewWebSession("test", "127.0.0.1")
|
|
|
session.SetSuccessMessage("saved")
|
|
|
|
|
|
- r := httptest.NewRequest(http.MethodGet, "/share/code", nil)
|
|
|
- r = r.WithContext(context.WithValue(r.Context(), request.WebSessionContextKey, session))
|
|
|
+ r, _ := webSessionRequest("/share/code")
|
|
|
|
|
|
publicView := NewPublic(nil, r)
|
|
|
|
|
|
@@ -53,10 +60,8 @@ func TestNewPublicOmitsSessionSecrets(t *testing.T) {
|
|
|
|
|
|
func TestNewIncludesCSRFToken(t *testing.T) {
|
|
|
configureTestOptions(t)
|
|
|
- session, _ := model.NewWebSession("test", "127.0.0.1")
|
|
|
|
|
|
- r := httptest.NewRequest(http.MethodGet, "/unread", nil)
|
|
|
- r = r.WithContext(context.WithValue(r.Context(), request.WebSessionContextKey, session))
|
|
|
+ r, session := webSessionRequest("/unread")
|
|
|
|
|
|
defaultView := New(nil, r)
|
|
|
|
|
|
@@ -64,3 +69,54 @@ func TestNewIncludesCSRFToken(t *testing.T) {
|
|
|
t.Fatalf(`Unexpected CSRF token, got %v`, csrf)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// Every HTML view must carry a per-request CSP nonce and be able to
|
|
|
+// render its Content-Security-Policy header value (no meta tag).
|
|
|
+func TestViewSetsCSPNonceAndPolicy(t *testing.T) {
|
|
|
+ configureTestOptions(t)
|
|
|
+
|
|
|
+ engine := template.NewEngine("")
|
|
|
+ r, _ := webSessionRequest("/")
|
|
|
+
|
|
|
+ v := New(engine, r)
|
|
|
+
|
|
|
+ nonce, ok := v.params["cspNonce"].(string)
|
|
|
+ if !ok || nonce == "" {
|
|
|
+ t.Fatalf("view params must contain a non-empty cspNonce, got %v", v.params["cspNonce"])
|
|
|
+ }
|
|
|
+
|
|
|
+ policy := v.CSP()
|
|
|
+ if strings.Contains(strings.ToLower(policy), "<meta") {
|
|
|
+ t.Fatalf("view CSP() must not return a <meta> tag, got %q", policy)
|
|
|
+ }
|
|
|
+ if !strings.Contains(policy, "'nonce-"+nonce+"'") {
|
|
|
+ t.Fatalf("view CSP() must contain the request nonce, nonce=%q policy=%q", nonce, policy)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestViewHTMLSetsCSPHeaderWithMatchingNonce(t *testing.T) {
|
|
|
+ configureTestOptions(t)
|
|
|
+
|
|
|
+ engine := template.NewEngine("")
|
|
|
+ engine.ParseTemplates()
|
|
|
+ r, _ := webSessionRequest("/")
|
|
|
+
|
|
|
+ w := httptest.NewRecorder()
|
|
|
+
|
|
|
+ v := New(engine, r)
|
|
|
+ v.HTML(w, r, "offline")
|
|
|
+
|
|
|
+ resp := w.Result()
|
|
|
+ policy := resp.Header.Get("Content-Security-Policy")
|
|
|
+ if policy == "" {
|
|
|
+ t.Fatal("view HTML() must set a Content-Security-Policy header")
|
|
|
+ }
|
|
|
+ if strings.Contains(strings.ToLower(policy), "<meta") {
|
|
|
+ t.Fatalf("CSP header must be a header value, not a meta tag, got %q", policy)
|
|
|
+ }
|
|
|
+
|
|
|
+ body := w.Body.String()
|
|
|
+ if strings.Contains(strings.ToLower(body), `http-equiv="content-security-policy"`) {
|
|
|
+ t.Fatal("rendered HTML must not contain a meta Content-Security-Policy tag")
|
|
|
+ }
|
|
|
+}
|