Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: move host attack protection to secure middleware

goenningcfacc4b4d937
diff --git a/app/middlewares/security.go b/app/middlewares/security.go
index 04e4ec67a..47805fc9a 100644
--- a/app/middlewares/security.go
+++ b/app/middlewares/security.go
@@ -4,14 +4,30 @@ import (
"fmt"
"strings"
+ "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
)
-// Secure adds web security related Http Headers to response
+// Secure middleware is responsible for
+// 1. Setting the HTTP Security Headers
+// 2. Protecting from Host attacks
func Secure() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c *web.Context) error {
+
+ // Host attack is only a problem if Fider is running on single host mode
+ if env.IsSingleHostMode() {
+ if c.Request.URL.Hostname() != env.Config.HostDomain {
+ log.Errorf(c, "Requested hostname '@{URLHostname}' does not match environment HOST_DOMAIN '@{HostDomain}'.", dto.Props{
+ "URLHostname": c.Request.URL.Hostname(),
+ "HostDomain": env.Config.HostDomain,
+ })
+ return c.NotFound()
+ }
+ }
+
cdnHost := env.Config.CDN.Host
if cdnHost != "" && !env.IsSingleHostMode() {
cdnHost = "*." + cdnHost
diff --git a/app/middlewares/security_test.go b/app/middlewares/security_test.go
index 6129a2738..47bac3a59 100644
--- a/app/middlewares/security_test.go
+++ b/app/middlewares/security_test.go
@@ -11,6 +11,20 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
+func TestSecure_HostMismatch(t *testing.T) {
+ RegisterT(t)
+ env.Config.HostDomain = "yoursite.com"
+
+ server := mock.NewSingleTenantServer()
+ server.Use(middlewares.Secure())
+
+ status, _ := server.WithURL("http://someothersite.com").Execute(func(c *web.Context) error {
+ return c.String(http.StatusOK, c.Tenant().Name)
+ })
+
+ Expect(status).Equals(http.StatusNotFound)
+}
+
func TestSecureWithoutCDN(t *testing.T) {
RegisterT(t)
@@ -23,7 +37,7 @@ func TestSecureWithoutCDN(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com ; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com ; img-src 'self' https: data: ; font-src 'self' https://fonts.gstatic.com data: ; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com ; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com ; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com ; img-src 'self' https: data: ; font-src 'self' https://fonts.gstatic.com data: ; object-src 'none'; media-src 'none'; connect-src 'self' https://ipinfo.io https://www.google-analytics.com ; frame-src 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
@@ -46,7 +60,7 @@ func TestSecureWithCDN(t *testing.T) {
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' https://fonts.gstatic.com data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com *.test.fider.io; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' https://fonts.gstatic.com data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://ipinfo.io https://www.google-analytics.com *.test.fider.io; frame-src 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
@@ -64,12 +78,12 @@ func TestSecureWithCDN_SingleHost(t *testing.T) {
server.Use(middlewares.Secure())
var ctxID string
- status, response := server.Execute(func(c *web.Context) error {
+ status, response := server.WithURL("http://test.fider.io").Execute(func(c *web.Context) error {
ctxID = c.ContextID()
return c.NoContent(http.StatusOK)
})
- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' https://fonts.gstatic.com data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com test.fider.io; frame-src 'self'"
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' https://fonts.gstatic.com data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://ipinfo.io https://www.google-analytics.com test.fider.io; frame-src 'self'"
Expect(status).Equals(http.StatusOK)
Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
diff --git a/app/middlewares/tenant.go b/app/middlewares/tenant.go
index a989defd7..573756748 100644
--- a/app/middlewares/tenant.go
+++ b/app/middlewares/tenant.go
@@ -3,7 +3,6 @@ package middlewares
import (
"net/http"
- "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
@@ -11,7 +10,6 @@ import (
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
- "github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
)
@@ -35,14 +33,6 @@ func SingleTenant() web.MiddlewareFunc {
if firstTenant.Result != nil && !firstTenant.Result.IsDisabled() {
c.SetTenant(firstTenant.Result)
-
- if c.Request.URL.Hostname() != env.Config.HostDomain {
- log.Errorf(c, "Requested hostname '@{URLHostname}' does not match environment HOST_DOMAIN '@{HostDomain}'.", dto.Props{
- "URLHostname": c.Request.URL.Hostname(),
- "HostDomain": env.Config.HostDomain,
- })
- return c.NotFound()
- }
}
return next(c)
diff --git a/app/middlewares/tenant_test.go b/app/middlewares/tenant_test.go
index 6b17e52fc..d289bd10e 100644
--- a/app/middlewares/tenant_test.go
+++ b/app/middlewares/tenant_test.go
@@ -13,7 +13,6 @@ import (
"github.com/getfider/fider/app/models/query"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/bus"
- "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/mock"
"github.com/getfider/fider/app/pkg/web"
)
@@ -248,25 +247,6 @@ func TestSingleTenant_WithTenants_ShouldSetFirstToContext(t *testing.T) {
Expect(response.Body.String()).Equals("MyCompany")
}
-func TestSingleTenant_HostMismatch(t *testing.T) {
- RegisterT(t)
- env.Config.HostDomain = "yoursite.com"
-
- bus.AddHandler(func(ctx context.Context, q *query.GetFirstTenant) error {
- q.Result = &entity.Tenant{Name: "MyCompany", Status: enum.TenantActive}
- return nil
- })
-
- server := mock.NewSingleTenantServer()
- server.Use(middlewares.SingleTenant())
-
- status, _ := server.WithURL("http://someothersite.com").Execute(func(c *web.Context) error {
- return c.String(http.StatusOK, c.Tenant().Name)
- })
-
- Expect(status).Equals(http.StatusNotFound)
-}
-
func TestBlockPendingTenants_Active(t *testing.T) {
RegisterT(t)