Skip to content

[java] InsecureCryptoIv/HardCodedCryptoKey: False negative when a hard-coded array is constructed through array element assignments #7014

Closed

Description

@ziwen-gong

Affects PMD Version: 7.26.0

Rules:

Description:
InsecureCryptoIv can detect hard-coded initialization vectors (IVs) constructed directly using an array initializer, for example:

byte[] iv = new byte[] {1};
new IvParameterSpec(iv);

However, for the semantically equivalent case where the array is allocated first and its contents are then initialized through individual array element assignments, the rule does not report a violation, for example:

byte[] iv = new byte[1];
iv[0] = 1;
new IvParameterSpec(iv);

The two forms ultimately produce exactly the same array contents: an array of length 1 whose only element is the constant value 1. Therefore, according to the semantics of the InsecureCryptoIv rule, it could also be useful to consider the second case for detection. The following test case demonstrates this behavior. The first case is detected by PMD, whereas the second case, which constructs the IV by assigning values to individual array elements, is not detected. This semantics-preserving case suggests a potential false negative in InsecureCryptoIv.

Code Sample demonstrating the issue:

// Direct array initialization
package com.example;
import javax.crypto.spec.IvParameterSpec;
public class BadCase{
    public static void main(String[] args) {
        // Hardcoded IV using a boolean literal
        IvParameterSpec ivSpec =
            new IvParameterSpec(new byte[] {1}); // [REPORTED LINE]
    }
}
// Array element assignment
package com.example;
import javax.crypto.spec.IvParameterSpec;
public class BadCase_Var {
    public static void main(String[] args) {
        byte[] iv = new byte[1];
        iv[0] = 1; // Hardcoded value assigned element-wise
        IvParameterSpec ivSpec = new IvParameterSpec(iv);// [should be flagged]
    }
}

Expected outcome:
InsecureCryptoIv could also detect hard-coded IVs when the contents of a local byte array are determined through constant element assignments before the array is passed to IvParameterSpec. For example, the following two forms could be considered equivalent from the perspective of detecting a hard-coded IV:

byte[] iv = new byte[] {1};
new IvParameterSpec(iv);

and:

byte[] iv = new byte[1];
iv[0] = 1;
new IvParameterSpec(iv);

A possible improvement would be to extend the rule's analysis of locally allocated arrays to include array element assignments. In particular, the checker could:

  1. Track writes to individual array elements such as iv[i] = constant;
  2. Track the known contents of locally allocated arrays;
  3. Propagate these values to subsequent uses of the array;
  4. Associate the array contents with a later new IvParameterSpec(iv)construction;
  5. Report the construction when the relevant IV contents are statically determined by constants.
    This would extend the existing handling of ASTArrayAllocation and ASTArrayInitializer to cover arrays whose contents are established through element-wise assignments.
    This is a false-negative case.

Running PMD through: CLI

Activity

  1. added
    a:false-negative
    on Aug 31, 2026
  2. drisko545 commented on Oct 6, 2026

    @drisko545

    Hello,
    I would like to work on this issue, if no one else is already working on it.
    I have reproduced the behavior with PMD 7.28.0.

    My plan is to follow the approach suggested above: extend the rule so that it also detects constant values assigned to individual array elements, and add test cases for it.

  3. adangel commented on Oct 23, 2026

    @adangel
    Member

    There is already work going on - see #7015 and #7059

  4. changed the title [-][java] InsecureCryptoIv: False negative when a hard-coded IV is constructed through array element assignments[/-] [+][java] InsecureCryptoIv/HardCodedCryptoKey: False negative when a hard-coded array is constructed through array element assignments[/+] on Oct 23, 2026
  5. added this to the 7.29.0 milestone on Oct 23, 2026
  6. added a commit that references this issue on Oct 23, 2026
    be7d0f6
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-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions