detect_test.go 90 KB

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