Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feat(security): add frame-ancestors, HSTS and Permissions-Policy headers

Matt Roberts63121e25a863
diff --git a/app/middlewares/security.go b/app/middlewares/security.go
index 2a660c2c3..97181133b 100644
--- a/app/middlewares/security.go
+++ b/app/middlewares/security.go
@@ -9,6 +9,9 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
+// permissionsPolicy disables browser features that Fider never uses
+const permissionsPolicy = "camera=(), microphone=(), geolocation=(), payment=(), usb=()"
+
// Secure middleware is responsible for
// 1. Setting the HTTP Security Headers
// 2. Protecting from Host attacks
@@ -32,6 +35,12 @@ func Secure() web.MiddlewareFunc {
c.Response.Header().Set("X-XSS-Protection", "1; mode=block")
c.Response.Header().Set("X-Content-Type-Options", "nosniff")
c.Response.Header().Set("Referrer-Policy", "no-referrer-when-downgrade")
+ c.Response.Header().Set("Permissions-Policy", permissionsPolicy)
+
+ // Only when Fider terminates TLS itself. Behind a reverse proxy, HSTS is the operator's decision.
+ if env.Config.TLS.Automatic || env.Config.TLS.Certificate != "" {
+ c.Response.Header().Set("Strict-Transport-Security", "max-age=15552000")
+ }
return next(c)
}
}
diff --git a/app/middlewares/security_test.go b/app/middlewares/security_test.go
index 3a8f6da85..a3982ed53 100644
--- a/app/middlewares/security_test.go
+++ b/app/middlewares/security_test.go
@@ -24,13 +24,46 @@ func TestSecureWithoutCDN(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "'; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self'; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "'; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self'; frame-src 'self'; frame-ancestors 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
Expect(response.Header().Get("X-XSS-Protection")).Equals("1; mode=block")
Expect(response.Header().Get("X-Content-Type-Options")).Equals("nosniff")
Expect(response.Header().Get("Referrer-Policy")).Equals("no-referrer-when-downgrade")
+ Expect(response.Header().Get("Permissions-Policy")).Equals("camera=(), microphone=(), geolocation=(), payment=(), usb=()")
+ Expect(response.Header().Get("Strict-Transport-Security")).Equals("")
+}
+
+func TestSecure_HSTS(t *testing.T) {
+ RegisterT(t)
+
+ testCases := []struct {
+ name string
+ automatic bool
+ certificate string
+ expected string
+ }{
+ {"plain HTTP (e.g. behind a reverse proxy)", false, "", ""},
+ {"automatic TLS", true, "", "max-age=15552000"},
+ {"configured certificate", false, "cert.pem", "max-age=15552000"},
+ }
+
+ for _, tc := range testCases {
+ env.Config.TLS.Automatic = tc.automatic
+ env.Config.TLS.Certificate = tc.certificate
+
+ server := mock.NewServer()
+ server.Use(middlewares.Secure())
+
+ _, response := server.
+ AddHeader("X-Forwarded-Proto", "https").
+ Execute(func(c *web.Context) error {
+ return c.NoContent(http.StatusOK)
+ })
+
+ Expect(response.Header().Get("Strict-Transport-Security")).Equals(tc.expected)
+ }
}
func TestSecureWithCDN(t *testing.T) {
@@ -47,7 +80,7 @@ func TestSecureWithCDN(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' *.test.fider.io; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' *.test.fider.io; frame-src 'self'; frame-ancestors 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
@@ -70,7 +103,7 @@ func TestSecureWithCDN_SingleHost(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' test.fider.io; script-src 'self' 'nonce-" + ctxID + "' test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' test.fider.io; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' test.fider.io; script-src 'self' 'nonce-" + ctxID + "' test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' test.fider.io; frame-src 'self'; frame-ancestors 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
@@ -93,7 +126,7 @@ func TestSecureWithGoogleAnalytics(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "' https://www.googletagmanager.com; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com https://*.g.doubleclick.net https://pagead2.googlesyndication.com; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "' https://www.googletagmanager.com; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com https://*.g.doubleclick.net https://pagead2.googlesyndication.com; frame-src 'self'; frame-ancestors 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.go
index b5763ddff..3a6c1ae31 100644
--- a/app/pkg/web/engine.go
+++ b/app/pkg/web/engine.go
@@ -37,13 +37,16 @@ var (
cspMedia = "media-src 'none'"
cspConnect = "connect-src 'self'%[4]s%[2]s"
+ // Fider has no embeddable pages, so other sites must not frame it (clickjacking)
+ cspFrameAncestors = "frame-ancestors 'self'"
+
// Google Analytics hosts are only allowed when GOOGLE_ANALYTICS is set (https://developers.google.com/tag-platform/security/guides/csp)
CspGoogleAnalyticsScript = " https://www.googletagmanager.com"
CspGoogleAnalyticsConnect = " https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com https://*.g.doubleclick.net https://pagead2.googlesyndication.com"
//CspPolicyTemplate is the template used to generate the policy
//Args: 1 = nonce, 2 = CDN host, 3 = analytics script hosts, 4 = analytics connect hosts
- CspPolicyTemplate = fmt.Sprintf("%s; %s; %s; %s; %s; %s; %s; %s; %s; %s", cspBase, cspDefault, cspStyle, cspScript, cspImage, cspFont, cspObject, cspMedia, cspConnect, cspFrame)
+ CspPolicyTemplate = fmt.Sprintf("%s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s", cspBase, cspDefault, cspStyle, cspScript, cspImage, cspFont, cspObject, cspMedia, cspConnect, cspFrame, cspFrameAncestors)
)
type notFoundHandler struct {