-
Notifications
You must be signed in to change notification settings - Fork 13
refactor: add clickjacking protection headers #1195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,25 @@ import ( | |
| "github.com/device-management-toolkit/console/pkg/logger" | ||
| ) | ||
|
|
||
| const ( | ||
| xFrameOptionsHeaderValue = "SAMEORIGIN" | ||
| contentSecurityPolicyHeaderValue = "frame-ancestors 'self'" | ||
| ) | ||
|
|
||
| func clickjackingProtectionMiddleware() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| c.Header("X-Frame-Options", xFrameOptionsHeaderValue) | ||
| c.Header("Content-Security-Policy", contentSecurityPolicyHeaderValue) | ||
| c.Next() | ||
| } | ||
| } | ||
|
|
||
| // NewRouter -. | ||
| func NewRouter(handler *gin.Engine, l logger.Interface, t usecase.Usecases, cfg *config.Config) { | ||
| // Options | ||
| handler.Use(gin.Logger()) | ||
| handler.Use(gin.Recovery()) | ||
| handler.Use(clickjackingProtectionMiddleware()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current approach adds these headers for container and headless deployments as well. I recommend limiting them to the standalone Console build with the embedded UI, since container/headless deployments expose only REST APIs and do not serve browser-rendered content where clickjacking or browser capability policies apply. @rsdmike / @MadhaviLosetty : Do you agree on this ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rsdmike @MadhaviLosetty please let us know your thoughts |
||
|
|
||
| // Add Prometheus middleware for automatic HTTP metrics | ||
| // Don't automatically register /metrics endpoint - we have our own | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package httpapi | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/device-management-toolkit/console/config" | ||
| "github.com/device-management-toolkit/console/internal/usecase" | ||
| "github.com/device-management-toolkit/console/pkg/logger" | ||
| ) | ||
|
|
||
| func TestClickjackingProtectionMiddlewareSetsHeaders(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| engine := gin.New() | ||
| engine.Use(clickjackingProtectionMiddleware()) | ||
| engine.GET("/healthz", func(c *gin.Context) { | ||
| c.Status(http.StatusOK) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/healthz", http.NoBody) | ||
| w := httptest.NewRecorder() | ||
|
|
||
| engine.ServeHTTP(w, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, w.Code) | ||
| require.Equal(t, xFrameOptionsHeaderValue, w.Header().Get("X-Frame-Options")) | ||
| require.Equal(t, contentSecurityPolicyHeaderValue, w.Header().Get("Content-Security-Policy")) | ||
| } | ||
|
|
||
| //nolint:paralleltest // mutates shared global config.ConsoleConfig | ||
| func TestNewRouterAuthorizeHasClickjackingHeaders(t *testing.T) { | ||
| prev := config.ConsoleConfig | ||
| config.ConsoleConfig = &config.Config{} | ||
|
|
||
| t.Cleanup(func() { config.ConsoleConfig = prev }) | ||
|
|
||
| engine := gin.New() | ||
| NewRouter(engine, logger.New("error"), usecase.Usecases{}, &config.Config{}) | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/api/v1/authorize", http.NoBody) | ||
| w := httptest.NewRecorder() | ||
|
|
||
| engine.ServeHTTP(w, req) | ||
|
|
||
| require.Equal(t, http.StatusBadRequest, w.Code) | ||
| require.Equal(t, xFrameOptionsHeaderValue, w.Header().Get("X-Frame-Options")) | ||
| require.Equal(t, contentSecurityPolicyHeaderValue, w.Header().Get("Content-Security-Policy")) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.