Skip to content

fix(secret): only match secrets of meaningful length, allow example strings to not be matched #8602

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

Merged
merged 5 commits into from
May 27, 2025

Conversation

PT-GD
Copy link
Contributor

@PT-GD PT-GD commented Mar 24, 2025

Description

This change requires a potentially matching secret Base64 text to be at least 64 characters long. This would be the typical encoded length of the smallest possible library-supported RSA private key, 384 bits. (n.b. 384*1.33/8 = 63.8)

Any actual key smaller than this matching rule is not usable with existing library implementations and is also small that it can't really be considered a practical "secret." It would be trivially crackable and also result in unusably small block sizes.

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • [N/A] I've updated the documentation with the relevant information (if needed).
  • [N/A] I've added usage information (if the PR introduces new options)
  • [N/A] I've included a "before" and "after" example to the description (if the PR is a user interface change).

Require a matching secret text to be at least 64 characters long, as that would be the typical length of the smallest possible library-supported RSA private key.

Everyone should be using 2048-bit or greater keys; any key smaller than this matching rule is so trivially small that it can't really be considered a practical "secret." It would be trivially crackable and also result in unusably small block sizies.
@CLAassistant
Copy link

CLAassistant commented Mar 24, 2025

CLA assistant check
All committers have signed the CLA.

@PT-GD PT-GD changed the title perf: only match secrets of meaningful length, allow example strings to not be matched perf(secret): only match secrets of meaningful length, allow example strings to not be matched Mar 24, 2025
@PT-GD
Copy link
Contributor Author

PT-GD commented Mar 24, 2025

Can anyone familiar with the test framework help write updates to https://github.com/aquasecurity/trivy/blob/main/pkg/fanal/secret/scanner_test.go#L528 and the other tests related to AsymmetricPrivateKey?

I don't see any negative test assertions in the suite. I'd think we want tests which continue to find matches for longer strings but now don't find matches for shorter strings.

@@ -185,7 +185,7 @@ var builtinRules = []Rule{
Category: CategoryAsymmetricPrivateKey,
Title: "Asymmetric Private Key",
Severity: "HIGH",
Regex: MustCompile(`(?i)-----\s*?BEGIN[ A-Z0-9_-]*?PRIVATE KEY( BLOCK)?\s*?-----[\s]*?(?P<secret>[A-Za-z0-9=+/\\\r\n][A-Za-z0-9=+/\\\s]+)[\s]*?-----\s*?END[ A-Z0-9_-]*? PRIVATE KEY( BLOCK)?\s*?-----`),
Regex: MustCompile(`(?i)-----\s*?BEGIN[ A-Z0-9_-]*?PRIVATE KEY( BLOCK)?\s*?-----[\s]*?(?P<secret>[A-Za-z0-9=+/\\\r\n][A-Za-z0-9=+/\\\s]+)[\s]{64,}?-----\s*?END[ A-Z0-9_-]*? PRIVATE KEY( BLOCK)?\s*?-----`),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this regex matches other keys such as DSA, we might want to consider using a smaller minimum size. 22 characters would suffice for 128-bit keys. This might increase false positives.

Looking at this, the regex already wouldn't match typical "redaction indicators" like "..." as they aren't part of the Base64 "alphabet." The reason those are still matching is because zero-length match is allowed by "*"

Whoops! The * I changed applies to white space, not the secret. Don't merge this quite yet!

Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops! The * I changed applies to white space, not the secret. Don't merge this quite yet!

Just FYI - you can convert this PR to draft, so that it would be clear that PR is not ready yet or requires revision

@DmitriyLewen
Copy link
Contributor

Hello @PT-GD
Thanks for your work!

Can anyone familiar with the test framework help write updates to https://github.com/aquasecurity/trivy/blob/main/pkg/fanal/secret/scanner_test.go#L528 and the other tests related to AsymmetricPrivateKey?

You can add one more key with lower lenght in asymmetric-private-key.txt (like i added key in line 6).
This will prove that Trivy correctly finds secret with valid length and ignores secret if length is less than valid length.

Copy link

This PR is stale because it has been labeled with inactivity.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label May 25, 2025
@DmitriyLewen DmitriyLewen removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label May 26, 2025
@DmitriyLewen DmitriyLewen added this pull request to the merge queue May 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 26, 2025
@DmitriyLewen DmitriyLewen added this pull request to the merge queue May 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 26, 2025
@DmitriyLewen DmitriyLewen added this pull request to the merge queue May 27, 2025
Merged via the queue into aquasecurity:main with commit 60fef1b May 27, 2025
13 checks passed
@DmitriyLewen DmitriyLewen changed the title perf(secret): only match secrets of meaningful length, allow example strings to not be matched fix(secret): only match secrets of meaningful length, allow example strings to not be matched May 27, 2025
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(secret): set minimal number of characters for AsymmetricPrivateKey
3 participants