@@ -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