Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify regex for secretPrefix, secretSuffix #1620

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

Ben-grmbl
Copy link
Contributor

@Ben-grmbl Ben-grmbl commented Nov 1, 2024

Description:

This pull request should not change any functionality, it only simplifies the regex generated by GenerateSemiGenericRegex and GenerateUniqueTokenRegex a bit, in order to make them easier to understand, (and to make gitleaks.toml 4 KB smaller :-)

based on the sidenote in #1467

Changes:

// in identifierSuffix:
- (?:[\s|']|[\s|"]){0,3}
+ [\s'"|]{0,3}

// in secretPrefix
- (?:'|\"|\s|=|\x60){0,5}
+ [`'"\s=]{0,5}

// in secretSuffix
- (?:['|\"|\n|\r|\s|\x60|;]|$)
+ (?:[`'"\s;|]|$)

I am not quite certain whether "|" was intended to be included in the character ranges, or whether the intention was that [a|b] should match only a or b, like [ab].

But since the previous regex for identifierSuffix and secretSuffix match for "|", this patch does not change that.

I removed \r\n from the secretSuffix, since it is already included in \s

I also replaced \x60 with `
As far as I could tell, the backtick does not need to be escaped in a toml file, and it was only slightly inconvenient to escape it in code:

- secretPrefix       = `(?:'|\"|\s|=|\x60){0,5}(`
+ secretPrefix       = "[`" + `'"\s=]{0,5}(`

However, I may have misunderstood the ramifications, and can of course undo that if desired.

I have also written some unit tests covering GenerateSemiGenericRegex and GenerateUniqueTokenRegex, in a separate pull request #1623

Checklist:

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

@Ben-grmbl Ben-grmbl changed the title simplify regex for secretPrefix, secretSSuffix simplify regex for secretPrefix, secretSuffix Nov 1, 2024
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch from f4960c9 to bb66e90 Compare November 2, 2024 18:24
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch 4 times, most recently from 0e4b933 to 41c671b Compare November 5, 2024 19:13
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch from 41c671b to 76aee7c Compare November 21, 2024 13:31
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch 2 times, most recently from ea269b3 to c2c7ab8 Compare December 12, 2024 21:20
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch 2 times, most recently from be98240 to f98e572 Compare January 2, 2025 22:00
@zricethezav
Copy link
Collaborator

zricethezav commented Jan 15, 2025

@Ben-grmbl sorry for the delay. I finally got around to testing these and agree with the changes. These are cleaner and easier to understand.

Would you mind fixing the conflicts?

combined character ranges,
removed duplicates in same character range,
replaced \x060 with literal `
combined \s with \r\n
@Ben-grmbl Ben-grmbl force-pushed the chore/simplify-secret-regex branch from f98e572 to 670aaad Compare January 15, 2025 15:06
@Ben-grmbl
Copy link
Contributor Author

@Ben-grmbl sorry for the delay. I finally got around to testing these and agree with the changes. These are cleaner and easier to understand.

Would you mind fixing the conflicts?

Of course, and no rush, if there are any other branches you want to merge first, I can rebase it again anytime.
Thanks for looking into this!

@zricethezav zricethezav merged commit a3f623c into gitleaks:master Jan 16, 2025
1 check passed
@Ben-grmbl Ben-grmbl deleted the chore/simplify-secret-regex branch January 16, 2025 08:55
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.

2 participants