feat(python): initial support for cross-file function resolution via function registry#395
feat(python): initial support for cross-file function resolution via function registry#395sachin9058 wants to merge 4 commits into
Conversation
…ple imports) - Track imported functions via visitImportFrom - Resolve and analyze imported modules on function calls - Prevent recursive scanning using visited file tracking - Add regression test for cross-file RSA sign detection - Limit scope to same-directory modules (prototype implementation) Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
|
I’ve implemented an initial version of cross-file function resolution This allows detection of cryptographic operations across files for basic scenarios. This PR is intentionally scoped as a first step. More advanced cases Would appreciate feedback on whether this direction aligns with the intended design. |
There was a problem hiding this comment.
Pull request overview
Adds an initial prototype for Python cross-file crypto detection by following simple from ... import ... calls into another module and scanning that module during analysis. This fits the existing Python detection pipeline by extending PythonBaseDetectionRule and adding a regression test around an imported signing helper.
Changes:
- Track
ImportFromsymbols and trigger imported-module analysis when an imported function is called. - Add path resolution / visited-file guarding for recursive module scanning.
- Add a regression test fixture covering an imported RSA signing helper.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
python/src/main/java/com/ibm/plugin/rules/detection/PythonBaseDetectionRule.java |
Adds import tracking, imported-module resolution, recursive scan guarding, and cross-file scan invocation. |
python/src/test/java/com/ibm/plugin/rules/resolve/ResolveImportedSignTest.java |
Adds a regression test that captures and asserts the detection tree for an imported signing helper. |
python/src/test/files/rules/resolve/ResolveImportedSignTestFile.py |
Provides the caller-side Python fixture that imports and invokes the helper function. |
python/src/test/files/rules/resolve/imports/ResolveImportedSignImport.py |
Provides the imported helper module containing RSA key generation and signing logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix lifecycle bug using scanDepth - Guard TestPythonVisitorRunner via reflection - Prevent repeated scans - Document limitations clearly Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
This keeps the implementation safe while validating the cross-file resolution approach. Happy to iterate further based on feedback. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- clear registry per top-level scan - add null-safe iteration - prevent duplicate function definitions Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
|
I’ve replaced the previous prototype (file re-scanning) with a registry-based approach The implementation is intentionally scoped and guarded behind a feature flag I’ve also addressed code-level concerns:
Current limitations (name-based resolution, analysis order dependency, static registry) Happy to iterate further based on guidance. |
…e prototype Signed-off-by: Sachin Kumar <sachinkumar905846@gmail.com>
Adds initial support for cross-file function resolution in Python analysis using a registry-based approach.
Motivation
Issue #9 highlights that cryptographic operations are not detected when wrapped inside functions defined in other files. Previous approaches relied on re-scanning files, which is not compatible with Sonar’s production model.
This PR introduces a safer, production-compatible approach.
What this PR does
Implementation Details
Map<String, List<Tree>>visitCallExpressionENABLE_REGISTRY = falseto ensure no regressionExample
With this change, the
signcall insidehelper.pyis detected.Known Limitations
These limitations are intentional to keep the implementation safe and incremental.
Validation
Added regression test for cross-file RSA sign detection
All Python tests pass:
No regressions observed with registry disabled by default
Notes
This PR focuses on validating a production-compatible approach without introducing breaking changes. Further improvements (context-scoped registry, symbol resolution) can be addressed in follow-up work.