Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve dlna: fix XML quote escaping for Samsung TV compatibility

Nick Craig-Wood8502532c22d8
diff --git a/cmd/serve/dlna/cds.go b/cmd/serve/dlna/cds.go
index 01f7e4453..0b3cb7063 100644
--- a/cmd/serve/dlna/cds.go
+++ b/cmd/serve/dlna/cds.go
@@ -331,9 +331,11 @@ func (cds *contentDirectoryService) Handle(action string, argsXML []byte, r *htt
if err != nil {
return nil, err
}
+ // Apply compatibility adjustments for strict DLNA clients
+ resultStr := adjustXML(result)
// Argument order must match SCPD definition in ContentDirectory.xml
return soapArgs(
- "Result", didlLite(string(result)),
+ "Result", didlLite(resultStr),
"NumberReturned", fmt.Sprint(len(objs)),
"TotalMatches", fmt.Sprint(totalMatches),
"UpdateID", cds.updateIDString(),
@@ -352,9 +354,11 @@ func (cds *contentDirectoryService) Handle(action string, argsXML []byte, r *htt
if err != nil {
return nil, err
}
+ // Apply compatibility adjustments for strict DLNA clients
+ resultStr := adjustXML(result)
// Argument order must match SCPD definition in ContentDirectory.xml
return soapArgs(
- "Result", didlLite(string(result)),
+ "Result", didlLite(resultStr),
"NumberReturned", "1",
"TotalMatches", "1",
"UpdateID", cds.updateIDString(),
diff --git a/cmd/serve/dlna/cds_test.go b/cmd/serve/dlna/cds_test.go
index 2a254d5d9..cef1804d4 100644
--- a/cmd/serve/dlna/cds_test.go
+++ b/cmd/serve/dlna/cds_test.go
@@ -2,9 +2,11 @@ package dlna
import (
"context"
+ "encoding/xml"
"sort"
"testing"
+ "github.com/anacrolix/dms/soap"
localBackend "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/vfs"
@@ -146,3 +148,66 @@ func TestMediaWithResources(t *testing.T) {
}
}
+
+func TestSOAPResponseQuoteEscaping(t *testing.T) {
+ // Test that demonstrates the double-escaping problem in SOAP responses
+ // This test should initially fail, showing " instead of " in the final output
+
+ // Simulate a DLNA Browse response with quotes in the content
+ didlContent := `<container id="0"><dc:title>Folder "with quotes"</dc:title></container>`
+ didlWrapped := didlLite(didlContent)
+
+ // Test just the mustMarshalXML function first to see the escaping
+ args := soapArgs("Result", didlWrapped)
+
+ // This simulates what marshalSOAPResponse does internally
+ xmlArgs := make([]soap.Arg, len(args))
+ for i, arg := range args {
+ xmlArgs[i] = soap.Arg{
+ XMLName: xml.Name{Local: arg.name},
+ Value: arg.value,
+ }
+ }
+ result := mustMarshalXML(xmlArgs)
+ resultStr := string(result)
+
+ // This should pass after the fix - quotes should be &quot; not &#34;
+ assert.NotContains(t, resultStr, "&#34;", "SOAP arguments should not contain &#34; entities")
+ assert.Contains(t, resultStr, "&quot;", "SOAP arguments should contain &quot; entities")
+}
+
+func TestAdjustXMLApostrophes(t *testing.T) {
+ tests := []struct {
+ name string
+ input []byte
+ expected string
+ }{
+ {
+ name: "apostrophes in filename",
+ input: []byte(`<dc:title>Testin&#39; it.jpg</dc:title>`),
+ expected: `<dc:title>Testin&apos; it.jpg</dc:title>`,
+ },
+ {
+ name: "mixed quotes and apostrophes",
+ input: []byte(`<dc:title>File &#34;name&#34; &amp; Testin&#39; it</dc:title>`),
+ expected: `<dc:title>File &quot;name&quot; &amp; Testin&apos; it</dc:title>`,
+ },
+ {
+ name: "already correct apostrophe entities",
+ input: []byte(`<dc:title>File &apos;already correct&apos;</dc:title>`),
+ expected: `<dc:title>File &apos;already correct&apos;</dc:title>`,
+ },
+ {
+ name: "all Big 5 XML entities",
+ input: []byte(`<dc:title>Test &#34; &#39; &#38; &#60; &#62;</dc:title>`),
+ expected: `<dc:title>Test &quot; &apos; &amp; &lt; &gt;</dc:title>`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := adjustXML(tt.input)
+ assert.Equal(t, tt.expected, result)
+ })
+ }
+}
diff --git a/cmd/serve/dlna/dlna_util.go b/cmd/serve/dlna/dlna_util.go
index d010c90f0..92b1fd4c4 100644
--- a/cmd/serve/dlna/dlna_util.go
+++ b/cmd/serve/dlna/dlna_util.go
@@ -12,6 +12,7 @@ import (
"net/http/httptest"
"net/http/httputil"
"os"
+ "strings"
"github.com/anacrolix/dms/soap"
"github.com/anacrolix/dms/upnp"
@@ -70,12 +71,29 @@ func didlLite(chardata string) string {
`</DIDL-Lite>`
}
+// adjustXML applies compatibility fixes to marshaled XML for DLNA clients
+// that have strict or non-standard XML parsing requirements.
+func adjustXML(xmlData []byte) string {
+ xmlStr := string(xmlData)
+ // Samsung TV compatibility: replace numeric entities with named entities.
+ // Samsung TVs have strict XML parsers that fail on numeric entities but accept named ones.
+ // Convert the "Big 5" XML entities to ensure maximum compatibility.
+ xmlStr = strings.ReplaceAll(xmlStr, "&#34;", "&quot;") // double quotes
+ xmlStr = strings.ReplaceAll(xmlStr, "&#39;", "&apos;") // apostrophes
+ xmlStr = strings.ReplaceAll(xmlStr, "&#38;", "&amp;") // ampersands (rarely used by Go, but just in case)
+ xmlStr = strings.ReplaceAll(xmlStr, "&#60;", "&lt;") // less than (rarely used by Go, but just in case)
+ xmlStr = strings.ReplaceAll(xmlStr, "&#62;", "&gt;") // greater than (rarely used by Go, but just in case)
+ return xmlStr
+}
+
func mustMarshalXML(value any) []byte {
ret, err := xml.MarshalIndent(value, "", " ")
if err != nil {
fs.Panicf(nil, "mustMarshalXML failed to marshal %v: %s", value, err)
}
- return ret
+ // Apply XML compatibility fixes for DLNA clients
+ adjustedXML := adjustXML(ret)
+ return []byte(adjustedXML)
}
// soapArg is an ordered SOAP response argument.
diff --git a/cmd/serve/dlna/dlna_util_test.go b/cmd/serve/dlna/dlna_util_test.go
new file mode 100644
index 000000000..836b1eb3d
--- /dev/null
+++ b/cmd/serve/dlna/dlna_util_test.go
@@ -0,0 +1,48 @@
+package dlna
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestAdjustXML(t *testing.T) {
+ tests := []struct {
+ name string
+ input []byte
+ expected string
+ }{
+ {
+ name: "no quotes",
+ input: []byte(`<title>Simple File</title>`),
+ expected: `<title>Simple File</title>`,
+ },
+ {
+ name: "numeric quote entities",
+ input: []byte(`<title>File &#34;with quotes&#34; in name</title>`),
+ expected: `<title>File &quot;with quotes&quot; in name</title>`,
+ },
+ {
+ name: "mixed entities",
+ input: []byte(`<title>File &#34;test&#34; &amp; &#34;demo&#34;</title>`),
+ expected: `<title>File &quot;test&quot; &amp; &quot;demo&quot;</title>`,
+ },
+ {
+ name: "already correct entities",
+ input: []byte(`<title>File &quot;already correct&quot;</title>`),
+ expected: `<title>File &quot;already correct&quot;</title>`,
+ },
+ {
+ name: "complex XML structure",
+ input: []byte(`<item><dc:title>Movie &#34;Title&#34;</dc:title><upnp:artist>Artist &#34;Name&#34;</upnp:artist></item>`),
+ expected: `<item><dc:title>Movie &quot;Title&quot;</dc:title><upnp:artist>Artist &quot;Name&quot;</upnp:artist></item>`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := adjustXML(tt.input)
+ assert.Equal(t, tt.expected, result)
+ })
+ }
+}