path.go 753 B

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