mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04:22 +01:00
[progress] stricter regex, tests
This commit is contained in:
1 parent
595da56eef
commit
ec654441bb
3 files changed
+44
-21
No files matched your search
@@ -1,26 +1,28 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
_ "fmt"
|
_ "fmt"
|
||||||
"regexp"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkRegex(diff []byte) {
|
func checkRegex(diff string) ([]string, bool) {
|
||||||
var re *regexp.Regexp
|
var match string
|
||||||
var found string
|
var results []string
|
||||||
lines := bytes.Split(diff, []byte("\n"))
|
secretsPresent := false
|
||||||
|
lines := strings.Split(diff, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range regexes {
|
for _, re := range regexes {
|
||||||
re = regexp.MustCompile(v)
|
match = re.FindString(line)
|
||||||
found = re.FindString(string(line))
|
if len(match) == 0 {
|
||||||
if len(found) == 0 {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
secretsPresent = true
|
||||||
|
results = append(results, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return results, secretsPresent
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckRegex(t *testing.T) {
|
||||||
|
var secretsPresent bool
|
||||||
|
checks := map[string]bool{
|
||||||
|
"github.com": false,
|
||||||
|
"github.com/user/": false,
|
||||||
|
"github_api_client = \"sample key\"": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range checks {
|
||||||
|
_, secretsPresent = checkRegex(k)
|
||||||
|
if v != secretsPresent {
|
||||||
|
t.Errorf("regexCheck failed on string %s. Expected secretsPresent: %t, got %t", k, v, secretsPresent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,20 +22,19 @@ type Repo struct {
|
|||||||
// memoization for commit1+commit2 hash
|
// memoization for commit1+commit2 hash
|
||||||
var cache map[string]bool
|
var cache map[string]bool
|
||||||
var appRoot string
|
var appRoot string
|
||||||
var regexes map[string]string
|
var regexes map[string]*regexp.Regexp
|
||||||
var strictRegexes map[string]string
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
appRoot, _ = os.Getwd()
|
appRoot, _ = os.Getwd()
|
||||||
cache = make(map[string]bool)
|
cache = make(map[string]bool)
|
||||||
regexes = map[string]string{
|
regexes = map[string]*regexp.Regexp{
|
||||||
"github": "[g|G][i|I][t|T][h|H][u|U][b|B].*",
|
"github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].+[=|:=|:|<-].*\w+.*`),
|
||||||
"aws": "[a|A][w|W][s|S].*",
|
"aws": regexp.MustCompile(`[a|A][w|W][s|S].+[=|:=|:|<-].*\w+.*`),
|
||||||
"heroku": "[h|H][e|E][r|R][o|O][k|K][u|U].*",
|
"heroku": regexp.MustCompile(`[h|H][e|E][r|R][o|O][k|K][u|U].+[=|:=|:|<-].*\w+.*`),
|
||||||
"facebook": "[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*",
|
"facebook": regexp.MustCompile(`[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].+[=|:=|:|<-].*\w+.*`),
|
||||||
"twitter": "[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*",
|
"twitter": regexp.MustCompile(`[t|T][w|W][i|I][t|T][t|T][e|E][r|R].+[=|:=|:|<-].*\w+.*`),
|
||||||
"reddit": "[r|R][e|E][d|D][d|D][i|I][t|T].*",
|
"reddit": regexp.MustCompile(`[r|R][e|E][d|D][d|D][i|I][t|T].+[=|:=|:|<-].*\w+.*`),
|
||||||
"twilio": "[t|T][w|W][i|I][l|L][i|I][o|O].*",
|
"twilio": regexp.MustCompile(`[t|T][w|W][i|I][l|L][i|I][o|O].+[=|:=|:|<-].*\w+.*`),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,5 +123,5 @@ func diff(commit1 string, commit2 string) {
|
|||||||
log.Fatalf("error retrieving commits %v\n", err)
|
log.Fatalf("error retrieving commits %v\n", err)
|
||||||
}
|
}
|
||||||
cache[commit1+commit2] = true
|
cache[commit1+commit2] = true
|
||||||
checkRegex(out)
|
checkRegex(string(out))
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user