harden websocket CheckOrigin

This commit is contained in:
mateea326 2026-04-01 19:01:06 +03:00
parent e2a9bb97c7
commit 16d717884d
2 changed files with 14 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -73,10 +74,19 @@ func NewPicoChannel(cfg config.PicoConfig, messageBus *bus.MessageBus) (*PicoCha
allowOrigins := cfg.AllowOrigins allowOrigins := cfg.AllowOrigins
checkOrigin := func(r *http.Request) bool { checkOrigin := func(r *http.Request) bool {
if len(allowOrigins) == 0 {
return true // allow all if not configured
}
origin := r.Header.Get("Origin") origin := r.Header.Get("Origin")
// If no origins are configured, allow same-origin only (default Gorilla behavior).
if len(allowOrigins) == 0 {
if origin == "" {
return true
}
u, err := url.Parse(origin)
if err != nil {
return false
}
return u.Host == r.Host
}
// If origins are configured, check for '*' or exact match.
for _, allowed := range allowOrigins { for _, allowed := range allowOrigins {
if allowed == "*" || allowed == origin { if allowed == "*" || allowed == origin {
return true return true

File diff suppressed because one or more lines are too long