path,path/filepath: add Join examples with ".." components

People sometimes expect Join to trim .. components from its arguments
before joining, and are surprised that it doesn't. This is bad if they
were relying on that assumed behaviour to prevent directory traversal
attacks.

While a careful reading of the documentation for Join and Clean
might dispel this notion, it is not obvious at first glance.

Add a case to the examples to nudge people in the right direction.

Updates #40373

Change-Id: Ib5792c12ba1000811a0c0eb77048196d0b26da60
Reviewed-on: https://go-review.googlesource.com/c/go/+/249177
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Andrew Ekstedt 2020-07-24 12:48:30 -07:00 committed by Rob Pike
parent 0941fc3f9f
commit 98a0071a53
2 changed files with 9 additions and 0 deletions

View File

@ -79,13 +79,18 @@ func ExampleJoin() {
fmt.Println(path.Join("a", "b", "c"))
fmt.Println(path.Join("a", "b/c"))
fmt.Println(path.Join("a/b", "c"))
fmt.Println(path.Join("a/b", "../../../xyz"))
fmt.Println(path.Join("", ""))
fmt.Println(path.Join("a", ""))
fmt.Println(path.Join("", "a"))
// Output:
// a/b/c
// a/b/c
// a/b/c
// ../xyz
//
// a
// a

View File

@ -72,12 +72,16 @@ func ExampleJoin() {
fmt.Println(filepath.Join("a", "b/c"))
fmt.Println(filepath.Join("a/b", "c"))
fmt.Println(filepath.Join("a/b", "/c"))
fmt.Println(filepath.Join("a/b", "../../../xyz"))
// Output:
// On Unix:
// a/b/c
// a/b/c
// a/b/c
// a/b/c
// ../xyz
}
func ExampleMatch() {