Merge pull request #1841 from hugoaboud/master
Security Patch: Sanitize credentials on websocket error messages
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/internal/api"
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app"
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
@@ -132,7 +133,8 @@ func apiWS(w http.ResponseWriter, r *http.Request) {
|
|||||||
if handler := wsHandlers[msg.Type]; handler != nil {
|
if handler := wsHandlers[msg.Type]; handler != nil {
|
||||||
go func() {
|
go func() {
|
||||||
if err = handler(tr, msg); err != nil {
|
if err = handler(tr, msg); err != nil {
|
||||||
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + err.Error()})
|
errMsg := core.StripUserinfo(err.Error())
|
||||||
|
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + errMsg})
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,3 +118,17 @@ func TestName(t *testing.T) {
|
|||||||
// stage3
|
// stage3
|
||||||
_ = prod2.Stop()
|
_ = prod2.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStripUserinfo(t *testing.T) {
|
||||||
|
s := `streams:
|
||||||
|
test:
|
||||||
|
- ffmpeg:rtsp://username:password@10.1.2.3:554/stream1
|
||||||
|
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
|
||||||
|
`
|
||||||
|
s = StripUserinfo(s)
|
||||||
|
require.Equal(t, `streams:
|
||||||
|
test:
|
||||||
|
- ffmpeg:rtsp://***@10.1.2.3:554/stream1
|
||||||
|
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
|
||||||
|
`, s)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -77,3 +78,14 @@ func Caller() string {
|
|||||||
_, file, line, _ := runtime.Caller(1)
|
_, file, line, _ := runtime.Caller(1)
|
||||||
return file + ":" + strconv.Itoa(line)
|
return file + ":" + strconv.Itoa(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
unreserved = `A-Za-z0-9-._~`
|
||||||
|
subdelims = `!$&'()*+,;=`
|
||||||
|
userinfo = unreserved + subdelims + `%:`
|
||||||
|
)
|
||||||
|
|
||||||
|
func StripUserinfo(s string) string {
|
||||||
|
sanitizer := regexp.MustCompile(`://[` + userinfo + `]+@`)
|
||||||
|
return sanitizer.ReplaceAllString(s, `://***@`)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user