GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] HardCodedCryptoKey: False positive when the key comes from System.getProperty() · Issue #7008 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#7084
#7012
Closed
[java] HardCodedCryptoKey: False positive when the key comes from System.getProperty()#7008
#7084
#7012
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.28.0

Description

@ziwen-gong
ziwen-gong
opened on Aug 30, 2026
Issue body actions

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. ziwen-gong
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Aug 30, 2026
  2. MakerYuichi commented on Aug 30, 2026

    @MakerYuichi
    MakerYuichi
    on Aug 30, 2026
    Contributor
    More actions

    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. MakerYuichi
    added 3 commits that reference this issue on Sep 7, 2026

    Handle System.getProperty() as the qualifier of a chained call (pmd#7008

    0b3c1d1

    Remove already-released entries from Fixed Issues; keep only pmd#7008

    e54ee14

    Fix HardCodedCryptoKey FP: recognize chained/nested System.getPropert…

    9a52c67
  4. lukasgraef
    added this to the 7.28.0 milestone on Sep 14, 2026
  5. lukasgraef
    closed this as completedin #7012on Sep 14, 2026
  6. adangel commented on Sep 14, 2026

    @adangel
    adangel
    on Sep 14, 2026
    Member
    More actions

    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?

  7. UncleOwen
    added a commit that references this issue on Sep 14, 2026

    [java] Fix pmd#7008: HardCodedCryptoKey: False positive when a defaul…

    This issue will close once commit e3e3403 is merged into the 'main' branch.
    e3e3403
  8. MakerYuichi commented on Sep 15, 2026

    @MakerYuichi
    MakerYuichi
    on Sep 15, 2026
    Contributor
    More actions

    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.

  9. lukasgraef commented on Sep 15, 2026

    @lukasgraef
    lukasgraef
    on Sep 15, 2026
    Member
    More actions

    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.

    Reacted by Andreas Dangel
  10. lukasgraef
    reopened this on Sep 15, 2026
  11. lukasgraef
    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
  12. adangel
    added a commit that references this issue on Sep 18, 2026

    [java] Followup for #7008 (HardCodedCryptoKey): Fix FN when hard-code…

    3bec06f
  13. adangel
    closed this as completedin #7084on 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-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.28.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.