Repository navigation
AIML-239 Part 5: Fix code review findings - #39
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses 10 code review findings from AIML-239 Part 4 by implementing security fixes, performance optimizations, and improved error handling. Changes include removing sensitive data logging, adding bounded memory limits, handling partial API failures, fixing pagination for filtered results, and adding comprehensive input validation.
Key changes:
- Added bounded memory limit (50K traces) with early termination to prevent OOM in large datasets
- Return partial results with warnings instead of failing completely on mid-fetch API errors
- Fixed pagination offset calculation to correctly handle filtered result counts
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| SCAService.java | Removed sensitive CVE data logging |
| RouteCoverageService.java | Added appId validation and changed session metadata logging to DEBUG level |
| AssessService.java | Implemented early termination, bounded memory limits, partial result handling, and fixed pagination offset calculation for filtered results |
| AssessServiceTest.java | Added 397+ lines of tests covering bounded memory limits, partial results, pagination edge cases, and early termination |
| RouteCoverageServiceTest.java | Expanded tests to verify appId validation for null, empty, and whitespace-only values |
Comments suppressed due to low confidence (1)
src/main/java/com/contrast/labs/ai/mcp/contrast/AssessService.java:1
- [nitpick] The comment references 'mcp-6xd' which appears to be an internal ticket or issue identifier. Consider either removing this reference or adding context about what mcp-6xd represents for future maintainers unfamiliar with the ticket system.
/*
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Note: All stubs must be lenient because early termination filters traces during fetch, | ||
| // so getters for filtered traces are never called (they're not converted to VulnLight) |
There was a problem hiding this comment.
[nitpick] This comment appears in multiple test methods with identical wording. Consider extracting this explanation into a single location (e.g., a class-level comment or test section header) and referencing it to reduce duplication and improve maintainability.
| private record SessionFilteringResult( | ||
| List<Trace> traces, boolean wasTruncated, boolean hadFetchError, String errorMessage) { |
There was a problem hiding this comment.
[nitpick] The record name 'SessionFilteringResult' is inconsistent with the method name 'fetchTracesWithEarlyTermination' and doesn't clearly convey it handles fetch results. Consider renaming to 'FetchResult' or 'TraceFetchResult' to better align with its usage context and match the PR description which mentions 'FetchResult'.
| "IMPORTANT: Results were truncated due to limits (max %d traces or 100 pages). " | ||
| + "This application may have more matching vulnerabilities than returned. " | ||
| + "To get complete results, narrow your search using filters: " | ||
| + "severity (e.g., 'CRITICAL,HIGH'), status (e.g., 'Confirmed'), " | ||
| + "or environment (e.g., 'PRODUCTION'). " | ||
| + "Without narrower filters, you may be missing critical security findings.", | ||
| maxTracesForSessionFiltering)); |
There was a problem hiding this comment.
The hardcoded '100 pages' value in the warning message should reference the MAX_PAGES constant defined in fetchTracesWithEarlyTermination (line 950). This creates a maintenance risk if MAX_PAGES is changed but the message isn't updated.
| "IMPORTANT: Results were truncated due to limits (max %d traces or 100 pages). " | |
| + "This application may have more matching vulnerabilities than returned. " | |
| + "To get complete results, narrow your search using filters: " | |
| + "severity (e.g., 'CRITICAL,HIGH'), status (e.g., 'Confirmed'), " | |
| + "or environment (e.g., 'PRODUCTION'). " | |
| + "Without narrower filters, you may be missing critical security findings.", | |
| maxTracesForSessionFiltering)); | |
| "IMPORTANT: Results were truncated due to limits (max %d traces or %d pages). " | |
| + "This application may have more matching vulnerabilities than returned. " | |
| + "To get complete results, narrow your search using filters: " | |
| + "severity (e.g., 'CRITICAL,HIGH'), status (e.g., 'Confirmed'), " | |
| + "or environment (e.g., 'PRODUCTION'). " | |
| + "Without narrower filters, you may be missing critical security findings.", | |
| maxTracesForSessionFiltering, MAX_PAGES)); |
| * property for testing. Default: 50,000. | ||
| */ | ||
| @Value("${contrast.max-traces-for-session-filtering:50000}") | ||
| private int maxTracesForSessionFiltering = 50_000; |
There was a problem hiding this comment.
usually class level variables that spring inject the value into are defined at the top of the class
There was a problem hiding this comment.
I'll add a new coding standard and fix in the next Code Review Fixes PR.
| } | ||
|
|
||
| // Add session metadata name/value filter if specified | ||
| if (StringUtils.hasText(sessionMetadataName)) { |
There was a problem hiding this comment.
This uses StringUtils.hasText() but the if clause above does not. Not a big deal but seems like code standards are not always being used by the ai
There was a problem hiding this comment.
Will address in the next Code Review PR
| var traceList = new ArrayList<Trace>(); | ||
|
|
||
| // Create 200 traces: 80 matching session metadata, 120 non-matching | ||
| for (int i = 0; i < 80; i++) { |
There was a problem hiding this comment.
This code is duplicated with the test above. Typically we create a FakeDataFactory class that creates reusable stubbed out data objects. So this is a good example of where it works well and stay dry even in tests
There was a problem hiding this comment.
I need to think about this one. As functionality changes, the tests get updated frequently. I don't want anything locking them in place. If a human were editing it, I would DRY them up, but with AI doing it, I think this could be more efficient. (can't believe I am saying this...but its a new world)
|
|
||
| // Create SessionMetadataResponse with AgentSession | ||
| var mockAgentSession = | ||
| new com.contrast.labs.ai.mcp.contrast.sdkextension.data.sessionmetadata.AgentSession(); |
There was a problem hiding this comment.
is there a need for the fully qualified class name for all these instantiations?
There was a problem hiding this comment.
No. I have standards saying not to use them...but to use imports (not the first time you called this out). I need to refine the prompting I am using. I wish there was a linter that just fixed this so AI doesn't have to care. I will add it to the next Code Reveiw PR.
| } | ||
|
|
||
| @Test | ||
| void testGetRouteCoverage_NullAppId_ReturnsErrorResponse() throws Exception { |
There was a problem hiding this comment.
we do have standards on test method naming conventions in case this helps:
https://contrast.atlassian.net/wiki/spaces/TS/pages/1442743028/Test+Naming+Standards
There was a problem hiding this comment.
The pattern here is MethodUnderTest_Condition_Expectation. But when i look at the code, there are like 4 patterns used. This is the default that AI tends to use. But it is also emulating whatever it found in the test file before it started. So the AssessServiceTests use the standard you linked to, because that file was written that way to start. Other files use whatever they were written with to start.
I'll standardize this.
| // Calculate target count for early termination | ||
| // When we have a selective filter, we only need enough results for the requested page | ||
| // When no selective filter, fetch all traces for accurate totalItems count | ||
| int targetCount = |
There was a problem hiding this comment.
nit: several cases here where the ai is using a mixed bag of either var or the primitive data type in the variable declaration and appears to not follow the coding standards..
There was a problem hiding this comment.
The standard states to use var only if its obvious what the type is from the statement. I wonder if it thinks this is not obvious enough, so it didn't use it. Regardless, I will do a code standards review of the codebase before release. Adding it to the list.
|
|
||
| // Convert to VulnLight directly from the list | ||
| vulnerabilities = allTraces.stream().map(vulnerabilityMapper::toVulnLight).toList(); | ||
| if (fetchResult.traces().isEmpty()) { |
There was a problem hiding this comment.
can traces() be null here?
There was a problem hiding this comment.
No. All paths either return a value or throw an exception. I did a thorough review with Codex, and it loves to catch NPE errors.
Security: - Remove sensitive CVE data logging at INFO level in SCAService (mcp-14o) Performance: - Add bounded limit (50,000) to fetchAllTracesForSessionFiltering to prevent OOM (mcp-6r5) - Add clear truncation warning to AI clients when limit exceeded, explaining security implications and how to use filters to get complete results - Make limit configurable via Spring property for testing Tests: - Add test for truncation warning behavior
When API errors occur during multi-page vulnerability fetches for session filtering, return partial results instead of discarding all fetched data. Changes: - Extended SessionFilteringResult record with hadFetchError and errorMessage - Added factory methods success() and partial() for clearer intent - Modified fetchAllTracesForSessionFiltering() to return partial data on error - Added warning message when partial data is returned due to API failure This prevents catastrophic data loss when transient network errors occur during large application vulnerability scans. Users now receive partial results with a clear warning instead of an error with no data.
Add note in the tool description explaining that API errors during multi-page fetching return partial results with a warning instead of failing completely. Completes mcp-9qb acceptance criteria.
…cp-6xd) Analysis confirmed existing implementation is correct: - pagination.offset() correctly calculates (page-1)*pageSize - finalVulns.size() (filtered count) is passed to createPaginatedResponse - PaginationHandler correctly calculates hasMorePages based on filtered count Tests verify: - Page 3 of 80 filtered results returns empty with totalItems=80 - Page 2 correctly returns items 50-79 from filtered results
- Add StringUtils.hasText validation for appId parameter - Throw IllegalArgumentException with clear message for null/empty appId - Add unit tests for null and empty appId cases - Matches validation pattern from AssessService
Session metadata (branch names, build numbers) may contain sensitive info. WARN logs often go to external monitoring systems and could expose internal development practices. Changed to DEBUG level to prevent data exposure.
Implement early termination when enough filtered results are found during session filtering, reducing API calls from O(total/pageSize) to O(targetCount/matchRate/pageSize). Changes: - Add fetchTracesWithEarlyTermination() with predicate-based filtering - Add buildSessionFilterPredicate() helper for combining session filters - Add MAX_PAGES (100) safety limit to prevent runaway pagination - Only apply early termination when selective filters active (preserves accurate totalItems for pagination when no filtering applied) All 298 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1736a16 to
884fc9f
Compare

Rebased onto main after #38 merged (squash merge).
Why
A comprehensive code review of PR #38 using 6 specialized review agents identified 5 P1 (critical) and 5 P2 (high priority) issues. These findings include security vulnerabilities, performance bottlenecks, and data integrity concerns that need to be fixed before the consolidation can be considered complete.
What
This PR addresses all 10 code review findings:
P1 (Critical) Fixes
log.info(result.toString())in SCAService that exposed vulnerability detailsP2 (High Priority) Fixes
How
AssessService.java (core changes)
fetchAllTracesForSessionFilteringnow has a configurablemaxTracesparameter (default 50K)FetchResultrecord to track partial results and termination reasonRouteCoverageService.java
StringUtils.hasText(appId)validation at method entrySCAService.java
result.toString()logging lineTest Coverage
Step-by-Step Walkthrough
FetchResultrecord for tracking partial resultsfetchAllTracesForSessionFilteringto accept maxTraces and return FetchResultTesting