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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // SPDX-License-Identifier: Apache-2.0
  17. package context
  18. import (
  19. "encoding/base64"
  20. "testing"
  21. "time"
  22. "github.com/stretchr/testify/assert"
  23. )
  24. const (
  25. key = "quay"
  26. userID = "12345678"
  27. actionID = "POST /form"
  28. )
  29. var (
  30. now = time.Now()
  31. oneMinuteFromNow = now.Add(1 * time.Minute)
  32. )
  33. func Test_ValidToken(t *testing.T) {
  34. t.Run("Validate token", func(t *testing.T) {
  35. tok := GenerateCsrfToken(key, userID, actionID, now)
  36. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, oneMinuteFromNow))
  37. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, now.Add(CsrfTokenTimeout-1*time.Nanosecond)))
  38. assert.True(t, ValidCsrfToken(tok, key, userID, actionID, now.Add(-1*time.Minute)))
  39. })
  40. }
  41. // Test_SeparatorReplacement tests that separators are being correctly substituted
  42. func Test_SeparatorReplacement(t *testing.T) {
  43. t.Run("Test two separator replacements", func(t *testing.T) {
  44. assert.NotEqual(t, GenerateCsrfToken("foo:bar", "baz", "wah", now),
  45. GenerateCsrfToken("foo", "bar:baz", "wah", now))
  46. })
  47. }
  48. func Test_InvalidToken(t *testing.T) {
  49. t.Run("Test invalid tokens", func(t *testing.T) {
  50. invalidTokenTests := []struct {
  51. name, key, userID, actionID string
  52. t time.Time
  53. }{
  54. {"Bad key", "foobar", userID, actionID, oneMinuteFromNow},
  55. {"Bad userID", key, "foobar", actionID, oneMinuteFromNow},
  56. {"Bad actionID", key, userID, "foobar", oneMinuteFromNow},
  57. {"Expired", key, userID, actionID, now.Add(CsrfTokenTimeout)},
  58. {"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)},
  59. }
  60. tok := GenerateCsrfToken(key, userID, actionID, now)
  61. for _, itt := range invalidTokenTests {
  62. assert.False(t, ValidCsrfToken(tok, itt.key, itt.userID, itt.actionID, itt.t))
  63. }
  64. })
  65. }
  66. // Test_ValidateBadData primarily tests that no unexpected panics are triggered during parsing
  67. func Test_ValidateBadData(t *testing.T) {
  68. t.Run("Validate bad data", func(t *testing.T) {
  69. badDataTests := []struct {
  70. name, tok string
  71. }{
  72. {"Invalid Base64", "ASDab24(@)$*=="},
  73. {"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))},
  74. {"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))},
  75. }
  76. for _, bdt := range badDataTests {
  77. assert.False(t, ValidCsrfToken(bdt.tok, key, userID, actionID, oneMinuteFromNow))
  78. }
  79. })
  80. }