Skip to content

Commit 21f5a70

Browse files
committed
rtsp: fix authentication regression
since #4267 it was impossible to perform authentication when protocol is RTSP and credentials are hashed
1 parent e6a7a87 commit 21f5a70

File tree

4 files changed

+274
-199
lines changed

4 files changed

+274
-199
lines changed

internal/protocols/webrtc/peer_connection.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ type PeerConnection struct {
102102
newLocalCandidate chan *webrtc.ICECandidateInit
103103
connected chan struct{}
104104
failed chan struct{}
105-
done chan struct{}
106105
gatheringDone chan struct{}
107106
incomingTrack chan trackRecvPair
108107
ctx context.Context
@@ -236,7 +235,6 @@ func (co *PeerConnection) Start() error {
236235
co.newLocalCandidate = make(chan *webrtc.ICECandidateInit)
237236
co.connected = make(chan struct{})
238237
co.failed = make(chan struct{})
239-
co.done = make(chan struct{})
240238
co.gatheringDone = make(chan struct{})
241239
co.incomingTrack = make(chan trackRecvPair)
242240

@@ -280,7 +278,7 @@ func (co *PeerConnection) Start() error {
280278
defer co.stateChangeMutex.Unlock()
281279

282280
select {
283-
case <-co.done:
281+
case <-co.failed:
284282
return
285283
default:
286284
}
@@ -316,8 +314,6 @@ func (co *PeerConnection) Start() error {
316314
default:
317315
close(co.failed)
318316
}
319-
320-
close(co.done)
321317
}
322318
})
323319

@@ -347,9 +343,7 @@ func (co *PeerConnection) Close() {
347343
}
348344

349345
co.ctxCancel()
350-
co.wr.Close() //nolint:errcheck
351-
352-
<-co.done
346+
co.wr.GracefulClose() //nolint:errcheck
353347
}
354348

355349
// CreatePartialOffer creates a partial offer.

internal/servers/rtsp/conn.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func credentialsProvided(req *base.Request) bool {
4242
return err == nil && auth.Username != ""
4343
}
4444

45+
func contains(list []rtspauth.VerifyMethod, item rtspauth.VerifyMethod) bool {
46+
for _, i := range list {
47+
if i == item {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
4554
type connParent interface {
4655
logger.Writer
4756
findSessionByRSessionUnsafe(rsession *gortsplib.ServerSession) *session
@@ -138,16 +147,23 @@ func (c *conn) onDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx,
138147
}
139148
ctx.Path = ctx.Path[1:]
140149

141-
req := defs.PathAccessRequest{
142-
Name: ctx.Path,
143-
Query: ctx.Query,
144-
Proto: auth.ProtocolRTSP,
145-
ID: &c.uuid,
146-
Credentials: rtsp.Credentials(ctx.Request),
147-
IP: c.ip(),
148-
CustomVerifyFunc: func(expectedUser, expectedPass string) bool {
150+
// CustomVerifyFunc prevents hashed credentials from working.
151+
// Use it only when strictly needed.
152+
var customVerifyFunc func(expectedUser, expectedPass string) bool
153+
if contains(c.authMethods, rtspauth.VerifyMethodDigestMD5) {
154+
customVerifyFunc = func(expectedUser, expectedPass string) bool {
149155
return c.rconn.VerifyCredentials(ctx.Request, expectedUser, expectedPass)
150-
},
156+
}
157+
}
158+
159+
req := defs.PathAccessRequest{
160+
Name: ctx.Path,
161+
Query: ctx.Query,
162+
Proto: auth.ProtocolRTSP,
163+
ID: &c.uuid,
164+
Credentials: rtsp.Credentials(ctx.Request),
165+
IP: c.ip(),
166+
CustomVerifyFunc: customVerifyFunc,
151167
}
152168

153169
res := c.pathManager.Describe(defs.PathDescribeReq{

0 commit comments

Comments
 (0)