Skip to content

Commit

Permalink
Align href url encoding with oc10
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Jan 28, 2021
1 parent 216605e commit 221f445
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
29 changes: 29 additions & 0 deletions internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -272,3 +273,31 @@ func extractDestination(dstHeader, baseURI string) (string, error) {

return urlSplit[1], nil
}

// replaceAllStringSubmatchFunc is taken from 'Go: Replace String with Regular Expression Callback'
// see: https://elliotchance.medium.com/go-replace-string-with-regular-expression-callback-f89948bad0bb
func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
groups = append(groups, str[v[i]:v[i+1]])
}
result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}
return result + str[lastIndex:]
}

var hrefre = regexp.MustCompile(`([^A-Za-z0-9_\-.~()/:@])`)

// encodePath encodes the path of a url.
//
// slashes (/) are treated as path-separators.
// ported from https://github.com/sabre-io/http/blob/bb27d1a8c92217b34e778ee09dcf79d9a2936e84/lib/functions.php#L369-L379
func encodePath(path string) string {
return replaceAllStringSubmatchFunc(hrefre, path, func(groups []string) string {
return fmt.Sprintf("%%%02x", []byte(groups[1]))
})
}
3 changes: 1 addition & 2 deletions internal/http/services/owncloud/ocdav/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -319,7 +318,7 @@ func (s *svc) mdToPropResponse(ctx context.Context, pf *propfindXML, md *provide
}

response := responseXML{
Href: (&url.URL{Path: ref}).EscapedPath(), // url encode response.Href
Href: encodePath(ref),
Propstat: []propstatXML{},
}

Expand Down
3 changes: 1 addition & 2 deletions internal/http/services/owncloud/ocdav/proppatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"

Expand Down Expand Up @@ -176,7 +175,7 @@ func (s *svc) handleProppatch(w http.ResponseWriter, r *http.Request, ns string)
func (s *svc) formatProppatchResponse(ctx context.Context, acceptedProps []xml.Name, removedProps []xml.Name, ref string) (string, error) {
responses := make([]responseXML, 0, 1)
response := responseXML{
Href: (&url.URL{Path: ref}).EscapedPath(), // url encode response.Href
Href: encodePath(ref),
Propstat: []propstatXML{},
}

Expand Down
5 changes: 2 additions & 3 deletions internal/http/services/owncloud/ocdav/trashbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"time"
Expand Down Expand Up @@ -203,7 +202,7 @@ func (h *TrashbinHandler) formatTrashPropfind(ctx context.Context, s *svc, u *us
responses := make([]*responseXML, 0, len(items)+1)
// add trashbin dir . entry
responses = append(responses, &responseXML{
Href: (&url.URL{Path: ctx.Value(ctxKeyBaseURI).(string) + "/"}).EscapedPath(), // url encode response.Href TODO (jfd) really? /should be ok ... we may actually only need to escape the username
Href: encodePath(ctx.Value(ctxKeyBaseURI).(string) + "/"), // url encode response.Href TODO
Propstat: []propstatXML{
{
Status: "HTTP/1.1 200 OK",
Expand Down Expand Up @@ -253,7 +252,7 @@ func (h *TrashbinHandler) itemToPropResponse(ctx context.Context, s *svc, pf *pr
}

response := responseXML{
Href: (&url.URL{Path: ref}).EscapedPath(), // url encode response.Href
Href: encodePath(ref), // url encode response.Href
Propstat: []propstatXML{},
}

Expand Down

0 comments on commit 221f445

Please sign in to comment.