Repository navigation
[java] Fix #7014/#7015: InsecureCryptoIv/HardCodedCryptoKey: Detect default-initialized arrays - #7059
Conversation
87f4eea to
b541f27
Compare
|
Agreed. I’ll try moving the shared hard-coded value resolution/traversal into AbstractHardCodedConstructorArgsVisitor and keep the rule-specific constructor/argument checks in the concrete rules. |
|
Sorry, I’ll handle this once I’m discharged in about a week. |
|
Compared to main: (comment created at 2026-10-23 14:40:11+00:00 for f32b5b3) |
…rArgsVisitor That way, HardCodedCryptoKey benefits from the same improvements. Violations are reported now again consistently at the allocation of these default-initialized arrays. Improved rule doc examples.
adangel
left a comment
There was a problem hiding this comment.
Thanks for the PR. As @zbynek mentioned, it would be good to move the implementation into the base class. The default array problematic equally applies to HardCodedCryptoKey as well.
I went ahead and implemented this. This also changed the report location back to where it is for all the other cases: Not at the usage, but as the allocation of the array.
It also covers not only #7015 but #7014 as well.
I'll shortly push my changes to this branch.
| void example(boolean refill) { | ||
| byte[] iv = new byte[16]; | ||
| if (refill) { | ||
| new SecureRandom().nextBytes(iv); |
There was a problem hiding this comment.
Depending on the expectations, this could be considered a false-negative, because there is at least one code path, which uses the default initialized array.
Describe the PR
InsecureCryptoIvmissesbyte[] iv = new byte[16]; new IvParameterSpec(iv);even when the array remains unchanged. This change reports default-initialized byte arrays with a positive constant length, both inline and through a local variable.The local-variable check is deliberately conservative. It supports an initializer or a separate unconditional assignment in the same block, and checks uses before each
IvParameterSpecconstruction. Element writes, unknown calls, aliases, and storage elsewhere prevent a report. Readingiv.lengthand an earlierIvParameterSpecconstruction are allowed because they do not modify the array. Later uses do not suppress an earlier violation, and uses in subsequent constructor arguments are checked before reporting.The check does not follow aliases, reassignment chains, or control flow between blocks. It also does not analyze method bodies reached through calls. Only direct
IvParameterSpecconstructions are checked for default arrays: a subclass may transform its arguments. Empty arrays are excluded. The existing hard-coded-source check is preserved throughsuper.visit, which does not traverse children in this superclass. A class-level suppression documents this exception toDontCallSuperVisitWhenUsingRuleChain.The regression tests cover the reported case, all four non-reporting examples from the discussion, separate assignments, constant expressions, the offset/length overload, mutations and escapes, ordering, nested execution contexts, zero-length arrays, and subclass constructors. Each new case explains why a report is expected or excluded. The rule documentation and release notes are updated.
Default-array violations are reported at each constructor call, so later mutation does not hide an earlier violation and unchanged uses can be reported individually. Existing hard-coded-value checks retain their source locations. The rule documentation explains where to put
// NOPMDfor the new reports. Is this per-construction reporting location acceptable?Examples of these source patterns:
CryptUtils.decryptCBCcopies input into the allocated IV before constructingIvParameterSpec.SensitiveString.decryptfills its allocated IV buffer from the input before transforming it and constructingIvParameterSpec.Related issues
Validation
./mvnw -B -pl pmd-java -Dtest=InsecureCryptoIvTest,HardCodedCryptoKeyTest verifypasses on JDK 21: 54 tests, no failures, errors, or skips. PMD, Checkstyle, and API compatibility checks also pass../mvnw -B -pl pmd-java verifypassed on JDK 21: 10,191 tests, no failures or errors, 21 skipped../mvnw clean verifyhas not been completed locally; its checklist item is left unchecked for CI.Ready?
./mvnw clean verifypasses (checked automatically by github actions)