Skip to content

Commit 27a76b5

Browse files
committed
fix: escape error codes in HTML response to prevent XSS vulnerabilities
1 parent 6b00c16 commit 27a76b5

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

cmd/zeroproxy-server/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"flag"
77
"fmt"
8+
"html"
89
"io"
910
"log"
1011
"mime"
@@ -107,12 +108,12 @@ func (s *server) workerBootstrap(w http.ResponseWriter, r *http.Request) {
107108
}
108109

109110
func (s *server) safeError(w http.ResponseWriter, r *http.Request, code string, status int) {
110-
code = sanitizeCode(code)
111+
escapedCode := html.EscapeString(sanitizeCode(code))
111112
w.Header().Set("Content-Type", "text/html; charset=utf-8")
112113
w.Header().Set("Cache-Control", "no-store")
113114
w.Header().Set("Content-Security-Policy", zeroCSP(r))
114115
w.WriteHeader(status)
115-
_, _ = fmt.Fprintf(w, `<!doctype html><meta charset="utf-8"><title>ZeroProxy %s</title><main><h1>ZeroProxy</h1><p>%s</p><button onclick="history.back()">Back</button><button onclick="location.reload()">Retry</button></main>`, code, code)
116+
_, _ = fmt.Fprintf(w, `<!doctype html><meta charset="utf-8"><title>ZeroProxy %s</title><main><h1>ZeroProxy</h1><p>%s</p><button onclick="history.back()">Back</button><button onclick="location.reload()">Retry</button></main>`, escapedCode, escapedCode)
116117
}
117118

118119
func sanitizeCode(code string) string {

0 commit comments

Comments
 (0)