Skip to content

Commit 3f68360

Browse files
authored
Prevent vicadmin from being loaded in an iframe (vmware#8177)
In order to protect the vicadmin page from being embedded in a page controlled by an attacker, set the Content-Security-Policy header to disallow rendering of the page within an iframe.
1 parent 6680484 commit 3f68360

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

cmd/vicadmin/server.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,21 @@ func (s *server) loginPage(res http.ResponseWriter, req *http.Request) {
380380
}
381381
}
382382

383+
// cspMiddleware sets the Content-Security-Policy header to prevent clickjacking
384+
// https://www.owasp.org/index.php/Content_Security_Policy_Cheat_Sheet#Preventing_Clickjacking
385+
func (s *server) cspMiddleware() func(next http.Handler) http.Handler {
386+
header := "Content-Security-Policy"
387+
value := "frame-ancestors 'none';"
388+
389+
return func(next http.Handler) http.Handler {
390+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
391+
w.Header().Add(header, value)
392+
393+
next.ServeHTTP(w, r)
394+
})
395+
}
396+
}
397+
383398
func (s *server) serve() error {
384399
defer trace.End(trace.Begin(""))
385400

@@ -420,7 +435,7 @@ func (s *server) serve() error {
420435
s.Authenticated("/logout", s.logoutHandler)
421436
s.Authenticated("/", s.index)
422437
server := &http.Server{
423-
Handler: s.mux,
438+
Handler: s.cspMiddleware()(s.mux),
424439
}
425440

426441
return server.Serve(s.l)

tests/test-cases/Group9-VIC-Admin/9-01-VICAdmin-ShowHTML.robot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ Display HTML
7272
${rc} ${output}= Run And Return Rc And Output curl -sk %{VIC-ADMIN} -b ${cookies}
7373
Should contain ${output} <title>VIC: %{VCH-NAME}</title>
7474

75+
Content-Security-Policy
76+
${cookies}= Login To VCH Admin And Save Cookies
77+
${rc} ${output}= Run And Return Rc And Output curl -sk -vvv %{VIC-ADMIN} -b ${cookies}
78+
Should contain ${output} Content-Security-Policy: frame-ancestors 'none';
79+
7580
Get Portlayer Log
7681
${cookies}= Login To VCH Admin And Save Cookies
7782
${rc} ${output}= Run And Return Rc And Output curl -sk %{VIC-ADMIN}/logs/port-layer.log -b ${cookies}

0 commit comments

Comments
 (0)