diff --git a/strings.go b/strings.go index 67c1891..8084287 100644 --- a/strings.go +++ b/strings.go @@ -10,6 +10,7 @@ var ( strSlash = []byte("/") strSlashSlash = []byte("//") strSlashDotDot = []byte("/..") + strSlashDotSlash = []byte("/./") strSlashDotDotSlash = []byte("/../") strCRLF = []byte("\r\n") strHTTP = []byte("http") diff --git a/uri.go b/uri.go index 3e733cd..225a113 100644 --- a/uri.go +++ b/uri.go @@ -263,6 +263,17 @@ func normalizePath(dst, src []byte) []byte { b = b[:len(b)-n+nn] } + // remove /./ parts + for { + n := bytes.Index(b, strSlashDotSlash) + if n < 0 { + break + } + nn := n + len(strSlashDotSlash) - 1 + copy(b[n:], b[nn:]) + b = b[:len(b)-nn+n] + } + // remove trailing /foo/.. n := bytes.LastIndex(b, strSlashDotDot) if n >= 0 && n+len(strSlashDotDot) == len(b) { diff --git a/uri_test.go b/uri_test.go index 4cf2506..637f00e 100644 --- a/uri_test.go +++ b/uri_test.go @@ -71,6 +71,11 @@ func TestURIPathNormalize(t *testing.T) { // fake dotdot testURIPathNormalize(t, &u, "/aaa/..bbb/ccc/..", "/aaa/..bbb/") + + // single dot + testURIPathNormalize(t, &u, "/a/./b/././c/./d.html", "/a/b/c/d.html") + testURIPathNormalize(t, &u, "./foo/", "/foo/") + testURIPathNormalize(t, &u, "./../.././../../aaa/bbb/../../../././../", "/") } func testURIPathNormalize(t *testing.T, u *URI, requestURI, expectedPath string) {