Skip to content

Commit 0c80156

Browse files
authored
playback: fix crash during authentication errors (#4960) (#4966)
1 parent a1f3631 commit 0c80156

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

internal/playback/on_list_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
1818
"github.com/bluenviron/mediacommon/v2/pkg/formats/mp4"
19+
"github.com/bluenviron/mediamtx/internal/auth"
1920
"github.com/bluenviron/mediamtx/internal/conf"
2021
"github.com/bluenviron/mediamtx/internal/test"
2122
"github.com/stretchr/testify/require"
@@ -320,3 +321,51 @@ func TestOnListCachedDuration(t *testing.T) {
320321
},
321322
}, out)
322323
}
324+
325+
func TestOnListAuthError(t *testing.T) {
326+
dir, err := os.MkdirTemp("", "mediamtx-playback")
327+
require.NoError(t, err)
328+
defer os.RemoveAll(dir)
329+
330+
s := &Server{
331+
Address: "127.0.0.1:9996",
332+
ReadTimeout: conf.Duration(10 * time.Second),
333+
PathConfs: map[string]*conf.Path{
334+
"mypath": {
335+
Name: "mypath",
336+
RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"),
337+
},
338+
},
339+
AuthManager: &test.AuthManager{
340+
AuthenticateImpl: func(_ *auth.Request) error {
341+
return auth.Error{Wrapped: fmt.Errorf("auth error")}
342+
},
343+
RefreshJWTJWKSImpl: func() {
344+
},
345+
},
346+
Parent: test.NilLogger,
347+
}
348+
err = s.Initialize()
349+
require.NoError(t, err)
350+
defer s.Close()
351+
352+
u, err := url.Parse("http://myuser:mypass@localhost:9996/list")
353+
require.NoError(t, err)
354+
355+
v := url.Values{}
356+
v.Set("path", "mypath")
357+
u.RawQuery = v.Encode()
358+
359+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
360+
require.NoError(t, err)
361+
362+
start := time.Now()
363+
364+
res, err := http.DefaultClient.Do(req)
365+
require.NoError(t, err)
366+
defer res.Body.Close()
367+
368+
require.Greater(t, time.Since(start), 2*time.Second)
369+
370+
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
371+
}

internal/playback/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (s *Server) doAuth(ctx *gin.Context, pathName string) bool {
130130
}
131131

132132
s.Log(logger.Info, "connection %v failed to authenticate: %v",
133-
httpp.RemoteAddr(ctx), err.(*auth.Error).Message) //nolint:errorlint
133+
httpp.RemoteAddr(ctx), err.(auth.Error).Message) //nolint:errorlint
134134

135135
// wait some seconds to mitigate brute force attacks
136136
<-time.After(auth.PauseAfterError)

0 commit comments

Comments
 (0)