Skip to content

Conversation

rgmz
Copy link
Contributor

@rgmz rgmz commented Sep 23, 2024

Description:

This fixes #1523.

Checklist:

  • Does your PR pass tests?
  • Have you written new tests for your changes?
  • Have you lint your code locally prior to submission?

@rgmz

This comment was marked as outdated.

@rgmz rgmz force-pushed the fix/extend-validate branch 2 times, most recently from d825dd8 to 3e99a86 Compare September 23, 2024 21:15
@rgmz rgmz marked this pull request as ready for review September 23, 2024 21:16
@rgmz rgmz force-pushed the fix/extend-validate branch from 3e99a86 to 87bec7b Compare September 23, 2024 21:17
c.OrderedRules = append(c.OrderedRules, ruleID)
} else {
// Rule exists, merge our changes into the base.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, ty for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there are any other fields we should append or overwrite?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding RegexTarget as well:

diff --git a/config/config.go b/config/config.go
index caea22f..34e5f98 100644
--- a/config/config.go
+++ b/config/config.go
@@ -252,6 +252,7 @@ func (c *Config) extend(extensionConfig Config) {
                        baseRule.Allowlist.Commits = append(baseRule.Allowlist.Commits, currentRule.Allowlist.Commits...)
                        baseRule.Allowlist.Paths = append(baseRule.Allowlist.Paths, currentRule.Allowlist.Paths...)
                        baseRule.Allowlist.Regexes = append(baseRule.Allowlist.Regexes, currentRule.Allowlist.Regexes...)
+                       baseRule.Allowlist.RegexTarget = currentRule.Allowlist.RegexTarget
                        baseRule.Allowlist.StopWords = append(baseRule.Allowlist.StopWords, currentRule.Allowlist.StopWords...)
 
                        delete(c.Rules, ruleID)

I have a case where I must use line and the change above would enable extend for us.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just made #1536 to cover that case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this didn't get caught earlier; I believe this makes it impossible to use most of the rule attributes documented in the README:

# An array of tables that contain information that define instructions
# on how to detect secrets
[[rules]]

# Unique identifier for this rule
id = "awesome-rule-1"

# Short human readable description of the rule.
description = "awesome rule 1"

# Golang regular expression used to detect secrets. Note Golang's regex engine
# does not support lookaheads.
regex = '''one-go-style-regex-for-this-rule'''

# Golang regular expression used to match paths. This can be used as a standalone rule or it can be used
# in conjunction with a valid `regex` entry.
path = '''a-file-path-regex'''

# Array of strings used for metadata and reporting purposes.
tags = ["tag","another tag"]

# Int used to extract secret from regex match and used as the group that will have
# its entropy checked if `entropy` is set.
secretGroup = 3

# Float representing the minimum shannon entropy a regex group must have to be considered a secret.
entropy = 3.5

https://github.com/gitleaks/gitleaks?tab=readme-ov-file#configuration

@zricethezav zricethezav merged commit ed19c4e into gitleaks:master Sep 25, 2024
1 check passed
@rgmz rgmz deleted the fix/extend-validate branch September 25, 2024 13:20
@9999years
Copy link
Contributor

Hello, this broke the ability to override the entropy of rules.

With .gitleaks.toml:

[extend]
useDefault = true

[[rules]]
id = "finicity-client-secret"
entropy = 4
$ echo "newtype FinicityDataResponse = FinicityDataResponse" | gitleaks --config=.gitleaks.toml --redact=0 --no-banner --verbose --log-level trace stdin
11:54AM DBG using gitleaks config .gitleaks.toml from `--config`
11:54AM DBG extending config with default config

Finding:     newtype FinicityDataResponse = FinicityDataResponse
Secret:      FinicityDataResponse
RuleID:      finicity-client-secret
Entropy:     3.584184

11:54AM INF scan completed in 1.72ms
11:54AM WRN leaks found: 1

@rgmz
Copy link
Contributor Author

rgmz commented Oct 9, 2024

Hello, this broke the ability to override the entropy of rules.

Hey @9999years, can you elaborate on what you expect to happen and which version you upgraded from? As far as I'm aware, that would have never worked.

Prior this this PR, extend only did did two things:

  1. Merged your custom rules with the extended config, provided there were no ID conflicts
  2. Appended values to the global allowlist.

https://github.com/gitleaks/gitleaks/blob/v8.19.2/config/config.go#L237

Edit: prior to v8.19.3, the example you provided makes finicity-client-secret a no-op rule:

 "finicity-client-secret": {
 	RuleID:      "finicity-client-secret",
- 	Description: "Identified a Finicity Client Secret, which could lead to compromised financial service integrations and data breaches.",
+ 	Description: "",
- 	Regex: s`(?i)(?:finicity)(?:[0-9a-z\-_\t .]{0,20})(?:[\s|']|[\s|"]){0,3}(?:=|>|:{1,3}=|\|\|:|<=|=>|:|\?=)(?:'|\"|\s|=|\x60){0,5}([a-z0-9]`...,
- 	Regex: nil.
+ 	Entropy:  4,
- 	Keywords: []string{"finicity"},
+ 	Keywords: []string{},
},

@9999years
Copy link
Contributor

@rgmz Just a minimal example. Our actual configuration looks like this:

[[rules]]
description = "Finicity Client Secret"
id = "finicity-client-secret"
regex = '''(?i)(?:finicity)(?:[0-9a-z\-_\t .]{0,20})(?:[\s|']|[\s|"]){0,3}(?:=|>|:{1,3}=|\|\|:|<=|=>|:|\?=)(?:'|\"|\s|=|\x60){0,5}([a-z0-9]{20})(?:['|\"|\n|\r|\s|\x60|;]|$)'''
secretGroup = 1
entropy = 4
keywords = ["finicity"]

9999years added a commit to 9999years/gitleaks that referenced this pull request Oct 9, 2024
PR gitleaks#1524 broke the ability to specify a `[[rules]]` with the same ID as
a built-in rule in order to extend the default configuration.

This PR restores this ability, but merges the provided configuration
with the default rule instead of overwriting the default rule wholesale.
@9999years 9999years mentioned this pull request Oct 9, 2024
3 tasks
zricethezav pushed a commit that referenced this pull request Oct 10, 2024
* Reimplement the ability to override built-in rules

PR #1524 broke the ability to specify a `[[rules]]` with the same ID as
a built-in rule in order to extend the default configuration.

This PR restores this ability, but merges the provided configuration
with the default rule instead of overwriting the default rule wholesale.

* Add tests

* Append keywords and tags
alayne222 pushed a commit to alayne222/gitleaks that referenced this pull request May 28, 2025
alayne222 pushed a commit to alayne222/gitleaks that referenced this pull request May 28, 2025
* Reimplement the ability to override built-in rules

PR gitleaks#1524 broke the ability to specify a `[[rules]]` with the same ID as
a built-in rule in order to extend the default configuration.

This PR restores this ability, but merges the provided configuration
with the default rule instead of overwriting the default rule wholesale.

* Add tests

* Append keywords and tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix config extend validation + allowlist
4 participants