Integrate Secure middleware for enhanced HTTP security headers.
- commit
- 4daba37a8224ec5872d24e7cda06a2aee95679bb
- author
- bnema · · 2023-09-05 01:52 UTC
- parent
- 267770c
- signature
- unsigned
7 files · +47 -18
M cmd/main.go +0 -3
| @@ -6,7 +6,6 @@ import ( | |||
| 6 | 6 | "github.com/labstack/echo/v4" | |
| 7 | 7 | "gogs.bnema.dev/gordon-echo/internal/app" | |
| 8 | 8 | "gogs.bnema.dev/gordon-echo/internal/http/routes" | |
| 9 | - | "gogs.bnema.dev/gordon-echo/pkg/docker" | |
| 10 | 9 | "gogs.bnema.dev/gordon-echo/pkg/utils" | |
| 11 | 10 | ) | |
| 12 | 11 | ||
| @@ -25,8 +24,6 @@ func main() { | |||
| 25 | 24 | return | |
| 26 | 25 | } | |
| 27 | 26 | ||
| 28 | - | docker.CreateImageFromDockerfile() | |
| 29 | - | ||
| 30 | 27 | e := echo.New() | |
| 31 | 28 | e.HideBanner = true | |
| 32 | 29 | e.HidePort = true | |
| @@ -6,7 +6,6 @@ import ( | |||||
| 6 | "github.com/labstack/echo/v4" | 6 | "github.com/labstack/echo/v4" | ||
| 7 | "gogs.bnema.dev/gordon-echo/internal/app" | 7 | "gogs.bnema.dev/gordon-echo/internal/app" | ||
| 8 | "gogs.bnema.dev/gordon-echo/internal/http/routes" | 8 | "gogs.bnema.dev/gordon-echo/internal/http/routes" | ||
| 9 | - | "gogs.bnema.dev/gordon-echo/pkg/docker" | |||
| 10 | "gogs.bnema.dev/gordon-echo/pkg/utils" | 9 | "gogs.bnema.dev/gordon-echo/pkg/utils" | ||
| 11 | ) | 10 | ) | ||
| 12 | 11 | ||||
| @@ -25,8 +24,6 @@ func main() { | |||||
| 25 | return | 24 | return | ||
| 26 | } | 25 | } | ||
| 27 | 26 | ||||
| 28 | - | docker.CreateImageFromDockerfile() | |||
| 29 | - | ||||
| 30 | e := echo.New() | 27 | e := echo.New() | ||
| 31 | e.HideBanner = true | 28 | e.HideBanner = true | ||
| 32 | e.HidePort = true | 29 | e.HidePort = true | ||
M go.mod +9 -6
| @@ -4,7 +4,9 @@ go 1.21.0 | |||
| 4 | 4 | ||
| 5 | 5 | require ( | |
| 6 | 6 | github.com/PuerkitoBio/goquery v1.8.1 | |
| 7 | + | github.com/docker/docker v24.0.5+incompatible | |
| 7 | 8 | github.com/labstack/echo/v4 v4.11.1 | |
| 9 | + | github.com/tidwall/gjson v1.16.0 | |
| 8 | 10 | gopkg.in/yaml.v3 v3.0.1 | |
| 9 | 11 | ) | |
| 10 | 12 | ||
| @@ -13,33 +15,34 @@ require ( | |||
| 13 | 15 | github.com/andybalholm/cascadia v1.3.1 // indirect | |
| 14 | 16 | github.com/containerd/containerd v1.7.5 // indirect | |
| 15 | 17 | github.com/docker/distribution v2.8.2+incompatible // indirect | |
| 16 | - | github.com/docker/docker v24.0.5+incompatible // indirect | |
| 17 | 18 | github.com/docker/go-connections v0.4.0 // indirect | |
| 18 | 19 | github.com/docker/go-units v0.5.0 // indirect | |
| 19 | 20 | github.com/gogo/protobuf v1.3.2 // indirect | |
| 20 | 21 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | |
| 21 | - | github.com/klauspost/compress v1.16.7 // indirect | |
| 22 | - | github.com/moby/patternmatcher v0.6.0 // indirect | |
| 23 | - | github.com/moby/sys/sequential v0.5.0 // indirect | |
| 24 | 22 | github.com/gorilla/context v1.1.1 // indirect | |
| 25 | 23 | github.com/gorilla/securecookie v1.1.1 // indirect | |
| 26 | 24 | github.com/gorilla/sessions v1.2.1 // indirect | |
| 25 | + | github.com/klauspost/compress v1.16.7 // indirect | |
| 26 | + | github.com/moby/patternmatcher v0.6.0 // indirect | |
| 27 | + | github.com/moby/sys/sequential v0.5.0 // indirect | |
| 28 | + | github.com/moby/term v0.5.0 // indirect | |
| 29 | + | github.com/morikuni/aec v1.0.0 // indirect | |
| 27 | 30 | github.com/opencontainers/go-digest v1.0.0 // indirect | |
| 28 | 31 | github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect | |
| 29 | 32 | github.com/opencontainers/runc v1.1.9 // indirect | |
| 30 | 33 | github.com/pkg/errors v0.9.1 // indirect | |
| 31 | 34 | github.com/sirupsen/logrus v1.9.0 // indirect | |
| 32 | - | github.com/tidwall/gjson v1.16.0 // indirect | |
| 33 | 35 | github.com/tidwall/match v1.1.1 // indirect | |
| 34 | 36 | github.com/tidwall/pretty v1.2.1 // indirect | |
| 35 | 37 | golang.org/x/mod v0.9.0 // indirect | |
| 36 | 38 | golang.org/x/time v0.3.0 // indirect | |
| 37 | 39 | golang.org/x/tools v0.7.0 // indirect | |
| 40 | + | gotest.tools/v3 v3.5.0 // indirect | |
| 38 | 41 | ) | |
| 39 | 42 | ||
| 40 | 43 | require ( | |
| 41 | 44 | github.com/labstack/echo-contrib v0.15.0 | |
| 42 | - | github.com/labstack/gommon v0.4.0 // indirect | |
| 45 | + | github.com/labstack/gommon v0.4.0 | |
| 43 | 46 | github.com/mattn/go-colorable v0.1.13 // indirect | |
| 44 | 47 | github.com/mattn/go-isatty v0.0.19 // indirect | |
| 45 | 48 | github.com/rs/zerolog v1.30.0 | |
| @@ -4,7 +4,9 @@ go 1.21.0 | |||||
| 4 | 4 | ||||
| 5 | require ( | 5 | require ( | ||
| 6 | github.com/PuerkitoBio/goquery v1.8.1 | 6 | github.com/PuerkitoBio/goquery v1.8.1 | ||
| 7 | + | github.com/docker/docker v24.0.5+incompatible | |||
| 7 | github.com/labstack/echo/v4 v4.11.1 | 8 | github.com/labstack/echo/v4 v4.11.1 | ||
| 9 | + | github.com/tidwall/gjson v1.16.0 | |||
| 8 | gopkg.in/yaml.v3 v3.0.1 | 10 | gopkg.in/yaml.v3 v3.0.1 | ||
| 9 | ) | 11 | ) | ||
| 10 | 12 | ||||
| @@ -13,33 +15,34 @@ require ( | |||||
| 13 | github.com/andybalholm/cascadia v1.3.1 // indirect | 15 | github.com/andybalholm/cascadia v1.3.1 // indirect | ||
| 14 | github.com/containerd/containerd v1.7.5 // indirect | 16 | github.com/containerd/containerd v1.7.5 // indirect | ||
| 15 | github.com/docker/distribution v2.8.2+incompatible // indirect | 17 | github.com/docker/distribution v2.8.2+incompatible // indirect | ||
| 16 | - | github.com/docker/docker v24.0.5+incompatible // indirect | |||
| 17 | github.com/docker/go-connections v0.4.0 // indirect | 18 | github.com/docker/go-connections v0.4.0 // indirect | ||
| 18 | github.com/docker/go-units v0.5.0 // indirect | 19 | github.com/docker/go-units v0.5.0 // indirect | ||
| 19 | github.com/gogo/protobuf v1.3.2 // indirect | 20 | github.com/gogo/protobuf v1.3.2 // indirect | ||
| 20 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | 21 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | ||
| 21 | - | github.com/klauspost/compress v1.16.7 // indirect | |||
| 22 | - | github.com/moby/patternmatcher v0.6.0 // indirect | |||
| 23 | - | github.com/moby/sys/sequential v0.5.0 // indirect | |||
| 24 | github.com/gorilla/context v1.1.1 // indirect | 22 | github.com/gorilla/context v1.1.1 // indirect | ||
| 25 | github.com/gorilla/securecookie v1.1.1 // indirect | 23 | github.com/gorilla/securecookie v1.1.1 // indirect | ||
| 26 | github.com/gorilla/sessions v1.2.1 // indirect | 24 | github.com/gorilla/sessions v1.2.1 // indirect | ||
| 25 | + | github.com/klauspost/compress v1.16.7 // indirect | |||
| 26 | + | github.com/moby/patternmatcher v0.6.0 // indirect | |||
| 27 | + | github.com/moby/sys/sequential v0.5.0 // indirect | |||
| 28 | + | github.com/moby/term v0.5.0 // indirect | |||
| 29 | + | github.com/morikuni/aec v1.0.0 // indirect | |||
| 27 | github.com/opencontainers/go-digest v1.0.0 // indirect | 30 | github.com/opencontainers/go-digest v1.0.0 // indirect | ||
| 28 | github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect | 31 | github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect | ||
| 29 | github.com/opencontainers/runc v1.1.9 // indirect | 32 | github.com/opencontainers/runc v1.1.9 // indirect | ||
| 30 | github.com/pkg/errors v0.9.1 // indirect | 33 | github.com/pkg/errors v0.9.1 // indirect | ||
| 31 | github.com/sirupsen/logrus v1.9.0 // indirect | 34 | github.com/sirupsen/logrus v1.9.0 // indirect | ||
| 32 | - | github.com/tidwall/gjson v1.16.0 // indirect | |||
| 33 | github.com/tidwall/match v1.1.1 // indirect | 35 | github.com/tidwall/match v1.1.1 // indirect | ||
| 34 | github.com/tidwall/pretty v1.2.1 // indirect | 36 | github.com/tidwall/pretty v1.2.1 // indirect | ||
| 35 | golang.org/x/mod v0.9.0 // indirect | 37 | golang.org/x/mod v0.9.0 // indirect | ||
| 36 | golang.org/x/time v0.3.0 // indirect | 38 | golang.org/x/time v0.3.0 // indirect | ||
| 37 | golang.org/x/tools v0.7.0 // indirect | 39 | golang.org/x/tools v0.7.0 // indirect | ||
| 40 | + | gotest.tools/v3 v3.5.0 // indirect | |||
| 38 | ) | 41 | ) | ||
| 39 | 42 | ||||
| 40 | require ( | 43 | require ( | ||
| 41 | github.com/labstack/echo-contrib v0.15.0 | 44 | github.com/labstack/echo-contrib v0.15.0 | ||
| 42 | - | github.com/labstack/gommon v0.4.0 // indirect | 45 | + | github.com/labstack/gommon v0.4.0 |
| 43 | github.com/mattn/go-colorable v0.1.13 // indirect | 46 | github.com/mattn/go-colorable v0.1.13 // indirect | ||
| 44 | github.com/mattn/go-isatty v0.0.19 // indirect | 47 | github.com/mattn/go-isatty v0.0.19 // indirect | ||
| 45 | github.com/rs/zerolog v1.30.0 | 48 | github.com/rs/zerolog v1.30.0 | ||
M go.sum +17 -8 generated file: collapsed
A internal/http/middlewares/secure.go +18 -0
| @@ -0,0 +1,18 @@ | |||
| 1 | + | package middlewares | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "github.com/labstack/echo/v4" | |
| 5 | + | "github.com/labstack/echo/v4/middleware" | |
| 6 | + | ) | |
| 7 | + | ||
| 8 | + | func SecureMiddleware() echo.MiddlewareFunc { | |
| 9 | + | return middleware.SecureWithConfig(middleware.SecureConfig{ | |
| 10 | + | XSSProtection: "1; mode=block", | |
| 11 | + | ContentTypeNosniff: "nosniff", | |
| 12 | + | XFrameOptions: "SAMEORIGIN", | |
| 13 | + | HSTSMaxAge: 3600, | |
| 14 | + | HSTSExcludeSubdomains: false, | |
| 15 | + | ContentSecurityPolicy: "default-src 'self'", | |
| 16 | + | // Skipper: YourCustomSkipperFunction, // If you have a custom skipper | |
| 17 | + | }) | |
| 18 | + | } | |
| @@ -0,0 +1,18 @@ | |||||
| 1 | + | package middlewares | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "github.com/labstack/echo/v4" | |||
| 5 | + | "github.com/labstack/echo/v4/middleware" | |||
| 6 | + | ) | |||
| 7 | + | ||||
| 8 | + | func SecureMiddleware() echo.MiddlewareFunc { | |||
| 9 | + | return middleware.SecureWithConfig(middleware.SecureConfig{ | |||
| 10 | + | XSSProtection: "1; mode=block", | |||
| 11 | + | ContentTypeNosniff: "nosniff", | |||
| 12 | + | XFrameOptions: "SAMEORIGIN", | |||
| 13 | + | HSTSMaxAge: 3600, | |||
| 14 | + | HSTSExcludeSubdomains: false, | |||
| 15 | + | ContentSecurityPolicy: "default-src 'self'", | |||
| 16 | + | // Skipper: YourCustomSkipperFunction, // If you have a custom skipper | |||
| 17 | + | }) | |||
| 18 | + | } | |||
M internal/http/routes/router.go +1 -1
| @@ -13,7 +13,7 @@ func ConfigureRouter(e *echo.Echo, app *app.App) *echo.Echo { | |||
| 13 | 13 | // Register middlewares | |
| 14 | 14 | e.Use(middlewares.NewRequestLoggerMiddleware(app.HTTPLogger.Logger)) | |
| 15 | 15 | e.Use(middlewares.LanguageDetectionMiddleware) | |
| 16 | - | ||
| 16 | + | // e.Use(middlewares.SecureMiddleware()) // Uncomment this line to enable the secure middleware | |
| 17 | 17 | // Register routes | |
| 18 | 18 | e = bindStaticAdminUI(e) | |
| 19 | 19 | ||
| @@ -13,7 +13,7 @@ func ConfigureRouter(e *echo.Echo, app *app.App) *echo.Echo { | |||||
| 13 | // Register middlewares | 13 | // Register middlewares | ||
| 14 | e.Use(middlewares.NewRequestLoggerMiddleware(app.HTTPLogger.Logger)) | 14 | e.Use(middlewares.NewRequestLoggerMiddleware(app.HTTPLogger.Logger)) | ||
| 15 | e.Use(middlewares.LanguageDetectionMiddleware) | 15 | e.Use(middlewares.LanguageDetectionMiddleware) | ||
| 16 | - | 16 | + | // e.Use(middlewares.SecureMiddleware()) // Uncomment this line to enable the secure middleware | |
| 17 | // Register routes | 17 | // Register routes | ||
| 18 | e = bindStaticAdminUI(e) | 18 | e = bindStaticAdminUI(e) | ||
| 19 | 19 | ||||
A internal/ui/public/tmp/build-errors.log +1 -0
| @@ -0,0 +1,1 @@ | |||
| 1 | + | exit status 1⏎̸ | |
| @@ -0,0 +1,1 @@ | |||||
| 1 | + | exit status 1⏎̸ | |||
M logs/app.log +1 -0
| @@ -48,3 +48,4 @@ | |||
| 48 | 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | |
| 49 | 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | |
| 50 | 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | |
| 51 | + | {"level":"info","time":"2023-09-05T03:50:53+02:00","message":"Server is listening on port 1323"} | |
| @@ -48,3 +48,4 @@ | |||||
| 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | ||
| 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | ||
| 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | ||
| 51 | + | {"level":"info","time":"2023-09-05T03:50:53+02:00","message":"Server is listening on port 1323"} | |||