| 1 | package npm |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/detectors/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"auth token - default domain", `//registry.npmjs.org/:_authToken=0af23f36-c523-4c87-b543-18ff3ed79bce`, true}, |
| 11 | {"auth token - ip", `//192.168.1.1/:_authToken=Hardcoded_Npm_Token`, true}, |
| 12 | {"auth token - ip & port", `//127.0.0.1:8281/:_authToken=832aa750-1f92-450c-916a-e81e61740991`, true}, |
| 13 | |
| 14 | {"auth token - missing leading slash", `/registry.npmjs.org/:_authToken=b1cc9fe0-9c1b-42fe-afaf-f06c0bbb65e2`, false}, |
| 15 | {"auth token - missing prefix", `_authToken=a5714101-fdcf-4b46-9202-00070aa84b52`, false}, |
| 16 | {"auth token - missing token key", `//registry.npmjs.org/1e58e90b-28c5-465c-911a-15e58211ca23`, false}, |
| 17 | {"auth token - missing token value", `//registry.npmjs.org/:_authToken=`, false}, |
| 18 | {"auth token - token value is a $ variable", `//registry.npmjs.org/:_authToken=$NPM_TOKEN`, false}, |
| 19 | {"auth token - token value is a {} expression", `//registry.npmjs.org/:_authToken={NPM_TOKEN}`, false}, |
| 20 | |
| 21 | {"access token - valid", `npm_abcdefghijklmNOPQRSTUVWXYZ1234567890`, true}, |
| 22 | |
| 23 | {"access token - missing prefix", `abcdefghijklmNOPQRSTUVWXYZ1234567890`, false}, |
| 24 | {"access token - wrong prefix", `npc_abcdefghijklmNOPQRSTUVWXYZ1234567890`, false}, |
| 25 | {"access token - uppercase prefix", `NPM_abcdefghijklmNOPQRSTUVWXYZ1234567890`, false}, |
| 26 | {"access token - missing separator", `npmabcdefghijklmNOPQRSTUVWXYZ1234567890`, false}, |
| 27 | {"access token - wrong separator", `npm-abcdefghijklmNOPQRSTUVWXYZ1234567890`, false}, |
| 28 | {"access token - missing body", `npm_`, false}, |
| 29 | {"access token - body too short", `npm_abcdefghijklmNOPQRSTUVWXYZ123456789`, false}, |
| 30 | {"access token - body too long", `npm_abcdefghijklmNOPQRSTUVWXYZ1234567890a`, false}, |
| 31 | {"access token - illegal characters in body", `npm_abcdefghijklmNOPQRSTUVWXY_1234567890`, false}, |
| 32 | |
| 33 | {"empty input", "", false}, |
| 34 | } |
| 35 | |
| 36 | var npmTokenDetector = NewDetector() |
| 37 | |
| 38 | func TestScan(t *testing.T) { |
| 39 | tests.TestScan(t, npmTokenDetector, testCases) |
| 40 | } |
| 41 | |
| 42 | func TestScanWithKey(t *testing.T) { |
| 43 | tests.TestScanWithKey(t, npmTokenDetector, testCases) |
| 44 | } |
| 45 | |
| 46 | func TestScanWithMultipleMatches(t *testing.T) { |
| 47 | tests.TestScanWithMultipleMatches(t, npmTokenDetector, testCases) |
| 48 | } |
| 49 | |
| 50 | func TestScanMap(t *testing.T) { |
| 51 | tests.TestScanMap(t, npmTokenDetector, testCases) |
| 52 | } |