detect_test.go 93 KB

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