Repository navigation
Description
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:
- Track writes to individual array elements such as
iv[i] = constant; - Track the known contents of locally allocated arrays;
- Propagate these values to subsequent uses of the array;
- Associate the array contents with a later
new IvParameterSpec(iv)construction; - 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
drisko545 commentedon Oct 6, 2026
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.
adangel commentedon Oct 23, 2026
There is already work going on - see #7015 and #7059
[-][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[/+][java] Fix pmd#7014/pmd#7015: InsecureCryptoIv/HardCodedCryptoKey: De…