path_test.go 1.6 KB

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