You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xsrf_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. // Copyright 2014 The Macaron Authors
  3. // Copyright 2020 The Gitea Authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package context
  17. import (
  18. "encoding/base64"
  19. "testing"
  20. "time"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. const (
  24. key = "quay"
  25. userID = "12345678"
  26. actionID = "POST /form"
  27. )
  28. var (
  29. now = time.Now()
  30. oneMinuteFromNow = now.Add(1 * time.Minute)
  31. )
  32. func Test_ValidToken(t *testing.T) {
  33. t.Run("Validate token", func(t *testing.T) {
  34. tok := GenerateCsrfToken(key, userID, actionID, now)
  35. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, oneMinuteFromNow))
  36. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, now.Add(CsrfTokenTimeout-1*time.Nanosecond)))
  37. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, now.Add(-1*time.Minute)))
  38. })
  39. }
  40. // Test_SeparatorReplacement tests that separators are being correctly substituted
  41. func Test_SeparatorReplacement(t *testing.T) {
  42. t.Run("Test two separator replacements", func(t *testing.T) {
  43. assert.NotEqual(t, GenerateCsrfToken("foo:bar", "baz", "wah", now),
  44. GenerateCsrfToken("foo", "bar:baz", "wah", now))
  45. })
  46. }
  47. func Test_InvalidToken(t *testing.T) {
  48. t.Run("Test invalid tokens", func(t *testing.T) {
  49. invalidTokenTests := []struct {
  50. name, key, userID, actionID string
  51. t time.Time
  52. }{
  53. {"Bad key", "foobar", userID, actionID, oneMinuteFromNow},
  54. {"Bad userID", key, "foobar", actionID, oneMinuteFromNow},
  55. {"Bad actionID", key, userID, "foobar", oneMinuteFromNow},
  56. {"Expired", key, userID, actionID, now.Add(CsrfTokenTimeout)},
  57. {"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)},
  58. }
  59. tok := GenerateCsrfToken(key, userID, actionID, now)
  60. for _, itt := range invalidTokenTests {
  61. assert.False(t, ValidCsrfToken(tok, itt.key, itt.userID, itt.actionID, itt.t))
  62. }
  63. })
  64. }
  65. // Test_ValidateBadData primarily tests that no unexpected panics are triggered during parsing
  66. func Test_ValidateBadData(t *testing.T) {
  67. t.Run("Validate bad data", func(t *testing.T) {
  68. badDataTests := []struct {
  69. name, tok string
  70. }{
  71. {"Invalid Base64", "ASDab24(@)$*=="},
  72. {"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))},
  73. {"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))},
  74. }
  75. for _, bdt := range badDataTests {
  76. assert.False(t, ValidCsrfToken(bdt.tok, key, userID, actionID, oneMinuteFromNow))
  77. }
  78. })
  79. }