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.
This commit is contained in:
phatlc authored and Nick Craig-Wood committed 2026-11-05 10:23:52 +00:00
1 parent 334e7046e3
commit 5795966909
2 files changed
+43

No files matched your search

+4
View File
@@ -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"
+39
View File
@@ -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(`
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
<ObjectID>0</ObjectID>
<BrowseFlag>BrowseDirectChildren</BrowseFlag>
<Filter>*</Filter>
<StartingIndex>`+test.startingIndex+`</StartingIndex>
<RequestedCount>`+test.requestedCount+`</RequestedCount>
<SortCriteria></SortCriteria>
</u:Browse>
</s:Body>
</s:Envelope>`))
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), "<errorCode>600</errorCode>")
})
}
}
// Check that the X_MS_MediaReceiverRegistrar is faked out properly.
func TestMediaReceiverRegistrarService(t *testing.T) {
env := soap.Envelope{