From 5795966909c66e2fe22fdc9484be7332cdcfaa4e Mon Sep 17 00:00:00 2001 From: phatlc Date: Sat, 31 Oct 2026 06:05:22 +0700 Subject: [PATCH] serve dlna: fix crash on Browse with a negative StartingIndex or RequestedCount A Browse request with a negative StartingIndex or RequestedCount made the handler panic slicing the directory listing, so the client got a plain text 500 with the Go runtime error instead of a SOAP fault. These arguments are ui4 in the ContentDirectory SCPD, so reject negative values with the UPnP "Argument Value Invalid" (600) error. --- cmd/serve/dlna/cds.go | 4 ++++ cmd/serve/dlna/dlna_test.go | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/cmd/serve/dlna/cds.go b/cmd/serve/dlna/cds.go index 102879b18..80b4d0d96 100644 --- a/cmd/serve/dlna/cds.go +++ b/cmd/serve/dlna/cds.go @@ -307,6 +307,10 @@ func (cds *contentDirectoryService) Handle(action string, argsXML []byte, r *htt if err := xml.Unmarshal(argsXML, &browse); err != nil { return nil, err } + // StartingIndex and RequestedCount are ui4 in the SCPD + if browse.StartingIndex < 0 || browse.RequestedCount < 0 { + return nil, upnp.Errorf(upnp.ArgumentValueInvalidErrorCode, "negative StartingIndex %d or RequestedCount %d", browse.StartingIndex, browse.RequestedCount) + } // Samsung TVs sometimes send empty ObjectID, default to root container if browse.ObjectID == "" { browse.ObjectID = "0" diff --git a/cmd/serve/dlna/dlna_test.go b/cmd/serve/dlna/dlna_test.go index 0a4bc738a..66e01ce11 100644 --- a/cmd/serve/dlna/dlna_test.go +++ b/cmd/serve/dlna/dlna_test.go @@ -197,6 +197,45 @@ func TestContentDirectoryBrowseResponseOrder(t *testing.T) { assert.Less(t, totalMatchesIdx, updateIDIdx, "TotalMatches should come before UpdateID") } +// Check that a Browse with a negative StartingIndex or RequestedCount is +// rejected with a UPnP error rather than crashing the handler. +func TestContentDirectoryBrowseNegativeIndex(t *testing.T) { + for _, test := range []struct { + startingIndex string + requestedCount string + }{ + {"-1", "0"}, + {"0", "-1"}, + } { + t.Run(test.startingIndex+","+test.requestedCount, func(t *testing.T) { + req, err := http.NewRequest("POST", baseURL+serviceControlURL, strings.NewReader(` + + + + + 0 + BrowseDirectChildren + * + `+test.startingIndex+` + `+test.requestedCount+` + + + +`)) + require.NoError(t, err) + req.Header.Set("SOAPACTION", `"urn:schemas-upnp-org:service:ContentDirectory:1#Browse"`) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + require.NoError(t, resp.Body.Close()) + assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) + assert.Contains(t, string(body), "600") + }) + } +} + // Check that the X_MS_MediaReceiverRegistrar is faked out properly. func TestMediaReceiverRegistrarService(t *testing.T) { env := soap.Envelope{