mirror of
https://github.com/git-pkgs/proxy.git
synced 2026-06-02 00:38:16 -04:00
Compare commits
1 commit
main
...
path-trave
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6093376d7 |
2 changed files with 23 additions and 2 deletions
|
|
@ -24,9 +24,21 @@ import (
|
|||
)
|
||||
|
||||
// containsPathTraversal returns true if the path contains ".." segments
|
||||
// that could be used to escape the intended directory.
|
||||
// that could be used to escape the intended directory. It checks the path
|
||||
// as given and after URL-decoding, and treats backslashes as separators.
|
||||
func containsPathTraversal(path string) bool {
|
||||
for _, segment := range strings.Split(path, "/") {
|
||||
if hasDotDotSegment(path) {
|
||||
return true
|
||||
}
|
||||
if decoded, err := url.PathUnescape(path); err == nil && decoded != path {
|
||||
return hasDotDotSegment(decoded)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasDotDotSegment(path string) bool {
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
for segment := range strings.SplitSeq(path, "/") {
|
||||
if segment == ".." {
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,15 @@ func TestContainsPathTraversal(t *testing.T) {
|
|||
{"pool/main/../../../etc/shadow", true},
|
||||
{"pool/..hidden/file", false}, // ".." as a segment, not "..hidden"
|
||||
{"", false},
|
||||
{"%2e%2e/etc/passwd", true},
|
||||
{"%2e%2e%2fetc%2fpasswd", true},
|
||||
{"pool/%2e%2e/%2e%2e/etc/shadow", true},
|
||||
{"%2E%2E%2Fetc", true},
|
||||
{`..\\etc\\passwd`, true},
|
||||
{`pool\\..\\..\\etc`, true},
|
||||
{"%2e%2e%5cetc%5cpasswd", true},
|
||||
{"pool/%2e%2ehidden/file", false},
|
||||
{"pool/%zz/bad-encoding", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue