Skip to content

Commit dbdc246

Browse files
Merge pull request #1 from orange-cloudfoundry/clickjacking_prevention
Add frame ancestor configuration for web app to prevent clickjacking
2 parents 0f39eb7 + a1fde0d commit dbdc246

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

cmd/dex/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ type Web struct {
150150
TLSCert string `json:"tlsCert"`
151151
TLSKey string `json:"tlsKey"`
152152
AllowedOrigins []string `json:"allowedOrigins"`
153+
FrameAncestors []string `json:"frameAncestors"`
153154
}
154155

155156
// Telemetry is the config format for telemetry including the HTTP server config.

cmd/dex/serve.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ func runServe(options serveOptions) error {
253253
logger.Infof("config allowed origins: %s", c.Web.AllowedOrigins)
254254
}
255255

256+
if len(c.Web.FrameAncestors) > 0 {
257+
logger.Infof("config allowed frame ancestors: %s", c.Web.FrameAncestors)
258+
}
259+
256260
// explicitly convert to UTC.
257261
now := func() time.Time { return time.Now().UTC() }
258262

server/server.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ type Config struct {
7777
// domain.
7878
AllowedOrigins []string
7979

80+
// List of domain allowed to frame the content of the application.
81+
// By default no one is accepted to prevent against clickjacking.
82+
// Passing in "*" will allow any domain
83+
FrameAncestors []string
84+
8085
// If enabled, the server won't prompt the user to approve authorization requests.
8186
// Logging in implies approval.
8287
SkipApprovalScreen bool
@@ -339,7 +344,28 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
339344
}
340345
}
341346

347+
348+
// frame-ancestors middleware
349+
frameAncestorsMidldleware := func(next http.Handler) http.Handler {
350+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
351+
var ancestors string
352+
if len(c.FrameAncestors) > 0 {
353+
for i := 0; i < len(c.FrameAncestors); i++ {
354+
if c.FrameAncestors[i] == issuerURL.String() {
355+
c.FrameAncestors[i] = "'self'"
356+
}
357+
}
358+
ancestors = strings.Join(c.FrameAncestors, " ")
359+
} else {
360+
ancestors = "'none'"
361+
}
362+
w.Header().Set("Content-Security-Policy", "frame-ancestors "+ancestors)
363+
next.ServeHTTP(w, r)
364+
})
365+
}
366+
342367
r := mux.NewRouter().SkipClean(true).UseEncodedPath()
368+
r.Use(frameAncestorsMidldleware)
343369
handle := func(p string, h http.Handler) {
344370
r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h))
345371
}

0 commit comments

Comments
 (0)