]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.17] path/filepath: do not remove prefix "." when following path...
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Fri, 22 Apr 2022 01:07:51 +0000 (10:07 +0900)
committerDmitri Shuralyov <dmitshur@golang.org>
Tue, 31 May 2022 17:30:12 +0000 (17:30 +0000)
For #52476
Fixes #52478
Fixes CVE-2022-29804

Change-Id: I9eb72ac7dbccd6322d060291f31831dc389eb9bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/401595
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/405235
Reviewed-by: Yasuhiro Matsumoto <mattn.jp@gmail.com>
src/path/filepath/path.go
src/path/filepath/path_test.go
src/path/filepath/path_windows_test.go

index b56534deadecfe27ae4bf9afab06fe7182c758b1..8300a32cb1362400fb56b315d3eb684815b387d3 100644 (file)
@@ -117,9 +117,21 @@ func Clean(path string) string {
                case os.IsPathSeparator(path[r]):
                        // empty path element
                        r++
-               case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
+               case path[r] == '.' && r+1 == n:
                        // . element
                        r++
+               case path[r] == '.' && os.IsPathSeparator(path[r+1]):
+                       // ./ element
+                       r++
+
+                       for r < len(path) && os.IsPathSeparator(path[r]) {
+                               r++
+                       }
+                       if out.w == 0 && volumeNameLen(path[r:]) > 0 {
+                               // When joining prefix "." and an absolute path on Windows,
+                               // the prefix should not be removed.
+                               out.append('.')
+                       }
                case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
                        // .. element: remove to last separator
                        r += 2
index bc5509b49cf1ab7e4820405a10a869d9b56edd61..ed17a8854dc81d757b44aad93ad48755e3012531 100644 (file)
@@ -93,6 +93,9 @@ var wincleantests = []PathTest{
        {`//host/share/foo/../baz`, `\\host\share\baz`},
        {`\\a\b\..\c`, `\\a\b\c`},
        {`\\a\b`, `\\a\b`},
+       {`.\c:`, `.\c:`},
+       {`.\c:\foo`, `.\c:\foo`},
+       {`.\c:foo`, `.\c:foo`},
 }
 
 func TestClean(t *testing.T) {
index 76a459ac96b39da98029e1757a9a1e2faae23596..3edafb5a85b9f8cdc3da2baf19e8b302fef16057 100644 (file)
@@ -530,3 +530,29 @@ func TestNTNamespaceSymlink(t *testing.T) {
                t.Errorf(`EvalSymlinks(%q): got %q, want %q`, filelink, got, want)
        }
 }
+
+func TestIssue52476(t *testing.T) {
+       tests := []struct {
+               lhs, rhs string
+               want     string
+       }{
+               {`..\.`, `C:`, `..\C:`},
+               {`..`, `C:`, `..\C:`},
+               {`.`, `:`, `:`},
+               {`.`, `C:`, `.\C:`},
+               {`.`, `C:/a/b/../c`, `.\C:\a\c`},
+               {`.`, `\C:`, `.\C:`},
+               {`C:\`, `.`, `C:\`},
+               {`C:\`, `C:\`, `C:\C:`},
+               {`C`, `:`, `C\:`},
+               {`\.`, `C:`, `\C:`},
+               {`\`, `C:`, `\C:`},
+       }
+
+       for _, test := range tests {
+               got := filepath.Join(test.lhs, test.rhs)
+               if got != test.want {
+                       t.Errorf(`Join(%q, %q): got %q, want %q`, test.lhs, test.rhs, got, test.want)
+               }
+       }
+}