From 2accf58e2d142f94e212cd1e3989bba444db7cd5 Mon Sep 17 00:00:00 2001 From: Michael Barz Date: Mon, 22 Jan 2024 17:32:13 +0100 Subject: [PATCH] feat: add secure middleware --- internal/http/interceptors/secure/secure.go | 70 +++++++++++++++++++++ pkg/micro/ocdav/option.go | 22 ++++--- pkg/micro/ocdav/service.go | 7 ++- 3 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 internal/http/interceptors/secure/secure.go diff --git a/internal/http/interceptors/secure/secure.go b/internal/http/interceptors/secure/secure.go new file mode 100644 index 0000000000..928adb1164 --- /dev/null +++ b/internal/http/interceptors/secure/secure.go @@ -0,0 +1,70 @@ +package secure + +import ( + "net/http" + + "github.com/cs3org/reva/v2/pkg/rhttp/global" + "github.com/mitchellh/mapstructure" +) + +const ( + defaultPriority = 200 +) + +func init() { + global.RegisterMiddleware("secure", New) +} + +type secure struct { + ContentSecurityPolicy string `mapstructure:"content_security_policy"` + Priority int `mapstructure:"priority"` +} + +// New creates a new secure middleware. +func New(m map[string]interface{}) (global.Middleware, int, error) { + s := &secure{} + if err := mapstructure.Decode(m, s); err != nil { + return nil, 0, err + } + + if s.Priority == 0 { + s.Priority = defaultPriority + } + + if s.ContentSecurityPolicy == "" { + s.ContentSecurityPolicy = "frame-ancestors 'none'" + } + + return s.Handler, s.Priority, nil +} + +// Handler is the middleware function. +func (m *secure) Handler(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Indicates whether the browser is allowed to render this page in a ,