fix: resolve intermediary variable detection in Java and Python engines
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
traceSymbolto follow variable assignments through the AST. - Deep Equivalence: Implemented
areSymbolsEquivalentto compare symbols after tracing them to their source declaration. - Engine Updates: Modified
isInvocationOnVariableandisInitForVariablein bothJavaDetectionEngineandPythonDetectionEngineto use deep symbol resolution. - Regression Testing: Added
Issue8IntermediaryVariableTestcovering nested rule detection across assignment levels.
Verification
- Unit Tests: All 157 existing tests passed.
- Manual Verification: Confirmed via
DetectionStoreLoggerthat 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.
Addressed in the latest commit:
- Added chain-length > 1 test case (
b → a → s) to the existing intermediary variable test - Added
Issue8MethodReceiverTestto cover theisInvocationOnVariablepath (s.describe()wherestraces through an intermediary) - Added Python
Issue8IntermediaryVariableTestcoveringCipher(intermediary, ...)resolving through a variable chain Cleaned up the existing test - removed@Rule,HeatedSeats, and refactoredtraceVariableto delegate totraceSymbol
Will open a separate issue for the recursion guard. C# is intentionally out of scope as it doesn't have a traceSymbol equivalent yet.
Issue created: https://github.com/cbomkit/sonar-cryptography/issues/400
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.