detect_test.go 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  1. package detect
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "github.com/google/go-cmp/cmp"
  11. "github.com/rs/zerolog"
  12. "github.com/spf13/viper"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "golang.org/x/exp/maps"
  16. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  17. "github.com/zricethezav/gitleaks/v8/config"
  18. "github.com/zricethezav/gitleaks/v8/detect/codec"
  19. "github.com/zricethezav/gitleaks/v8/logging"
  20. "github.com/zricethezav/gitleaks/v8/regexp"
  21. "github.com/zricethezav/gitleaks/v8/report"
  22. "github.com/zricethezav/gitleaks/v8/sources"
  23. )
  24. const maxDecodeDepth = 8
  25. const configPath = "../testdata/config/"
  26. const repoBasePath = "../testdata/repos/"
  27. const archivesBasePath = "../testdata/archives/"
  28. const encodedTestValues = `
  29. # Decoded
  30. -----BEGIN PRIVATE KEY-----
  31. 135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb
  32. u+QDkg0spw==
  33. -----END PRIVATE KEY-----
  34. # Encoded
  35. private_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'
  36. # Double Encoded: b64 encoded aws config inside a jwt
  37. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA
  38. # A small secret at the end to make sure that as the other ones above shrink
  39. # when decoded, the positions are taken into consideration for overlaps
  40. c21hbGwtc2VjcmV0
  41. # This tests how it handles when the match bounds go outside the decoded value
  42. secret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==
  43. # The above encoded again
  44. c2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=
  45. # Confirm you can ignore on the decoded value
  46. password="bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw=="
  47. # This tests that it can do hex encoded data
  48. secret=6465636F6465642D7365637265742D76616C756576484558
  49. # This tests that it can do percent encoded data
  50. ## partial encoded data
  51. secret=decoded-%73%65%63%72%65%74-valuev2
  52. ## scattered encoded
  53. secret=%64%65coded-%73%65%63%72%65%74-valuev3
  54. # Test multi levels of encoding where the source is a partal encoding
  55. # it is important that the bounds of the predecessors are properly
  56. # considered
  57. ## single percent encoding in the middle of multi layer b64
  58. c2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09
  59. ## single percent encoding at the beginning of hex
  60. secret%3d6465636F6465642D7365637265742D76616C75657635
  61. ## multiple percent encodings in a single layer base64
  62. secret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86
  63. ## base64 encoded partially percent encoded value
  64. secret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=
  65. ## one of the lines above that went through... a lot
  66. ## and there's surrounding text around it
  67. Look at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?
  68. ## Multi percent encode two random characters close to the bounds of the base64
  69. ## encoded data to make sure that the bounds are still correctly calculated
  70. 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=
  71. ## The similar to the above but also touching the edge of the base64
  72. secret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
  73. ## The similar to the above but also touching and overlapping the base64
  74. secret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34
  75. `
  76. func TestDetect(t *testing.T) {
  77. logging.Logger = logging.Logger.Level(zerolog.TraceLevel)
  78. tests := map[string]struct {
  79. cfgName string
  80. baselinePath string
  81. fragment Fragment
  82. // NOTE: for expected findings, all line numbers will be 0
  83. // because line deltas are added _after_ the finding is created.
  84. // I.e., if the finding is from a --no-git file, the line number will be
  85. // increase by 1 in DetectFromFiles(). If the finding is from git,
  86. // the line number will be increased by the patch delta.
  87. expectedFindings []report.Finding
  88. wantError error
  89. }{
  90. // General
  91. "valid allow comment (1)": {
  92. cfgName: "simple",
  93. fragment: Fragment{
  94. Raw: `awsToken := \"AKIALALEMEL33243OKIA\ // gitleaks:allow"`,
  95. FilePath: "tmp.go",
  96. },
  97. },
  98. "valid allow comment (2)": {
  99. cfgName: "simple",
  100. fragment: Fragment{
  101. Raw: `awsToken := \
  102. \"AKIALALEMEL33243OKIA\ // gitleaks:allow"
  103. `,
  104. FilePath: "tmp.go",
  105. },
  106. },
  107. "invalid allow comment": {
  108. cfgName: "simple",
  109. fragment: Fragment{
  110. Raw: `awsToken := \"AKIALALEMEL33243OKIA\"
  111. // gitleaks:allow"
  112. `,
  113. FilePath: "tmp.go",
  114. },
  115. expectedFindings: []report.Finding{
  116. {
  117. Description: "AWS Access Key",
  118. Secret: "AKIALALEMEL33243OKIA",
  119. Match: "AKIALALEMEL33243OKIA",
  120. File: "tmp.go",
  121. Line: `awsToken := \"AKIALALEMEL33243OKIA\"`,
  122. RuleID: "aws-access-key",
  123. Tags: []string{"key", "AWS"},
  124. StartLine: 0,
  125. EndLine: 0,
  126. StartColumn: 15,
  127. EndColumn: 34,
  128. Entropy: 3.1464393,
  129. },
  130. },
  131. },
  132. "detect finding - aws": {
  133. cfgName: "simple",
  134. fragment: Fragment{
  135. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  136. FilePath: "tmp.go",
  137. },
  138. expectedFindings: []report.Finding{
  139. {
  140. RuleID: "aws-access-key",
  141. Description: "AWS Access Key",
  142. File: "tmp.go",
  143. Line: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  144. Match: "AKIALALEMEL33243OLIA",
  145. Secret: "AKIALALEMEL33243OLIA",
  146. Entropy: 3.0841837,
  147. StartLine: 0,
  148. EndLine: 0,
  149. StartColumn: 15,
  150. EndColumn: 34,
  151. Tags: []string{"key", "AWS"},
  152. },
  153. },
  154. },
  155. "detect finding - sidekiq env var": {
  156. cfgName: "simple",
  157. fragment: Fragment{
  158. Raw: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  159. FilePath: "tmp.sh",
  160. },
  161. expectedFindings: []report.Finding{
  162. {
  163. RuleID: "sidekiq-secret",
  164. Description: "Sidekiq Secret",
  165. File: "tmp.sh",
  166. Line: `export BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;`,
  167. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=cafebabe:deadbeef;",
  168. Secret: "cafebabe:deadbeef",
  169. Entropy: 2.6098502,
  170. StartLine: 0,
  171. EndLine: 0,
  172. StartColumn: 8,
  173. EndColumn: 60,
  174. Tags: []string{},
  175. },
  176. },
  177. },
  178. "detect finding - sidekiq env var, semicolon": {
  179. cfgName: "simple",
  180. fragment: Fragment{
  181. Raw: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  182. FilePath: "tmp.sh",
  183. },
  184. expectedFindings: []report.Finding{
  185. {
  186. RuleID: "sidekiq-secret",
  187. Description: "Sidekiq Secret",
  188. File: "tmp.sh",
  189. Line: `echo hello1; export BUNDLE_ENTERPRISE__CONTRIBSYS__COM="cafebabe:deadbeef" && echo hello2`,
  190. Match: "BUNDLE_ENTERPRISE__CONTRIBSYS__COM=\"cafebabe:deadbeef\"",
  191. Secret: "cafebabe:deadbeef",
  192. Entropy: 2.6098502,
  193. StartLine: 0,
  194. EndLine: 0,
  195. StartColumn: 21,
  196. EndColumn: 74,
  197. Tags: []string{},
  198. },
  199. },
  200. },
  201. "detect finding - sidekiq url": {
  202. cfgName: "simple",
  203. fragment: Fragment{
  204. Raw: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  205. FilePath: "tmp.sh",
  206. },
  207. expectedFindings: []report.Finding{
  208. {
  209. RuleID: "sidekiq-sensitive-url",
  210. Description: "Sidekiq Sensitive URL",
  211. File: "tmp.sh",
  212. Line: `url = "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:80/path?param1=true&param2=false#heading1"`,
  213. Match: "http://cafeb4b3:d3adb33f@enterprise.contribsys.com:",
  214. Secret: "cafeb4b3:d3adb33f",
  215. Entropy: 2.984234,
  216. StartLine: 0,
  217. EndLine: 0,
  218. StartColumn: 8,
  219. EndColumn: 58,
  220. Tags: []string{},
  221. },
  222. },
  223. },
  224. "ignore finding - our config file": {
  225. cfgName: "simple",
  226. fragment: Fragment{
  227. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  228. FilePath: filepath.Join(configPath, "simple.toml"),
  229. },
  230. },
  231. "ignore finding - doesn't match path": {
  232. cfgName: "generic_with_py_path",
  233. fragment: Fragment{
  234. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  235. FilePath: "tmp.go",
  236. },
  237. },
  238. "detect finding - matches path,regex,entropy": {
  239. cfgName: "generic_with_py_path",
  240. fragment: Fragment{
  241. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  242. FilePath: "tmp.py",
  243. },
  244. expectedFindings: []report.Finding{
  245. {
  246. RuleID: "generic-api-key",
  247. Description: "Generic API Key",
  248. File: "tmp.py",
  249. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  250. Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  251. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  252. Entropy: 3.7906237,
  253. StartLine: 0,
  254. EndLine: 0,
  255. StartColumn: 22,
  256. EndColumn: 93,
  257. Tags: []string{},
  258. },
  259. },
  260. },
  261. "ignore finding - allowlist regex": {
  262. cfgName: "generic_with_py_path",
  263. fragment: Fragment{
  264. Raw: `const Discord_Public_Key = "load2523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  265. FilePath: "tmp.py",
  266. },
  267. },
  268. // Rule
  269. "rule - ignore path": {
  270. cfgName: "valid/rule_path_only",
  271. baselinePath: ".baseline.json",
  272. fragment: Fragment{
  273. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  274. FilePath: ".baseline.json",
  275. },
  276. },
  277. "rule - detect path ": {
  278. cfgName: "valid/rule_path_only",
  279. fragment: Fragment{
  280. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  281. FilePath: "tmp.py",
  282. },
  283. expectedFindings: []report.Finding{
  284. {
  285. Description: "Python Files",
  286. Match: "file detected: tmp.py",
  287. File: "tmp.py",
  288. RuleID: "python-files-only",
  289. Tags: []string{},
  290. },
  291. },
  292. },
  293. "rule - match based on entropy": {
  294. cfgName: "valid/rule_entropy_group",
  295. fragment: Fragment{
  296. Raw: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"
  297. //const Discord_Public_Key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  298. `,
  299. FilePath: "tmp.go",
  300. },
  301. expectedFindings: []report.Finding{
  302. {
  303. RuleID: "discord-api-key",
  304. Description: "Discord API key",
  305. File: "tmp.go",
  306. Line: `const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`,
  307. Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
  308. Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
  309. Entropy: 3.7906237,
  310. StartLine: 0,
  311. EndLine: 0,
  312. StartColumn: 7,
  313. EndColumn: 93,
  314. Tags: []string{},
  315. },
  316. },
  317. },
  318. // Allowlists
  319. "global allowlist - ignore regex": {
  320. cfgName: "valid/allowlist_global_regex",
  321. fragment: Fragment{
  322. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  323. FilePath: "tmp.go",
  324. },
  325. },
  326. "global allowlist - detect, doesn't match all conditions": {
  327. cfgName: "valid/allowlist_global_multiple",
  328. fragment: Fragment{
  329. Raw: `
  330. const token = "mockSecret";
  331. // const token = "changeit";`,
  332. FilePath: "config.txt",
  333. },
  334. expectedFindings: []report.Finding{
  335. {
  336. RuleID: "test",
  337. File: "config.txt",
  338. Line: "\nconst token = \"mockSecret\";",
  339. Match: `token = "mockSecret"`,
  340. Secret: "mockSecret",
  341. Entropy: 2.9219282,
  342. StartLine: 1,
  343. EndLine: 1,
  344. StartColumn: 8,
  345. EndColumn: 27,
  346. Tags: []string{},
  347. },
  348. },
  349. },
  350. "global allowlist - ignore, matches all conditions": {
  351. cfgName: "valid/allowlist_global_multiple",
  352. fragment: Fragment{
  353. Raw: `token := "mockSecret";`,
  354. FilePath: "node_modules/config.txt",
  355. },
  356. },
  357. "global allowlist - detect path, doesn't match all conditions": {
  358. cfgName: "valid/allowlist_global_multiple",
  359. fragment: Fragment{
  360. Raw: `var token = "fakeSecret";`,
  361. FilePath: "node_modules/config.txt",
  362. },
  363. expectedFindings: []report.Finding{
  364. {
  365. RuleID: "test",
  366. File: "node_modules/config.txt",
  367. Line: "var token = \"fakeSecret\";",
  368. Match: `token = "fakeSecret"`,
  369. Secret: "fakeSecret",
  370. Entropy: 2.8464394,
  371. StartLine: 0,
  372. EndLine: 0,
  373. StartColumn: 5,
  374. EndColumn: 24,
  375. Tags: []string{},
  376. },
  377. },
  378. },
  379. "allowlist - ignore commit": {
  380. cfgName: "valid/allowlist_rule_commit",
  381. fragment: Fragment{
  382. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  383. FilePath: "tmp.go",
  384. CommitSHA: "allowthiscommit",
  385. },
  386. },
  387. "allowlist - ignore path": {
  388. cfgName: "valid/allowlist_rule_path",
  389. fragment: Fragment{
  390. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  391. FilePath: "tmp.go",
  392. },
  393. },
  394. "allowlist - ignore path when extending": {
  395. cfgName: "valid/allowlist_rule_extend_default",
  396. fragment: Fragment{
  397. Raw: `token = "aebfab88-7596-481d-82e8-c60c8f7de0c0"`,
  398. FilePath: "path/to/your/problematic/file.js",
  399. },
  400. },
  401. "allowlist - ignore regex": {
  402. cfgName: "valid/allowlist_rule_regex",
  403. fragment: Fragment{
  404. Raw: `awsToken := \"AKIALALEMEL33243OLIA\"`,
  405. FilePath: "tmp.go",
  406. },
  407. },
  408. // Decoding
  409. "detect encoded": {
  410. cfgName: "encoded",
  411. fragment: Fragment{
  412. Raw: encodedTestValues,
  413. FilePath: "tmp.go",
  414. },
  415. expectedFindings: []report.Finding{
  416. { // Plain text key captured by normal rule
  417. Description: "Private Key",
  418. Secret: "-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  419. Match: "-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  420. File: "tmp.go",
  421. Line: "\n-----BEGIN PRIVATE KEY-----\n135f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  422. RuleID: "private-key",
  423. Tags: []string{"key", "private"},
  424. StartLine: 2,
  425. EndLine: 5,
  426. StartColumn: 2,
  427. EndColumn: 26,
  428. Entropy: 5.350665,
  429. },
  430. { // Encoded key captured by custom b64 regex rule
  431. Description: "Private Key",
  432. Secret: "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K",
  433. Match: "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K",
  434. File: "tmp.go",
  435. Line: "\nprivate_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'",
  436. RuleID: "b64-encoded-private-key",
  437. Tags: []string{"key", "private"},
  438. StartLine: 8,
  439. EndLine: 8,
  440. StartColumn: 16,
  441. EndColumn: 207,
  442. Entropy: 5.3861146,
  443. },
  444. { // Encoded key captured by plain text rule using the decoder
  445. Description: "Private Key",
  446. Secret: "-----BEGIN PRIVATE KEY-----\n435f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  447. Match: "-----BEGIN PRIVATE KEY-----\n435f/bRUBHrbHqLY/xS3I7Oth+8rgG+0tBwfMcbk05Sgxq6QUzSYIQAop+WvsTwk2sR+C38g0Mnb\nu+QDkg0spw==\n-----END PRIVATE KEY-----",
  448. File: "tmp.go",
  449. Line: "\nprivate_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCjQzNWYvYlJVQkhyYkhxTFkveFMzSTdPdGgrOHJnRyswdEJ3Zk1jYmswNVNneHE2UVV6U1lJUUFvcCtXdnNUd2syc1IrQzM4ZzBNbmIKdStRRGtnMHNwdz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K'",
  450. RuleID: "private-key",
  451. Tags: []string{"key", "private", "decoded:base64", "decode-depth:1"},
  452. StartLine: 8,
  453. EndLine: 8,
  454. StartColumn: 16,
  455. EndColumn: 207,
  456. Entropy: 5.350665,
  457. },
  458. { // Encoded Small secret at the end to make sure it's picked up by the decoding
  459. Description: "Small Secret",
  460. Secret: "small-secret",
  461. Match: "small-secret",
  462. File: "tmp.go",
  463. Line: "\nc21hbGwtc2VjcmV0",
  464. RuleID: "small-secret",
  465. Tags: []string{"small", "secret", "decoded:base64", "decode-depth:1"},
  466. StartLine: 15,
  467. EndLine: 15,
  468. StartColumn: 2,
  469. EndColumn: 17,
  470. Entropy: 3.0849626,
  471. },
  472. { // Secret where the decoded match goes outside the encoded value
  473. Description: "Overlapping",
  474. Secret: "decoded-secret-value00",
  475. Match: "secret=decoded-secret-value00",
  476. File: "tmp.go",
  477. Line: "\nsecret=ZGVjb2RlZC1zZWNyZXQtdmFsdWUwMA==",
  478. RuleID: "overlapping",
  479. Tags: []string{"overlapping", "decoded:base64", "decode-depth:1"},
  480. StartLine: 18,
  481. EndLine: 18,
  482. StartColumn: 2,
  483. EndColumn: 40,
  484. Entropy: 3.4428623,
  485. },
  486. { // This just confirms that with no allowlist the pattern is detected (i.e. the regex is good)
  487. Description: "Make sure this would be detected with no allowlist",
  488. Secret: "lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw",
  489. Match: "password=\"lRqBK-z5kf4-please-ignore-me-X-XIJM2Pddw\"",
  490. File: "tmp.go",
  491. Line: "\npassword=\"bFJxQkstejVrZjQtcGxlYXNlLWlnbm9yZS1tZS1YLVhJSk0yUGRkdw==\"",
  492. RuleID: "decoded-password-dont-ignore",
  493. Tags: []string{"decode-ignore", "decoded:base64", "decode-depth:1"},
  494. StartLine: 23,
  495. EndLine: 23,
  496. StartColumn: 2,
  497. EndColumn: 68,
  498. Entropy: 4.5841837,
  499. },
  500. { // Hex encoded data check
  501. Description: "Overlapping",
  502. Secret: "decoded-secret-valuevHEX",
  503. Match: "secret=decoded-secret-valuevHEX",
  504. File: "tmp.go",
  505. Line: "\nsecret=6465636F6465642D7365637265742D76616C756576484558",
  506. RuleID: "overlapping",
  507. Tags: []string{"overlapping", "decoded:hex", "decode-depth:1"},
  508. StartLine: 26,
  509. EndLine: 26,
  510. StartColumn: 2,
  511. EndColumn: 56,
  512. Entropy: 3.6531072,
  513. },
  514. { // handle partial encoded percent data
  515. Description: "Overlapping",
  516. Secret: "decoded-secret-valuev2",
  517. Match: "secret=decoded-secret-valuev2",
  518. File: "tmp.go",
  519. Line: "\nsecret=decoded-%73%65%63%72%65%74-valuev2",
  520. RuleID: "overlapping",
  521. Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
  522. StartLine: 30,
  523. EndLine: 30,
  524. StartColumn: 2,
  525. EndColumn: 42,
  526. Entropy: 3.4428623,
  527. },
  528. { // handle partial encoded percent data
  529. Description: "Overlapping",
  530. Secret: "decoded-secret-valuev3",
  531. Match: "secret=decoded-secret-valuev3",
  532. File: "tmp.go",
  533. Line: "\nsecret=%64%65coded-%73%65%63%72%65%74-valuev3",
  534. RuleID: "overlapping",
  535. Tags: []string{"overlapping", "decoded:percent", "decode-depth:1"},
  536. StartLine: 32,
  537. EndLine: 32,
  538. StartColumn: 2,
  539. EndColumn: 46,
  540. Entropy: 3.4428623,
  541. },
  542. { // Encoded AWS config with a access key id inside a JWT
  543. Description: "AWS IAM Unique Identifier",
  544. Secret: "ASIAIOSFODNN7LXM10JI",
  545. Match: " ASIAIOSFODNN7LXM10JI",
  546. File: "tmp.go",
  547. Line: "\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA",
  548. RuleID: "aws-iam-unique-identifier",
  549. Tags: []string{"aws", "identifier", "decoded:base64", "decode-depth:2"},
  550. StartLine: 11,
  551. EndLine: 11,
  552. StartColumn: 39,
  553. EndColumn: 344,
  554. Entropy: 3.6841838,
  555. },
  556. { // Encoded AWS config with a secret access key inside a JWT
  557. Description: "AWS Secret Access Key",
  558. Secret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEFUCDlEX1A",
  559. Match: "aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEFUCDlEX1A",
  560. File: "tmp.go",
  561. Line: "\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29uZmlnIjoiVzJSbFptRjFiSFJkQ25KbFoybHZiaUE5SUhWekxXVmhjM1F0TWdwaGQzTmZZV05qWlhOelgydGxlVjlwWkNBOUlFRlRTVUZKVDFOR1QwUk9UamRNV0UweE1FcEpDbUYzYzE5elpXTnlaWFJmWVdOalpYTnpYMnRsZVNBOUlIZEtZV3h5V0ZWMGJrWkZUVWt2U3pkTlJFVk9SeTlpVUhoU1ptbERXVVZHVlVORWJFVllNVUVLIiwiaWF0IjoxNTE2MjM5MDIyfQ.8gxviXEOuIBQk2LvTYHSf-wXVhnEKC3h4yM5nlOF4zA",
  562. RuleID: "aws-secret-access-key",
  563. Tags: []string{"aws", "secret", "decoded:base64", "decode-depth:2"},
  564. StartLine: 11,
  565. EndLine: 11,
  566. StartColumn: 39,
  567. EndColumn: 344,
  568. Entropy: 4.721928,
  569. },
  570. { // Secret where the decoded match goes outside the encoded value and then encoded again
  571. Description: "Overlapping",
  572. Secret: "decoded-secret-value",
  573. Match: "secret=decoded-secret-value",
  574. File: "tmp.go",
  575. Line: "\nc2VjcmV0PVpHVmpiMlJsWkMxelpXTnlaWFF0ZG1Gc2RXVT0=",
  576. RuleID: "overlapping",
  577. Tags: []string{"overlapping", "decoded:base64", "decode-depth:2"},
  578. StartLine: 20,
  579. EndLine: 20,
  580. StartColumn: 2,
  581. EndColumn: 49,
  582. Entropy: 3.3037016,
  583. },
  584. { // handle encodings that touch eachother
  585. Description: "Overlapping",
  586. Secret: "decoded-secret-valuev5",
  587. Match: "secret=decoded-secret-valuev5",
  588. File: "tmp.go",
  589. Line: "\nsecret%3d6465636F6465642D7365637265742D76616C75657635",
  590. RuleID: "overlapping",
  591. Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decode-depth:2"},
  592. StartLine: 40,
  593. EndLine: 40,
  594. StartColumn: 2,
  595. EndColumn: 54,
  596. Entropy: 3.4428623,
  597. },
  598. { // handle partial encoded percent data465642D7365637265742D76616C75657635
  599. Description: "Overlapping",
  600. Secret: "decoded-secret-valuev4",
  601. Match: "secret=decoded-secret-valuev4",
  602. File: "tmp.go",
  603. Line: "\nc2VjcmV0PVpHVmpiMl%4AsWkMxelpXTnlaWFF0ZG1Gc2RXVjJOQT09",
  604. RuleID: "overlapping",
  605. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:3"},
  606. StartLine: 38,
  607. EndLine: 38,
  608. StartColumn: 2,
  609. EndColumn: 55,
  610. Entropy: 3.4428623,
  611. },
  612. { // multiple percent encodings in a single layer base64
  613. Description: "Overlapping",
  614. Secret: "decoded-secret-valuex86",
  615. Match: "secret=decoded-secret-valuex86",
  616. File: "tmp.go",
  617. Line: "\nsecret=ZGVjb2%52lZC1zZWNyZXQtdm%46sdWV4ODY= # ends in x86",
  618. RuleID: "overlapping",
  619. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
  620. StartLine: 42,
  621. EndLine: 42,
  622. StartColumn: 2,
  623. EndColumn: 44,
  624. Entropy: 3.6381476,
  625. },
  626. { // base64 encoded partially percent encoded value
  627. Description: "Overlapping",
  628. Secret: "decoded-secret-value",
  629. Match: "secret=decoded-secret-value",
  630. File: "tmp.go",
  631. Line: "\nsecret=ZGVjb2RlZC0lNzMlNjUlNjMlNzIlNjUlNzQtdmFsdWU=",
  632. RuleID: "overlapping",
  633. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:2"},
  634. StartLine: 44,
  635. EndLine: 44,
  636. StartColumn: 2,
  637. EndColumn: 52,
  638. Entropy: 3.3037016,
  639. },
  640. { // one of the lines above that went through... a lot
  641. Description: "Overlapping",
  642. Secret: "decoded-secret-value",
  643. Match: "secret=decoded-secret-value",
  644. File: "tmp.go",
  645. Line: "\nLook at this value: %4EjMzMjU2NkE2MzZENTYzMDUwNTY3MDQ4%4eTY2RDcwNjk0RDY5NTUzMTRENkQ3ODYx%25%34%65TE3QTQ2MzY1NzZDNjQ0RjY1NTY3MDU5NTU1ODUyNkI2MjUzNTUzMDRFNkU0RTZCNTYzMTU1MzkwQQ== # isn't it crazy?",
  646. RuleID: "overlapping",
  647. Tags: []string{"overlapping", "decoded:percent", "decoded:hex", "decoded:base64", "decode-depth:7"},
  648. StartLine: 47,
  649. EndLine: 47,
  650. StartColumn: 22,
  651. EndColumn: 177,
  652. Entropy: 3.3037016,
  653. },
  654. { // Multi percent encode two random characters close to the bounds of the base64
  655. Description: "Overlapping",
  656. Secret: "decoded-secret-value",
  657. Match: "secret=decoded-secret-value",
  658. File: "tmp.go",
  659. 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=",
  660. RuleID: "overlapping",
  661. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:5"},
  662. StartLine: 50,
  663. EndLine: 50,
  664. StartColumn: 2,
  665. EndColumn: 300,
  666. Entropy: 3.3037016,
  667. },
  668. { // The similar to the above but also touching the edge of the base64
  669. Description: "Overlapping",
  670. Secret: "decoded-secret-value",
  671. Match: "secret=decoded-secret-value",
  672. File: "tmp.go",
  673. Line: "\nsecret=%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
  674. RuleID: "overlapping",
  675. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
  676. StartLine: 52,
  677. EndLine: 52,
  678. StartColumn: 2,
  679. EndColumn: 86,
  680. Entropy: 3.3037016,
  681. },
  682. { // The similar to the above but also touching and overlapping the base64
  683. Description: "Overlapping",
  684. Secret: "decoded-secret-value",
  685. Match: "secret=decoded-secret-value",
  686. File: "tmp.go",
  687. Line: "\nsecret%3D%25%35%61%25%34%37%25%35%36jb2RlZC1zZWNyZXQtdmFsdWU%25%32%35%25%33%33%25%36%34",
  688. RuleID: "overlapping",
  689. Tags: []string{"overlapping", "decoded:percent", "decoded:base64", "decode-depth:4"},
  690. StartLine: 54,
  691. EndLine: 54,
  692. StartColumn: 2,
  693. EndColumn: 88,
  694. Entropy: 3.3037016,
  695. },
  696. },
  697. },
  698. }
  699. for name, tt := range tests {
  700. t.Run(name, func(t *testing.T) {
  701. viper.Reset()
  702. viper.AddConfigPath(configPath)
  703. viper.SetConfigName(tt.cfgName)
  704. viper.SetConfigType("toml")
  705. err := viper.ReadInConfig()
  706. require.NoError(t, err)
  707. var vc config.ViperConfig
  708. err = viper.Unmarshal(&vc)
  709. require.NoError(t, err)
  710. cfg, err := vc.Translate()
  711. cfg.Path = filepath.Join(configPath, tt.cfgName+".toml")
  712. assert.Equal(t, tt.wantError, err)
  713. d := NewDetector(cfg)
  714. d.MaxDecodeDepth = maxDecodeDepth
  715. d.baselinePath = tt.baselinePath
  716. findings := d.Detect(tt.fragment)
  717. assert.ElementsMatch(t, tt.expectedFindings, findings)
  718. })
  719. }
  720. }
  721. // TestFromGit tests the FromGit function
  722. func TestFromGit(t *testing.T) {
  723. // TODO: Fix this test on windows.
  724. if runtime.GOOS == "windows" {
  725. t.Skipf("TODO: this fails on Windows: [git] fatal: bad object refs/remotes/origin/main?")
  726. return
  727. }
  728. tests := []struct {
  729. cfgName string
  730. source string
  731. logOpts string
  732. expectedFindings []report.Finding
  733. }{
  734. {
  735. source: filepath.Join(repoBasePath, "small"),
  736. cfgName: "simple", // the remote url is `git@github.com:gitleaks/test.git`
  737. expectedFindings: []report.Finding{
  738. {
  739. RuleID: "aws-access-key",
  740. Description: "AWS Access Key",
  741. StartLine: 20,
  742. EndLine: 20,
  743. StartColumn: 19,
  744. EndColumn: 38,
  745. Line: "\n awsToken := \"AKIALALEMEL33243OLIA\"",
  746. Secret: "AKIALALEMEL33243OLIA",
  747. Match: "AKIALALEMEL33243OLIA",
  748. Entropy: 3.0841837,
  749. File: "main.go",
  750. Date: "2021-11-02T23:37:53Z",
  751. Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
  752. Author: "Zachary Rice",
  753. Email: "zricer@protonmail.com",
  754. Message: "Accidentally add a secret",
  755. Tags: []string{"key", "AWS"},
  756. Fingerprint: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587:main.go:aws-access-key:20",
  757. Link: "https://github.com/gitleaks/test/blob/1b6da43b82b22e4eaa10bcf8ee591e91abbfc587/main.go#L20",
  758. },
  759. {
  760. RuleID: "aws-access-key",
  761. Description: "AWS Access Key",
  762. StartLine: 9,
  763. EndLine: 9,
  764. StartColumn: 17,
  765. EndColumn: 36,
  766. Secret: "AKIALALEMEL33243OLIA",
  767. Match: "AKIALALEMEL33243OLIA",
  768. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  769. File: "foo/foo.go",
  770. Date: "2021-11-02T23:48:06Z",
  771. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  772. Author: "Zach Rice",
  773. Email: "zricer@protonmail.com",
  774. Message: "adding foo package with secret",
  775. Tags: []string{"key", "AWS"},
  776. Entropy: 3.0841837,
  777. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  778. Link: "https://github.com/gitleaks/test/blob/491504d5a31946ce75e22554cc34203d8e5ff3ca/foo/foo.go#L9",
  779. },
  780. },
  781. },
  782. {
  783. source: filepath.Join(repoBasePath, "small"),
  784. logOpts: "--all foo...",
  785. cfgName: "simple",
  786. expectedFindings: []report.Finding{
  787. {
  788. RuleID: "aws-access-key",
  789. Description: "AWS Access Key",
  790. StartLine: 9,
  791. EndLine: 9,
  792. StartColumn: 17,
  793. EndColumn: 36,
  794. Secret: "AKIALALEMEL33243OLIA",
  795. Line: "\n\taws_token := \"AKIALALEMEL33243OLIA\"",
  796. Match: "AKIALALEMEL33243OLIA",
  797. Date: "2021-11-02T23:48:06Z",
  798. File: "foo/foo.go",
  799. Commit: "491504d5a31946ce75e22554cc34203d8e5ff3ca",
  800. Author: "Zach Rice",
  801. Email: "zricer@protonmail.com",
  802. Message: "adding foo package with secret",
  803. Tags: []string{"key", "AWS"},
  804. Entropy: 3.0841837,
  805. Fingerprint: "491504d5a31946ce75e22554cc34203d8e5ff3ca:foo/foo.go:aws-access-key:9",
  806. Link: "https://github.com/gitleaks/test/blob/491504d5a31946ce75e22554cc34203d8e5ff3ca/foo/foo.go#L9",
  807. },
  808. },
  809. },
  810. {
  811. source: filepath.Join(repoBasePath, "archives"),
  812. cfgName: "archives",
  813. expectedFindings: []report.Finding{
  814. {
  815. RuleID: "aws-access-key",
  816. Description: "AWS Access Key",
  817. StartLine: 20,
  818. EndLine: 20,
  819. StartColumn: 16,
  820. EndColumn: 35,
  821. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  822. Match: "AKIALALEMEL33243OLIA",
  823. Secret: "AKIALALEMEL33243OLIA",
  824. File: "main.go.zst",
  825. Commit: "db8789716fc664dbce0ed2d492570e92abf717a5",
  826. Author: "Test User",
  827. Email: "user@example.com",
  828. Date: "2025-05-27T05:10:39Z",
  829. Message: "Add main.go.zst",
  830. Tags: []string{"key", "AWS"},
  831. Entropy: 3.0841837,
  832. Fingerprint: "db8789716fc664dbce0ed2d492570e92abf717a5:main.go.zst:aws-access-key:20",
  833. Link: "https://github.com/gitleaks/test/blob/db8789716fc664dbce0ed2d492570e92abf717a5/main.go.zst#L20",
  834. },
  835. {
  836. RuleID: "aws-access-key",
  837. Description: "AWS Access Key",
  838. StartLine: 20,
  839. EndLine: 20,
  840. StartColumn: 16,
  841. EndColumn: 35,
  842. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  843. Match: "AKIALALEMEL33243OLIA",
  844. Secret: "AKIALALEMEL33243OLIA",
  845. File: "nested.tar.gz!archives/files.tar!files/api.go",
  846. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  847. Author: "Test User",
  848. Email: "user@example.com",
  849. Date: "2025-05-27T05:08:50Z",
  850. Message: "Add nested.tar.gz",
  851. Tags: []string{"key", "AWS"},
  852. Entropy: 3.0841837,
  853. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar!files/api.go:aws-access-key:20",
  854. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  855. },
  856. {
  857. RuleID: "aws-access-key",
  858. Description: "AWS Access Key",
  859. StartLine: 20,
  860. EndLine: 20,
  861. StartColumn: 16,
  862. EndColumn: 35,
  863. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  864. Match: "AKIALALEMEL33243OLIA",
  865. Secret: "AKIALALEMEL33243OLIA",
  866. File: "nested.tar.gz!archives/files.tar!files/main.go",
  867. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  868. Author: "Test User",
  869. Email: "user@example.com",
  870. Date: "2025-05-27T05:08:50Z",
  871. Message: "Add nested.tar.gz",
  872. Tags: []string{"key", "AWS"},
  873. Entropy: 3.0841837,
  874. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar!files/main.go:aws-access-key:20",
  875. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  876. },
  877. {
  878. RuleID: "aws-access-key",
  879. Description: "AWS Access Key",
  880. StartLine: 20,
  881. EndLine: 20,
  882. StartColumn: 16,
  883. EndColumn: 35,
  884. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  885. Match: "AKIALALEMEL33243OLIA",
  886. Secret: "AKIALALEMEL33243OLIA",
  887. File: "nested.tar.gz!archives/files.zip!files/api.go",
  888. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  889. Author: "Test User",
  890. Email: "user@example.com",
  891. Date: "2025-05-27T05:08:50Z",
  892. Message: "Add nested.tar.gz",
  893. Tags: []string{"key", "AWS"},
  894. Entropy: 3.0841837,
  895. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.zip!files/api.go:aws-access-key:20",
  896. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  897. },
  898. {
  899. RuleID: "aws-access-key",
  900. Description: "AWS Access Key",
  901. StartLine: 20,
  902. EndLine: 20,
  903. StartColumn: 16,
  904. EndColumn: 35,
  905. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  906. Match: "AKIALALEMEL33243OLIA",
  907. Secret: "AKIALALEMEL33243OLIA",
  908. File: "nested.tar.gz!archives/files.zip!files/main.go",
  909. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  910. Author: "Test User",
  911. Email: "user@example.com",
  912. Date: "2025-05-27T05:08:50Z",
  913. Message: "Add nested.tar.gz",
  914. Tags: []string{"key", "AWS"},
  915. Entropy: 3.0841837,
  916. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.zip!files/main.go:aws-access-key:20",
  917. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  918. },
  919. {
  920. RuleID: "aws-access-key",
  921. Description: "AWS Access Key",
  922. StartLine: 20,
  923. EndLine: 20,
  924. StartColumn: 16,
  925. EndColumn: 35,
  926. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  927. Match: "AKIALALEMEL33243OLIA",
  928. Secret: "AKIALALEMEL33243OLIA",
  929. File: "nested.tar.gz!archives/files.7z!files/api.go",
  930. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  931. Author: "Test User",
  932. Email: "user@example.com",
  933. Date: "2025-05-27T05:08:50Z",
  934. Message: "Add nested.tar.gz",
  935. Tags: []string{"key", "AWS"},
  936. Entropy: 3.0841837,
  937. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.7z!files/api.go:aws-access-key:20",
  938. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  939. },
  940. {
  941. RuleID: "aws-access-key",
  942. Description: "AWS Access Key",
  943. StartLine: 20,
  944. EndLine: 20,
  945. StartColumn: 16,
  946. EndColumn: 35,
  947. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  948. Match: "AKIALALEMEL33243OLIA",
  949. Secret: "AKIALALEMEL33243OLIA",
  950. File: "nested.tar.gz!archives/files.7z!files/main.go",
  951. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  952. Author: "Test User",
  953. Email: "user@example.com",
  954. Date: "2025-05-27T05:08:50Z",
  955. Message: "Add nested.tar.gz",
  956. Tags: []string{"key", "AWS"},
  957. Entropy: 3.0841837,
  958. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.7z!files/main.go:aws-access-key:20",
  959. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  960. },
  961. {
  962. RuleID: "aws-access-key",
  963. Description: "AWS Access Key",
  964. StartLine: 20,
  965. EndLine: 20,
  966. StartColumn: 16,
  967. EndColumn: 35,
  968. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  969. Match: "AKIALALEMEL33243OLIA",
  970. Secret: "AKIALALEMEL33243OLIA",
  971. File: "nested.tar.gz!archives/files.tar.zst!files/api.go",
  972. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  973. Author: "Test User",
  974. Email: "user@example.com",
  975. Date: "2025-05-27T05:08:50Z",
  976. Message: "Add nested.tar.gz",
  977. Tags: []string{"key", "AWS"},
  978. Entropy: 3.0841837,
  979. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar.zst!files/api.go:aws-access-key:20",
  980. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  981. },
  982. {
  983. RuleID: "aws-access-key",
  984. Description: "AWS Access Key",
  985. StartLine: 20,
  986. EndLine: 20,
  987. StartColumn: 16,
  988. EndColumn: 35,
  989. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  990. Match: "AKIALALEMEL33243OLIA",
  991. Secret: "AKIALALEMEL33243OLIA",
  992. File: "nested.tar.gz!archives/files.tar.zst!files/main.go",
  993. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  994. Author: "Test User",
  995. Email: "user@example.com",
  996. Date: "2025-05-27T05:08:50Z",
  997. Message: "Add nested.tar.gz",
  998. Tags: []string{"key", "AWS"},
  999. Entropy: 3.0841837,
  1000. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar.zst!files/main.go:aws-access-key:20",
  1001. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1002. },
  1003. {
  1004. RuleID: "aws-access-key",
  1005. Description: "AWS Access Key",
  1006. StartLine: 20,
  1007. EndLine: 20,
  1008. StartColumn: 16,
  1009. EndColumn: 35,
  1010. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1011. Match: "AKIALALEMEL33243OLIA",
  1012. Secret: "AKIALALEMEL33243OLIA",
  1013. File: "nested.tar.gz!archives/files/api.go",
  1014. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1015. Author: "Test User",
  1016. Email: "user@example.com",
  1017. Date: "2025-05-27T05:08:50Z",
  1018. Message: "Add nested.tar.gz",
  1019. Tags: []string{"key", "AWS"},
  1020. Entropy: 3.0841837,
  1021. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files/api.go:aws-access-key:20",
  1022. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1023. },
  1024. {
  1025. RuleID: "aws-access-key",
  1026. Description: "AWS Access Key",
  1027. StartLine: 20,
  1028. EndLine: 20,
  1029. StartColumn: 16,
  1030. EndColumn: 35,
  1031. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1032. Match: "AKIALALEMEL33243OLIA",
  1033. Secret: "AKIALALEMEL33243OLIA",
  1034. File: "nested.tar.gz!archives/files/main.go",
  1035. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1036. Author: "Test User",
  1037. Email: "user@example.com",
  1038. Date: "2025-05-27T05:08:50Z",
  1039. Message: "Add nested.tar.gz",
  1040. Tags: []string{"key", "AWS"},
  1041. Entropy: 3.0841837,
  1042. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files/main.go:aws-access-key:20",
  1043. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1044. },
  1045. {
  1046. RuleID: "aws-access-key",
  1047. Description: "AWS Access Key",
  1048. StartLine: 20,
  1049. EndLine: 20,
  1050. StartColumn: 16,
  1051. EndColumn: 35,
  1052. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1053. Match: "AKIALALEMEL33243OLIA",
  1054. Secret: "AKIALALEMEL33243OLIA",
  1055. File: "nested.tar.gz!archives/files/main.go.xz",
  1056. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1057. Author: "Test User",
  1058. Email: "user@example.com",
  1059. Date: "2025-05-27T05:08:50Z",
  1060. Message: "Add nested.tar.gz",
  1061. Tags: []string{"key", "AWS"},
  1062. Entropy: 3.0841837,
  1063. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files/main.go.xz:aws-access-key:20",
  1064. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1065. },
  1066. {
  1067. RuleID: "aws-access-key",
  1068. Description: "AWS Access Key",
  1069. StartLine: 20,
  1070. EndLine: 20,
  1071. StartColumn: 16,
  1072. EndColumn: 35,
  1073. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1074. Match: "AKIALALEMEL33243OLIA",
  1075. Secret: "AKIALALEMEL33243OLIA",
  1076. File: "nested.tar.gz!archives/files/main.go.zst",
  1077. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1078. Author: "Test User",
  1079. Email: "user@example.com",
  1080. Date: "2025-05-27T05:08:50Z",
  1081. Message: "Add nested.tar.gz",
  1082. Tags: []string{"key", "AWS"},
  1083. Entropy: 3.0841837,
  1084. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files/main.go.zst:aws-access-key:20",
  1085. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1086. },
  1087. {
  1088. RuleID: "aws-access-key",
  1089. Description: "AWS Access Key",
  1090. StartLine: 20,
  1091. EndLine: 20,
  1092. StartColumn: 16,
  1093. EndColumn: 35,
  1094. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1095. Match: "AKIALALEMEL33243OLIA",
  1096. Secret: "AKIALALEMEL33243OLIA",
  1097. File: "nested.tar.gz!archives/files/main.go.gz",
  1098. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1099. Author: "Test User",
  1100. Email: "user@example.com",
  1101. Date: "2025-05-27T05:08:50Z",
  1102. Message: "Add nested.tar.gz",
  1103. Tags: []string{"key", "AWS"},
  1104. Entropy: 3.0841837,
  1105. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files/main.go.gz:aws-access-key:20",
  1106. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1107. },
  1108. {
  1109. RuleID: "aws-access-key",
  1110. Description: "AWS Access Key",
  1111. StartLine: 20,
  1112. EndLine: 20,
  1113. StartColumn: 16,
  1114. EndColumn: 35,
  1115. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1116. Match: "AKIALALEMEL33243OLIA",
  1117. Secret: "AKIALALEMEL33243OLIA",
  1118. File: "nested.tar.gz!archives/files.tar.xz!files/api.go",
  1119. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1120. Author: "Test User",
  1121. Email: "user@example.com",
  1122. Date: "2025-05-27T05:08:50Z",
  1123. Message: "Add nested.tar.gz",
  1124. Tags: []string{"key", "AWS"},
  1125. Entropy: 3.0841837,
  1126. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar.xz!files/api.go:aws-access-key:20",
  1127. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1128. },
  1129. {
  1130. RuleID: "aws-access-key",
  1131. Description: "AWS Access Key",
  1132. StartLine: 20,
  1133. EndLine: 20,
  1134. StartColumn: 16,
  1135. EndColumn: 35,
  1136. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1137. Match: "AKIALALEMEL33243OLIA",
  1138. Secret: "AKIALALEMEL33243OLIA",
  1139. File: "nested.tar.gz!archives/files.tar.xz!files/main.go",
  1140. Commit: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68",
  1141. Author: "Test User",
  1142. Email: "user@example.com",
  1143. Date: "2025-05-27T05:08:50Z",
  1144. Message: "Add nested.tar.gz",
  1145. Tags: []string{"key", "AWS"},
  1146. Entropy: 3.0841837,
  1147. Fingerprint: "07d2bd71800f1abf0421abe9bc4a83a6fdca1f68:nested.tar.gz!archives/files.tar.xz!files/main.go:aws-access-key:20",
  1148. Link: "https://github.com/gitleaks/test/blob/07d2bd71800f1abf0421abe9bc4a83a6fdca1f68/nested.tar.gz",
  1149. },
  1150. },
  1151. },
  1152. }
  1153. moveDotGit(t, "dotGit", ".git")
  1154. defer moveDotGit(t, ".git", "dotGit")
  1155. for _, tt := range tests {
  1156. t.Run(strings.Join([]string{tt.cfgName, tt.source, tt.logOpts}, "/"), func(t *testing.T) {
  1157. viper.AddConfigPath(configPath)
  1158. viper.SetConfigName("simple")
  1159. viper.SetConfigType("toml")
  1160. err := viper.ReadInConfig()
  1161. require.NoError(t, err)
  1162. var vc config.ViperConfig
  1163. err = viper.Unmarshal(&vc)
  1164. require.NoError(t, err)
  1165. cfg, err := vc.Translate()
  1166. require.NoError(t, err)
  1167. detector := NewDetector(cfg)
  1168. detector.MaxArchiveDepth = 8
  1169. var ignorePath string
  1170. info, err := os.Stat(tt.source)
  1171. require.NoError(t, err)
  1172. if info.IsDir() {
  1173. ignorePath = filepath.Join(tt.source, ".gitleaksignore")
  1174. } else {
  1175. ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
  1176. }
  1177. err = detector.AddGitleaksIgnore(ignorePath)
  1178. require.NoError(t, err)
  1179. gitCmd, err := sources.NewGitLogCmd(tt.source, tt.logOpts)
  1180. require.NoError(t, err)
  1181. remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
  1182. findings, err := detector.DetectGit(gitCmd, remote)
  1183. require.NoError(t, err)
  1184. for _, f := range findings {
  1185. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  1186. }
  1187. assert.ElementsMatch(t, tt.expectedFindings, findings)
  1188. })
  1189. }
  1190. }
  1191. func TestFromGitStaged(t *testing.T) {
  1192. tests := []struct {
  1193. cfgName string
  1194. source string
  1195. logOpts string
  1196. expectedFindings []report.Finding
  1197. }{
  1198. {
  1199. source: filepath.Join(repoBasePath, "staged"),
  1200. cfgName: "simple",
  1201. expectedFindings: []report.Finding{
  1202. {
  1203. RuleID: "aws-access-key",
  1204. Description: "AWS Access Key",
  1205. StartLine: 7,
  1206. EndLine: 7,
  1207. StartColumn: 18,
  1208. EndColumn: 37,
  1209. Line: "\n\taws_token2 := \"AKIALALEMEL33243OLIA\" // this one is not",
  1210. Match: "AKIALALEMEL33243OLIA",
  1211. Secret: "AKIALALEMEL33243OLIA",
  1212. File: "api/api.go",
  1213. SymlinkFile: "",
  1214. Commit: "",
  1215. Entropy: 3.0841837,
  1216. Author: "",
  1217. Email: "",
  1218. Date: "0001-01-01T00:00:00Z",
  1219. Message: "",
  1220. Tags: []string{
  1221. "key",
  1222. "AWS",
  1223. },
  1224. Fingerprint: "api/api.go:aws-access-key:7",
  1225. Link: "",
  1226. },
  1227. },
  1228. },
  1229. }
  1230. moveDotGit(t, "dotGit", ".git")
  1231. defer moveDotGit(t, ".git", "dotGit")
  1232. for _, tt := range tests {
  1233. viper.AddConfigPath(configPath)
  1234. viper.SetConfigName("simple")
  1235. viper.SetConfigType("toml")
  1236. err := viper.ReadInConfig()
  1237. require.NoError(t, err)
  1238. var vc config.ViperConfig
  1239. err = viper.Unmarshal(&vc)
  1240. require.NoError(t, err)
  1241. cfg, err := vc.Translate()
  1242. require.NoError(t, err)
  1243. detector := NewDetector(cfg)
  1244. err = detector.AddGitleaksIgnore(filepath.Join(tt.source, ".gitleaksignore"))
  1245. require.NoError(t, err)
  1246. gitCmd, err := sources.NewGitDiffCmd(tt.source, true)
  1247. require.NoError(t, err)
  1248. remote := NewRemoteInfo(scm.UnknownPlatform, tt.source)
  1249. findings, err := detector.DetectGit(gitCmd, remote)
  1250. require.NoError(t, err)
  1251. for _, f := range findings {
  1252. f.Match = "" // remove lines cause copying and pasting them has some wack formatting
  1253. }
  1254. assert.ElementsMatch(t, tt.expectedFindings, findings)
  1255. }
  1256. }
  1257. // TestFromFiles tests the FromFiles function
  1258. func TestFromFiles(t *testing.T) {
  1259. tests := []struct {
  1260. cfgName string
  1261. source string
  1262. expectedFindings []report.Finding
  1263. }{
  1264. {
  1265. source: filepath.Join(repoBasePath, "nogit"),
  1266. cfgName: "simple",
  1267. expectedFindings: []report.Finding{
  1268. {
  1269. RuleID: "aws-access-key",
  1270. Description: "AWS Access Key",
  1271. StartLine: 20,
  1272. EndLine: 20,
  1273. StartColumn: 16,
  1274. EndColumn: 35,
  1275. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1276. Match: "AKIALALEMEL33243OLIA",
  1277. Secret: "AKIALALEMEL33243OLIA",
  1278. File: "../testdata/repos/nogit/main.go",
  1279. SymlinkFile: "",
  1280. Tags: []string{"key", "AWS"},
  1281. Entropy: 3.0841837,
  1282. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  1283. },
  1284. },
  1285. },
  1286. {
  1287. source: filepath.Join(repoBasePath, "nogit", "main.go"),
  1288. cfgName: "simple",
  1289. expectedFindings: []report.Finding{
  1290. {
  1291. RuleID: "aws-access-key",
  1292. Description: "AWS Access Key",
  1293. StartLine: 20,
  1294. EndLine: 20,
  1295. StartColumn: 16,
  1296. EndColumn: 35,
  1297. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1298. Match: "AKIALALEMEL33243OLIA",
  1299. Secret: "AKIALALEMEL33243OLIA",
  1300. File: "../testdata/repos/nogit/main.go",
  1301. Tags: []string{"key", "AWS"},
  1302. Entropy: 3.0841837,
  1303. Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
  1304. },
  1305. },
  1306. },
  1307. {
  1308. source: filepath.Join(repoBasePath, "nogit", "api.go"),
  1309. cfgName: "simple",
  1310. expectedFindings: []report.Finding{},
  1311. },
  1312. {
  1313. source: filepath.Join(repoBasePath, "nogit", ".env.prod"),
  1314. cfgName: "generic",
  1315. expectedFindings: []report.Finding{
  1316. {
  1317. RuleID: "generic-api-key",
  1318. Description: "Generic API Key",
  1319. StartLine: 4,
  1320. EndLine: 4,
  1321. StartColumn: 5,
  1322. EndColumn: 35,
  1323. Line: "\nDB_PASSWORD=8ae31cacf141669ddfb5da",
  1324. Match: "PASSWORD=8ae31cacf141669ddfb5da",
  1325. Secret: "8ae31cacf141669ddfb5da",
  1326. File: "../testdata/repos/nogit/.env.prod",
  1327. Tags: []string{},
  1328. Entropy: 3.5383105,
  1329. Fingerprint: "../testdata/repos/nogit/.env.prod:generic-api-key:4",
  1330. },
  1331. },
  1332. },
  1333. }
  1334. for _, tt := range tests {
  1335. t.Run(tt.cfgName+" - "+tt.source, func(t *testing.T) {
  1336. viper.AddConfigPath(configPath)
  1337. viper.SetConfigName(tt.cfgName)
  1338. viper.SetConfigType("toml")
  1339. err := viper.ReadInConfig()
  1340. require.NoError(t, err)
  1341. var vc config.ViperConfig
  1342. err = viper.Unmarshal(&vc)
  1343. require.NoError(t, err)
  1344. cfg, _ := vc.Translate()
  1345. detector := NewDetector(cfg)
  1346. info, err := os.Stat(tt.source)
  1347. require.NoError(t, err)
  1348. var ignorePath string
  1349. if info.IsDir() {
  1350. ignorePath = filepath.Join(tt.source, ".gitleaksignore")
  1351. } else {
  1352. ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
  1353. }
  1354. err = detector.AddGitleaksIgnore(ignorePath)
  1355. require.NoError(t, err)
  1356. detector.FollowSymlinks = true
  1357. paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
  1358. require.NoError(t, err)
  1359. findings, err := detector.DetectFiles(paths)
  1360. require.NoError(t, err)
  1361. // TODO: Temporary mitigation.
  1362. // https://github.com/gitleaks/gitleaks/issues/1641
  1363. normalizedFindings := make([]report.Finding, len(findings))
  1364. for i, f := range findings {
  1365. if strings.HasSuffix(f.Line, "\r") {
  1366. f.Line = strings.ReplaceAll(f.Line, "\r", "")
  1367. }
  1368. if strings.HasSuffix(f.Match, "\r") {
  1369. f.EndColumn = f.EndColumn - 1
  1370. f.Match = strings.ReplaceAll(f.Match, "\r", "")
  1371. }
  1372. normalizedFindings[i] = f
  1373. }
  1374. assert.ElementsMatch(t, tt.expectedFindings, normalizedFindings)
  1375. })
  1376. }
  1377. }
  1378. func TestDetectWithArchives(t *testing.T) {
  1379. tests := []struct {
  1380. cfgName string
  1381. source string
  1382. expectedFindings []report.Finding
  1383. }{
  1384. {
  1385. source: filepath.Join(archivesBasePath, "this-path-does-not-exist"),
  1386. cfgName: "archives",
  1387. expectedFindings: []report.Finding{},
  1388. },
  1389. {
  1390. source: filepath.Join(archivesBasePath, "files"),
  1391. cfgName: "archives",
  1392. expectedFindings: []report.Finding{
  1393. {
  1394. RuleID: "aws-access-key",
  1395. Description: "AWS Access Key",
  1396. StartLine: 20,
  1397. EndLine: 20,
  1398. StartColumn: 16,
  1399. EndColumn: 35,
  1400. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1401. Match: "AKIALALEMEL33243OLIA",
  1402. Secret: "AKIALALEMEL33243OLIA",
  1403. File: "../testdata/archives/files/api.go",
  1404. SymlinkFile: "",
  1405. Tags: []string{"key", "AWS"},
  1406. Entropy: 3.0841837,
  1407. Fingerprint: "../testdata/archives/files/api.go:aws-access-key:20",
  1408. },
  1409. {
  1410. RuleID: "aws-access-key",
  1411. Description: "AWS Access Key",
  1412. StartLine: 20,
  1413. EndLine: 20,
  1414. StartColumn: 16,
  1415. EndColumn: 35,
  1416. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1417. Match: "AKIALALEMEL33243OLIA",
  1418. Secret: "AKIALALEMEL33243OLIA",
  1419. File: "../testdata/archives/files/main.go",
  1420. SymlinkFile: "",
  1421. Tags: []string{"key", "AWS"},
  1422. Entropy: 3.0841837,
  1423. Fingerprint: "../testdata/archives/files/main.go:aws-access-key:20",
  1424. },
  1425. {
  1426. RuleID: "aws-access-key",
  1427. Description: "AWS Access Key",
  1428. StartLine: 20,
  1429. EndLine: 20,
  1430. StartColumn: 16,
  1431. EndColumn: 35,
  1432. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1433. Match: "AKIALALEMEL33243OLIA",
  1434. Secret: "AKIALALEMEL33243OLIA",
  1435. File: "../testdata/archives/files/main.go.gz",
  1436. SymlinkFile: "",
  1437. Tags: []string{"key", "AWS"},
  1438. Entropy: 3.0841837,
  1439. Fingerprint: "../testdata/archives/files/main.go.gz:aws-access-key:20",
  1440. },
  1441. {
  1442. RuleID: "aws-access-key",
  1443. Description: "AWS Access Key",
  1444. StartLine: 20,
  1445. EndLine: 20,
  1446. StartColumn: 16,
  1447. EndColumn: 35,
  1448. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1449. Match: "AKIALALEMEL33243OLIA",
  1450. Secret: "AKIALALEMEL33243OLIA",
  1451. File: "../testdata/archives/files/main.go.xz",
  1452. SymlinkFile: "",
  1453. Tags: []string{"key", "AWS"},
  1454. Entropy: 3.0841837,
  1455. Fingerprint: "../testdata/archives/files/main.go.xz:aws-access-key:20",
  1456. },
  1457. {
  1458. RuleID: "aws-access-key",
  1459. Description: "AWS Access Key",
  1460. StartLine: 20,
  1461. EndLine: 20,
  1462. StartColumn: 16,
  1463. EndColumn: 35,
  1464. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1465. Match: "AKIALALEMEL33243OLIA",
  1466. Secret: "AKIALALEMEL33243OLIA",
  1467. File: "../testdata/archives/files/main.go.zst",
  1468. SymlinkFile: "",
  1469. Tags: []string{"key", "AWS"},
  1470. Entropy: 3.0841837,
  1471. Fingerprint: "../testdata/archives/files/main.go.zst:aws-access-key:20",
  1472. },
  1473. },
  1474. },
  1475. {
  1476. source: filepath.Join(archivesBasePath, "files.7z"),
  1477. cfgName: "archives",
  1478. expectedFindings: []report.Finding{
  1479. {
  1480. RuleID: "aws-access-key",
  1481. Description: "AWS Access Key",
  1482. StartLine: 20,
  1483. EndLine: 20,
  1484. StartColumn: 16,
  1485. EndColumn: 35,
  1486. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1487. Match: "AKIALALEMEL33243OLIA",
  1488. Secret: "AKIALALEMEL33243OLIA",
  1489. File: "../testdata/archives/files.7z!files/api.go",
  1490. SymlinkFile: "",
  1491. Tags: []string{"key", "AWS"},
  1492. Entropy: 3.0841837,
  1493. Fingerprint: "../testdata/archives/files.7z!files/api.go:aws-access-key:20",
  1494. },
  1495. {
  1496. RuleID: "aws-access-key",
  1497. Description: "AWS Access Key",
  1498. StartLine: 20,
  1499. EndLine: 20,
  1500. StartColumn: 16,
  1501. EndColumn: 35,
  1502. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1503. Match: "AKIALALEMEL33243OLIA",
  1504. Secret: "AKIALALEMEL33243OLIA",
  1505. File: "../testdata/archives/files.7z!files/main.go",
  1506. SymlinkFile: "",
  1507. Tags: []string{"key", "AWS"},
  1508. Entropy: 3.0841837,
  1509. Fingerprint: "../testdata/archives/files.7z!files/main.go:aws-access-key:20",
  1510. },
  1511. },
  1512. },
  1513. {
  1514. source: filepath.Join(archivesBasePath, "files.tar"),
  1515. cfgName: "archives",
  1516. expectedFindings: []report.Finding{
  1517. {
  1518. RuleID: "aws-access-key",
  1519. Description: "AWS Access Key",
  1520. StartLine: 20,
  1521. EndLine: 20,
  1522. StartColumn: 16,
  1523. EndColumn: 35,
  1524. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1525. Match: "AKIALALEMEL33243OLIA",
  1526. Secret: "AKIALALEMEL33243OLIA",
  1527. File: "../testdata/archives/files.tar!files/api.go",
  1528. SymlinkFile: "",
  1529. Tags: []string{"key", "AWS"},
  1530. Entropy: 3.0841837,
  1531. Fingerprint: "../testdata/archives/files.tar!files/api.go:aws-access-key:20",
  1532. },
  1533. {
  1534. RuleID: "aws-access-key",
  1535. Description: "AWS Access Key",
  1536. StartLine: 20,
  1537. EndLine: 20,
  1538. StartColumn: 16,
  1539. EndColumn: 35,
  1540. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1541. Match: "AKIALALEMEL33243OLIA",
  1542. Secret: "AKIALALEMEL33243OLIA",
  1543. File: "../testdata/archives/files.tar!files/main.go",
  1544. SymlinkFile: "",
  1545. Tags: []string{"key", "AWS"},
  1546. Entropy: 3.0841837,
  1547. Fingerprint: "../testdata/archives/files.tar!files/main.go:aws-access-key:20",
  1548. },
  1549. },
  1550. },
  1551. {
  1552. source: filepath.Join(archivesBasePath, "files.tar.xz"),
  1553. cfgName: "archives",
  1554. expectedFindings: []report.Finding{
  1555. {
  1556. RuleID: "aws-access-key",
  1557. Description: "AWS Access Key",
  1558. StartLine: 20,
  1559. EndLine: 20,
  1560. StartColumn: 16,
  1561. EndColumn: 35,
  1562. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1563. Match: "AKIALALEMEL33243OLIA",
  1564. Secret: "AKIALALEMEL33243OLIA",
  1565. File: "../testdata/archives/files.tar.xz!files/api.go",
  1566. SymlinkFile: "",
  1567. Tags: []string{"key", "AWS"},
  1568. Entropy: 3.0841837,
  1569. Fingerprint: "../testdata/archives/files.tar.xz!files/api.go:aws-access-key:20",
  1570. },
  1571. {
  1572. RuleID: "aws-access-key",
  1573. Description: "AWS Access Key",
  1574. StartLine: 20,
  1575. EndLine: 20,
  1576. StartColumn: 16,
  1577. EndColumn: 35,
  1578. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1579. Match: "AKIALALEMEL33243OLIA",
  1580. Secret: "AKIALALEMEL33243OLIA",
  1581. File: "../testdata/archives/files.tar.xz!files/main.go",
  1582. SymlinkFile: "",
  1583. Tags: []string{"key", "AWS"},
  1584. Entropy: 3.0841837,
  1585. Fingerprint: "../testdata/archives/files.tar.xz!files/main.go:aws-access-key:20",
  1586. },
  1587. },
  1588. },
  1589. {
  1590. source: filepath.Join(archivesBasePath, "files.tar.zst"),
  1591. cfgName: "archives",
  1592. expectedFindings: []report.Finding{
  1593. {
  1594. RuleID: "aws-access-key",
  1595. Description: "AWS Access Key",
  1596. StartLine: 20,
  1597. EndLine: 20,
  1598. StartColumn: 16,
  1599. EndColumn: 35,
  1600. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1601. Match: "AKIALALEMEL33243OLIA",
  1602. Secret: "AKIALALEMEL33243OLIA",
  1603. File: "../testdata/archives/files.tar.zst!files/api.go",
  1604. SymlinkFile: "",
  1605. Tags: []string{"key", "AWS"},
  1606. Entropy: 3.0841837,
  1607. Fingerprint: "../testdata/archives/files.tar.zst!files/api.go:aws-access-key:20",
  1608. },
  1609. {
  1610. RuleID: "aws-access-key",
  1611. Description: "AWS Access Key",
  1612. StartLine: 20,
  1613. EndLine: 20,
  1614. StartColumn: 16,
  1615. EndColumn: 35,
  1616. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1617. Match: "AKIALALEMEL33243OLIA",
  1618. Secret: "AKIALALEMEL33243OLIA",
  1619. File: "../testdata/archives/files.tar.zst!files/main.go",
  1620. SymlinkFile: "",
  1621. Tags: []string{"key", "AWS"},
  1622. Entropy: 3.0841837,
  1623. Fingerprint: "../testdata/archives/files.tar.zst!files/main.go:aws-access-key:20",
  1624. },
  1625. },
  1626. },
  1627. {
  1628. source: filepath.Join(archivesBasePath, "files.zip"),
  1629. cfgName: "archives",
  1630. expectedFindings: []report.Finding{
  1631. {
  1632. RuleID: "aws-access-key",
  1633. Description: "AWS Access Key",
  1634. StartLine: 20,
  1635. EndLine: 20,
  1636. StartColumn: 16,
  1637. EndColumn: 35,
  1638. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1639. Match: "AKIALALEMEL33243OLIA",
  1640. Secret: "AKIALALEMEL33243OLIA",
  1641. File: "../testdata/archives/files.zip!files/api.go",
  1642. SymlinkFile: "",
  1643. Tags: []string{"key", "AWS"},
  1644. Entropy: 3.0841837,
  1645. Fingerprint: "../testdata/archives/files.zip!files/api.go:aws-access-key:20",
  1646. },
  1647. {
  1648. RuleID: "aws-access-key",
  1649. Description: "AWS Access Key",
  1650. StartLine: 20,
  1651. EndLine: 20,
  1652. StartColumn: 16,
  1653. EndColumn: 35,
  1654. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1655. Match: "AKIALALEMEL33243OLIA",
  1656. Secret: "AKIALALEMEL33243OLIA",
  1657. File: "../testdata/archives/files.zip!files/main.go",
  1658. SymlinkFile: "",
  1659. Tags: []string{"key", "AWS"},
  1660. Entropy: 3.0841837,
  1661. Fingerprint: "../testdata/archives/files.zip!files/main.go:aws-access-key:20",
  1662. },
  1663. },
  1664. },
  1665. {
  1666. source: filepath.Join(archivesBasePath, "nested.tar.gz"),
  1667. cfgName: "archives",
  1668. expectedFindings: []report.Finding{
  1669. {
  1670. RuleID: "aws-access-key",
  1671. Description: "AWS Access Key",
  1672. StartLine: 20,
  1673. EndLine: 20,
  1674. StartColumn: 16,
  1675. EndColumn: 35,
  1676. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1677. Match: "AKIALALEMEL33243OLIA",
  1678. Secret: "AKIALALEMEL33243OLIA",
  1679. File: "../testdata/archives/nested.tar.gz!archives/files.tar!files/api.go",
  1680. SymlinkFile: "",
  1681. Tags: []string{"key", "AWS"},
  1682. Entropy: 3.0841837,
  1683. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar!files/api.go:aws-access-key:20",
  1684. },
  1685. {
  1686. RuleID: "aws-access-key",
  1687. Description: "AWS Access Key",
  1688. StartLine: 20,
  1689. EndLine: 20,
  1690. StartColumn: 16,
  1691. EndColumn: 35,
  1692. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1693. Match: "AKIALALEMEL33243OLIA",
  1694. Secret: "AKIALALEMEL33243OLIA",
  1695. File: "../testdata/archives/nested.tar.gz!archives/files.tar!files/main.go",
  1696. SymlinkFile: "",
  1697. Tags: []string{"key", "AWS"},
  1698. Entropy: 3.0841837,
  1699. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar!files/main.go:aws-access-key:20",
  1700. },
  1701. {
  1702. RuleID: "aws-access-key",
  1703. Description: "AWS Access Key",
  1704. StartLine: 20,
  1705. EndLine: 20,
  1706. StartColumn: 16,
  1707. EndColumn: 35,
  1708. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1709. Match: "AKIALALEMEL33243OLIA",
  1710. Secret: "AKIALALEMEL33243OLIA",
  1711. File: "../testdata/archives/nested.tar.gz!archives/files.zip!files/api.go",
  1712. SymlinkFile: "",
  1713. Tags: []string{"key", "AWS"},
  1714. Entropy: 3.0841837,
  1715. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.zip!files/api.go:aws-access-key:20",
  1716. },
  1717. {
  1718. RuleID: "aws-access-key",
  1719. Description: "AWS Access Key",
  1720. StartLine: 20,
  1721. EndLine: 20,
  1722. StartColumn: 16,
  1723. EndColumn: 35,
  1724. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1725. Match: "AKIALALEMEL33243OLIA",
  1726. Secret: "AKIALALEMEL33243OLIA",
  1727. File: "../testdata/archives/nested.tar.gz!archives/files.zip!files/main.go",
  1728. SymlinkFile: "",
  1729. Tags: []string{"key", "AWS"},
  1730. Entropy: 3.0841837,
  1731. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.zip!files/main.go:aws-access-key:20",
  1732. },
  1733. {
  1734. RuleID: "aws-access-key",
  1735. Description: "AWS Access Key",
  1736. StartLine: 20,
  1737. EndLine: 20,
  1738. StartColumn: 16,
  1739. EndColumn: 35,
  1740. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1741. Match: "AKIALALEMEL33243OLIA",
  1742. Secret: "AKIALALEMEL33243OLIA",
  1743. File: "../testdata/archives/nested.tar.gz!archives/files.7z!files/api.go",
  1744. SymlinkFile: "",
  1745. Tags: []string{"key", "AWS"},
  1746. Entropy: 3.0841837,
  1747. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.7z!files/api.go:aws-access-key:20",
  1748. },
  1749. {
  1750. RuleID: "aws-access-key",
  1751. Description: "AWS Access Key",
  1752. StartLine: 20,
  1753. EndLine: 20,
  1754. StartColumn: 16,
  1755. EndColumn: 35,
  1756. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1757. Match: "AKIALALEMEL33243OLIA",
  1758. Secret: "AKIALALEMEL33243OLIA",
  1759. File: "../testdata/archives/nested.tar.gz!archives/files.7z!files/main.go",
  1760. SymlinkFile: "",
  1761. Tags: []string{"key", "AWS"},
  1762. Entropy: 3.0841837,
  1763. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.7z!files/main.go:aws-access-key:20",
  1764. },
  1765. {
  1766. RuleID: "aws-access-key",
  1767. Description: "AWS Access Key",
  1768. StartLine: 20,
  1769. EndLine: 20,
  1770. StartColumn: 16,
  1771. EndColumn: 35,
  1772. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1773. Match: "AKIALALEMEL33243OLIA",
  1774. Secret: "AKIALALEMEL33243OLIA",
  1775. File: "../testdata/archives/nested.tar.gz!archives/files.tar.zst!files/api.go",
  1776. SymlinkFile: "",
  1777. Tags: []string{"key", "AWS"},
  1778. Entropy: 3.0841837,
  1779. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar.zst!files/api.go:aws-access-key:20",
  1780. },
  1781. {
  1782. RuleID: "aws-access-key",
  1783. Description: "AWS Access Key",
  1784. StartLine: 20,
  1785. EndLine: 20,
  1786. StartColumn: 16,
  1787. EndColumn: 35,
  1788. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1789. Match: "AKIALALEMEL33243OLIA",
  1790. Secret: "AKIALALEMEL33243OLIA",
  1791. File: "../testdata/archives/nested.tar.gz!archives/files.tar.zst!files/main.go",
  1792. SymlinkFile: "",
  1793. Tags: []string{"key", "AWS"},
  1794. Entropy: 3.0841837,
  1795. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar.zst!files/main.go:aws-access-key:20",
  1796. },
  1797. {
  1798. RuleID: "aws-access-key",
  1799. Description: "AWS Access Key",
  1800. StartLine: 20,
  1801. EndLine: 20,
  1802. StartColumn: 16,
  1803. EndColumn: 35,
  1804. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1805. Match: "AKIALALEMEL33243OLIA",
  1806. Secret: "AKIALALEMEL33243OLIA",
  1807. File: "../testdata/archives/nested.tar.gz!archives/files/api.go",
  1808. SymlinkFile: "",
  1809. Tags: []string{"key", "AWS"},
  1810. Entropy: 3.0841837,
  1811. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files/api.go:aws-access-key:20",
  1812. },
  1813. {
  1814. RuleID: "aws-access-key",
  1815. Description: "AWS Access Key",
  1816. StartLine: 20,
  1817. EndLine: 20,
  1818. StartColumn: 16,
  1819. EndColumn: 35,
  1820. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1821. Match: "AKIALALEMEL33243OLIA",
  1822. Secret: "AKIALALEMEL33243OLIA",
  1823. File: "../testdata/archives/nested.tar.gz!archives/files/main.go",
  1824. SymlinkFile: "",
  1825. Tags: []string{"key", "AWS"},
  1826. Entropy: 3.0841837,
  1827. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files/main.go:aws-access-key:20",
  1828. },
  1829. {
  1830. RuleID: "aws-access-key",
  1831. Description: "AWS Access Key",
  1832. StartLine: 20,
  1833. EndLine: 20,
  1834. StartColumn: 16,
  1835. EndColumn: 35,
  1836. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1837. Match: "AKIALALEMEL33243OLIA",
  1838. Secret: "AKIALALEMEL33243OLIA",
  1839. File: "../testdata/archives/nested.tar.gz!archives/files/main.go.xz",
  1840. SymlinkFile: "",
  1841. Tags: []string{"key", "AWS"},
  1842. Entropy: 3.0841837,
  1843. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files/main.go.xz:aws-access-key:20",
  1844. },
  1845. {
  1846. RuleID: "aws-access-key",
  1847. Description: "AWS Access Key",
  1848. StartLine: 20,
  1849. EndLine: 20,
  1850. StartColumn: 16,
  1851. EndColumn: 35,
  1852. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1853. Match: "AKIALALEMEL33243OLIA",
  1854. Secret: "AKIALALEMEL33243OLIA",
  1855. File: "../testdata/archives/nested.tar.gz!archives/files/main.go.zst",
  1856. SymlinkFile: "",
  1857. Tags: []string{"key", "AWS"},
  1858. Entropy: 3.0841837,
  1859. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files/main.go.zst:aws-access-key:20",
  1860. },
  1861. {
  1862. RuleID: "aws-access-key",
  1863. Description: "AWS Access Key",
  1864. StartLine: 20,
  1865. EndLine: 20,
  1866. StartColumn: 16,
  1867. EndColumn: 35,
  1868. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1869. Match: "AKIALALEMEL33243OLIA",
  1870. Secret: "AKIALALEMEL33243OLIA",
  1871. File: "../testdata/archives/nested.tar.gz!archives/files/main.go.gz",
  1872. SymlinkFile: "",
  1873. Tags: []string{"key", "AWS"},
  1874. Entropy: 3.0841837,
  1875. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files/main.go.gz:aws-access-key:20",
  1876. },
  1877. {
  1878. RuleID: "aws-access-key",
  1879. Description: "AWS Access Key",
  1880. StartLine: 20,
  1881. EndLine: 20,
  1882. StartColumn: 16,
  1883. EndColumn: 35,
  1884. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1885. Match: "AKIALALEMEL33243OLIA",
  1886. Secret: "AKIALALEMEL33243OLIA",
  1887. File: "../testdata/archives/nested.tar.gz!archives/files.tar.xz!files/api.go",
  1888. SymlinkFile: "",
  1889. Tags: []string{"key", "AWS"},
  1890. Entropy: 3.0841837,
  1891. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar.xz!files/api.go:aws-access-key:20",
  1892. },
  1893. {
  1894. RuleID: "aws-access-key",
  1895. Description: "AWS Access Key",
  1896. StartLine: 20,
  1897. EndLine: 20,
  1898. StartColumn: 16,
  1899. EndColumn: 35,
  1900. Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
  1901. Match: "AKIALALEMEL33243OLIA",
  1902. Secret: "AKIALALEMEL33243OLIA",
  1903. File: "../testdata/archives/nested.tar.gz!archives/files.tar.xz!files/main.go",
  1904. SymlinkFile: "",
  1905. Tags: []string{"key", "AWS"},
  1906. Entropy: 3.0841837,
  1907. Fingerprint: "../testdata/archives/nested.tar.gz!archives/files.tar.xz!files/main.go:aws-access-key:20",
  1908. },
  1909. },
  1910. },
  1911. }
  1912. for _, tt := range tests {
  1913. t.Run(tt.cfgName+" - "+tt.source, func(t *testing.T) {
  1914. viper.AddConfigPath(configPath)
  1915. viper.SetConfigName(tt.cfgName)
  1916. viper.SetConfigType("toml")
  1917. err := viper.ReadInConfig()
  1918. require.NoError(t, err)
  1919. var vc config.ViperConfig
  1920. err = viper.Unmarshal(&vc)
  1921. require.NoError(t, err)
  1922. cfg, _ := vc.Translate()
  1923. detector := NewDetector(cfg)
  1924. detector.MaxArchiveDepth = 8
  1925. findings, err := detector.DetectSource(
  1926. context.Background(),
  1927. &sources.Files{
  1928. Path: tt.source,
  1929. Sema: detector.Sema,
  1930. Config: &cfg,
  1931. MaxArchiveDepth: detector.MaxArchiveDepth,
  1932. },
  1933. )
  1934. require.NoError(t, err)
  1935. // TODO: Temporary mitigation.
  1936. // https://github.com/gitleaks/gitleaks/issues/1641
  1937. normalizedFindings := make([]report.Finding, len(findings))
  1938. for i, f := range findings {
  1939. if strings.HasSuffix(f.Line, "\r") {
  1940. f.Line = strings.ReplaceAll(f.Line, "\r", "")
  1941. }
  1942. if strings.HasSuffix(f.Match, "\r") {
  1943. f.EndColumn = f.EndColumn - 1
  1944. f.Match = strings.ReplaceAll(f.Match, "\r", "")
  1945. }
  1946. normalizedFindings[i] = f
  1947. }
  1948. assert.ElementsMatch(t, tt.expectedFindings, normalizedFindings)
  1949. })
  1950. }
  1951. }
  1952. func TestDetectWithSymlinks(t *testing.T) {
  1953. // TODO: Fix this test on windows.
  1954. if runtime.GOOS == "windows" {
  1955. t.Skipf("TODO: this returns no results on windows, I'm not sure why.")
  1956. return
  1957. }
  1958. tests := []struct {
  1959. cfgName string
  1960. source string
  1961. expectedFindings []report.Finding
  1962. }{
  1963. {
  1964. source: filepath.Join(repoBasePath, "symlinks/file_symlink"),
  1965. cfgName: "simple",
  1966. expectedFindings: []report.Finding{
  1967. {
  1968. RuleID: "apkey",
  1969. Description: "Asymmetric Private Key",
  1970. StartLine: 1,
  1971. EndLine: 1,
  1972. StartColumn: 1,
  1973. EndColumn: 35,
  1974. Match: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1975. Secret: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1976. Line: "-----BEGIN OPENSSH PRIVATE KEY-----",
  1977. File: "../testdata/repos/symlinks/source_file/id_ed25519",
  1978. SymlinkFile: "../testdata/repos/symlinks/file_symlink/symlinked_id_ed25519",
  1979. Tags: []string{"key", "AsymmetricPrivateKey"},
  1980. Entropy: 3.587164,
  1981. Fingerprint: "../testdata/repos/symlinks/source_file/id_ed25519:apkey:1",
  1982. },
  1983. },
  1984. },
  1985. }
  1986. for _, tt := range tests {
  1987. viper.AddConfigPath(configPath)
  1988. viper.SetConfigName("simple")
  1989. viper.SetConfigType("toml")
  1990. err := viper.ReadInConfig()
  1991. require.NoError(t, err)
  1992. var vc config.ViperConfig
  1993. err = viper.Unmarshal(&vc)
  1994. require.NoError(t, err)
  1995. cfg, _ := vc.Translate()
  1996. detector := NewDetector(cfg)
  1997. detector.FollowSymlinks = true
  1998. paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlists)
  1999. require.NoError(t, err)
  2000. findings, err := detector.DetectFiles(paths)
  2001. require.NoError(t, err)
  2002. assert.ElementsMatch(t, tt.expectedFindings, findings)
  2003. }
  2004. }
  2005. func TestDetectRuleAllowlist(t *testing.T) {
  2006. cases := map[string]struct {
  2007. fragment Fragment
  2008. allowlist *config.Allowlist
  2009. expected []report.Finding
  2010. }{
  2011. // Commit / path
  2012. "commit allowed": {
  2013. fragment: Fragment{
  2014. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2015. },
  2016. allowlist: &config.Allowlist{
  2017. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2018. },
  2019. },
  2020. "path allowed": {
  2021. fragment: Fragment{
  2022. FilePath: "package-lock.json",
  2023. },
  2024. allowlist: &config.Allowlist{
  2025. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2026. },
  2027. },
  2028. "commit AND path allowed": {
  2029. fragment: Fragment{
  2030. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2031. FilePath: "package-lock.json",
  2032. },
  2033. allowlist: &config.Allowlist{
  2034. MatchCondition: config.AllowlistMatchAnd,
  2035. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2036. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2037. },
  2038. },
  2039. "commit AND path NOT allowed": {
  2040. fragment: Fragment{
  2041. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2042. FilePath: "package.json",
  2043. },
  2044. allowlist: &config.Allowlist{
  2045. MatchCondition: config.AllowlistMatchAnd,
  2046. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2047. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2048. },
  2049. expected: []report.Finding{
  2050. {
  2051. StartColumn: 50,
  2052. EndColumn: 60,
  2053. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  2054. Match: "Summer2024!",
  2055. Secret: "Summer2024!",
  2056. File: "package.json",
  2057. Commit: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2058. Entropy: 3.095795154571533,
  2059. RuleID: "test-rule",
  2060. },
  2061. },
  2062. },
  2063. "commit AND path NOT allowed - other conditions": {
  2064. fragment: Fragment{
  2065. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2066. FilePath: "package-lock.json",
  2067. },
  2068. allowlist: &config.Allowlist{
  2069. MatchCondition: config.AllowlistMatchAnd,
  2070. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2071. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2072. Regexes: []*regexp.Regexp{regexp.MustCompile("password")},
  2073. },
  2074. expected: []report.Finding{
  2075. {
  2076. StartColumn: 50,
  2077. EndColumn: 60,
  2078. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  2079. Match: "Summer2024!",
  2080. Secret: "Summer2024!",
  2081. File: "package-lock.json",
  2082. Commit: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2083. Entropy: 3.095795154571533,
  2084. RuleID: "test-rule",
  2085. },
  2086. },
  2087. },
  2088. "commit OR path allowed": {
  2089. fragment: Fragment{
  2090. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2091. FilePath: "package-lock.json",
  2092. },
  2093. allowlist: &config.Allowlist{
  2094. MatchCondition: config.AllowlistMatchOr,
  2095. Commits: []string{"704178e7dca77ff143778a31cff0fc192d59b030"},
  2096. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2097. },
  2098. },
  2099. // Regex / stopwords
  2100. "regex allowed": {
  2101. fragment: Fragment{},
  2102. allowlist: &config.Allowlist{
  2103. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  2104. },
  2105. },
  2106. "stopwords allowed": {
  2107. fragment: Fragment{},
  2108. allowlist: &config.Allowlist{
  2109. StopWords: []string{"summer"},
  2110. },
  2111. },
  2112. "regex AND stopword allowed": {
  2113. fragment: Fragment{},
  2114. allowlist: &config.Allowlist{
  2115. MatchCondition: config.AllowlistMatchAnd,
  2116. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  2117. StopWords: []string{"2024"},
  2118. },
  2119. },
  2120. "regex AND stopword allowed - other conditions": {
  2121. fragment: Fragment{
  2122. CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
  2123. FilePath: "config.js",
  2124. },
  2125. allowlist: &config.Allowlist{
  2126. MatchCondition: config.AllowlistMatchAnd,
  2127. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2128. Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
  2129. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  2130. StopWords: []string{"2024"},
  2131. },
  2132. },
  2133. "regex AND stopword NOT allowed - non-git, other conditions": {
  2134. fragment: Fragment{
  2135. FilePath: "config.js",
  2136. },
  2137. allowlist: &config.Allowlist{
  2138. MatchCondition: config.AllowlistMatchAnd,
  2139. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2140. Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
  2141. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  2142. StopWords: []string{"2024"},
  2143. },
  2144. expected: []report.Finding{
  2145. {
  2146. StartColumn: 50,
  2147. EndColumn: 60,
  2148. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  2149. Match: "Summer2024!",
  2150. Secret: "Summer2024!",
  2151. File: "config.js",
  2152. Entropy: 3.095795154571533,
  2153. RuleID: "test-rule",
  2154. },
  2155. },
  2156. },
  2157. "regex AND stopword NOT allowed": {
  2158. fragment: Fragment{},
  2159. allowlist: &config.Allowlist{
  2160. MatchCondition: config.AllowlistMatchAnd,
  2161. Regexes: []*regexp.Regexp{
  2162. regexp.MustCompile(`(?i)winter.+`),
  2163. },
  2164. StopWords: []string{"2024"},
  2165. },
  2166. expected: []report.Finding{
  2167. {
  2168. StartColumn: 50,
  2169. EndColumn: 60,
  2170. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  2171. Match: "Summer2024!",
  2172. Secret: "Summer2024!",
  2173. Entropy: 3.095795154571533,
  2174. RuleID: "test-rule",
  2175. },
  2176. },
  2177. },
  2178. "regex AND stopword NOT allowed - other conditions": {
  2179. fragment: Fragment{
  2180. CommitSHA: "a060c9d2d5e90c992763f1bd4c3cd2a6f121241b",
  2181. FilePath: "config.js",
  2182. },
  2183. allowlist: &config.Allowlist{
  2184. MatchCondition: config.AllowlistMatchAnd,
  2185. Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
  2186. Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
  2187. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)winter.+`)},
  2188. StopWords: []string{"2024"},
  2189. },
  2190. expected: []report.Finding{
  2191. {
  2192. StartColumn: 50,
  2193. EndColumn: 60,
  2194. Line: "let username = 'james@mail.com';\nlet password = 'Summer2024!';",
  2195. Match: "Summer2024!",
  2196. Secret: "Summer2024!",
  2197. File: "config.js",
  2198. Commit: "a060c9d2d5e90c992763f1bd4c3cd2a6f121241b",
  2199. Entropy: 3.095795154571533,
  2200. RuleID: "test-rule",
  2201. },
  2202. },
  2203. },
  2204. "regex OR stopword allowed": {
  2205. fragment: Fragment{},
  2206. allowlist: &config.Allowlist{
  2207. MatchCondition: config.AllowlistMatchOr,
  2208. Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
  2209. StopWords: []string{"winter"},
  2210. },
  2211. },
  2212. }
  2213. raw := `let username = 'james@mail.com';
  2214. let password = 'Summer2024!';`
  2215. for name, tc := range cases {
  2216. t.Run(name, func(t *testing.T) {
  2217. err := tc.allowlist.Validate()
  2218. require.NoError(t, err)
  2219. rule := config.Rule{
  2220. RuleID: "test-rule",
  2221. Regex: regexp.MustCompile(`Summer2024!`),
  2222. Allowlists: []*config.Allowlist{
  2223. tc.allowlist,
  2224. },
  2225. }
  2226. d, err := NewDetectorDefaultConfig()
  2227. require.NoError(t, err)
  2228. f := tc.fragment
  2229. f.Raw = raw
  2230. actual := d.detectRule(f, [][]int{}, raw, rule, []*codec.EncodedSegment{})
  2231. if diff := cmp.Diff(tc.expected, actual); diff != "" {
  2232. t.Errorf("diff: (-want +got)\n%s", diff)
  2233. }
  2234. })
  2235. }
  2236. }
  2237. func moveDotGit(t *testing.T, from, to string) {
  2238. t.Helper()
  2239. repoDirs, err := os.ReadDir("../testdata/repos")
  2240. require.NoError(t, err)
  2241. for _, dir := range repoDirs {
  2242. if to == ".git" {
  2243. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), "dotGit"))
  2244. if os.IsNotExist(err) {
  2245. // dont want to delete the only copy of .git accidentally
  2246. continue
  2247. }
  2248. _ = os.RemoveAll(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), ".git"))
  2249. }
  2250. if !dir.IsDir() {
  2251. continue
  2252. }
  2253. _, err := os.Stat(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from))
  2254. if os.IsNotExist(err) {
  2255. continue
  2256. }
  2257. err = os.Rename(fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), from),
  2258. fmt.Sprintf("%s/%s/%s", repoBasePath, dir.Name(), to))
  2259. require.NoError(t, err)
  2260. }
  2261. }
  2262. // region Windows-specific tests[]
  2263. func TestNormalizeGitleaksIgnorePaths(t *testing.T) {
  2264. d, err := NewDetectorDefaultConfig()
  2265. require.NoError(t, err)
  2266. err = d.AddGitleaksIgnore("../testdata/gitleaksignore/.windowspaths")
  2267. require.NoError(t, err)
  2268. assert.Len(t, d.gitleaksIgnore, 3)
  2269. expected := map[string]struct{}{
  2270. "foo/bar/gitleaks-false-positive.yaml:aws-access-token:4": {},
  2271. "foo/bar/gitleaks-false-positive.yaml:aws-access-token:5": {},
  2272. "b55d88dc151f7022901cda41a03d43e0e508f2b7:test_data/test_local_repo_three_leaks.json:aws-access-token:73": {},
  2273. }
  2274. assert.ElementsMatch(t, maps.Keys(d.gitleaksIgnore), maps.Keys(expected))
  2275. }
  2276. func TestWindowsFileSeparator_RulePath(t *testing.T) {
  2277. unixRule := config.Rule{
  2278. RuleID: "test-rule",
  2279. Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
  2280. }
  2281. windowsRule := config.Rule{
  2282. RuleID: "test-rule",
  2283. Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
  2284. }
  2285. expected := []report.Finding{
  2286. {
  2287. RuleID: "test-rule",
  2288. Match: "file detected: .m2/settings.xml",
  2289. File: ".m2/settings.xml",
  2290. },
  2291. }
  2292. tests := map[string]struct {
  2293. fragment Fragment
  2294. rule config.Rule
  2295. expected []report.Finding
  2296. }{
  2297. // unix rule
  2298. "unix rule - unix path separator": {
  2299. fragment: Fragment{
  2300. FilePath: `.m2/settings.xml`,
  2301. },
  2302. rule: unixRule,
  2303. expected: expected,
  2304. },
  2305. "unix rule - windows path separator": {
  2306. fragment: Fragment{
  2307. FilePath: `.m2/settings.xml`,
  2308. WindowsFilePath: `.m2\settings.xml`,
  2309. },
  2310. rule: unixRule,
  2311. expected: expected,
  2312. },
  2313. "unix regex+path rule - windows path separator": {
  2314. fragment: Fragment{
  2315. Raw: `<password>s3cr3t</password>`,
  2316. FilePath: `.m2/settings.xml`,
  2317. },
  2318. rule: config.Rule{
  2319. RuleID: "test-rule",
  2320. Regex: regexp.MustCompile(`<password>(.+?)</password>`),
  2321. Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
  2322. },
  2323. expected: []report.Finding{
  2324. {
  2325. RuleID: "test-rule",
  2326. StartColumn: 1,
  2327. EndColumn: 27,
  2328. Line: "<password>s3cr3t</password>",
  2329. Match: "<password>s3cr3t</password>",
  2330. Secret: "s3cr3t",
  2331. Entropy: 2.251629114151001,
  2332. File: ".m2/settings.xml",
  2333. },
  2334. },
  2335. },
  2336. // windows rule
  2337. "windows rule - unix path separator": {
  2338. fragment: Fragment{
  2339. FilePath: `.m2/settings.xml`,
  2340. },
  2341. rule: windowsRule,
  2342. // This never worked, and continues not to work.
  2343. // Paths should be normalized to use Unix file separators.
  2344. expected: nil,
  2345. },
  2346. "windows rule - windows path separator": {
  2347. fragment: Fragment{
  2348. FilePath: `.m2/settings.xml`,
  2349. WindowsFilePath: `.m2\settings.xml`,
  2350. },
  2351. rule: windowsRule,
  2352. expected: expected,
  2353. },
  2354. "windows regex+path rule - windows path separator": {
  2355. fragment: Fragment{
  2356. Raw: `<password>s3cr3t</password>`,
  2357. FilePath: `.m2/settings.xml`,
  2358. WindowsFilePath: `.m2\settings.xml`,
  2359. },
  2360. rule: config.Rule{
  2361. RuleID: "test-rule",
  2362. Regex: regexp.MustCompile(`<password>(.+?)</password>`),
  2363. Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
  2364. },
  2365. expected: []report.Finding{
  2366. {
  2367. RuleID: "test-rule",
  2368. StartColumn: 1,
  2369. EndColumn: 27,
  2370. Line: "<password>s3cr3t</password>",
  2371. Match: "<password>s3cr3t</password>",
  2372. Secret: "s3cr3t",
  2373. Entropy: 2.251629114151001,
  2374. File: ".m2/settings.xml",
  2375. },
  2376. }},
  2377. }
  2378. d, err := NewDetectorDefaultConfig()
  2379. require.NoError(t, err)
  2380. for name, test := range tests {
  2381. t.Run(name, func(t *testing.T) {
  2382. actual := d.detectRule(test.fragment, [][]int{}, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
  2383. if diff := cmp.Diff(test.expected, actual); diff != "" {
  2384. t.Errorf("diff: (-want +got)\n%s", diff)
  2385. }
  2386. })
  2387. }
  2388. }
  2389. func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
  2390. tests := map[string]struct {
  2391. fragment Fragment
  2392. rule config.Rule
  2393. expected []report.Finding
  2394. }{
  2395. // unix
  2396. "unix path separator - unix rule - OR allowlist path-only": {
  2397. fragment: Fragment{
  2398. Raw: `value: "s3cr3t"`,
  2399. FilePath: `ignoreme/unix.txt`,
  2400. },
  2401. rule: config.Rule{
  2402. RuleID: "unix-rule",
  2403. Regex: regexp.MustCompile(`s3cr3t`),
  2404. Allowlists: []*config.Allowlist{
  2405. {
  2406. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  2407. },
  2408. },
  2409. },
  2410. expected: nil,
  2411. },
  2412. "unix path separator - windows rule - OR allowlist path-only": {
  2413. fragment: Fragment{
  2414. Raw: `value: "s3cr3t"`,
  2415. FilePath: `ignoreme/unix.txt`,
  2416. },
  2417. rule: config.Rule{
  2418. RuleID: "windows-rule",
  2419. Regex: regexp.MustCompile(`s3cr3t`),
  2420. Allowlists: []*config.Allowlist{
  2421. {
  2422. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  2423. },
  2424. },
  2425. },
  2426. // Windows separators in regex don't work for unix.
  2427. expected: []report.Finding{
  2428. {
  2429. RuleID: "windows-rule",
  2430. StartColumn: 9,
  2431. EndColumn: 14,
  2432. Line: `value: "s3cr3t"`,
  2433. Match: `s3cr3t`,
  2434. Secret: `s3cr3t`,
  2435. File: "ignoreme/unix.txt",
  2436. Entropy: 2.251629114151001,
  2437. },
  2438. },
  2439. },
  2440. "unix path separator - unix rule - AND allowlist path+stopwords": {
  2441. fragment: Fragment{
  2442. Raw: `value: "f4k3s3cr3t"`,
  2443. FilePath: `ignoreme/unix.txt`,
  2444. },
  2445. rule: config.Rule{
  2446. RuleID: "unix-rule",
  2447. Regex: regexp.MustCompile(`value: "[^"]+"`),
  2448. Allowlists: []*config.Allowlist{
  2449. {
  2450. MatchCondition: config.AllowlistMatchAnd,
  2451. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  2452. StopWords: []string{"f4k3"},
  2453. },
  2454. },
  2455. },
  2456. expected: nil,
  2457. },
  2458. "unix path separator - windows rule - AND allowlist path+stopwords": {
  2459. fragment: Fragment{
  2460. Raw: `value: "f4k3s3cr3t"`,
  2461. FilePath: `ignoreme/unix.txt`,
  2462. },
  2463. rule: config.Rule{
  2464. RuleID: "windows-rule",
  2465. Regex: regexp.MustCompile(`value: "[^"]+"`),
  2466. Allowlists: []*config.Allowlist{
  2467. {
  2468. MatchCondition: config.AllowlistMatchAnd,
  2469. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  2470. StopWords: []string{"f4k3"},
  2471. },
  2472. },
  2473. },
  2474. expected: []report.Finding{
  2475. {
  2476. RuleID: "windows-rule",
  2477. StartColumn: 1,
  2478. EndColumn: 19,
  2479. Line: `value: "f4k3s3cr3t"`,
  2480. Match: `value: "f4k3s3cr3t"`,
  2481. Secret: `value: "f4k3s3cr3t"`,
  2482. File: "ignoreme/unix.txt",
  2483. Entropy: 3.892407178878784,
  2484. },
  2485. },
  2486. },
  2487. // windows
  2488. "windows path separator - unix rule - OR allowlist path-only": {
  2489. fragment: Fragment{
  2490. Raw: `value: "s3cr3t"`,
  2491. FilePath: `ignoreme/windows.txt`,
  2492. WindowsFilePath: `ignoreme\windows.txt`,
  2493. },
  2494. rule: config.Rule{
  2495. RuleID: "unix-rule",
  2496. Regex: regexp.MustCompile(`s3cr3t`),
  2497. Allowlists: []*config.Allowlist{
  2498. {
  2499. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  2500. },
  2501. },
  2502. },
  2503. expected: nil,
  2504. },
  2505. "windows path separator - windows rule - OR allowlist path-only": {
  2506. fragment: Fragment{
  2507. Raw: `value: "s3cr3t"`,
  2508. FilePath: `ignoreme/windows.txt`,
  2509. WindowsFilePath: `ignoreme\windows.txt`,
  2510. },
  2511. rule: config.Rule{
  2512. RuleID: "windows-rule",
  2513. Regex: regexp.MustCompile(`s3cr3t`),
  2514. Allowlists: []*config.Allowlist{
  2515. {
  2516. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  2517. },
  2518. },
  2519. },
  2520. expected: nil,
  2521. },
  2522. "windows path separator - unix rule - AND allowlist path+stopwords": {
  2523. fragment: Fragment{
  2524. Raw: `value: "f4k3s3cr3t"`,
  2525. FilePath: `ignoreme/unix.txt`,
  2526. WindowsFilePath: `ignoreme\windows.txt`,
  2527. },
  2528. rule: config.Rule{
  2529. RuleID: "unix-rule",
  2530. Regex: regexp.MustCompile(`value: "[^"]+"`),
  2531. Allowlists: []*config.Allowlist{
  2532. {
  2533. MatchCondition: config.AllowlistMatchAnd,
  2534. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
  2535. StopWords: []string{"f4k3"},
  2536. },
  2537. },
  2538. },
  2539. expected: nil,
  2540. },
  2541. "windows path separator - windows rule - AND allowlist path+stopwords": {
  2542. fragment: Fragment{
  2543. Raw: `value: "f4k3s3cr3t"`,
  2544. FilePath: `ignoreme/unix.txt`,
  2545. WindowsFilePath: `ignoreme\windows.txt`,
  2546. },
  2547. rule: config.Rule{
  2548. RuleID: "windows-rule",
  2549. Regex: regexp.MustCompile(`value: "[^"]+"`),
  2550. Allowlists: []*config.Allowlist{
  2551. {
  2552. MatchCondition: config.AllowlistMatchAnd,
  2553. Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
  2554. StopWords: []string{"f4k3"},
  2555. },
  2556. },
  2557. },
  2558. expected: nil,
  2559. },
  2560. }
  2561. d, err := NewDetectorDefaultConfig()
  2562. require.NoError(t, err)
  2563. for name, test := range tests {
  2564. t.Run(name, func(t *testing.T) {
  2565. actual := d.detectRule(test.fragment, [][]int{}, test.fragment.Raw, test.rule, []*codec.EncodedSegment{})
  2566. if diff := cmp.Diff(test.expected, actual); diff != "" {
  2567. t.Errorf("diff: (-want +got)\n%s", diff)
  2568. }
  2569. })
  2570. }
  2571. }
  2572. //endregion