Skip to content

fix(java): propagate nested cryptographic values across method parameters#432

Open
sachin9058 wants to merge 3 commits into
cbomkit:mainfrom
sachin9058:fix/parameter-value-capture
Open

fix(java): propagate nested cryptographic values across method parameters#432
sachin9058 wants to merge 3 commits into
cbomkit:mainfrom
sachin9058:fix/parameter-value-capture

Conversation

@sachin9058
Copy link
Copy Markdown
Contributor

@sachin9058 sachin9058 commented May 23, 2026

Description

This PR resolves an issue where nested cryptographic detections are lost when cryptographic values are passed through method parameters.

During analysis, parameter variables were only evaluated inside the local method scope and the original values supplied by the caller were not propagated into the final detection hierarchy.

As a result, nested cryptographic implementations disappeared when helper methods, wrappers, or delegated constructions were used.

Reproduction

Example:

public static byte[] calculateMac(
        BlockCipher cipher,
        int macSizeInBits,
        byte[] key,
        byte[] input) throws Exception {

    BlockCipherMac mac =
            new BlockCipherMac(
                    cipher,
                    macSizeInBits);

    ...
}

calculateMac(
        new AESEngine(),
        128,
        key,
        input);

Expected behavior

(MacContext) BlockCipherMac
└── (CipherContext) AESEngine

Additional parameter values should also remain available:

(MacSize) 128

Previous behavior

(MacContext) BlockCipherMac

The nested AESEngine detection was missing.

Changes

  • Added parameter value resolution for nested propagation
  • Preserved caller values during analysis
  • Updated parameter handling logic
  • Added regression coverage:
    • ParameterValuesNotCapturedTest
    • ParameterValuesNotCapturedTestFile
  • Updated nested hierarchy assertions

Validation

Executed locally:

mvn -pl java test -Dtest=ParameterValuesNotCapturedTest

Result:

Tests run: 1
Failures: 0
Errors: 0
Skipped: 0

BUILD SUCCESS

Related issue

Fixes #431

…ters

Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
@sachin9058 sachin9058 requested a review from a team as a code owner May 23, 2026 23:42
Copilot AI review requested due to automatic review settings May 23, 2026 23:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@sachin9058
Copy link
Copy Markdown
Contributor Author

Hi @n1ckl0sk0rtge @san-zrl
While validating the propagation changes I found an additional failure in BcNTRUParametersTest where MessageDigest appears during intermediate stages (translation/enrichment) but disappears before the final hierarchy assertion.

I opened #433 separately since it appears independent from parameter propagation and I wanted to keep this PR scoped to nested value propagation.

Copy link
Copy Markdown
Contributor

@n1ckl0sk0rtge n1ckl0sk0rtge left a comment

Choose a reason for hiding this comment

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

PR Review

Linked Issue: #431 — "Nested cryptographic detections are not propagated when cryptographic values are passed as method parameters" (OPEN)

Area Status
CI / Build FAILINGBcNTRUParametersTest regression
Requirements coverage Partial — fix appears to work for #431 example, but test skips an extra finding without justification
Architecture OK — changes stay within engine/language/java
Scope CREEP — bundles an unrelated fix for #339 (UI location)
Security No concerns
Tests Re-enables existing ParameterValuesNotCapturedTest; no test for the JavaScanContext changes
Breaking changes Internal only

MUST FIX (Blocking)

1. [Build] CI build is failing — BcNTRUParametersTest regression

  • java/src/test/java/com/ibm/plugin/rules/detection/bc/cipherparameters/BcNTRUParametersTest.java:93
  • CI log shows Expected size: 4 but was: 3 — the AES BlockCipher node now has only {BlockSize, Oid, Encrypt}, missing one child (most likely KeyLength or a mode marker).
  • Likely root cause: the change in JavaLanguageTranslation.java:201 removing matchContext.isHookContext() from the type-match condition alters how hook-context parameter types are matched, breaking the detection chain for aesEngine.init(true, parameters).
  • Fix: Reproduce locally with mvn -pl java test -Dtest=BcNTRUParametersTest, identify which child node was dropped, and either revert the JavaLanguageTranslation change or add the hook-context branch back with a comment explaining why it is no longer needed.

