Skip to content

Commit bdf07c2

Browse files
neildgopherbot
authored andcommitted
[release-branch.go1.20] path/filepath: do not Clean("a/../c:/b") into c:\b on Windows
Do not permit Clean to convert a relative path into one starting with a drive reference. This change causes Clean to insert a . path element at the start of a path when the original path does not start with a volume name, and the first path element would contain a colon. This may introduce a spurious but harmless . path element under some circumstances. For example, Clean("a/../b:/../c") becomes `.\c`. This reverts CL 401595, since the change here supersedes the one in that CL. Thanks to RyotaK (https://twitter.com/ryotkak) for reporting this issue. Updates #57274 Fixes #57276 Fixes CVE-2022-41722 Change-Id: I837446285a03aa74c79d7642720e01f354c2ca17 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1675249 Reviewed-by: Roland Shoemaker <[email protected]> Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Julie Qiu <[email protected]> TryBot-Result: Security TryBots <[email protected]> (cherry picked from commit 8ca37f4813ef2f64600c92b83f17c9f3ca6c03a5) Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1728944 Run-TryBot: Roland Shoemaker <[email protected]> Reviewed-by: Tatiana Bradley <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/468119 Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Michael Pratt <[email protected]>
1 parent 3a04b6e commit bdf07c2

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/path/filepath/path.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"errors"
1616
"io/fs"
1717
"os"
18+
"runtime"
1819
"sort"
1920
"strings"
2021
)
@@ -117,21 +118,9 @@ func Clean(path string) string {
117118
case os.IsPathSeparator(path[r]):
118119
// empty path element
119120
r++
120-
case path[r] == '.' && r+1 == n:
121+
case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
121122
// . element
122123
r++
123-
case path[r] == '.' && os.IsPathSeparator(path[r+1]):
124-
// ./ element
125-
r++
126-
127-
for r < len(path) && os.IsPathSeparator(path[r]) {
128-
r++
129-
}
130-
if out.w == 0 && volumeNameLen(path[r:]) > 0 {
131-
// When joining prefix "." and an absolute path on Windows,
132-
// the prefix should not be removed.
133-
out.append('.')
134-
}
135124
case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
136125
// .. element: remove to last separator
137126
r += 2
@@ -157,6 +146,18 @@ func Clean(path string) string {
157146
if rooted && out.w != 1 || !rooted && out.w != 0 {
158147
out.append(Separator)
159148
}
149+
// If a ':' appears in the path element at the start of a Windows path,
150+
// insert a .\ at the beginning to avoid converting relative paths
151+
// like a/../c: into c:.
152+
if runtime.GOOS == "windows" && out.w == 0 && out.volLen == 0 && r != 0 {
153+
for i := r; i < n && !os.IsPathSeparator(path[i]); i++ {
154+
if path[i] == ':' {
155+
out.append('.')
156+
out.append(Separator)
157+
break
158+
}
159+
}
160+
}
160161
// copy element
161162
for ; r < n && !os.IsPathSeparator(path[r]); r++ {
162163
out.append(path[r])

src/path/filepath/path_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ var wincleantests = []PathTest{
106106
{`//abc`, `\\abc`},
107107
{`///abc`, `\\\abc`},
108108
{`//abc//`, `\\abc\\`},
109+
110+
// Don't allow cleaning to move an element with a colon to the start of the path.
111+
{`a/../c:`, `.\c:`},
112+
{`a\..\c:`, `.\c:`},
113+
{`a/../c:/a`, `.\c:\a`},
114+
{`a/../../c:`, `..\c:`},
115+
{`foo:bar`, `foo:bar`},
109116
}
110117

111118
func TestClean(t *testing.T) {
@@ -174,6 +181,7 @@ var winislocaltests = []IsLocalTest{
174181
{`C:`, false},
175182
{`C:\a`, false},
176183
{`..\a`, false},
184+
{`a/../c:`, false},
177185
{`CONIN$`, false},
178186
{`conin$`, false},
179187
{`CONOUT$`, false},

src/path/filepath/path_windows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ func TestIssue52476(t *testing.T) {
542542
}{
543543
{`..\.`, `C:`, `..\C:`},
544544
{`..`, `C:`, `..\C:`},
545-
{`.`, `:`, `:`},
545+
{`.`, `:`, `.\:`},
546546
{`.`, `C:`, `.\C:`},
547547
{`.`, `C:/a/b/../c`, `.\C:\a\c`},
548548
{`.`, `\C:`, `.\C:`},

0 commit comments

Comments
 (0)