From 9326f35380636bcbe61e94b0584d1618c4b5c2c2 Mon Sep 17 00:00:00 2001 From: Isaac Dawson <60455448+idawson-gl@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:33:06 +0900 Subject: [PATCH] Escape - character in regex character groups (#802) * fix char escape * add test * fix verbosity in make test --- Makefile | 4 ++-- config/gitleaks.toml | 16 +++++----------- detect/detect_test.go | 19 +++++++++++++++++++ testdata/config/escaped_character_group.toml | 8 ++++++++ testdata/repos/small/dotGit/index | Bin 317 -> 317 bytes 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 testdata/config/escaped_character_group.toml diff --git a/Makefile b/Makefile index 4f60007d..f53e3796 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LDFLAGS=-ldflags "-X=github.com/zricethezav/gitleaks/v8/cmd.Version=$(VERSION)" COVER=--cover --coverprofile=cover.out test-cover: - go test ./... --race $(COVER) $(PKG) -v + go test -v ./... --race $(COVER) $(PKG) go tool cover -html=cover.out format: @@ -14,7 +14,7 @@ format: test: format go vet ./... - go test ./... --race $(PKG) -v + go test -v ./... --race $(PKG) build: format go vet ./... diff --git a/config/gitleaks.toml b/config/gitleaks.toml index 843f3cfb..3047e5c5 100644 --- a/config/gitleaks.toml +++ b/config/gitleaks.toml @@ -124,7 +124,7 @@ regex = '''(?i)(sk|pk)_(test|live)_[0-9a-z]{10,32}''' [[rules]] id = "pypi-upload-token" description = "PyPI upload token" -regex = '''pypi-AgEIcHlwaS5vcmc[A-Za-z0-9-_]{50,1000}''' +regex = '''pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,1000}''' [[rules]] id = "gcp-service-account" @@ -233,12 +233,6 @@ description = "Contentful delivery API token" regex = '''(?i)(contentful[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9\-=_]{43})['\"]''' secretGroup = 3 -[[rules]] -id = "contentful-preview-api-token" -description = "Contentful preview API token" -regex = '''(?i)(contentful[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-z0-9\-=_]{43})['\"]''' -secretGroup = 3 - [[rules]] id = "databricks-api-token" description = "Databricks API token" @@ -343,22 +337,22 @@ regex = '''FLWSECK_TEST[a-h0-9]{12}''' [[rules]] id = "frameio-api-token" description = "Frame.io API token" -regex = '''fio-u-(?i)[a-z0-9-_=]{64}''' +regex = '''fio-u-(?i)[a-z0-9\-_=]{64}''' [[rules]] id = "gocardless-api-token" description = "GoCardless API token" -regex = '''['\"]live_(?i)[a-z0-9-_=]{40}['\"]''' +regex = '''['\"]live_(?i)[a-z0-9\-_=]{40}['\"]''' [[rules]] id = "grafana-api-token" description = "Grafana API token" -regex = '''['\"]eyJrIjoi(?i)[a-z0-9-_=]{72,92}['\"]''' +regex = '''['\"]eyJrIjoi(?i)[a-z0-9\-_=]{72,92}['\"]''' [[rules]] id = "hashicorp-tf-api-token" description = "HashiCorp Terraform user/org API token" -regex = '''['\"](?i)[a-z0-9]{14}\.atlasv1\.[a-z0-9-_=]{60,70}['\"]''' +regex = '''['\"](?i)[a-z0-9]{14}\.atlasv1\.[a-z0-9\-_=]{60,70}['\"]''' [[rules]] id = "hubspot-api-token" diff --git a/detect/detect_test.go b/detect/detect_test.go index 89834efb..496de7c0 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -22,6 +22,25 @@ func TestDetectFindings(t *testing.T) { expectedFindings []report.Finding wantError error }{ + { + cfgName: "escaped_character_group", + bytes: []byte(`pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB`), + filePath: "tmp.go", + expectedFindings: []report.Finding{ + { + Description: "PyPI upload token", + Secret: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB", + Match: "pypi-AgEIcHlwaS5vcmcAAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAA-AAAAAAAAAAB", + File: "tmp.go", + RuleID: "pypi-upload-token", + Tags: []string{"key", "pypi"}, + StartLine: 1, + EndLine: 1, + StartColumn: 1, + EndColumn: 86, + }, + }, + }, { cfgName: "simple", bytes: []byte(`awsToken := \"AKIALALEMEL33243OLIA\"`), diff --git a/testdata/config/escaped_character_group.toml b/testdata/config/escaped_character_group.toml new file mode 100644 index 00000000..b2803953 --- /dev/null +++ b/testdata/config/escaped_character_group.toml @@ -0,0 +1,8 @@ +title = "gitleaks config" +# https://learnxinyminutes.com/docs/toml/ for toml reference + +[[rules]] + id = "pypi-upload-token" + description = "PyPI upload token" + regex = '''pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,1000}''' + tags = ["key", "pypi"] \ No newline at end of file diff --git a/testdata/repos/small/dotGit/index b/testdata/repos/small/dotGit/index index 812b2d3b149c4ae371acf3af9eebc976484df1c0..fec9889ae79cb0af45c8665b198e56dd8d7c80d4 100644 GIT binary patch delta 116 zcmdnXw3o@i#WTp6fq{Vuh?$dA3uY?5+!PI?85tP(8C(uLW?*Ps!oa}z6{tpJqJgS1 rNZIStpTNq1G*sDB6lE0~MI3g%Nc;F~y71DMZwh;kA75_xVZ(U