sonar-cryptography icon indicating copy to clipboard operation
sonar-cryptography copied to clipboard

fix: resolve intermediary variable detection in Java and Python engines

Open Ayush-Patel-56 opened this issue 7 months ago • 3 comments

Description

Resolves #8 by implementing recursive symbol tracing in the Java and Python detection engines.

The engine previously used shallow name-based matching for variables, which caused cryptographic rules to fail when intermediary variables were introduced (e.g., SeatInterface intermediary = s; Car c = new Car(intermediary);). This PR introduces a tracing mechanism that resolves variables back to their original initialization point.

Changes

  • Recursive Tracing: Added traceSymbol to follow variable assignments through the AST.
  • Deep Equivalence: Implemented areSymbolsEquivalent to compare symbols after tracing them to their source declaration.
  • Engine Updates: Modified isInvocationOnVariable and isInitForVariable in both JavaDetectionEngine and PythonDetectionEngine to use deep symbol resolution.
  • Regression Testing: Added Issue8IntermediaryVariableTest covering nested rule detection across assignment levels.

Verification

  • Unit Tests: All 157 existing tests passed.
  • Manual Verification: Confirmed via DetectionStoreLogger that the engine correctly associates children nodes through intermediary variables.
  • Trace Proof: Logs confirm successful symbol resolution: Comparing symbols: s (traced to s) with intermediary (traced to s).

See attached screenshot for the successful test execution and detection hierarchy. Screenshot 2026-05-04 103511

Ayush-Patel-56 avatar May 04 '26 05:05 Ayush-Patel-56

Addressed in the latest commit:

  • Added chain-length > 1 test case (b → a → s) to the existing intermediary variable test
  • Added Issue8MethodReceiverTest to cover the isInvocationOnVariable path (s.describe() where s traces through an intermediary)
  • Added Python Issue8IntermediaryVariableTest covering Cipher(intermediary, ...) resolving through a variable chain Cleaned up the existing test - removed @Rule, HeatedSeats, and refactored traceVariable to delegate to traceSymbol

Will open a separate issue for the recursion guard. C# is intentionally out of scope as it doesn't have a traceSymbol equivalent yet.

Ayush-Patel-56 avatar May 05 '26 16:05 Ayush-Patel-56

Issue created: https://github.com/cbomkit/sonar-cryptography/issues/400

Ayush-Patel-56 avatar May 06 '26 03:05 Ayush-Patel-56

Opened #401 to add a recursion guard to traceSymbol for the mutual assignment cycle case (a = b; b = a) that scenario is not covered by this PR but is a natural edge case of the same tracing logic.

Ayush-Patel-56 avatar May 06 '26 11:05 Ayush-Patel-56