detect_test.go 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. package detect
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "github.com/google/go-cmp/cmp"
  10. "github.com/rs/zerolog"
  11. "github.com/spf13/viper"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "golang.org/x/exp/maps"
  15. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  16. "github.com/zricethezav/gitleaks/v8/config"
  17. "github.com/zricethezav/gitleaks/v8/detect/codec"
  18. "github.com/zricethezav/gitleaks/v8/logging"
  19. "github.com/zricethezav/gitleaks/v8/regexp"
  20. "github.com/zricethezav/gitleaks/v8/report"
  21. "github.com/zricethezav/gitleaks/v8/sources"
  22. )
  23. const maxDecodeDepth = 8
  24. const configPath = "../testdata/config/"
  25. const repoBasePath = "../testdata/repos/"
  26. const encodedTestValues = `
  27. # Decoded
  28. -----BEGIN PRIVATE KEY-----
  29. 135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb
  30. u+QDkg0spw==
  31. -----END PRIVATE KEY-----
  32. # Encoded
  33. private_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'
  34. # Double Encoded: b64 encoded aws config inside a jwt
  35. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA
  36. # A small secret at the end to make sure that as the other ones above shrink
  37. # when decoded, the positions are taken into consideratoin for overlaps
  38. c21hbGwtc2VjcmV0
  39. # This tests how it handles when the match bounds go outside the decoded value
  40. secret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==
  41. # The above encoded again
  42. c2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=
  43. # Confirm you can ignore on the decoded value
  44. password="bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw=="
  45. # This tests that it can do hex encoded data
  46. secret=6465636F6465642D7365637265742D76616C756576484558
  47. # This tests that it can do percent encoded data
  48. ## partial encoded data
  49. secret=decoded-%73%65%63%72%65%74-valuev2
  50. ## scattered encoded
  51. secret=%64%65coded-%73%65%63%72%65%74-valuev3
  52. # Test multi levels of encoding where the source is a partal encoding
  53. # it is important that the bounds of the predecessors are properly
  54. # considered
  55. ## single percent encoding in the middle of multi layer b64
  56. c2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09
  57. ## single percent encoding at the beginning of hex
  58. secret%3d6465636F6465642D7365637265742D76616C75657635
  59. ## multiple percent encodings in a single layer base64
  60. secret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86
  61. ## base64 encoded partially percent encoded value
  62. secret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=
  63. ## one of the lines above that went through... a lot
  64. ## and there's surrounding text around it
  65. Look at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?
  66. ## Multi percent encode two random characters close to the bounds of the base64
  67. ## encoded data to make sure that the bounds are still correctly calculated
  68. secret=ZG%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%36%25%33%31%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%33%25%33%322RlZC1zZWNyZXQtd%25%36%64%25%34%36%25%37%33dWU=
  69. ## The similar to the above but also touching the edge of the base64
  70. secret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
  71. ## The similar to the above but also touching and overlapping the base64
  72. secret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
  73. `
  74. func TestDetect(t *testing.T) {
  75. logging.Logger = logging.Logger.Level(zerolog.TraceLevel)
  76. tests := map[string]struct {
  77. cfgName string
  78. baselinePath string
  79. fragment Fragment
  80. // NOTE: for expected findings, all line numbers will be 0
  81. // because line deltas are added _after_ the finding is created.
  82. // I.e., if the finding is from a --no-git file, the line number will be
  83. // increase by 1 in DetectFromFiles(). If the finding is from git,
  84. // the line number will be increased by the patch delta.
  85. expectedFindings []report.Finding
  86. wantError error
  87. }{
  88. // General
  89. "valid allow comment (1)": {
  90. cfgName: "simple",
  91. fragment: Fragment{
  92. Raw: `awsToken := \"AKIALALEMEL33243OKIA\ // gitleaks:allow"`,
  93. FilePath: "tmp.go",
  94. },
  95. },
  96. "valid allow comment (2)": {
  97. cfgName: "simple",
  98. fragment: Fragment{
  99. Raw: `awsToken := \
  100. \"AKIALALEMEL33243OKIA\ // gitleaks:allow"
  101. `,
  102. FilePath: "tmp.go",
  103. },
  104. },
  105. "invalid allow comment": {
  106. cfgName: "simple",
  107. fragment: Fragment{
  108. Raw: `awsToken := \"AKIALALEMEL33243OKIA\"
  109. // gitleaks:allow"
  110. `,
  111. FilePath: "tmp.go",
  112. },
  113. expectedFindings: []report.Finding{
  114. {
  115. Description: "AWS Access Key",
  116. Secret: "AKIALALEMEL33243OKIA",
  117. Match: "AKIALALEMEL33243OKIA",
  118. File: "tmp.go",
  119. Line: `awsToken := \"AKIALALEMEL33243OKIA\"`,
  120. RuleID: "aws-access-key",
  121. Tags: []string{"key", "AWS"},
  122. StartLine: 0,
  123. EndLine: 0,
  124. StartColumn: 15,
  125. EndColumn: 34,
  126. Entropy: 3.1464393,
  127. },
  128. },
  129. },
  130. "detect finding - aws": {
  131. cfgName: "simple",
  132. fragment: Fragment{
  133. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  134. FilePath: "tmp.go",
  135. },
  136. expectedFindings: []report.Finding{
  137. {
  138. RuleID: "aws-access-key",
  139. Description: "AWS Access Key",
  140. File: "tmp.go",
  141. Line: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  142. Match: "AKIALALEMEL33243OLIA",
  143. Secret: "AKIALALEMEL33243OLIA",
  144. Entropy: 3.0841837,
  145. StartLine: 0,
  146. EndLine: 0,
  147. StartColumn: 15,
  148. EndColumn: 34,
  149. Tags: []string{"key", "AWS"},
  150. },
  151. },
  152. },
  153. "detect finding - sidekiq env var": {
  154. cfgName: "simple",
  155. fragment: Fragment{
  156. Raw: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  157. FilePath: "tmp.sh",
  158. },
  159. expectedFindings: []report.Finding{
  160. {
  161. RuleID: "sidekiq-secret",
  162. Description: "Sidekiq Secret",
  163. File: "tmp.sh",
  164. Line: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  165. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;",
  166. Secret: "cafebabe:deadbeef",
  167. Entropy: 2.6098502,
  168. StartLine: 0,
  169. EndLine: 0,
  170. StartColumn: 8,
  171. EndColumn: 60,
  172. Tags: []string{},
  173. },
  174. },
  175. },
  176. "detect finding - sidekiq env var, semicolon": {
  177. cfgName: "simple",
  178. fragment: Fragment{
  179. Raw: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  180. FilePath: "tmp.sh",
  181. },
  182. expectedFindings: []report.Finding{
  183. {
  184. RuleID: "sidekiq-secret",
  185. Description: "Sidekiq Secret",
  186. File: "tmp.sh",
  187. Line: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  188. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=\"cafebabe:deadbeef\"",
  189. Secret: "cafebabe:deadbeef",
  190. Entropy: 2.6098502,
  191. StartLine: 0,
  192. EndLine: 0,
  193. StartColumn: 21,
  194. EndColumn: 74,
  195. Tags: []string{},
  196. },
  197. },
  198. },
  199. "detect finding - sidekiq url": {
  200. cfgName: "simple",
  201. fragment: Fragment{
  202. Raw: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  203. FilePath: "tmp.sh",
  204. },
  205. expectedFindings: []report.Finding{
  206. {
  207. RuleID: "sidekiq-sensitive-url",
  208. Description: "Sidekiq Sensitive URL",
  209. File: "tmp.sh",
  210. Line: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  211. Match: "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:",
  212. Secret: "cafeb4b3:d3adb33f",
  213. Entropy: 2.984234,
  214. StartLine: 0,
  215. EndLine: 0,
  216. StartColumn: 8,
  217. EndColumn: 58,
  218. Tags: []string{},
  219. },
  220. },
  221. },
  222. "ignore finding - our config file": {
  223. cfgName: "simple",
  224. fragment: Fragment{
  225. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  226. FilePath: filepath.Join(configPath, "simple.toml"),
  227. },
  228. },
  229. "ignore finding - doesn't match path": {
  230. cfgName: "generic_with_py_path",
  231. fragment: Fragment{
  232. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  233. FilePath: "tmp.go",
  234. },
  235. },
  236. "detect finding - matches path,regex,entropy": {
  237. cfgName: "generic_with_py_path",
  238. fragment: Fragment{
  239. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  240. FilePath: "tmp.py",
  241. },
  242. expectedFindings: []report.Finding{
  243. {
  244. RuleID: "generic-api-key",
  245. Description: "Generic API Key",
  246. File: "tmp.py",
  247. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  248. Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  249. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  250. Entropy: 3.7906237,
  251. StartLine: 0,
  252. EndLine: 0,
  253. StartColumn: 22,
  254. EndColumn: 93,
  255. Tags: []string{},
  256. },
  257. },
  258. },
  259. "ignore finding - allowlist regex": {
  260. cfgName: "generic_with_py_path",
  261. fragment: Fragment{
  262. Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  263. FilePath: "tmp.py",
  264. },
  265. },
  266. // Rule
  267. "rule - ignore path": {
  268. cfgName: "valid/rule_path_only",
  269. baselinePath: ".baseline.json",
  270. fragment: Fragment{
  271. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  272. FilePath: ".baseline.json",
  273. },
  274. },
  275. "rule - detect path ": {
  276. cfgName: "valid/rule_path_only",
  277. fragment: Fragment{
  278. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  279. FilePath: "tmp.py",
  280. },
  281. expectedFindings: []report.Finding{
  282. {
  283. Description: "Python Files",
  284. Match: "file detected: tmp.py",
  285. File: "tmp.py",
  286. RuleID: "python-files-only",
  287. Tags: []string{},
  288. },
  289. },
  290. },
  291. "rule - match based on entropy": {
  292. cfgName: "valid/rule_entropy_group",
  293. fragment: Fragment{
  294. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"
  295. //const Discord_Public_Key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  296. `,
  297. FilePath: "tmp.go",
  298. },
  299. expectedFindings: []report.Finding{
  300. {
  301. RuleID: "discord-api-key",
  302. Description: "Discord API key",
  303. File: "tmp.go",
  304. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  305. Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  306. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  307. Entropy: 3.7906237,
  308. StartLine: 0,
  309. EndLine: 0,
  310. StartColumn: 7,
  311. EndColumn: 93,
  312. Tags: []string{},
  313. },
  314. },
  315. },
  316. // Allowlists
  317. "global allowlist - ignore regex": {
  318. cfgName: "valid/allowlist_global_regex",
  319. fragment: Fragment{
  320. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  321. FilePath: "tmp.go",
  322. },
  323. },
  324. "global allowlist - detect, doesn't match all conditions": {
  325. cfgName: "valid/allowlist_global_multiple",
  326. fragment: Fragment{
  327. Raw: `
  328. const token = "mockSecret";
  329. // const token = "changeit";`,
  330. FilePath: "config.txt",
  331. },
  332. expectedFindings: []report.Finding{
  333. {
  334. RuleID: "test",
  335. File: "config.txt",
  336. Line: "\nconst token = \"mockSecret\";",
  337. Match: `token = "mockSecret"`,
  338. Secret: "mockSecret",
  339. Entropy: 2.9219282,
  340. StartLine: 1,
  341. EndLine: 1,
  342. StartColumn: 8,
  343. EndColumn: 27,
  344. Tags: []string{},
  345. },
  346. },
  347. },
  348. "global allowlist - ignore, matches all conditions": {
  349. cfgName: "valid/allowlist_global_multiple",
  350. fragment: Fragment{
  351. Raw: `token := "mockSecret";`,
  352. FilePath: "node_modules/config.txt",
  353. },
  354. },
  355. "global allowlist - detect path, doesn't match all conditions": {
  356. cfgName: "valid/allowlist_global_multiple",
  357. fragment: Fragment{
  358. Raw: `var token = "fakeSecret";`,
  359. FilePath: "node_modules/config.txt",
  360. },
  361. expectedFindings: []report.Finding{
  362. {
  363. RuleID: "test",
  364. File: "node_modules/config.txt",
  365. Line: "var token = \"fakeSecret\";",
  366. Match: `token = "fakeSecret"`,
  367. Secret: "fakeSecret",
  368. Entropy: 2.8464394,
  369. StartLine: 0,
  370. EndLine: 0,
  371. StartColumn: 5,
  372. EndColumn: 24,
  373. Tags: []string{},
  374. },
  375. },
  376. },
  377. "allowlist - ignore commit": {
  378. cfgName: "valid/allowlist_rule_commit",
  379. fragment: Fragment{
  380. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  381. FilePath: "tmp.go",
  382. CommitSHA: "allowthiscommit",
  383. },
  384. },
  385. "allowlist - ignore path": {
  386. cfgName: "valid/allowlist_rule_path",
  387. fragment: Fragment{
  388. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  389. FilePath: "tmp.go",
  390. },
  391. },
  392. "allowlist - ignore path when extending": {
  393. cfgName: "valid/allowlist_rule_extend_default",
  394. fragment: Fragment{
  395. Raw: `token = "aebfab88-7596-481d-82e8-c60c8f7de0c0"`,
  396. FilePath: "path/to/your/problematic/file.js",
  397. },
  398. },
  399. "allowlist - ignore regex": {
  400. cfgName: "valid/allowlist_rule_regex",
  401. fragment: Fragment{
  402. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  403. FilePath: "tmp.go",
  404. },
  405. },
  406. // Decoding
  407. "detect encoded": {
  408. cfgName: "encoded",
  409. fragment: Fragment{
  410. Raw: encodedTestValues,
  411. FilePath: "tmp.go",
  412. },
  413. expectedFindings: []report.Finding{
  414. { // Plain text key captured by normal rule
  415. Description: "Private Key",
  416. Secret: "-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  417. Match: "-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  418. File: "tmp.go",
  419. Line: "\n-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  420. RuleID: "private-key",
  421. Tags: []string{"key", "private"},
  422. StartLine: 2,
  423. EndLine: 5,
  424. StartColumn: 2,
  425. EndColumn: 26,
  426. Entropy: 5.350665,
  427. },
  428. { // Encoded key captured by custom b64 regex rule
  429. Description: "Private Key",
  430. Secret: "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K",
  431. Match: "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K",
  432. File: "tmp.go",
  433. Line: "\nprivate_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'",
  434. RuleID: "b64-encoded-private-key",
  435. Tags: []string{"key", "private"},
  436. StartLine: 8,
  437. EndLine: 8,
  438. StartColumn: 16,
  439. EndColumn: 207,
  440. Entropy: 5.3861146,
  441. },
  442. { // Encoded key captured by plain text rule using the decoder
  443. Description: "Private Key",
  444. Secret: "-----BEGIN PRIVATE KEY-----\n435f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  445. Match: "-----BEGIN PRIVATE KEY-----\n435f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  446. File: "tmp.go",
  447. Line: "\nprivate_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'",
  448. RuleID: "private-key",
  449. Tags: []string{"key", "private", "decoded:base64", "decode-depth:1"},
  450. StartLine: 8,
  451. EndLine: 8,
  452. StartColumn: 16,
  453. EndColumn: 207,
  454. Entropy: 5.350665,
  455. },
  456. { // Encoded Small secret at the end to make sure it's picked up by the decoding
  457. Description: "Small Secret",
  458. Secret: "small-secret",
  459. Match: "small-secret",
  460. File: "tmp.go",
  461. Line: "\nc21hbGwtc2VjcmV0",
  462. RuleID: "small-secret",
  463. Tags: []string{"small", "secret", "decoded:base64", "decode-depth:1"},
  464. StartLine: 15,
  465. EndLine: 15,
  466. StartColumn: 2,
  467. EndColumn: 17,
  468. Entropy: 3.0849626,
  469. },
  470. { // Secret where the decoded match goes outside the encoded value
  471. Description: "Overlapping",
  472. Secret: "decoded-secret-value00",
  473. Match: "secret=decoded-secret-value00",
  474. File: "tmp.go",
  475. Line: "\nsecret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==",
  476. RuleID: "overlapping",
  477. Tags: []string{"overlapping", "decoded:base64", "decode-depth:1"},
  478. StartLine: 18,
  479. EndLine: 18,
  480. StartColumn: 2,
  481. EndColumn: 40,
  482. Entropy: 3.4428623,
  483. },
  484. { // This just confirms that with no allowlist the pattern is detected (i.e. the regex is good)
  485. Description: "Make sure this would be detected with no allowlist",
  486. Secret: "lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw",
  487. Match: "password=\"lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw\"",
  488. File: "tmp.go",
  489. Line: "\npassword=\"bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw==\"",
  490. RuleID: "decoded-password-dont-ignore",
  491. Tags: []string{"decode-ignore", "decoded:base64", "decode-depth:1"},
  492. StartLine: 23,
  493. EndLine: 23,
  494. StartColumn: 2,
  495. EndColumn: 68,
  496. Entropy: 4.5841837,
  497. },
  498. { // Hex encoded data check
  499. Description: "Overlapping",
  500. Secret: "decoded-secret-valuevHEX",
  501. Match: "secret=decoded-secret-valuevHEX",
  502. File: "tmp.go",
  503. Line: "\nsecret=6465636F6465642D7365637265742D76616C756576484558",
  504. RuleID: "overlapping",
  505. Tags: []string{"overlapping", "decoded:hex", "decode-depth:1"},
  506. StartLine: 26,
  507. EndLine: 26,
  508. StartColumn: 2,
  509. EndColumn: 56,
  510. Entropy: 3.6531072,
  511. },
  512. { // handle partial encoded percent data
  513. Description: "Overlapping",
  514. Secret: "decoded-secret-valuev2",
  515. Match: "secret=decoded-secret-valuev2",
  516. File: "tmp.go",
  517. Line: "\nsecret=decoded-%73%65%63%72%65%74-valuev2",
  518. RuleID: "overlapping",
  519. Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
  520. StartLine: 30,
  521. EndLine: 30,
  522. StartColumn: 2,
  523. EndColumn: 42,
  524. Entropy: 3.4428623,
  525. },
  526. { // handle partial encoded percent data
  527. Description: "Overlapping",
  528. Secret: "decoded-secret-valuev3",
  529. Match: "secret=decoded-secret-valuev3",
  530. File: "tmp.go",
  531. Line: "\nsecret=%64%65coded-%73%65%63%72%65%74-valuev3",
  532. RuleID: "overlapping",
  533. Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
  534. StartLine: 32,
  535. EndLine: 32,
  536. StartColumn: 2,
  537. EndColumn: 46,
  538. Entropy: 3.4428623,
  539. },
  540. { // Encoded AWS config with a access key id inside a JWT
  541. Description: "AWS IAM Unique Identifier",
  542. Secret: "ASIAIOSFODNN7LXM10JI",
  543. Match: " ASIAIOSFODNN7LXM10JI",
  544. File: "tmp.go",
  545. Line: "\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA",
  546. RuleID: "aws-iam-unique-identifier",
  547. Tags: []string{"aws", "identifier", "decoded:base64", "decode-depth:2"},
  548. StartLine: 11,
  549. EndLine: 11,
  550. StartColumn: 39,
  551. EndColumn: 344,
  552. Entropy: 3.6841838,
  553. },
  554. { // Encoded AWS config with a secret access key inside a JWT
  555. Description: "AWS Secret Access Key",
  556. Secret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEFUCDlEX1A",
  557. Match: "aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEFUCDlEX1A",
  558. File: "tmp.go",
  559. Line: "\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA",
  560. RuleID: "aws-secret-access-key",
  561. Tags: []string{"aws", "secret", "decoded:base64", "decode-depth:2"},
  562. StartLine: 11,
  563. EndLine: 11,
  564. StartColumn: 39,
  565. EndColumn: 344,
  566. Entropy: 4.721928,
  567. },
  568. { // Secret where the decoded match goes outside the encoded value and then encoded again
  569. Description: "Overlapping",
  570. Secret: "decoded-secret-value",
  571. Match: "secret=decoded-secret-value",
  572. File: "tmp.go",
  573. Line: "\nc2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=",
  574. RuleID: "overlapping",
  575. Tags: []string{"overlapping", "decoded:base64", "decode-depth:2"},
  576. StartLine: 20,
  577. EndLine: 20,
  578. StartColumn: 2,
  579. EndColumn: 49,
  580. Entropy: 3.3037016,
  581. },
  582. { // handle encodings that touch eachother
  583. Description: "Overlapping",
  584. Secret: "decoded-secret-valuev5",
  585. Match: "secret=decoded-secret-valuev5",
  586. File: "tmp.go",
  587. Line: "\nsecret%3d6465636F6465642D7365637265742D76616C75657635",
  588. RuleID: "overlapping",
  589. Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decode-depth:2"},
  590. StartLine: 40,
  591. EndLine: 40,
  592. StartColumn: 2,
  593. EndColumn: 54,
  594. Entropy: 3.4428623,
  595. },
  596. { // handle partial encoded percent data465642D7365637265742D76616C75657635
  597. Description: "Overlapping",
  598. Secret: "decoded-secret-valuev4",
  599. Match: "secret=decoded-secret-valuev4",
  600. File: "tmp.go",
  601. Line: "\nc2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09",
  602. RuleID: "overlapping",
  603. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:3"},
  604. StartLine: 38,
  605. EndLine: 38,
  606. StartColumn: 2,
  607. EndColumn: 55,
  608. Entropy: 3.4428623,
  609. },
  610. { // multiple percent encodings in a single layer base64
  611. Description: "Overlapping",
  612. Secret: "decoded-secret-valuex86",
  613. Match: "secret=decoded-secret-valuex86",
  614. File: "tmp.go",
  615. Line: "\nsecret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86",
  616. RuleID: "overlapping",
  617. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
  618. StartLine: 42,
  619. EndLine: 42,
  620. StartColumn: 2,
  621. EndColumn: 44,
  622. Entropy: 3.6381476,
  623. },
  624. { // base64 encoded partially percent encoded value
  625. Description: "Overlapping",
  626. Secret: "decoded-secret-value",
  627. Match: "secret=decoded-secret-value",
  628. File: "tmp.go",
  629. Line: "\nsecret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=",
  630. RuleID: "overlapping",
  631. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
  632. StartLine: 44,
  633. EndLine: 44,
  634. StartColumn: 2,
  635. EndColumn: 52,
  636. Entropy: 3.3037016,
  637. },
  638. { // one of the lines above that went through... a lot
  639. Description: "Overlapping",
  640. Secret: "decoded-secret-value",
  641. Match: "secret=decoded-secret-value",
  642. File: "tmp.go",
  643. Line: "\nLook at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?",
  644. RuleID: "overlapping",
  645. Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decoded:base64", "decode-depth:7"},
  646. StartLine: 47,
  647. EndLine: 47,
  648. StartColumn: 22,
  649. EndColumn: 177,
  650. Entropy: 3.3037016,
  651. },
  652. { // Multi percent encode two random characters close to the bounds of the base64
  653. Description: "Overlapping",
  654. Secret: "decoded-secret-value",
  655. Match: "secret=decoded-secret-value",
  656. File: "tmp.go",
  657. Line: "\nsecret=ZG%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%36%25%33%31%25%32%35%25%33%32%25%33%35%25%32%35%25%33%33%25%33%36%25%32%35%25%33%33%25%33%322RlZC1zZWNyZXQtd%25%36%64%25%34%36%25%37%33dWU=",
  658. RuleID: "overlapping",
  659. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:5"},
  660. StartLine: 50,
  661. EndLine: 50,
  662. StartColumn: 2,
  663. EndColumn: 300,
  664. Entropy: 3.3037016,
  665. },
  666. { // The similar to the above but also touching the edge of the base64
  667. Description: "Overlapping",
  668. Secret: "decoded-secret-value",
  669. Match: "secret=decoded-secret-value",
  670. File: "tmp.go",
  671. Line: "\nsecret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
  672. RuleID: "overlapping",
  673. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
  674. StartLine: 52,
  675. EndLine: 52,
  676. StartColumn: 2,
  677. EndColumn: 86,
  678. Entropy: 3.3037016,
  679. },
  680. { // The similar to the above but also touching and overlapping the base64
  681. Description: "Overlapping",
  682. Secret: "decoded-secret-value",
  683. Match: "secret=decoded-secret-value",
  684. File: "tmp.go",
  685. Line: "\nsecret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
  686. RuleID: "overlapping",
  687. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
  688. StartLine: 54,
  689. EndLine: 54,
  690. StartColumn: 2,
  691. EndColumn: 88,
  692. Entropy: 3.3037016,
  693. },
  694. },
  695. },
  696. }
  697. for name, tt := range tests {
  698. t.Run(name, func(t *testing.T) {
  699. viper.Reset()
  700. viper.AddConfigPath(configPath)
  701. viper.SetConfigName(tt.cfgName)
  702. viper.SetConfigType("toml")
  703. err := viper.ReadInConfig()
  704. require.NoError(t, err)
  705. var vc config.ViperConfig
  706. err = viper.Unmarshal(&vc)
  707. require.NoError(t, err)
  708. cfg, err := vc.Translate()
  709. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  710. assert.Equal(t, tt.wantError, err)
  711. d := NewDetector(cfg)
  712. d.MaxDecodeDepth = maxDecodeDepth
  713. d.baselinePath = tt.baselinePath
  714. findings := d.Detect(tt.fragment)
  715. assert.ElementsMatch(t, tt.expectedFindings, findings)
  716. })
  717. }
  718. }
  719. // TestFromGit tests the FromGit function
  720. func TestFromGit(t *testing.T) {
  721. // TODO: Fix this test on windows.
  722. if runtime.GOOS == "windows" {
  723. t.Skipf("TODO: this fails on Windows: [git] fatal: bad object refs/remotes/origin/main?")
  724. return
  725. }
  726. tests := []struct {
  727. cfgName string
  728. source string
  729. logOpts string
  730. expectedFindings []report.Finding
  731. }{
  732. {
  733. source: filepath.Join(repoBasePath, "small"),
  734. cfgName: "simple", // the remote url is `git@github.com:gitleaks/test.git`
  735. expectedFindings: []report.Finding{
  736. {
  737. RuleID: "aws-access-key",
  738. Description: "AWS Access Key",
  739. StartLine: 20,
  740. EndLine: 20,
  741. StartColumn: 19,
  742. EndColumn: 38,
  743. Line: "\n awsToken := \"AKIALALEMEL33243OLIA\"",
  744. Secret: "AKIALALEMEL33243OLIA",
  745. Match: "AKIALALEMEL33243OLIA",
  746. Entropy: 3.0841837,
  747. File: "main.go",
  748. Date: "2021-11-02T23:37:53Z",
  749. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  750. Author: "Zachary Rice",
  751. Email: "zricer@protonmail.com",
  752. Message: "Accidentally add a secret",
  753. Tags: []string{"key", "AWS"},
  754. Fingerprint: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587:main.go:aws-access-key:20",
  755. Link: "https://github.com/gitleaks/test/blob/1b6da43b82b22e4eaa10bcf8ee591e91abbfc587/main.go#L20",
  756. },
  757. {
  758. RuleID: "aws-access-key",
  759. Description: "AWS Access Key",
  760. StartLine: 9,
  761. EndLine: 9,
  762. StartColumn: 17,
  763. EndColumn: 36,
  764. Secret: "AKIALALEMEL33243OLIA",
  765. Match: "AKIALALEMEL33243OLIA",
  766. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  767. File: "foo/foo.go",
  768. Date: "2021-11-02T23:48:06Z",
  769. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  770. Author: "Zach Rice",
  771. Email: "zricer@protonmail.com",
  772. Message: "adding foo package with secret",
  773. Tags: []string{"key", "AWS"},
  774. Entropy: 3.0841837,
  775. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  776. Link: "https://github.com/gitleaks/test/blob/491504d5a31946ce75e22554cc34203d8e5ff3ca/foo/foo.go#L9",
  777. },
  778. },
  779. },
  780. {
  781. source: filepath.Join(repoBasePath, "small"),
  782. logOpts: "--all foo...",
  783. cfgName: "simple",
  784. expectedFindings: []report.Finding{
  785. {
  786. RuleID: "aws-access-key",
  787. Description: "AWS Access Key",
  788. StartLine: 9,
  789. EndLine: 9,
  790. StartColumn: 17,
  791. EndColumn: 36,
  792. Secret: "AKIALALEMEL33243OLIA",
  793. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  794. Match: "AKIALALEMEL33243OLIA",
  795. Date: "2021-11-02T23:48:06Z",
  796. File: "foo/foo.go",
  797. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  798. Author: "Zach Rice",
  799. Email: "zricer@protonmail.com",
  800. Message: "adding foo package with secret",
  801. Tags: []string{"key", "AWS"},
  802. Entropy: 3.0841837,
  803. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  804. Link: "https://github.com/gitleaks/test/blob/491504d5a31946ce75e22554cc34203d8e5ff3ca/foo/foo.go#L9",
  805. },
  806. },
  807. },
  808. }
  809. moveDotGit(t, "dotGit", ".git")
  810. defer moveDotGit(t, ".git", "dotGit")
  811. for _, tt := range tests {
  812. t.Run(strings.Join([]string{tt.cfgName, tt.source, tt.logOpts}, "/"), func(t *testing.T) {
  813. viper.AddConfigPath(configPath)
  814. viper.SetConfigName("simple")
  815. viper.SetConfigType("toml")
  816. err := viper.ReadInConfig()
  817. require.NoError(t, err)
  818. var vc config.ViperConfig
  819. err = viper.Unmarshal(&vc)
  820. require.NoError(t, err)
  821. cfg, err := vc.Translate()
  822. require.NoError(t, err)
  823. detector := NewDetector(cfg)
  824. var ignorePath string
  825. info, err := os.Stat(tt.source)
  826. require.NoError(t, err)
  827. if info.IsDir() {
  828. ignorePath = filepath.Join(tt.source, ".gitleaksignore")
  829. } else {
  830. ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
  831. }
  832. err = detector.AddGitleaksIgnore(ignorePath)
  833. require.NoError(t, err)
  834. gitCmd, err := sources.NewGitLogCmd(tt.source, tt.logOpts)
  835. require.NoError(t, err)
  836. remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
  837. findings, err := detector.DetectGit(gitCmd, remote)
  838. require.NoError(t, err)
  839. for _, f := range findings {
  840. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  841. }
  842. assert.ElementsMatch(t, tt.expectedFindings, findings)
  843. })
  844. }
  845. }
  846. func TestFromGitStaged(t *testing.T) {
  847. tests := []struct {
  848. cfgName string
  849. source string
  850. logOpts string
  851. expectedFindings []report.Finding
  852. }{
  853. {
  854. source: filepath.Join(repoBasePath, "staged"),
  855. cfgName: "simple",
  856. expectedFindings: []report.Finding{
  857. {
  858. RuleID: "aws-access-key",
  859. Description: "AWS Access Key",
  860. StartLine: 7,
  861. EndLine: 7,
  862. StartColumn: 18,
  863. EndColumn: 37,
  864. Line: "\n\taws_token2 := \"AKIALALEMEL33243OLIA\" // this one is not",
  865. Match: "AKIALALEMEL33243OLIA",
  866. Secret: "AKIALALEMEL33243OLIA",
  867. File: "api/api.go",
  868. SymlinkFile: "",
  869. Commit: "",
  870. Entropy: 3.0841837,
  871. Author: "",
  872. Email: "",
  873. Date: "0001-01-01T00:00:00Z",
  874. Message: "",
  875. Tags: []string{
  876. "key",
  877. "AWS",
  878. },
  879. Fingerprint: "api/api.go:aws-access-key:7",
  880. Link: "",
  881. },
  882. },
  883. },
  884. }
  885. moveDotGit(t, "dotGit", ".git")
  886. defer moveDotGit(t, ".git", "dotGit")
  887. for _, tt := range tests {
  888. viper.AddConfigPath(configPath)
  889. viper.SetConfigName("simple")
  890. viper.SetConfigType("toml")
  891. err := viper.ReadInConfig()
  892. require.NoError(t, err)
  893. var vc config.ViperConfig
  894. err = viper.Unmarshal(&vc)
  895. require.NoError(t, err)
  896. cfg, err := vc.Translate()
  897. require.NoError(t, err)
  898. detector := NewDetector(cfg)
  899. err = detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore"))
  900. require.NoError(t, err)
  901. gitCmd, err := sources.NewGitDiffCmd(tt.source, true)
  902. require.NoError(t, err)
  903. remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
  904. findings, err := detector.DetectGit(gitCmd, remote)
  905. require.NoError(t, err)
  906. for _, f := range findings {
  907. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  908. }
  909. assert.ElementsMatch(t, tt.expectedFindings, findings)
  910. }
  911. }
  912. // TestFromFiles tests the FromFiles function
  913. func TestFromFiles(t *testing.T) {
  914. tests := []struct {
  915. cfgName string
  916. source string
  917. expectedFindings []report.Finding
  918. }{
  919. {
  920. source: filepath.Join(repoBasePath, "nogit"),
  921. cfgName: "simple",
  922. expectedFindings: []report.Finding{
  923. {
  924. RuleID: "aws-access-key",
  925. Description: "AWS Access Key",
  926. StartLine: 20,
  927. EndLine: 20,
  928. StartColumn: 16,
  929. EndColumn: 35,
  930. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  931. Match: "AKIALALEMEL33243OLIA",
  932. Secret: "AKIALALEMEL33243OLIA",
  933. File: "../testdata/repos/nogit/main.go",
  934. SymlinkFile: "",
  935. Tags: []string{"key", "AWS"},
  936. Entropy: 3.0841837,
  937. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  938. },
  939. },
  940. },
  941. {
  942. source: filepath.Join(repoBasePath, "nogit", "main.go"),
  943. cfgName: "simple",
  944. expectedFindings: []report.Finding{
  945. {
  946. RuleID: "aws-access-key",
  947. Description: "AWS Access Key",
  948. StartLine: 20,
  949. EndLine: 20,
  950. StartColumn: 16,
  951. EndColumn: 35,
  952. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  953. Match: "AKIALALEMEL33243OLIA",
  954. Secret: "AKIALALEMEL33243OLIA",
  955. File: "../testdata/repos/nogit/main.go",
  956. Tags: []string{"key", "AWS"},
  957. Entropy: 3.0841837,
  958. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  959. },
  960. },
  961. },
  962. {
  963. source: filepath.Join(repoBasePath, "nogit", "api.go"),
  964. cfgName: "simple",
  965. expectedFindings: []report.Finding{},
  966. },
  967. {
  968. source: filepath.Join(repoBasePath, "nogit", ".env.prod"),
  969. cfgName: "generic",
  970. expectedFindings: []report.Finding{
  971. {
  972. RuleID: "generic-api-key",
  973. Description: "Generic API Key",
  974. StartLine: 4,
  975. EndLine: 4,
  976. StartColumn: 5,
  977. EndColumn: 35,
  978. Line: "\nDB_PASSWORD=8ae31cacf141669ddfb5da",
  979. Match: "PASSWORD=8ae31cacf141669ddfb5da",
  980. Secret: "8ae31cacf141669ddfb5da",
  981. File: "../testdata/repos/nogit/.env.prod",
  982. Tags: []string{},
  983. Entropy: 3.5383105,
  984. Fingerprint: "../testdata/repos/nogit/.env.prod:generic-api-key:4",
  985. },
  986. },
  987. },
  988. {
  989. source: filepath.Join(repoBasePath, "archive"),
  990. cfgName: "simple",
  991. expectedFindings: []report.Finding{
  992. {
  993. RuleID: "aws-access-key",
  994. Description: "AWS Access Key",
  995. StartLine: 20,
  996. EndLine: 20,
  997. StartColumn: 16,
  998. EndColumn: 35,
  999. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1000. Match: "AKIALALEMEL33243OLIA",
  1001. Secret: "AKIALALEMEL33243OLIA",
  1002. File: "archive.zip/main.go",
  1003. SymlinkFile: "",
  1004. Tags: []string{"key", "AWS"},
  1005. Entropy: 3.0841837,
  1006. Fingerprint: "archive.zip/main.go:aws-access-key:20",
  1007. },
  1008. },
  1009. },
  1010. }
  1011. for _, tt := range tests {
  1012. t.Run(tt.cfgName+" - "+tt.source, func(t *testing.T) {
  1013. viper.AddConfigPath(configPath)
  1014. viper.SetConfigName(tt.cfgName)
  1015. viper.SetConfigType("toml")
  1016. err := viper.ReadInConfig()
  1017. require.NoError(t, err)
  1018. var vc config.ViperConfig
  1019. err = viper.Unmarshal(&vc)
  1020. require.NoError(t, err)
  1021. cfg, _ := vc.Translate()
  1022. detector := NewDetector(cfg)
  1023. info, err := os.Stat(tt.source)
  1024. require.NoError(t, err)
  1025. var ignorePath string
  1026. if info.IsDir() {
  1027. ignorePath = filepath.Join(tt.source, ".gitleaksignore")
  1028. } else {
  1029. ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
  1030. }
  1031. err = detector.AddGitleaksIgnore(ignorePath)
  1032. require.NoError(t, err)
  1033. detector.FollowSymlinks = true
  1034. paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
  1035. require.NoError(t, err)
  1036. findings, err := detector.DetectFiles(paths)
  1037. require.NoError(t, err)
  1038. // TODO: Temporary mitigation.
  1039. // https://github.com/gitleaks/gitleaks/issues/1641
  1040. normalizedFindings := make([]report.Finding, len(findings))
  1041. for i, f := range findings {
  1042. if strings.HasSuffix(f.Line, "\r") {
  1043. f.Line = strings.ReplaceAll(f.Line, "\r", "")
  1044. }
  1045. if strings.HasSuffix(f.Match, "\r") {
  1046. f.EndColumn = f.EndColumn - 1
  1047. f.Match = strings.ReplaceAll(f.Match, "\r", "")
  1048. }
  1049. normalizedFindings[i] = f
  1050. }
  1051. assert.ElementsMatch(t, tt.expectedFindings, normalizedFindings)
  1052. })
  1053. }
  1054. }
  1055. func TestDetectWithSymlinks(t *testing.T) {
  1056. // TODO: Fix this test on windows.
  1057. if runtime.GOOS == "windows" {
  1058. t.Skipf("TODO: this returns no results on windows, I'm not sure why.")
  1059. return
  1060. }
  1061. tests := []struct {
  1062. cfgName string
  1063. source string
  1064. expectedFindings []report.Finding
  1065. }{
  1066. {
  1067. source: filepath.Join(repoBasePath, "symlinks/file_symlink"),
  1068. cfgName: "simple",
  1069. expectedFindings: []report.Finding{
  1070. {
  1071. RuleID: "apkey",
  1072. Description: "Asymmetric Private Key",
  1073. StartLine: 1,
  1074. EndLine: 1,
  1075. StartColumn: 1,
  1076. EndColumn: 35,
  1077. Match: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1078. Secret: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1079. Line: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1080. File: "../testdata/repos/symlinks/source_file/id_ed25519",
  1081. SymlinkFile: "../testdata/repos/symlinks/file_symlink/symlinked_id_ed25519",
  1082. Tags: []string{"key", "AsymmetricPrivateKey"},
  1083. Entropy: 3.587164,
  1084. Fingerprint: "../testdata/repos/symlinks/source_file/id_ed25519:apkey:1",
  1085. },
  1086. },
  1087. },
  1088. }
  1089. for _, tt := range tests {
  1090. viper.AddConfigPath(configPath)
  1091. viper.SetConfigName("simple")
  1092. viper.SetConfigType("toml")
  1093. err := viper.ReadInConfig()
  1094. require.NoError(t, err)
  1095. var vc config.ViperConfig
  1096. err = viper.Unmarshal(&vc)
  1097. require.NoError(t, err)
  1098. cfg, _ := vc.Translate()
  1099. detector := NewDetector(cfg)
  1100. detector.FollowSymlinks = true
  1101. paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
  1102. require.NoError(t, err)
  1103. findings, err := detector.DetectFiles(paths)
  1104. require.NoError(t, err)
  1105. assert.ElementsMatch(t, tt.expectedFindings, findings)
  1106. }
  1107. }
  1108. func TestDetectRuleAllowlist(t *testing.T) {
  1109. cases := map[string]struct {
  1110. fragment Fragment
  1111. allowlist *config.Allowlist
  1112. expected []report.Finding
  1113. }{
  1114. // Commit / path
  1115. "commit allowed": {
  1116. fragment: Fragment{
  1117. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1118. },
  1119. allowlist: &config.Allowlist{
  1120. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1121. },
  1122. },
  1123. "path allowed": {
  1124. fragment: Fragment{
  1125. FilePath: "package-lock.json",
  1126. },
  1127. allowlist: &config.Allowlist{
  1128. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1129. },
  1130. },
  1131. "commit AND path allowed": {
  1132. fragment: Fragment{
  1133. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1134. FilePath: "package-lock.json",
  1135. },
  1136. allowlist: &config.Allowlist{
  1137. MatchCondition: config.AllowlistMatchAnd,
  1138. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1139. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1140. },
  1141. },
  1142. "commit AND path NOT allowed": {
  1143. fragment: Fragment{
  1144. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1145. FilePath: "package.json",
  1146. },
  1147. allowlist: &config.Allowlist{
  1148. MatchCondition: config.AllowlistMatchAnd,
  1149. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1150. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1151. },
  1152. expected: []report.Finding{
  1153. {
  1154. StartColumn: 50,
  1155. EndColumn: 60,
  1156. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  1157. Match: "Summer2024!",
  1158. Secret: "Summer2024!",
  1159. File: "package.json",
  1160. Entropy: 3.095795154571533,
  1161. RuleID: "test-rule",
  1162. },
  1163. },
  1164. },
  1165. "commit AND path NOT allowed - other conditions": {
  1166. fragment: Fragment{
  1167. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1168. FilePath: "package-lock.json",
  1169. },
  1170. allowlist: &config.Allowlist{
  1171. MatchCondition: config.AllowlistMatchAnd,
  1172. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1173. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1174. Regexes: []*regexp.Regexp{regexp.MustCompile("password")},
  1175. },
  1176. expected: []report.Finding{
  1177. {
  1178. StartColumn: 50,
  1179. EndColumn: 60,
  1180. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  1181. Match: "Summer2024!",
  1182. Secret: "Summer2024!",
  1183. File: "package-lock.json",
  1184. Entropy: 3.095795154571533,
  1185. RuleID: "test-rule",
  1186. },
  1187. },
  1188. },
  1189. "commit OR path allowed": {
  1190. fragment: Fragment{
  1191. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1192. FilePath: "package-lock.json",
  1193. },
  1194. allowlist: &config.Allowlist{
  1195. MatchCondition: config.AllowlistMatchOr,
  1196. Commits: []string{"704178e7dca77ff143778a31cff0fc192d59b030"},
  1197. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1198. },
  1199. },
  1200. // Regex / stopwords
  1201. "regex allowed": {
  1202. fragment: Fragment{},
  1203. allowlist: &config.Allowlist{
  1204. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  1205. },
  1206. },
  1207. "stopwords allowed": {
  1208. fragment: Fragment{},
  1209. allowlist: &config.Allowlist{
  1210. StopWords: []string{"summer"},
  1211. },
  1212. },
  1213. "regex AND stopword allowed": {
  1214. fragment: Fragment{},
  1215. allowlist: &config.Allowlist{
  1216. MatchCondition: config.AllowlistMatchAnd,
  1217. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  1218. StopWords: []string{"2024"},
  1219. },
  1220. },
  1221. "regex AND stopword allowed - other conditions": {
  1222. fragment: Fragment{
  1223. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  1224. FilePath: "config.js",
  1225. },
  1226. allowlist: &config.Allowlist{
  1227. MatchCondition: config.AllowlistMatchAnd,
  1228. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1229. Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
  1230. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  1231. StopWords: []string{"2024"},
  1232. },
  1233. },
  1234. "regex AND stopword NOT allowed - non-git, other conditions": {
  1235. fragment: Fragment{
  1236. FilePath: "config.js",
  1237. },
  1238. allowlist: &config.Allowlist{
  1239. MatchCondition: config.AllowlistMatchAnd,
  1240. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1241. Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
  1242. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  1243. StopWords: []string{"2024"},
  1244. },
  1245. expected: []report.Finding{
  1246. {
  1247. StartColumn: 50,
  1248. EndColumn: 60,
  1249. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  1250. Match: "Summer2024!",
  1251. Secret: "Summer2024!",
  1252. File: "config.js",
  1253. Entropy: 3.095795154571533,
  1254. RuleID: "test-rule",
  1255. },
  1256. },
  1257. },
  1258. "regex AND stopword NOT allowed": {
  1259. fragment: Fragment{},
  1260. allowlist: &config.Allowlist{
  1261. MatchCondition: config.AllowlistMatchAnd,
  1262. Regexes: []*regexp.Regexp{
  1263. regexp.MustCompile(`(?i)winter.+`),
  1264. },
  1265. StopWords: []string{"2024"},
  1266. },
  1267. expected: []report.Finding{
  1268. {
  1269. StartColumn: 50,
  1270. EndColumn: 60,
  1271. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  1272. Match: "Summer2024!",
  1273. Secret: "Summer2024!",
  1274. Entropy: 3.095795154571533,
  1275. RuleID: "test-rule",
  1276. },
  1277. },
  1278. },
  1279. "regex AND stopword NOT allowed - other conditions": {
  1280. fragment: Fragment{
  1281. CommitSHA: "a060c9d2d5e90c992763f1bd4c3cd2a6f121241b",
  1282. FilePath: "config.js",
  1283. },
  1284. allowlist: &config.Allowlist{
  1285. MatchCondition: config.AllowlistMatchAnd,
  1286. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  1287. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  1288. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)winter.+`)},
  1289. StopWords: []string{"2024"},
  1290. },
  1291. expected: []report.Finding{
  1292. {
  1293. StartColumn: 50,
  1294. EndColumn: 60,
  1295. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  1296. Match: "Summer2024!",
  1297. Secret: "Summer2024!",
  1298. File: "config.js",
  1299. Entropy: 3.095795154571533,
  1300. RuleID: "test-rule",
  1301. },
  1302. },
  1303. },
  1304. "regex OR stopword allowed": {
  1305. fragment: Fragment{},
  1306. allowlist: &config.Allowlist{
  1307. MatchCondition: config.AllowlistMatchOr,
  1308. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  1309. StopWords: []string{"winter"},
  1310. },
  1311. },
  1312. }
  1313. raw := `let username = 'james@mail.com';
  1314. let password = 'Summer2024!';`
  1315. for name, tc := range cases {
  1316. t.Run(name, func(t *testing.T) {
  1317. rule := config.Rule{
  1318. RuleID: "test-rule",
  1319. Regex: regexp.MustCompile(`Summer2024!`),
  1320. Allowlists: []*config.Allowlist{
  1321. tc.allowlist,
  1322. },
  1323. }
  1324. d, err := NewDetectorDefaultConfig()
  1325. require.NoError(t, err)
  1326. f := tc.fragment
  1327. f.Raw = raw
  1328. actual := d.detectRule(f, raw, rule, []*codec.EncodedSegment{})
  1329. if diff := cmp.Diff(tc.expected, actual); diff != "" {
  1330. t.Errorf("diff: (-want +got)\n%s", diff)
  1331. }
  1332. })
  1333. }
  1334. }
  1335. func moveDotGit(t *testing.T, from, to string) {
  1336. t.Helper()
  1337. repoDirs, err := os.ReadDir("../testdata/repos")
  1338. require.NoError(t, err)
  1339. for _, dir := range repoDirs {
  1340. if to == ".git" {
  1341. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  1342. if os.IsNotExist(err) {
  1343. // dont want to delete the only copy of .git accidentally
  1344. continue
  1345. }
  1346. os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  1347. }
  1348. if !dir.IsDir() {
  1349. continue
  1350. }
  1351. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  1352. if os.IsNotExist(err) {
  1353. continue
  1354. }
  1355. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  1356. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  1357. require.NoError(t, err)
  1358. }
  1359. }
  1360. // region Windows-specific tests[]
  1361. func TestNormalizeGitleaksIgnorePaths(t *testing.T) {
  1362. d, err := NewDetectorDefaultConfig()
  1363. require.NoError(t, err)
  1364. err = d.AddGitleaksIgnore("../testdata/gitleaksignore/.windowspaths")
  1365. require.NoError(t, err)
  1366. assert.Len(t, d.gitleaksIgnore, 3)
  1367. expected := map[string]struct{}{
  1368. "foo/bar/gitleaks-false-positive.yaml:aws-access-token:4": {},
  1369. "foo/bar/gitleaks-false-positive.yaml:aws-access-token:5": {},
  1370. "b55d88dc151f7022901cda41a03d43e0e508f2b7:test_data/test_local_repo_three_leaks.json:aws-access-token:73": {},
  1371. }
  1372. assert.ElementsMatch(t, maps.Keys(d.gitleaksIgnore), maps.Keys(expected))
  1373. }
  1374. func TestWindowsFileSeparator_RulePath(t *testing.T) {
  1375. unixRule := config.Rule{
  1376. RuleID: "test-rule",
  1377. Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
  1378. }
  1379. windowsRule := config.Rule{
  1380. RuleID: "test-rule",
  1381. Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
  1382. }
  1383. expected := []report.Finding{
  1384. {
  1385. RuleID: "test-rule",
  1386. Match: "file detected: .m2/settings.xml",
  1387. File: ".m2/settings.xml",
  1388. },
  1389. }
  1390. tests := map[string]struct {
  1391. fragment Fragment
  1392. rule config.Rule
  1393. expected []report.Finding
  1394. }{
  1395. // unix rule
  1396. "unix rule - unix path separator": {
  1397. fragment: Fragment{
  1398. FilePath: `.m2/settings.xml`,
  1399. },
  1400. rule: unixRule,
  1401. expected: expected,
  1402. },
  1403. "unix rule - windows path separator": {
  1404. fragment: Fragment{
  1405. FilePath: `.m2/settings.xml`,
  1406. WindowsFilePath: `.m2\settings.xml`,
  1407. },
  1408. rule: unixRule,
  1409. expected: expected,
  1410. },
  1411. "unix regex+path rule - windows path separator": {
  1412. fragment: Fragment{
  1413. Raw: `<password>s3cr3t</password>`,
  1414. FilePath: `.m2/settings.xml`,
  1415. },
  1416. rule: config.Rule{
  1417. RuleID: "test-rule",
  1418. Regex: regexp.MustCompile(`<password>(.+?)</password>`),
  1419. Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
  1420. },
  1421. expected: []report.Finding{
  1422. {
  1423. RuleID: "test-rule",
  1424. StartColumn: 1,
  1425. EndColumn: 27,
  1426. Line: "<password>s3cr3t</password>",
  1427. Match: "<password>s3cr3t</password>",
  1428. Secret: "s3cr3t",
  1429. Entropy: 2.251629114151001,
  1430. File: ".m2/settings.xml",
  1431. },
  1432. },
  1433. },
  1434. // windows rule
  1435. "windows rule - unix path separator": {
  1436. fragment: Fragment{
  1437. FilePath: `.m2/settings.xml`,
  1438. },
  1439. rule: windowsRule,
  1440. // This never worked, and continues not to work.
  1441. // Paths should be normalized to use Unix file separators.
  1442. expected: nil,
  1443. },
  1444. "windows rule - windows path separator": {
  1445. fragment: Fragment{
  1446. FilePath: `.m2/settings.xml`,
  1447. WindowsFilePath: `.m2\settings.xml`,
  1448. },
  1449. rule: windowsRule,
  1450. expected: expected,
  1451. },
  1452. "windows regex+path rule - windows path separator": {
  1453. fragment: Fragment{
  1454. Raw: `<password>s3cr3t</password>`,
  1455. FilePath: `.m2/settings.xml`,
  1456. WindowsFilePath: `.m2\settings.xml`,
  1457. },
  1458. rule: config.Rule{
  1459. RuleID: "test-rule",
  1460. Regex: regexp.MustCompile(`<password>(.+?)</password>`),
  1461. Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
  1462. },
  1463. expected: []report.Finding{
  1464. {
  1465. RuleID: "test-rule",
  1466. StartColumn: 1,
  1467. EndColumn: 27,
  1468. Line: "<password>s3cr3t</password>",
  1469. Match: "<password>s3cr3t</password>",
  1470. Secret: "s3cr3t",
  1471. Entropy: 2.251629114151001,
  1472. File: ".m2/settings.xml",
  1473. },
  1474. }},
  1475. }
  1476. d, err := NewDetectorDefaultConfig()
  1477. require.NoError(t, err)
  1478. for name, test := range tests {
  1479. t.Run(name, func(t *testing.T) {
  1480. actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
  1481. if diff := cmp.Diff(test.expected, actual); diff != "" {
  1482. t.Errorf("diff: (-want +got)\n%s", diff)
  1483. }
  1484. })
  1485. }
  1486. }
  1487. func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
  1488. tests := map[string]struct {
  1489. fragment Fragment
  1490. rule config.Rule
  1491. expected []report.Finding
  1492. }{
  1493. // unix
  1494. "unix path separator - unix rule - OR allowlist path-only": {
  1495. fragment: Fragment{
  1496. Raw: `value: "s3cr3t"`,
  1497. FilePath: `ignoreme/unix.txt`,
  1498. },
  1499. rule: config.Rule{
  1500. RuleID: "unix-rule",
  1501. Regex: regexp.MustCompile(`s3cr3t`),
  1502. Allowlists: []*config.Allowlist{
  1503. {
  1504. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  1505. },
  1506. },
  1507. },
  1508. expected: nil,
  1509. },
  1510. "unix path separator - windows rule - OR allowlist path-only": {
  1511. fragment: Fragment{
  1512. Raw: `value: "s3cr3t"`,
  1513. FilePath: `ignoreme/unix.txt`,
  1514. },
  1515. rule: config.Rule{
  1516. RuleID: "windows-rule",
  1517. Regex: regexp.MustCompile(`s3cr3t`),
  1518. Allowlists: []*config.Allowlist{
  1519. {
  1520. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  1521. },
  1522. },
  1523. },
  1524. // Windows separators in regex don't work for unix.
  1525. expected: []report.Finding{
  1526. {
  1527. RuleID: "windows-rule",
  1528. StartColumn: 9,
  1529. EndColumn: 14,
  1530. Line: `value: "s3cr3t"`,
  1531. Match: `s3cr3t`,
  1532. Secret: `s3cr3t`,
  1533. File: "ignoreme/unix.txt",
  1534. Entropy: 2.251629114151001,
  1535. },
  1536. },
  1537. },
  1538. "unix path separator - unix rule - AND allowlist path+stopwords": {
  1539. fragment: Fragment{
  1540. Raw: `value: "f4k3s3cr3t"`,
  1541. FilePath: `ignoreme/unix.txt`,
  1542. },
  1543. rule: config.Rule{
  1544. RuleID: "unix-rule",
  1545. Regex: regexp.MustCompile(`value: "[^"]+"`),
  1546. Allowlists: []*config.Allowlist{
  1547. {
  1548. MatchCondition: config.AllowlistMatchAnd,
  1549. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  1550. StopWords: []string{"f4k3"},
  1551. },
  1552. },
  1553. },
  1554. expected: nil,
  1555. },
  1556. "unix path separator - windows rule - AND allowlist path+stopwords": {
  1557. fragment: Fragment{
  1558. Raw: `value: "f4k3s3cr3t"`,
  1559. FilePath: `ignoreme/unix.txt`,
  1560. },
  1561. rule: config.Rule{
  1562. RuleID: "windows-rule",
  1563. Regex: regexp.MustCompile(`value: "[^"]+"`),
  1564. Allowlists: []*config.Allowlist{
  1565. {
  1566. MatchCondition: config.AllowlistMatchAnd,
  1567. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  1568. StopWords: []string{"f4k3"},
  1569. },
  1570. },
  1571. },
  1572. expected: []report.Finding{
  1573. {
  1574. RuleID: "windows-rule",
  1575. StartColumn: 1,
  1576. EndColumn: 19,
  1577. Line: `value: "f4k3s3cr3t"`,
  1578. Match: `value: "f4k3s3cr3t"`,
  1579. Secret: `value: "f4k3s3cr3t"`,
  1580. File: "ignoreme/unix.txt",
  1581. Entropy: 3.892407178878784,
  1582. },
  1583. },
  1584. },
  1585. // windows
  1586. "windows path separator - unix rule - OR allowlist path-only": {
  1587. fragment: Fragment{
  1588. Raw: `value: "s3cr3t"`,
  1589. FilePath: `ignoreme/windows.txt`,
  1590. WindowsFilePath: `ignoreme\windows.txt`,
  1591. },
  1592. rule: config.Rule{
  1593. RuleID: "unix-rule",
  1594. Regex: regexp.MustCompile(`s3cr3t`),
  1595. Allowlists: []*config.Allowlist{
  1596. {
  1597. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  1598. },
  1599. },
  1600. },
  1601. expected: nil,
  1602. },
  1603. "windows path separator - windows rule - OR allowlist path-only": {
  1604. fragment: Fragment{
  1605. Raw: `value: "s3cr3t"`,
  1606. FilePath: `ignoreme/windows.txt`,
  1607. WindowsFilePath: `ignoreme\windows.txt`,
  1608. },
  1609. rule: config.Rule{
  1610. RuleID: "windows-rule",
  1611. Regex: regexp.MustCompile(`s3cr3t`),
  1612. Allowlists: []*config.Allowlist{
  1613. {
  1614. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  1615. },
  1616. },
  1617. },
  1618. expected: nil,
  1619. },
  1620. "windows path separator - unix rule - AND allowlist path+stopwords": {
  1621. fragment: Fragment{
  1622. Raw: `value: "f4k3s3cr3t"`,
  1623. FilePath: `ignoreme/unix.txt`,
  1624. WindowsFilePath: `ignoreme\windows.txt`,
  1625. },
  1626. rule: config.Rule{
  1627. RuleID: "unix-rule",
  1628. Regex: regexp.MustCompile(`value: "[^"]+"`),
  1629. Allowlists: []*config.Allowlist{
  1630. {
  1631. MatchCondition: config.AllowlistMatchAnd,
  1632. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  1633. StopWords: []string{"f4k3"},
  1634. },
  1635. },
  1636. },
  1637. expected: nil,
  1638. },
  1639. "windows path separator - windows rule - AND allowlist path+stopwords": {
  1640. fragment: Fragment{
  1641. Raw: `value: "f4k3s3cr3t"`,
  1642. FilePath: `ignoreme/unix.txt`,
  1643. WindowsFilePath: `ignoreme\windows.txt`,
  1644. },
  1645. rule: config.Rule{
  1646. RuleID: "windows-rule",
  1647. Regex: regexp.MustCompile(`value: "[^"]+"`),
  1648. Allowlists: []*config.Allowlist{
  1649. {
  1650. MatchCondition: config.AllowlistMatchAnd,
  1651. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  1652. StopWords: []string{"f4k3"},
  1653. },
  1654. },
  1655. },
  1656. expected: nil,
  1657. },
  1658. }
  1659. d, err := NewDetectorDefaultConfig()
  1660. require.NoError(t, err)
  1661. for name, test := range tests {
  1662. t.Run(name, func(t *testing.T) {
  1663. actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
  1664. if diff := cmp.Diff(test.expected, actual); diff != "" {
  1665. t.Errorf("diff: (-want +got)\n%s", diff)
  1666. }
  1667. })
  1668. }
  1669. }
  1670. //endregion