Skip to content

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

Description

@ziwen-gong

Affects PMD Version: 7.26.0

Rule:
HardCodedCryptoKey
https://docs.pmd-code.org/latest/pmd_rules_java_security.html#hardcodedcryptokey

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:

package com.example;
import javax.crypto.spec.SecretKeySpec;
public class Example {
    public void externalOnly() {
        String keyString = System.getProperty("test.key");
        new SecretKeySpec(keyString.getBytes(), "AES");  // false positive
    }

    public void withFallback() {
        String keyString = System.getProperty("test.key", "secretKey");
        new SecretKeySpec(keyString.getBytes(), "AES");  // should still be reported, due to the fallback value
    }
}

Expected outcome:

  1. The property name is never a cryptographic key. It should never be reported.
  2. 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.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on Aug 30, 2026
  2. MakerYuichi commented on Aug 30, 2026

    @MakerYuichi
    Contributor

    I'd like to work on this issue.

    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.

  3. added this to the 7.28.0 milestone on Sep 14, 2026
  4. adangel commented on Sep 14, 2026

    @adangel
    Member

    Sorry, a bit late: Now we don't create a violation for the following code:

    String keyString = System.getProperty("test.key", "secretKey");
    SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), "AES");

    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:

    String keyString = System.getProperty("test.key");
    if (keyString == null) {
        throw new RuntimeException("Sorry, can't start. You have to configure a key!");
    }
    SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), "AES");

    Am I missing something?

  5. added a commit that references this issue on Sep 14, 2026
    e3e3403
  6. MakerYuichi commented on Sep 15, 2026

    @MakerYuichi
    Contributor

    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.

  7. lukasgraef commented on Sep 15, 2026

    @lukasgraef
    Member

    Am I missing something?

    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.

  8. 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
  9. added a commit that references this issue on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions