Skip to content

[java] Fix #7014/#7015: InsecureCryptoIv/HardCodedCryptoKey: Detect default-initialized arrays - #7059

Merged
adangel merged 8 commits into
pmd:mainfrom
aoto-tech:fix-insecure-crypto-iv-default-array
Oct 23, 2026
Merged

adangel merged 8 commits into
pmd:mainfrom
aoto-tech:fix-insecure-crypto-iv-default-array

Conversation

@aoto-tech

@aoto-tech aoto-tech commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

InsecureCryptoIv misses byte[] 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 IvParameterSpec construction. Element writes, unknown calls, aliases, and storage elsewhere prevent a report. Reading iv.length and an earlier IvParameterSpec construction 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 IvParameterSpec constructions are checked for default arrays: a subclass may transform its arguments. Empty arrays are excluded. The existing hard-coded-source check is preserved through super.visit, which does not traverse children in this superclass. A class-level suppression documents this exception to DontCallSuperVisitWhenUsingRuleChain.

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 // NOPMD for the new reports. Is this per-construction reporting location acceptable?

Examples of these source patterns:

Related issues

Validation

  • Review revision: ./mvnw -B -pl pmd-java -Dtest=InsecureCryptoIvTest,HardCodedCryptoKeyTest verify passes on JDK 21: 54 tests, no failures, errors, or skips. PMD, Checkstyle, and API compatibility checks also pass.
  • Before the review revision, ./mvnw -B -pl pmd-java verify passed on JDK 21: 10,191 tests, no failures or errors, 21 skipped.
  • In the original regression run, restoring the upstream IV implementation caused 12 new positive cases to fail; existing cases and negative controls passed.
  • The full-repository ./mvnw clean verify has not been completed locally; its checklist item is left unchecked for CI.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@aoto-tech
aoto-tech force-pushed the fix-insecure-crypto-iv-default-array branch from 87f4eea to b541f27 Compare September 12, 2026 09:44
@aoto-tech
aoto-tech marked this pull request as ready for review September 12, 2026 09:46
@zbynek

zbynek commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

I didn't check this in detail, but there are two nearly identical rules that share an abstract class as a parent and maybe some of these changes should go to the parent class? See recent work on HardCodedCryptoKey
#7012
#7029

@aoto-tech

Copy link
Copy Markdown
Contributor Author

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.

@aoto-tech

Copy link
Copy Markdown
Contributor Author

Sorry, I’ll handle this once I’m discharged in about a week.

@adangel adangel changed the title [java] InsecureCryptoIv: Detect unchanged default-initialized IV arrays [java] Fix #7015: InsecureCryptoIv: Detect unchanged default-initialized IV arrays Oct 22, 2026
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 39 new violations, 0 new errors and 0 new configuration errors,
removes 8 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(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 adangel added this to the 7.29.0 milestone Oct 23, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adangel adangel changed the title [java] Fix #7015: InsecureCryptoIv: Detect unchanged default-initialized IV arrays [java] Fix #7014/#7015: InsecureCryptoIv/HardCodedCryptoKey: Detect default-initialized arrays Oct 23, 2026
@adangel
adangel merged commit be7d0f6 into pmd:main Oct 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants