path.go 726 B

123456789101112131415161718192021222324
  1. // Copyright 2018 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package tool
  5. import (
  6. "strings"
  7. )
  8. // IsSameSiteURLPath returns true if the URL path belongs to the same site, false otherwise.
  9. // False: //url, http://url, /\url
  10. // True: /url
  11. func IsSameSiteURLPath(url string) bool {
  12. return len(url) >= 2 && url[0] == '/' && url[1] != '/' && url[1] != '\\'
  13. }
  14. // SanitizePath sanitizes user-defined file paths to prevent remote code execution.
  15. func SanitizePath(path string) string {
  16. path = strings.TrimLeft(path, "/")
  17. path = strings.Replace(path, "../", "", -1)
  18. path = strings.Replace(path, "..\\", "", -1)
  19. return path
  20. }