path.go 831 B

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