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

lib/rest: fix URLPathEscapeAll breaking WebDAV servers (eg nzbdav) with strict path matching

Andrew Furman639bd8895967
diff --git a/lib/rest/url.go b/lib/rest/url.go
index fcda35693..3e6b7188d 100644
--- a/lib/rest/url.go
+++ b/lib/rest/url.go
@@ -28,7 +28,9 @@ func URLPathEscape(in string) string {
// URLPathEscapeAll escapes URL path the in string using URL escaping rules
//
-// It escapes every character except [A-Za-z0-9] and /
+// It escapes every character except the RFC 3986 unreserved characters
+// [A-Za-z0-9-._~] and the path separator /. Unreserved characters MUST NOT
+// be percent-encoded per RFC 3986 §2.3.
func URLPathEscapeAll(in string) string {
var b strings.Builder
b.Grow(len(in) * 3) // worst case: every byte escaped
@@ -36,7 +38,8 @@ func URLPathEscapeAll(in string) string {
for i := range len(in) {
c := in[i]
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9') || c == '/' {
+ (c >= '0' && c <= '9') || c == '/' ||
+ c == '-' || c == '.' || c == '_' || c == '~' {
b.WriteByte(c)
} else {
b.WriteByte('%')
diff --git a/lib/rest/url_test.go b/lib/rest/url_test.go
index 263634879..df3519c13 100644
--- a/lib/rest/url_test.go
+++ b/lib/rest/url_test.go
@@ -66,7 +66,10 @@ func TestURLPathEscapeAll(t *testing.T) {
want string
}{
{"", ""},
- {"/hello.txt", "/hello%2Etxt"},
+ // RFC 3986 unreserved characters must not be encoded
+ {"/hello.txt", "/hello.txt"},
+ {"file-name_v2.~bak", "file-name_v2.~bak"},
+ // Reserved and other characters must be encoded
{"With Space", "With%20Space"},
{"With Colon:", "With%20Colon%3A"},
{"With Percent%", "With%20Percent%25"},