Skip to content

Commit

Permalink
Improve PlanetScale token detection (#874)
Browse files Browse the repository at this point in the history
This improves the PlanetScale token detection. It add some flexibility
in length. There is no guarantee that the length is always 43 characters
(in fact, it's very likely to change a bit soon).

Additionally, it adds support for detecting oauth tokens as well.
  • Loading branch information
dbussink committed May 25, 2022
1 parent d6c0de7 commit 865478b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/generate/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func main() {
configRules = append(configRules, rules.NewRelicBrowserAPIKey())
configRules = append(configRules, rules.NPM())
configRules = append(configRules, rules.PlanetScalePassword())
configRules = append(configRules, rules.PlanetScaleToken())
configRules = append(configRules, rules.PlanetScaleAPIToken())
configRules = append(configRules, rules.PlanetScaleOAuthToken())
configRules = append(configRules, rules.PostManAPI())
configRules = append(configRules, rules.PrivateKey())
configRules = append(configRules, rules.PulumiAPIToken())
Expand Down
31 changes: 28 additions & 3 deletions cmd/generate/config/rules/planetscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func PlanetScalePassword() *config.Rule {
r := config.Rule{
RuleID: "planetscale-password",
Description: "PlanetScale password",
Regex: generateUniqueTokenRegex(`pscale_pw_(?i)[a-z0-9=\-_\.]{43}`),
Regex: generateUniqueTokenRegex(`pscale_pw_(?i)[a-z0-9=\-_\.]{32,64}`),
SecretGroup: 1,
Keywords: []string{
"pscale_pw_",
Expand All @@ -19,17 +19,19 @@ func PlanetScalePassword() *config.Rule {

// validate
tps := []string{
generateSampleSecret("planetScalePassword", "pscale_pw_"+secrets.NewSecret(alphaNumericExtended("32"))),
generateSampleSecret("planetScalePassword", "pscale_pw_"+secrets.NewSecret(alphaNumericExtended("43"))),
generateSampleSecret("planetScalePassword", "pscale_pw_"+secrets.NewSecret(alphaNumericExtended("64"))),
}
return validate(r, tps, nil)
}

func PlanetScaleToken() *config.Rule {
func PlanetScaleAPIToken() *config.Rule {
// define rule
r := config.Rule{
RuleID: "planetscale-api-token",
Description: "PlanetScale API token",
Regex: generateUniqueTokenRegex(`pscale_tkn_(?i)[a-z0-9=\-_\.]{43}`),
Regex: generateUniqueTokenRegex(`pscale_tkn_(?i)[a-z0-9=\-_\.]{32,64}`),
SecretGroup: 1,
Keywords: []string{
"pscale_tkn_",
Expand All @@ -38,7 +40,30 @@ func PlanetScaleToken() *config.Rule {

// validate
tps := []string{
generateSampleSecret("planetScalePassword", "pscale_tkn_"+secrets.NewSecret(alphaNumericExtended("32"))),
generateSampleSecret("planetScalePassword", "pscale_tkn_"+secrets.NewSecret(alphaNumericExtended("43"))),
generateSampleSecret("planetScalePassword", "pscale_tkn_"+secrets.NewSecret(alphaNumericExtended("64"))),
}
return validate(r, tps, nil)
}

func PlanetScaleOAuthToken() *config.Rule {
// define rule
r := config.Rule{
RuleID: "planetscale-oauth-token",
Description: "PlanetScale OAuth token",
Regex: generateUniqueTokenRegex(`pscale_oauth_(?i)[a-z0-9=\-_\.]{32,64}`),
SecretGroup: 1,
Keywords: []string{
"pscale_oauth_",
},
}

// validate
tps := []string{
generateSampleSecret("planetScalePassword", "pscale_oauth_"+secrets.NewSecret(alphaNumericExtended("32"))),
generateSampleSecret("planetScalePassword", "pscale_oauth_"+secrets.NewSecret(alphaNumericExtended("43"))),
generateSampleSecret("planetScalePassword", "pscale_oauth_"+secrets.NewSecret(alphaNumericExtended("64"))),
}
return validate(r, tps, nil)
}
13 changes: 11 additions & 2 deletions config/gitleaks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ keywords = [
[[rules]]
description = "PlanetScale password"
id = "planetscale-password"
regex = '''(?i)\b(pscale_pw_(?i)[a-z0-9=\-_\.]{43})(?:['|\"|\n|\r|\s|\x60]|$)'''
regex = '''(?i)\b(pscale_pw_(?i)[a-z0-9=\-_\.]{32,64})(?:['|\"|\n|\r|\s|\x60]|$)'''
secretGroup = 1
keywords = [
"pscale_pw_",
Expand All @@ -554,12 +554,21 @@ keywords = [
[[rules]]
description = "PlanetScale API token"
id = "planetscale-api-token"
regex = '''(?i)\b(pscale_tkn_(?i)[a-z0-9=\-_\.]{43})(?:['|\"|\n|\r|\s|\x60]|$)'''
regex = '''(?i)\b(pscale_tkn_(?i)[a-z0-9=\-_\.]{32,64})(?:['|\"|\n|\r|\s|\x60]|$)'''
secretGroup = 1
keywords = [
"pscale_tkn_",
]

[[rules]]
description = "PlanetScale OAuth token"
id = "planetscale-oauth-token"
regex = '''(?i)\b(pscale_oauth_(?i)[a-z0-9=\-_\.]{32,64})(?:['|\"|\n|\r|\s|\x60]|$)'''
secretGroup = 1
keywords = [
"pscale_oauth_",
]

[[rules]]
description = "Postman API token"
id = "postman-api-token"
Expand Down

0 comments on commit 865478b

Please sign in to comment.