4
0

decoder_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package codec
  2. import (
  3. "encoding/hex"
  4. "github.com/stretchr/testify/assert"
  5. "net/url"
  6. "testing"
  7. )
  8. func TestDecode(t *testing.T) {
  9. tests := []struct {
  10. chunk string
  11. expected string
  12. name string
  13. }{
  14. {
  15. name: "only b64 chunk",
  16. chunk: `bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q=`,
  17. expected: `longer-encoded-secret-test`,
  18. },
  19. {
  20. name: "mixed content",
  21. chunk: `token: bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q=`,
  22. expected: `token: longer-encoded-secret-test`,
  23. },
  24. {
  25. name: "no chunk",
  26. chunk: ``,
  27. expected: ``,
  28. },
  29. {
  30. name: "env var (looks like all b64 decodable but has `=` in the middle)",
  31. chunk: `some-encoded-secret=dGVzdC1zZWNyZXQtdmFsdWU=`,
  32. expected: `some-encoded-secret=test-secret-value`,
  33. },
  34. {
  35. name: "has longer b64 inside",
  36. chunk: `some-encoded-secret="bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q="`,
  37. expected: `some-encoded-secret="longer-encoded-secret-test"`,
  38. },
  39. {
  40. name: "many possible i := 0substrings",
  41. chunk: `Many substrings in this slack message could be base64 decoded
  42. but only dGhpcyBlbmNhcHN1bGF0ZWQgc2VjcmV0 should be decoded.`,
  43. expected: `Many substrings in this slack message could be base64 decoded
  44. but only this encapsulated secret should be decoded.`,
  45. },
  46. {
  47. name: "b64-url-safe: only b64 chunk",
  48. chunk: `bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q`,
  49. expected: `longer-encoded-secret-test`,
  50. },
  51. {
  52. name: "b64-url-safe: mixed content",
  53. chunk: `token: bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q`,
  54. expected: `token: longer-encoded-secret-test`,
  55. },
  56. {
  57. name: "b64-url-safe: env var (looks like all b64 decodable but has `=` in the middle)",
  58. chunk: `some-encoded-secret=dGVzdC1zZWNyZXQtdmFsdWU=`,
  59. expected: `some-encoded-secret=test-secret-value`,
  60. },
  61. {
  62. name: "b64-url-safe: has longer b64 inside",
  63. chunk: `some-encoded-secret="bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q"`,
  64. expected: `some-encoded-secret="longer-encoded-secret-test"`,
  65. },
  66. {
  67. name: "b64-url-safe: hyphen url b64",
  68. chunk: `Z2l0bGVha3M-PmZpbmRzLXNlY3JldHM`,
  69. expected: `gitleaks>>finds-secrets`,
  70. },
  71. {
  72. name: "b64-url-safe: underscore url b64",
  73. chunk: `YjY0dXJsc2FmZS10ZXN0LXNlY3JldC11bmRlcnNjb3Jlcz8_`,
  74. expected: `b64urlsafe-test-secret-underscores??`,
  75. },
  76. {
  77. name: "invalid base64 string",
  78. chunk: `a3d3fa7c2bb99e469ba55e5834ce79ee4853a8a3`,
  79. expected: `a3d3fa7c2bb99e469ba55e5834ce79ee4853a8a3`,
  80. },
  81. {
  82. name: "url encoded value",
  83. chunk: `secret%3D%22q%24%21%40%23%24%25%5E%26%2A%28%20asdf%22`,
  84. expected: `secret="q$!@#$%^&*( asdf"`,
  85. },
  86. {
  87. name: "hex encoded value",
  88. chunk: `secret="466973684D617048756E6B79212121363334"`,
  89. expected: `secret="FishMapHunky!!!634"`,
  90. },
  91. }
  92. decoder := NewDecoder()
  93. fullDecode := func(data string) string {
  94. segments := []*EncodedSegment{}
  95. for {
  96. data, segments = decoder.Decode(data, segments)
  97. if len(segments) == 0 {
  98. return data
  99. }
  100. }
  101. }
  102. // Test value decoding
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. assert.Equal(t, tt.expected, fullDecode(tt.chunk))
  106. })
  107. }
  108. // Percent encode the values to test percent decoding
  109. for _, tt := range tests {
  110. t.Run(tt.name, func(t *testing.T) {
  111. encodedChunk := url.PathEscape(tt.chunk)
  112. assert.Equal(t, tt.expected, fullDecode(encodedChunk))
  113. })
  114. }
  115. // Hex encode the values to test hex decoding
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. encodedChunk := hex.EncodeToString([]byte(tt.chunk))
  119. assert.Equal(t, tt.expected, fullDecode(encodedChunk))
  120. })
  121. }
  122. }