pathutil.go 567 B

1234567891011121314151617181920
  1. // Copyright 2020 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 pathutil
  5. import (
  6. "path"
  7. "strings"
  8. )
  9. // Clean cleans up given path and returns a relative path that goes straight
  10. // down to prevent path traversal.
  11. //
  12. // 🚨 SECURITY: This function MUST be used for any user input that is used as
  13. // file system path to prevent path traversal.
  14. func Clean(p string) string {
  15. p = strings.ReplaceAll(p, `\`, "/")
  16. return strings.Trim(path.Clean("/"+p), "/")
  17. }