Skip to content

[java] Fix #7008: HardCodedCryptoKey: False positive when a default value of System.getProperty() is treated as a hard-coded key - #7012

Merged
lukasgraef merged 18 commits into
pmd:mainfrom
MakerYuichi:fix/hardcoded-crypto-key-system-getproperty-7008
Sep 14, 2026
Merged

lukasgraef merged 18 commits into
pmd:mainfrom
MakerYuichi:fix/hardcoded-crypto-key-system-getproperty-7008

Conversation

@MakerYuichi

Copy link
Copy Markdown
Contributor

Describe the PR

HardCodedCryptoKeyRule (and the shared AbstractHardCodedConstructorArgsVisitor it's built on) was flagging calls like System.getProperty("test.key", "secretKey") as a hard-coded cryptographic key, because it scanned for any string literal inside the resolved argument expression including the property name and default value passed to System.getProperty, even though the actual value used at runtime may come from an external system property.

This PR adds a check using InvocationMatcher to recognize calls to System.getProperty(String) and System.getProperty(String, String), and skips flagging when the argument comes from one of these calls, since no string literal present is necessarily the actual key.

While adding a regression test for this, I found and fixed a related but separate gap: ASTArrayInitializer values on the direct assignment path (e.g. byte[] key = { 0, 1, 2, ... }; new SecretKeySpec(key, "AES")) were not being reported at all — only the ASTArrayAllocation form (byte[] key = new byte[] { ... }) was previously covered on that path.
Added a test case and fix for this too.

Related issues

Fix #7008

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-14 18:40:14+00:00 for 53e616e)

@renechoi renechoi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The System.getProperty guard only fires when that call is the expression being validated. When it is the qualifier, the FP survives. Both of these still report on this branch, on the property name, not the default:

new SecretKeySpec(System.getProperty("k", "d").getBytes(), "AES");
new SecretKeySpec(System.getProperty("k").getBytes(), "AES");

else if (SYSTEM_GET_PROPERTY.matchesCall(expr)) { return; } in the existing qualifier branch clears both, HardCodedCryptoKeyTest and InsecureCryptoIvTest green (21/21).

The array-initializer test also passes on unpatched main.

@lukasgraef lukasgraef left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @MakerYuichi,

thanks for your contribution!
Please have a look at the release notes formatting and @renechoi's review comment.

Both should be addressed.

Comment thread docs/pages/release_notes.md Resolved
Comment thread docs/pages/release_notes.md Resolved
@lukasgraef lukasgraef changed the title [java] HardCodedCryptoKey: False positive when a default value of System.getProperty() is treated as a hard-coded key [java] Fix #7008: HardCodedCryptoKey: False positive when a default value of System.getProperty() is treated as a hard-coded key Sep 14, 2026
@lukasgraef lukasgraef self-assigned this Sep 14, 2026
@lukasgraef

Copy link
Copy Markdown
Member

Thanks again!

I will take care of cleanup and merging.

@lukasgraef lukasgraef added this to the 7.28.0 milestone Sep 14, 2026
@lukasgraef
lukasgraef merged commit e3e3403 into pmd:main Sep 14, 2026
13 checks passed
@MakerYuichi
MakerYuichi deleted the fix/hardcoded-crypto-key-system-getproperty-7008 branch September 15, 2026 06:11
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.

[java] HardCodedCryptoKey: False positive when the key comes from System.getProperty()

3 participants