"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
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.goindex 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 *httif err != nil {return nil, err}+ // Apply compatibility adjustments for strict DLNA clients+ resultStr := adjustXML(result)// Argument order must match SCPD definition in ContentDirectory.xmlreturn 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 *httif err != nil {return nil, err}+ // Apply compatibility adjustments for strict DLNA clients+ resultStr := adjustXML(result)// Argument order must match SCPD definition in ContentDirectory.xmlreturn 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.goindex 2a254d5d9..cef1804d4 100644--- a/cmd/serve/dlna/cds_test.go+++ b/cmd/serve/dlna/cds_test.go@@ -2,9 +2,11 @@ package dlnaimport ("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 " not "+ assert.NotContains(t, resultStr, """, "SOAP arguments should not contain " entities")+ assert.Contains(t, resultStr, """, "SOAP arguments should contain " entities")+}++func TestAdjustXMLApostrophes(t *testing.T) {+ tests := []struct {+ name string+ input []byte+ expected string+ }{+ {+ name: "apostrophes in filename",+ input: []byte(`<dc:title>Testin' it.jpg</dc:title>`),+ expected: `<dc:title>Testin' it.jpg</dc:title>`,+ },+ {+ name: "mixed quotes and apostrophes",+ input: []byte(`<dc:title>File "name" & Testin' it</dc:title>`),+ expected: `<dc:title>File "name" & Testin' it</dc:title>`,+ },+ {+ name: "already correct apostrophe entities",+ input: []byte(`<dc:title>File 'already correct'</dc:title>`),+ expected: `<dc:title>File 'already correct'</dc:title>`,+ },+ {+ name: "all Big 5 XML entities",+ input: []byte(`<dc:title>Test " ' & < ></dc:title>`),+ expected: `<dc:title>Test " ' & < ></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.goindex 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, """, """) // double quotes+ xmlStr = strings.ReplaceAll(xmlStr, "'", "'") // apostrophes+ xmlStr = strings.ReplaceAll(xmlStr, "&", "&") // ampersands (rarely used by Go, but just in case)+ xmlStr = strings.ReplaceAll(xmlStr, "<", "<") // less than (rarely used by Go, but just in case)+ xmlStr = strings.ReplaceAll(xmlStr, ">", ">") // 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.gonew file mode 100644index 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 "with quotes" in name</title>`),+ expected: `<title>File "with quotes" in name</title>`,+ },+ {+ name: "mixed entities",+ input: []byte(`<title>File "test" & "demo"</title>`),+ expected: `<title>File "test" & "demo"</title>`,+ },+ {+ name: "already correct entities",+ input: []byte(`<title>File "already correct"</title>`),+ expected: `<title>File "already correct"</title>`,+ },+ {+ name: "complex XML structure",+ input: []byte(`<item><dc:title>Movie "Title"</dc:title><upnp:artist>Artist "Name"</upnp:artist></item>`),+ expected: `<item><dc:title>Movie "Title"</dc:title><upnp:artist>Artist "Name"</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)+ })+ }+}