Guard the Go server's request boundary: loopback Host, no Origin on /connect, shared token
The server answered any Host, so a DNS-rebinding page reached /connect (101) and could drive terminals. Every route now refuses a non-loopback Host on a loopback connection (421), /connect refuses any Origin (403), and a server started with -token-file requires X-Pipelines-Token (401; /mcp and /healthz exempt). The CLI/TUI send the token only to loopback URLs.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/servertoken"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
// dialOptions attaches the shared server token (internal/servertoken) when the
|
||||
// dialed endpoint is this machine's loopback server. The token is never sent to
|
||||
// another host: a remote server is reached through its web proxy, which has its
|
||||
// own sign-in. No token file means no header (an unconfigured server needs none).
|
||||
func dialOptions(target string) (*websocket.DialOptions, error) {
|
||||
if !loopbackEndpoint(target) {
|
||||
return nil, nil
|
||||
}
|
||||
token, err := servertoken.ForClient()
|
||||
if err != nil || token == "" {
|
||||
return nil, err
|
||||
}
|
||||
return &websocket.DialOptions{HTTPHeader: http.Header{servertoken.Header: {token}}}, nil
|
||||
}
|
||||
|
||||
func loopbackEndpoint(target string) bool {
|
||||
u, err := url.Parse(target)
|
||||
if err != nil || (u.Scheme != "ws" && u.Scheme != "wss") {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
if strings.EqualFold(host, "localhost") {
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && ip.IsLoopback()
|
||||
}
|
||||
Reference in New Issue
Block a user