Skip to content

Commit 3f542a6

Browse files
authored
Merge pull request #1841 from hugoaboud/master
Security Patch: Sanitize credentials on websocket error messages
2 parents 788afb7 + 8b4df5f commit 3f542a6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

internal/api/ws/ws.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/AlexxIT/go2rtc/internal/api"
1313
"github.com/AlexxIT/go2rtc/internal/app"
14+
"github.com/AlexxIT/go2rtc/pkg/core"
1415
"github.com/gorilla/websocket"
1516
"github.com/rs/zerolog"
1617
)
@@ -132,7 +133,8 @@ func apiWS(w http.ResponseWriter, r *http.Request) {
132133
if handler := wsHandlers[msg.Type]; handler != nil {
133134
go func() {
134135
if err = handler(tr, msg); err != nil {
135-
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + err.Error()})
136+
errMsg := core.StripUserinfo(err.Error())
137+
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + errMsg})
136138
}
137139
}()
138140
}

pkg/core/core_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ func TestName(t *testing.T) {
118118
// stage3
119119
_ = prod2.Stop()
120120
}
121+
122+
func TestStripUserinfo(t *testing.T) {
123+
s := `streams:
124+
test:
125+
- ffmpeg:rtsp://username:[email protected]:554/stream1
126+
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
127+
`
128+
s = StripUserinfo(s)
129+
require.Equal(t, `streams:
130+
test:
131+
- ffmpeg:rtsp://***@10.1.2.3:554/stream1
132+
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
133+
`, s)
134+
}

pkg/core/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"crypto/rand"
5+
"regexp"
56
"runtime"
67
"strconv"
78
"strings"
@@ -77,3 +78,14 @@ func Caller() string {
7778
_, file, line, _ := runtime.Caller(1)
7879
return file + ":" + strconv.Itoa(line)
7980
}
81+
82+
const (
83+
unreserved = `A-Za-z0-9-._~`
84+
subdelims = `!$&'()*+,;=`
85+
userinfo = unreserved + subdelims + `%:`
86+
)
87+
88+
func StripUserinfo(s string) string {
89+
sanitizer := regexp.MustCompile(`://[` + userinfo + `]+@`)
90+
return sanitizer.ReplaceAllString(s, `://***@`)
91+
}

0 commit comments

Comments
 (0)