playback: fix crash during authentication errors (#4960) (#4966)

This commit is contained in:
Alessandro Ros
2025-09-12 15:24:51 +02:00
committed by GitHub
parent a1f3631521
commit 0c801564fd
2 changed files with 50 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4" "github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
"github.com/bluenviron/mediacommon/v2/pkg/formats/mp4" "github.com/bluenviron/mediacommon/v2/pkg/formats/mp4"
"github.com/bluenviron/mediamtx/internal/auth"
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/test" "github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@@ -320,3 +321,51 @@ func TestOnListCachedDuration(t *testing.T) {
}, },
}, out) }, out)
} }
func TestOnListAuthError(t *testing.T) {
dir, err := os.MkdirTemp("", "mediamtx-playback")
require.NoError(t, err)
defer os.RemoveAll(dir)
s := &Server{
Address: "127.0.0.1:9996",
ReadTimeout: conf.Duration(10 * time.Second),
PathConfs: map[string]*conf.Path{
"mypath": {
Name: "mypath",
RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"),
},
},
AuthManager: &test.AuthManager{
AuthenticateImpl: func(_ *auth.Request) error {
return auth.Error{Wrapped: fmt.Errorf("auth error")}
},
RefreshJWTJWKSImpl: func() {
},
},
Parent: test.NilLogger,
}
err = s.Initialize()
require.NoError(t, err)
defer s.Close()
u, err := url.Parse("http://myuser:mypass@localhost:9996/list")
require.NoError(t, err)
v := url.Values{}
v.Set("path", "mypath")
u.RawQuery = v.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
require.NoError(t, err)
start := time.Now()
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer res.Body.Close()
require.Greater(t, time.Since(start), 2*time.Second)
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
}

View File

@@ -130,7 +130,7 @@ func (s *Server) doAuth(ctx *gin.Context, pathName string) bool {
} }
s.Log(logger.Info, "connection %v failed to authenticate: %v", s.Log(logger.Info, "connection %v failed to authenticate: %v",
httpp.RemoteAddr(ctx), err.(*auth.Error).Message) //nolint:errorlint httpp.RemoteAddr(ctx), err.(auth.Error).Message) //nolint:errorlint
// wait some seconds to mitigate brute force attacks // wait some seconds to mitigate brute force attacks
<-time.After(auth.PauseAfterError) <-time.After(auth.PauseAfterError)