mirror of
https://github.com/gogs/gogs.git
synced 2026-05-06 18:26:58 +02:00
models/repo_editor: sanitize user-defined file name to prevent RCE (#5558)
Reported by PentesterLab (https://pentesterlab.com).
This commit is contained in:
@@ -4,9 +4,18 @@
|
||||
|
||||
package tool
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsSameSiteURLPath returns true if the URL path belongs to the same site, false otherwise.
|
||||
// False: //url, http://url, /\url
|
||||
// True: /url
|
||||
func IsSameSiteURLPath(url string) bool {
|
||||
return len(url) >= 2 && url[0] == '/' && url[1] != '/' && url[1] != '\\'
|
||||
}
|
||||
|
||||
// SanitizePath sanitizes user-defined file paths to prevent remote code execution.
|
||||
func SanitizePath(path string) string {
|
||||
return strings.TrimLeft(path, "./")
|
||||
}
|
||||
|
||||
@@ -30,3 +30,19 @@ func Test_IsSameSiteURLPath(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SanitizePath(t *testing.T) {
|
||||
Convey("Sanitize malicious user-defined path", t, func() {
|
||||
testCases := []struct {
|
||||
path string
|
||||
expect string
|
||||
}{
|
||||
{"../../../../../../../../../data/gogs/data/sessions/a/9/a9f0ab6c3ef63dd8", "data/gogs/data/sessions/a/9/a9f0ab6c3ef63dd8"},
|
||||
|
||||
{"data/sessions/a/9/a9f0ab6c3ef63dd8", "data/sessions/a/9/a9f0ab6c3ef63dd8"},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
So(SanitizePath(tc.path), ShouldEqual, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user