path_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func Test_IsSameSiteURLPath(t *testing.T) {
  12. Convey("Check if a path belongs to the same site", t, func() {
  13. testCases := []struct {
  14. url string
  15. expect bool
  16. }{
  17. {"//github.com", false},
  18. {"http://github.com", false},
  19. {"https://github.com", false},
  20. {"/\\github.com", false},
  21. {"/admin", true},
  22. {"/user/repo", true},
  23. }
  24. for _, tc := range testCases {
  25. So(IsSameSiteURLPath(tc.url), ShouldEqual, tc.expect)
  26. }
  27. })
  28. }
  29. func Test_SanitizePath(t *testing.T) {
  30. Convey("Sanitize malicious user-defined path", t, func() {
  31. testCases := []struct {
  32. path string
  33. expect string
  34. }{
  35. {"../../../../../../../../../data/gitote/data/sessions/a/9/a9f0ab6c3ef63dd8", "data/gitote/data/sessions/a/9/a9f0ab6c3ef63dd8"},
  36. {"data/gitote/../../../../../../../../../data/sessions/a/9/a9f0ab6c3ef63dd8", "data/gitote/data/sessions/a/9/a9f0ab6c3ef63dd8"},
  37. {"data/sessions/a/9/a9f0ab6c3ef63dd8", "data/sessions/a/9/a9f0ab6c3ef63dd8"},
  38. }
  39. for _, tc := range testCases {
  40. So(SanitizePath(tc.path), ShouldEqual, tc.expect)
  41. }
  42. })
  43. }