Skip to content

Commit a3f82bd

Browse files
committed
fix: redact passwords in logs
Fixes #238 Replaces URLs of the format `rtsp://user:password@localhost:8554` with `rtsp://user:xxxxx@localhost:8554` in logs. This is best-effort for now and does not handle cases where passwords appear in query strings. It should be fairly easy to extend the `RedactPassword` function in the future in case there are other common password pattern that are worth handling.
1 parent a4885c2 commit a3f82bd

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

internal/expr/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Init() {
1717
return "", err
1818
}
1919

20-
log.Debug().Msgf("[expr] url=%s", url)
20+
log.Debug().Msgf("[expr] url=%s", streams.RedactPassword(url[5:]))
2121

2222
if url = v.(string); url == "" {
2323
return "", errors.New("expr: result is empty")

internal/streams/helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ func ParseQuery(s string) url.Values {
2020
}
2121
return params
2222
}
23+
24+
func RedactPassword(s string) string {
25+
if u, err := url.Parse(s); err == nil {
26+
return u.Redacted()
27+
}
28+
29+
return s
30+
}

internal/streams/helpers_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package streams
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestRedactPassword(t *testing.T) {
10+
require.Equal(t, "not_a_url", RedactPassword("not_a_url"))
11+
require.Equal(t, "rtsp://localhost:8554", RedactPassword("rtsp://localhost:8554"))
12+
require.Equal(t, "rtsp://user:xxxxx@localhost:8554", RedactPassword("rtsp://user:password@localhost:8554"))
13+
require.Equal(t, "rtsp://:xxxxx@localhost:8554", RedactPassword("rtsp://:password@localhost:8554"))
14+
}

internal/streams/producer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (p *Producer) start() {
149149
return
150150
}
151151

152-
log.Debug().Msgf("[streams] start producer url=%s", p.url)
152+
log.Debug().Msgf("[streams] start producer url=%s", RedactPassword(p.url))
153153

154154
p.state = stateStart
155155
p.workerID++
@@ -167,7 +167,7 @@ func (p *Producer) worker(conn core.Producer, workerID int) {
167167
return
168168
}
169169

170-
log.Warn().Err(err).Str("url", p.url).Caller().Send()
170+
log.Warn().Err(err).Str("url", RedactPassword(p.url)).Caller().Send()
171171
}
172172

173173
p.reconnect(workerID, 0)
@@ -178,11 +178,11 @@ func (p *Producer) reconnect(workerID, retry int) {
178178
defer p.mu.Unlock()
179179

180180
if p.workerID != workerID {
181-
log.Trace().Msgf("[streams] stop reconnect url=%s", p.url)
181+
log.Trace().Msgf("[streams] stop reconnect url=%s", RedactPassword(p.url))
182182
return
183183
}
184184

185-
log.Debug().Msgf("[streams] retry=%d to url=%s", retry, p.url)
185+
log.Debug().Msgf("[streams] retry=%d to url=%s", retry, RedactPassword(p.url))
186186

187187
conn, err := GetProducer(p.url)
188188
if err != nil {
@@ -257,7 +257,7 @@ func (p *Producer) stop() {
257257
p.workerID++
258258
}
259259

260-
log.Debug().Msgf("[streams] stop producer url=%s", p.url)
260+
log.Debug().Msgf("[streams] stop producer url=%s", RedactPassword(p.url))
261261

262262
if p.conn != nil {
263263
_ = p.conn.Stop()

internal/streams/streams.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func GetOrPatch(query url.Values) *Stream {
119119

120120
// check if name param provided
121121
if name := query.Get("name"); name != "" {
122-
log.Info().Msgf("[streams] create new stream url=%s", source)
122+
log.Info().Msgf("[streams] create new stream url=%s", RedactPassword(source))
123123

124124
return Patch(name, source)
125125
}
@@ -143,6 +143,8 @@ func Delete(id string) {
143143
delete(streams, id)
144144
}
145145

146-
var log zerolog.Logger
147-
var streams = map[string]*Stream{}
148-
var streamsMu sync.Mutex
146+
var (
147+
log zerolog.Logger
148+
streams = map[string]*Stream{}
149+
streamsMu sync.Mutex
150+
)

0 commit comments

Comments
 (0)