You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] HardCodedCryptoKey: False positive when the key comes from System.getProperty() #7008
Description:
HardCodedCryptoKey detects hard-coded cryptographic keys. When the key is obtained through an external configuration API such as System.getProperty(String) or System.getProperty(String, String), the rule reports the first string literal it finds anywhere inside the constructor argument. That literal is the property name, which is never the key.
Code Sample demonstrating the issue:
packagecom.example;
importjavax.crypto.spec.SecretKeySpec;
publicclassExample {
publicvoidexternalOnly() {
StringkeyString = System.getProperty("test.key");
newSecretKeySpec(keyString.getBytes(), "AES"); // false positive
}
publicvoidwithFallback() {
StringkeyString = System.getProperty("test.key", "secretKey");
newSecretKeySpec(keyString.getBytes(), "AES"); // should still be reported, due to the fallback value
}
}
Expected outcome:
The property name is never a cryptographic key. It should never be reported.
The default value of the two-argument overload is a hard-coded key, because it will be used whenever the property is unset. It should be reported.
I'll look at HardCodedCryptoKeyRule and add a check that recognizes when a string literal is only used as the default-value argument to System.getProperty(String, String) (and likely similar config-lookup APIs like System.getenv, if applicable), rather than being assigned directly as a cryptographic key. I'll aim to distinguish "this literal is used directly as/for a key" from "this literal is only a fallback used when external config is absent," per the example in the issue.
This code uses the hardcoded key "secretKey" if system property is not defined. So, the default value is a hard-coded key, isn't it?
I would expect something like this at the first place:
StringkeyString = System.getProperty("test.key");
if (keyString == null) {
thrownewRuntimeException("Sorry, can't start. You have to configure a key!");
}
SecretKeySpeckeySpec = newSecretKeySpec(keyString.getBytes(), "AES");
Good catch adangel, I traced through the actual code against your example and confirmed SYSTEM_GET_PROPERTY matches getProperty(String) and getProperty(String, String) under the same pattern (getProperty(_*)), so the exemption fires as soon as any System.getProperty call shows up in the chain, regardless of whether a literal default is actually present. For your snippet, keyString.getBytes() resolves back to keyString's initializer, hits System.getProperty("test.key", "secretKey"), matches, and returns no distinction between "no literal at all" and "the second arg is a real fallback key."
I think the fix is narrowing the exemption to the single-arg overload only, and letting the two-arg default fall through to the existing literal-detection path so it gets flagged normally. I still need to verify that lands on the default value and not the property name before I'd call it done, but I can put together a follow-up PR with tests for both cases.
You could make an argument that using System.getProperty with a fallback value is not always a violation, since we can't tell whether the fallback value is actually being used.
So it's more a decision of: Do we want to err towards a FN or FP?
And usually I would be in favor of erring towards a FN, since these won't "bother" users.
But on a second thought (and given the nature of this rule) it would probably be better to be more "strict" and produce a violation for a hardcoded fallback key as well.
I will reopen this issue and update the description accordingly.
changed the title [-][java] HardCodedCryptoKey: False positive when a default value of System.getProperty() is treated as a hard-coded key[/-][+][java] HardCodedCryptoKey: False positive when the key comes from System.getProperty()[/+]on Sep 15, 2026
Activity
MakerYuichi commentedon Aug 30, 2026
I'd like to work on this issue.
I'll look at
HardCodedCryptoKeyRuleand add a check that recognizes when a string literal is only used as the default-value argument toSystem.getProperty(String, String)(and likely similar config-lookup APIs likeSystem.getenv, if applicable), rather than being assigned directly as a cryptographic key. I'll aim to distinguish "this literal is used directly as/for a key" from "this literal is only a fallback used when external config is absent," per the example in the issue.Handle System.getProperty() as the qualifier of a chained call (pmd#7008
Remove already-released entries from Fixed Issues; keep only pmd#7008
Fix HardCodedCryptoKey FP: recognize chained/nested System.getPropert…
adangel commentedon Sep 14, 2026
Sorry, a bit late: Now we don't create a violation for the following code:
This code uses the hardcoded key "secretKey" if system property is not defined. So, the default value is a hard-coded key, isn't it?
I would expect something like this at the first place:
Am I missing something?
[java] Fix pmd#7008: HardCodedCryptoKey: False positive when a defaul…
MakerYuichi commentedon Sep 15, 2026
Good catch adangel, I traced through the actual code against your example and confirmed SYSTEM_GET_PROPERTY matches getProperty(String) and getProperty(String, String) under the same pattern (getProperty(_*)), so the exemption fires as soon as any System.getProperty call shows up in the chain, regardless of whether a literal default is actually present. For your snippet, keyString.getBytes() resolves back to keyString's initializer, hits System.getProperty("test.key", "secretKey"), matches, and returns no distinction between "no literal at all" and "the second arg is a real fallback key."
I think the fix is narrowing the exemption to the single-arg overload only, and letting the two-arg default fall through to the existing literal-detection path so it gets flagged normally. I still need to verify that lands on the default value and not the property name before I'd call it done, but I can put together a follow-up PR with tests for both cases.
lukasgraef commentedon Sep 15, 2026
You could make an argument that using
System.getPropertywith a fallback value is not always a violation, since we can't tell whether the fallback value is actually being used.So it's more a decision of: Do we want to err towards a FN or FP?
And usually I would be in favor of erring towards a FN, since these won't "bother" users.
But on a second thought (and given the nature of this rule) it would probably be better to be more "strict" and produce a violation for a hardcoded fallback key as well.
I will reopen this issue and update the description accordingly.
[-][java] HardCodedCryptoKey: False positive when a default value of System.getProperty() is treated as a hard-coded key[/-][+][java] HardCodedCryptoKey: False positive when the key comes from System.getProperty()[/+][java] Followup for #7008 (HardCodedCryptoKey): Fix FN when hard-code…