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{