path.go 737 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. "path/filepath"
  7. "strings"
  8. )
  9. // IsSameSiteURLPath returns true if the URL path belongs to the same site, false otherwise.
  10. // False: //url, http://url, /\url
  11. // True: /url
  12. func IsSameSiteURLPath(url string) bool {
  13. return len(url) >= 2 && url[0] == '/' && url[1] != '/' && url[1] != '\\'
  14. }
  15. // IsMaliciousPath returns true if given path is an absolute path or contains malicious content
  16. // which has potential to traverse upper level directories.
  17. func IsMaliciousPath(path string) bool {
  18. return filepath.IsAbs(path) || strings.Contains(path, "..")
  19. }