detect_test.go 93 KB

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