Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions awareness/perception/entropy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var sriPrefixes = []string{"sha1-", "sha256-", "sha384-", "sha512-"}
// highEntropy records standalone tokens of 32 or more characters from
// [A-Za-z0-9+/_\-=.] that mix at least three of lower case, upper case,
// digits and symbols and carry at least 4.5 bits of entropy per character.
// Hashes after a digest prefix, SRI hashes, pure hex, UUIDs, numbers, tokens
// Hashes after a digest prefix, SRI hashes, pure hex, UUIDs, TypeIDs, numbers, tokens
// made of words (identifiers, import paths), Kubernetes-style names and SSH
// public keys are never flagged. A URL or path is checked one segment at a
// time. A base64 token is judged by what it decodes to (see encodedSecret).
Expand Down Expand Up @@ -106,7 +106,7 @@ func entropyToken(c *collector, start, end int) {
return
}
}
if len(tok) < 32 || afterDigestPrefix(s, start) || afterSSHKeyType(s, start) || isHex(tok) || isNumber(tok) || isUUID(tok) {
if len(tok) < 32 || afterDigestPrefix(s, start) || afterSSHKeyType(s, start) || isHex(tok) || isNumber(tok) || isUUID(tok) || isTypeID(tok) {
return
}
if isPathLike(tok) {
Expand Down Expand Up @@ -304,6 +304,32 @@ func isUUID(v string) bool {
return true
}

// isTypeID reports whether v is a TypeID such as user_01h455vb4pex5vsknk084sn02q:
// a prefix of up to 63 lower-case letters and '_' that starts and ends with a
// letter, '_', then a UUID in 26 characters of lower-case Crockford base32,
// the first of them 0 to 7.
func isTypeID(v string) bool {
k := strings.LastIndexByte(v, '_')
if k < 1 || k > 63 || len(v)-k-1 != 26 {
return false
}
prefix, suffix := v[:k], v[k+1:]
if suffix[0] > '7' || prefix[0] == '_' || prefix[k-1] == '_' {
return false
}
for i := range len(prefix) {
if b := prefix[i]; b != '_' && (b < 'a' || b > 'z') {
return false
}
}
for i := range len(suffix) {
if strings.IndexByte("0123456789abcdefghjkmnpqrstvwxyz", suffix[i]) < 0 {
return false
}
}
return true
}

// charClasses counts which of lower case, upper case, digits and symbols v
// holds.
func charClasses(v string) int {
Expand Down
26 changes: 26 additions & 0 deletions awareness/perception/perception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
digits = "0123456789"
stdB64 = alnum + "+/"
urlB64 = alnum + "-_"
crockford = "0123456789abcdefghjkmnpqrstvwxyz"
)

// fake returns n characters drawn from alphabet by a fixed generator, so the
Expand Down Expand Up @@ -346,6 +347,31 @@ func TestBenignInfraTokensAreNotSecrets(t *testing.T) {
}
}

func TestTypeIDsAreNotSecrets(t *testing.T) {
// A TypeID's suffix is a UUID in base32, but with a prefix of five or
// more letters the whole ID is long enough to be checked.
for _, text := range []string{
"task atask_01m71vem2ge808nc95ad4hj3bq finished",
"release atask_0" + fake(167, 25, crockford),
`{"id":"provider_key_3` + fake(53, 25, crockford) + `"}`,
} {
if got := perception.Secrets(text, nil); len(got) != 0 {
t.Errorf("Secrets(%q) = %+v, want none", text, got)
}
}
// Close to a TypeID but not one, so still checked.
for _, text := range []string{
"atask_8" + fake(88, 25, crockford), // first suffix character past 7
"aTask_0" + fake(53, 25, crockford), // upper case in the prefix
"atask_0" + fake(53, 24, crockford) + "u", // u is not in the alphabet
"_atask_0" + fake(167, 25, crockford), // prefix starts with '_'
} {
if got := perception.Secrets(text, nil); len(got) != 1 {
t.Errorf("Secrets(%q) = %+v, want one match", text, got)
}
}
}

func TestEncodedSecretIsStillFlagged(t *testing.T) {
encoded := base64.StdEncoding.EncodeToString([]byte("GITHUB_TOKEN=ghp_" + fake(3, 36, alnum)))
got := perception.Secrets("data: "+encoded, nil)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.19.1 // indirect
github.com/klauspost/compress v1.20.2 // indirect
github.com/leodido/go-urn v1.5.0 // indirect
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/compress v1.20.2 h1:v5tiZvm1KGLyg5d5oXdXe3lsSPrbEUiKoXGia//I7Qo=
github.com/klauspost/compress v1.20.2/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
Loading