decoder_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package codec
  2. import (
  3. "encoding/hex"
  4. "net/url"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  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. name: "unicode encoded value",
  93. chunk: `secret=U+0061 U+0062 U+0063 U+0064 U+0065 U+0066`,
  94. expected: "secret=abcdef",
  95. },
  96. {
  97. name: "unicode encoded value backslashed",
  98. chunk: `secret=\\u0068\\u0065\\u006c\\u006c\\u006f\\u0020\\u0077\\u006f\\u0072\\u006c\\u0064\\u0020\\u0064\\u0075\\u0064\\u0065`,
  99. expected: "secret=hello world dude",
  100. },
  101. {
  102. name: "unicode encoded value backslashed mixed w/ hex",
  103. chunk: `secret=\u0068\u0065\u006c\u006c\u006f\u0020\u0077\u006f\u0072\u006c\u0064 6C6F76656C792070656F706C65206F66206561727468`,
  104. expected: "secret=hello world lovely people of earth",
  105. },
  106. }
  107. decoder := NewDecoder()
  108. fullDecode := func(data string) string {
  109. segments := []*EncodedSegment{}
  110. for {
  111. data, segments = decoder.Decode(data, segments)
  112. if len(segments) == 0 {
  113. return data
  114. }
  115. }
  116. }
  117. // Test value decoding
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. assert.Equal(t, tt.expected, fullDecode(tt.chunk))
  121. })
  122. }
  123. // Percent encode the values to test percent decoding
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. encodedChunk := url.PathEscape(tt.chunk)
  127. assert.Equal(t, tt.expected, fullDecode(encodedChunk))
  128. })
  129. }
  130. // Hex encode the values to test hex decoding
  131. for _, tt := range tests {
  132. t.Run(tt.name, func(t *testing.T) {
  133. encodedChunk := hex.EncodeToString([]byte(tt.chunk))
  134. assert.Equal(t, tt.expected, fullDecode(encodedChunk))
  135. })
  136. }
  137. }