1
0

path_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package tool
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func Test_IsSameSiteURLPath(t *testing.T) {
  7. tests := []struct {
  8. url string
  9. expVal bool
  10. }{
  11. {url: "//github.com", expVal: false},
  12. {url: "http://github.com", expVal: false},
  13. {url: "https://github.com", expVal: false},
  14. {url: "/\\github.com", expVal: false},
  15. {url: "/admin", expVal: true},
  16. {url: "/user/repo", expVal: true},
  17. }
  18. for _, test := range tests {
  19. t.Run(test.url, func(t *testing.T) {
  20. assert.Equal(t, test.expVal, IsSameSiteURLPath(test.url))
  21. })
  22. }
  23. }
  24. func Test_IsMaliciousPath(t *testing.T) {
  25. tests := []struct {
  26. path string
  27. expVal bool
  28. }{
  29. {path: "../../../../../../../../../data/gogs/data/sessions/a/9/a9f0ab6c3ef63dd8", expVal: true},
  30. {path: "..\\/..\\/../data/gogs/data/sessions/a/9/a9f0ab6c3ef63dd8", expVal: true},
  31. {path: "data/gogs/../../../../../../../../../data/sessions/a/9/a9f0ab6c3ef63dd8", expVal: true},
  32. {path: "..\\..\\..\\..\\..\\..\\..\\..\\..\\data\\gogs\\data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", expVal: true},
  33. {path: "data\\gogs\\..\\..\\..\\..\\..\\..\\..\\..\\..\\data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", expVal: true},
  34. {path: "data/sessions/a/9/a9f0ab6c3ef63dd8", expVal: false},
  35. {path: "data\\sessions\\a\\9\\a9f0ab6c3ef63dd8", expVal: false},
  36. }
  37. for _, test := range tests {
  38. t.Run(test.path, func(t *testing.T) {
  39. assert.Equal(t, test.expVal, IsMaliciousPath(test.path))
  40. })
  41. }
  42. }