| 1 | package bearerauth |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/detectors/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"valid token - minimal length", "bearer dTpw", true}, |
| 11 | {"valid token - minimal length without padding", "bearer dTpw\"", true}, |
| 12 | {"valid token - with header key", "Authorization: Bearer ZDp6cg==", true}, |
| 13 | {"valid token - with quoted header key", `"Authorization": "Bearer YTpn"`, true}, |
| 14 | {"valid token - long", "Bearer dXNlcm5mc2RzZ2dmc2dzZmdzZmFtZTpwZ3NzZmdzZ3Nnc2RndGFlZ2FnYXNzd29yZGRkZGQ=", true}, |
| 15 | {"valid token - with authorization", "\"Authorization\": \"Bearer YTpn\"", true}, |
| 16 | |
| 17 | {"invalid token - minimal length with invalid base 64", "bearer Og=", false}, |
| 18 | {"invalid token - without bearer", "headers = {\"Authorization\": \"aGkgeW8=\"}", false}, |
| 19 | {"invalid token - test token", " headers = {\"Authorization\": \"Bearer test\"}", false}, |
| 20 | {"false positive", "TIPC_NL_BEARER_DISABLE", false}, |
| 21 | {"false positive 2", "TIPC_NL_BEARER_GET", false}, |
| 22 | {"token too short", "bearer x", false}, |
| 23 | {"missing bearer auth scheme", "ABCDEFGHIJ", false}, |
| 24 | {"wrong auth scheme", "basic abcdefghij", false}, |
| 25 | {"illegal characters", "bearer ABCDEFGHIJ&", false}, |
| 26 | {"empty input", "", false}, |
| 27 | } |
| 28 | |
| 29 | var bearerAuthDetector = NewDetector() |
| 30 | |
| 31 | func TestScan(t *testing.T) { |
| 32 | tests.TestScan(t, bearerAuthDetector, testCases) |
| 33 | } |
| 34 | |
| 35 | func TestScanWithKey(t *testing.T) { |
| 36 | tests.TestScanWithKey(t, bearerAuthDetector, testCases) |
| 37 | } |
| 38 | |
| 39 | func TestScanWithMultipleMatches(t *testing.T) { |
| 40 | tests.TestScanWithMultipleMatches(t, bearerAuthDetector, testCases) |
| 41 | } |
| 42 | |
| 43 | func TestScanMap(t *testing.T) { |
| 44 | tests.TestScanMap(t, bearerAuthDetector, testCases) |
| 45 | } |