Reject cross-origin upgrades at the preview proxy
This commit is contained in:
@@ -145,7 +145,20 @@ func (m *Manager) servePreview(port int) error {
|
|||||||
},
|
},
|
||||||
FlushInterval: -1,
|
FlushInterval: -1,
|
||||||
}
|
}
|
||||||
|
protection := http.NewCrossOriginProtection()
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
check := r
|
||||||
|
if r.Header.Get("Upgrade") != "" {
|
||||||
|
// An upgraded GET opens a bidirectional channel, so it needs the
|
||||||
|
// same origin checks as a state-changing request.
|
||||||
|
upgrade := *r
|
||||||
|
upgrade.Method = http.MethodPost
|
||||||
|
check = &upgrade
|
||||||
|
}
|
||||||
|
if err := protection.Check(check); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
host := r.Host
|
host := r.Host
|
||||||
if h, _, err := net.SplitHostPort(host); err == nil {
|
if h, _, err := net.SplitHostPort(host); err == nil {
|
||||||
host = h
|
host = h
|
||||||
@@ -170,7 +183,7 @@ func (m *Manager) servePreview(port int) error {
|
|||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
go func() {
|
go func() {
|
||||||
err := (&http.Server{Handler: http.NewCrossOriginProtection().Handler(handler)}).Serve(tracked)
|
err := (&http.Server{Handler: handler}).Serve(tracked)
|
||||||
if !errors.Is(err, net.ErrClosed) {
|
if !errors.Is(err, net.ErrClosed) {
|
||||||
slog.Error("serve preview", "port", port, "err", err)
|
slog.Error("serve preview", "port", port, "err", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user