diff --git a/config/gitleaks.toml b/config/gitleaks.toml index b46fc601..ae159ef3 100644 --- a/config/gitleaks.toml +++ b/config/gitleaks.toml @@ -75,7 +75,7 @@ title = "gitleaks config" [[rules]] description = "Google API key" - regex = '''AIza[0-9A-Za-z\\-_]{35}''' + regex = '''AIza[0-9A-Za-z\-_]{35}''' tags = ["key", "Google"] [[rules]] @@ -130,7 +130,7 @@ title = "gitleaks config" [[rules]] description = "Square OAuth secret" - regex = '''sq0csp-[0-9A-Za-z\\-_]{43}''' + regex = '''sq0csp-[0-9A-Za-z\-_]{43}''' tags = ["key", "square"] [[rules]] diff --git a/scan/scan_test.go b/scan/scan_test.go index af3ffe77..f98f4085 100644 --- a/scan/scan_test.go +++ b/scan/scan_test.go @@ -5,13 +5,71 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" + "testing" + "github.com/zricethezav/gitleaks/v7/config" + "github.com/zricethezav/gitleaks/v7/options" "github.com/zricethezav/gitleaks/v7/scan" ) const repoBasePath = "../testdata/repos/" const expectPath = "../testdata/expect/" +func TestScan(t *testing.T) { + err := moveDotGit("dotGit", ".git") + if err != nil { + t.Fatal(err) + } + defer moveDotGit(".git", "dotGit") + tests := []struct { + description string + opts options.Options + wantPath string + }{ + { + description: "test google api key leak AND square oauth leak", + opts: options.Options{ + Path: filepath.Join(repoBasePath, "with_square_and_google"), + Report: filepath.Join(expectPath, "results_square_and_google.json.got"), + ReportFormat: "json", + NoGit: true, + }, + wantPath: filepath.Join(expectPath, "results_square_and_google.json"), + }, + } + + for _, test := range tests { + cfg, err := config.NewConfig(test.opts) + if err != nil { + t.Error(err) + } + + scanner, err := scan.NewScanner(test.opts, cfg) + if err != nil { + t.Error(test.description, err) + } + + scannerReport, err := scanner.Scan() + if err != nil { + t.Fatal(test.description, err) + } + + err = scan.WriteReport(scannerReport, test.opts, cfg) + if err != nil { + t.Error(test.description, err) + } + + if test.wantPath != "" { + err := fileCheck(test.wantPath, test.opts.Report) + if err != nil { + t.Error(test.description, err) + } + } + } +} + + func moveDotGit(from, to string) error { repoDirs, err := ioutil.ReadDir("../testdata/repos") if err != nil { diff --git a/testdata/expect/results_square_and_google.json b/testdata/expect/results_square_and_google.json new file mode 100644 index 00000000..cb4eb956 --- /dev/null +++ b/testdata/expect/results_square_and_google.json @@ -0,0 +1,36 @@ +[ + { + "line": "SQUARE_OAUTH=sq0csp-QZxqK3fHycIxFsjZ7_HoqRN08qY2by3wLXcfJCfsKEg", + "lineNumber": 1, + "offender": "sq0csp-QZxqK3fHycIxFsjZ7_HoqRN08qY2by3wLXcfJCfsKEg", + "offenderEntropy": -1, + "commit": "", + "repo": "", + "repoURL": "", + "leakURL": "", + "rule": "Square OAuth secret", + "commitMessage": "", + "author": "", + "email": "", + "file": ".", + "date": "0001-01-01T00:00:00Z", + "tags": "key, square" + }, + { + "line": "GOOGLE_MAPS_KEY=AIzaSyDtz3Di0ZrzeU5ET3UFU0p-xP4bx2FkSrg", + "lineNumber": 3, + "offender": "AIzaSyDtz3Di0ZrzeU5ET3UFU0p-xP4bx2FkSrg", + "offenderEntropy": -1, + "commit": "", + "repo": "", + "repoURL": "", + "leakURL": "", + "rule": "Google API key", + "commitMessage": "", + "author": "", + "email": "", + "file": ".", + "date": "0001-01-01T00:00:00Z", + "tags": "key, Google" + } + ] \ No newline at end of file diff --git a/testdata/repos/with_square_and_google/env b/testdata/repos/with_square_and_google/env new file mode 100644 index 00000000..db967dae --- /dev/null +++ b/testdata/repos/with_square_and_google/env @@ -0,0 +1,3 @@ +SQUARE_OAUTH=sq0csp-QZxqK3fHycIxFsjZ7_HoqRN08qY2by3wLXcfJCfsKEg + +GOOGLE_MAPS_KEY=AIzaSyDtz3Di0ZrzeU5ET3UFU0p-xP4bx2FkSrg