2. [Scope] Bundled unrelated fix for issue #339

  • engine/src/main/java/com/ibm/engine/language/java/JavaScanContext.java (+54 lines — the biggest change in this PR)
  • The extractPreciseTree() / reportIssue() refactor and the javadoc explicitly say "fixes the SonarQube UI issue location for cases like #339" — this is a separate concern from #431's parameter propagation.
  • Fix: Split into a second PR linked to #339. Keeping unrelated changes together makes the regression in MUST-FIX #1 hard to bisect — the offending change could be in either the parameter logic or the new extractPreciseTree path.

SHOULD FIX (Non-blocking)

3. [Logic] JavaLanguageTranslation.java:201 change is unjustified in the PR description

  • The diff drops matchContext.isHookContext() || from the condition with no explanation. This is a behavior change for every hook-context match, not just the parameter-propagation path being fixed. Given finding #1, this is the most suspicious line in the PR.
  • Fix: Add a code comment and a PR-body note explaining why hook context should no longer trigger exact matching, with a test that fails without the change and passes with it.

4. [Tests] No coverage for the new JavaScanContext precise-location logic

  • Three new branches (NEW_CLASS, METHOD_INVOCATION, default fallback) are added with no unit test.
  • Fix: Add a small test (e.g. JavaScanContextTest) that asserts the reported issue location for each case — new AESEngine() reports at AESEngine, obj.method() reports at method, other kinds fall back to firstToken().

5. [Tests] Extra finding skipped in ParameterValuesNotCapturedTest without explanation

  • java/src/test/java/com/ibm/plugin/rules/issues/ParameterValuesNotCapturedTest.java:71 — changed from findingId == 0 to findingId == 0 || findingId == 2. The existing TODO already calls out that these "direct detections of engines… appear in the depending detection rules"; adding finding 2 to the skip list quietly may mask a duplicate-detection regression introduced by the new parameter hook.
  • Fix: In the test javadoc/comment, document what finding 2 represents and why it is expected to duplicate finding 1 (or fix the duplication if unintended).

6. [Style] Fully-qualified class names instead of imports in JavaDetectionEngine.java:863–879

  • Uses org.sonar.plugins.java.api.tree.IdentifierTree, …VariableTree, …MethodTree, and org.sonar.java.model.ExpressionUtils inline. The rest of the file uses imports.
  • Fix: Add normal imports and use simple names — consistent with file style and the project's Spotless/Checkstyle config.

SUGGESTIONS

7. [Heuristic] Parameter-detection heuristic in JavaDetectionEngine.java:864

  • variableTree.initializer() == null is meant to identify method parameters, but it also matches declared-but-not-yet-initialized locals (String x; …). For the next iteration consider variableTree.parent().is(Tree.Kind.METHOD) or checking the symbol's owner is a method, which is more precise.

8. [Maintenance] extractPreciseTree duplicates token-selection logic

  • The javadoc says this mirrors JavaTranslator.getDetectionContextFrom(). Two parallel implementations will drift. Consider extracting to a single utility in common/ (or referencing the existing one).

Notes

  • The fix idea for #431 (recognize parameter identifiers and re-enter analysis at the caller site via createAMethodHook) is sound and the re-enabled regression test demonstrates value.
  • Please split the #339-related JavaScanContext changes into their own PR — keeping them here makes the failing BcNTRUParametersTest much harder to triage.
  • Once the CI failure is investigated and #339 is split out, this PR will be in much better shape to merge.

@sachin9058
Copy link
Copy Markdown
Contributor Author

@n1ckl0sk0rtge Thanks for the detailed review.

I agree that the JavaScanContext changes are unrelated to #431 and should be split out. I'll remove the #339-related changes from this PR and keep the focus on parameter propagation.

I'll also investigate the BcNTRUParametersTest regression and review the JavaLanguageTranslation change around hook-context matching to determine whether it is responsible for the missing child node.

Thanks for pointing out the additional test/documentation gaps as well.

…eters

Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
@sachin9058 sachin9058 force-pushed the fix/parameter-value-capture branch from 00443db to 9757b50 Compare May 29, 2026 12:33
@sachin9058 sachin9058 requested a review from n1ckl0sk0rtge May 29, 2026 12:34
Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
@sachin9058
Copy link
Copy Markdown
Contributor Author

Hi @n1ckl0sk0rtge,

I've addressed the review items:

Validation:

  • ParameterValuesNotCapturedTest passes
  • BcNTRUParametersTest passes
  • Full test suite passes (mvn clean test)
  • CI is green

Thanks for the review happy to make any further adjustments if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested cryptographic detections are not propagated when cryptographic values are passed as method parameters

3 participants