path_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "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_IsMaliciousPath(t *testing.T) {
  30. Convey("Detects malicious path", t, func() {
  31. testCases := []struct {
  32. path string
  33. expect bool
  34. }{
  35. {"../../../../../../../../../data/gitote/data/sessions/a/9/a9f0ab6c3ef63dd8", true},
  36. {"..\\/..\\/../data/gitote/data/sessions/a/9/a9f0ab6c3ef63dd8", true},
  37. {"data/gitote/../../../../../../../../../data/sessions/a/9/a9f0ab6c3ef63dd8", true},
  38. {"..\\..\\..\\..\\..\\..\\..\\..\\..\\data\\gitote\\data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", true},
  39. {"data\\gitote\\..\\..\\..\\..\\..\\..\\..\\..\\..\\data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", true},
  40. {"data/sessions/a/9/a9f0ab6c3ef63dd8", false},
  41. {"data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", false},
  42. }
  43. for _, tc := range testCases {
  44. So(IsMaliciousPath(tc.path), ShouldEqual, tc.expect)
  45. }
  46. })
  47. }