4
0

decoder_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package detect
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestDecode(t *testing.T) {
  7. tests := []struct {
  8. chunk string
  9. expected string
  10. name string
  11. }{
  12. {
  13. name: "only b64 chunk",
  14. chunk: `bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q=`,
  15. expected: `longer-encoded-secret-test`,
  16. },
  17. {
  18. name: "mixed content",
  19. chunk: `token: bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q=`,
  20. expected: `token: longer-encoded-secret-test`,
  21. },
  22. {
  23. name: "no chunk",
  24. chunk: ``,
  25. expected: ``,
  26. },
  27. {
  28. name: "env var (looks like all b64 decodable but has `=` in the middle)",
  29. chunk: `some-encoded-secret=dGVzdC1zZWNyZXQtdmFsdWU=`,
  30. expected: `some-encoded-secret=test-secret-value`,
  31. },
  32. {
  33. name: "has longer b64 inside",
  34. chunk: `some-encoded-secret="bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q="`,
  35. expected: `some-encoded-secret="longer-encoded-secret-test"`,
  36. },
  37. {
  38. name: "many possible i := 0substrings",
  39. chunk: `Many substrings in this slack message could be base64 decoded
  40. but only dGhpcyBlbmNhcHN1bGF0ZWQgc2VjcmV0 should be decoded.`,
  41. expected: `Many substrings in this slack message could be base64 decoded
  42. but only this encapsulated secret should be decoded.`,
  43. },
  44. {
  45. name: "b64-url-safe: only b64 chunk",
  46. chunk: `bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q`,
  47. expected: `longer-encoded-secret-test`,
  48. },
  49. {
  50. name: "b64-url-safe: mixed content",
  51. chunk: `token: bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q`,
  52. expected: `token: longer-encoded-secret-test`,
  53. },
  54. {
  55. name: "b64-url-safe: env var (looks like all b64 decodable but has `=` in the middle)",
  56. chunk: `some-encoded-secret=dGVzdC1zZWNyZXQtdmFsdWU=`,
  57. expected: `some-encoded-secret=test-secret-value`,
  58. },
  59. {
  60. name: "b64-url-safe: has longer b64 inside",
  61. chunk: `some-encoded-secret="bG9uZ2VyLWVuY29kZWQtc2VjcmV0LXRlc3Q"`,
  62. expected: `some-encoded-secret="longer-encoded-secret-test"`,
  63. },
  64. {
  65. name: "b64-url-safe: hyphen url b64",
  66. chunk: `dHJ1ZmZsZWhvZz4-ZmluZHMtc2VjcmV0cw`,
  67. expected: `trufflehog>>finds-secrets`,
  68. },
  69. {
  70. name: "b64-url-safe: underscore url b64",
  71. chunk: `YjY0dXJsc2FmZS10ZXN0LXNlY3JldC11bmRlcnNjb3Jlcz8_`,
  72. expected: `b64urlsafe-test-secret-underscores??`,
  73. },
  74. {
  75. name: "invalid base64 string",
  76. chunk: `a3d3fa7c2bb99e469ba55e5834ce79ee4853a8a3`,
  77. expected: `a3d3fa7c2bb99e469ba55e5834ce79ee4853a8a3`,
  78. },
  79. }
  80. decoder := NewDecoder()
  81. for _, tt := range tests {
  82. t.Run(tt.name, func(t *testing.T) {
  83. decoded, _ := decoder.decode(tt.chunk, []EncodedSegment{})
  84. assert.Equal(t, tt.expected, decoded)
  85. })
  86. }
  87. }