Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2025 Contrast Security
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.contrast.labs.ai.mcp.contrast.data.sast;

import com.contrastsecurity.sdk.scan.Project;
import java.time.Instant;
import java.util.List;

/**
* Serializable representation of a Contrast Scan (SAST) project.
*
* <p>This record wraps the SDK {@link Project} interface to provide Jackson-compatible
* serialization. The SDK interface uses method-style accessors (e.g., {@code id()}) which Jackson
* cannot serialize by default.
*
* @param id Unique project identifier (UUID)
* @param name Project name as displayed in Contrast UI
* @param organizationId Organization UUID that owns this project
* @param archived Whether the project has been archived
* @param language Programming language (JAVA, JAVASCRIPT, PYTHON, etc.)
* @param critical Count of critical severity vulnerabilities in last scan
* @param high Count of high severity vulnerabilities in last scan
* @param medium Count of medium severity vulnerabilities in last scan
* @param low Count of low severity vulnerabilities in last scan
* @param note Count of note severity vulnerabilities in last scan
* @param lastScanId ID of most recent completed scan (null if no scans)
* @param lastScanTime Timestamp of most recent completed scan (null if no scans)
* @param completedScans Total count of successfully completed scans
* @param includeNamespaceFilters Namespaces to include in scans
* @param excludeNamespaceFilters Namespaces to exclude from scans
*/
public record ScanProject(
String id,
String name,
String organizationId,
boolean archived,
String language,
int critical,
int high,
int medium,
int low,
int note,
String lastScanId,
Instant lastScanTime,
int completedScans,
List<String> includeNamespaceFilters,
List<String> excludeNamespaceFilters) {

/**
* Creates a ScanProject from an SDK Project interface.
*
* @param project SDK Project to convert
* @return new ScanProject with all fields mapped
* @throws NullPointerException if project is null
*/
public static ScanProject from(Project project) {
return new ScanProject(
project.id(),
project.name(),
project.organizationId(),
project.archived(),
project.language(),
project.critical(),
project.high(),
project.medium(),
project.low(),
project.note(),
project.lastScanId(),
project.lastScanTime(),
project.completedScans(),
project.includeNamespaceFilters() != null
? List.copyOf(project.includeNamespaceFilters())
: List.of(),
project.excludeNamespaceFilters() != null
? List.copyOf(project.excludeNamespaceFilters())
: List.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.contrast.labs.ai.mcp.contrast.tool.sast;

import com.contrast.labs.ai.mcp.contrast.data.sast.ScanProject;
import com.contrast.labs.ai.mcp.contrast.tool.base.BaseSingleTool;
import com.contrast.labs.ai.mcp.contrast.tool.base.SingleToolResponse;
import com.contrast.labs.ai.mcp.contrast.tool.sast.params.GetSastProjectParams;
import com.contrastsecurity.sdk.scan.Project;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
Expand All @@ -31,7 +31,7 @@
*/
@Service
@Slf4j
public class GetSastProjectTool extends BaseSingleTool<GetSastProjectParams, Project> {
public class GetSastProjectTool extends BaseSingleTool<GetSastProjectParams, ScanProject> {

@Tool(
name = "get_scan_project",
Expand All @@ -56,14 +56,15 @@ public class GetSastProjectTool extends BaseSingleTool<GetSastProjectParams, Pro
Related tools:
- get_scan_results: Get SARIF results for a project's latest scan
""")
public SingleToolResponse<Project> getScanProject(
public SingleToolResponse<ScanProject> getScanProject(
@ToolParam(description = "Scan project name (case-sensitive, must match exactly)")
String projectName) {
return executePipeline(() -> GetSastProjectParams.of(projectName));
}

@Override
protected Project doExecute(GetSastProjectParams params, List<String> warnings) throws Exception {
protected ScanProject doExecute(GetSastProjectParams params, List<String> warnings)
throws Exception {
var sdk = getContrastSDK();
var orgId = getOrgId();

Expand All @@ -83,6 +84,6 @@ protected Project doExecute(GetSastProjectParams params, List<String> warnings)
project.id(),
project.language());

return project;
return ScanProject.from(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright 2025 Contrast Security
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.contrast.labs.ai.mcp.contrast.data.sast;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.contrastsecurity.sdk.scan.Project;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

/** Unit tests for ScanProject record and its factory method. */
class ScanProjectTest {

@Test
void from_should_map_all_fields_correctly() {
// Given
Project mockProject = mock();
when(mockProject.id()).thenReturn("proj-123");
when(mockProject.name()).thenReturn("test-project");
when(mockProject.organizationId()).thenReturn("org-456");
when(mockProject.archived()).thenReturn(false);
when(mockProject.language()).thenReturn("JAVA");
when(mockProject.critical()).thenReturn(2);
when(mockProject.high()).thenReturn(5);
when(mockProject.medium()).thenReturn(10);
when(mockProject.low()).thenReturn(3);
when(mockProject.note()).thenReturn(1);
when(mockProject.lastScanId()).thenReturn("scan-789");
when(mockProject.lastScanTime()).thenReturn(Instant.parse("2025-01-01T12:00:00Z"));
when(mockProject.completedScans()).thenReturn(15);
when(mockProject.includeNamespaceFilters()).thenReturn(List.of("com.example"));
when(mockProject.excludeNamespaceFilters()).thenReturn(List.of("com.example.test"));

// When
ScanProject result = ScanProject.from(mockProject);

// Then
assertThat(result.id()).isEqualTo("proj-123");
assertThat(result.name()).isEqualTo("test-project");
assertThat(result.organizationId()).isEqualTo("org-456");
assertThat(result.archived()).isFalse();
assertThat(result.language()).isEqualTo("JAVA");
assertThat(result.critical()).isEqualTo(2);
assertThat(result.high()).isEqualTo(5);
assertThat(result.medium()).isEqualTo(10);
assertThat(result.low()).isEqualTo(3);
assertThat(result.note()).isEqualTo(1);
assertThat(result.lastScanId()).isEqualTo("scan-789");
assertThat(result.lastScanTime()).isEqualTo(Instant.parse("2025-01-01T12:00:00Z"));
assertThat(result.completedScans()).isEqualTo(15);
assertThat(result.includeNamespaceFilters()).containsExactly("com.example");
assertThat(result.excludeNamespaceFilters()).containsExactly("com.example.test");
}

@Test
void from_should_handle_null_lastScanId_and_lastScanTime() {
// Given - project with no completed scans
Project mockProject = createMinimalMockProject();
when(mockProject.lastScanId()).thenReturn(null);
when(mockProject.lastScanTime()).thenReturn(null);
when(mockProject.completedScans()).thenReturn(0);

// When
ScanProject result = ScanProject.from(mockProject);

// Then
assertThat(result.lastScanId()).isNull();
assertThat(result.lastScanTime()).isNull();
assertThat(result.completedScans()).isZero();
}

@Test
void from_should_handle_null_namespace_filters() {
// Given - SDK may return null for empty filters
Project mockProject = createMinimalMockProject();
when(mockProject.includeNamespaceFilters()).thenReturn(null);
when(mockProject.excludeNamespaceFilters()).thenReturn(null);

// When
ScanProject result = ScanProject.from(mockProject);

// Then - should be empty list, not null
assertThat(result.includeNamespaceFilters()).isEmpty();
assertThat(result.excludeNamespaceFilters()).isEmpty();
}

@Test
void from_should_create_defensive_copy_of_namespace_filters() {
// Given
var includeFilters = new ArrayList<>(List.of("com.example"));
Comment thread
ChrisEdwards marked this conversation as resolved.
Resolved
var excludeFilters = new ArrayList<>(List.of("com.test"));
Comment thread
ChrisEdwards marked this conversation as resolved.
Resolved

Project mockProject = createMinimalMockProject();
when(mockProject.includeNamespaceFilters()).thenReturn(includeFilters);
when(mockProject.excludeNamespaceFilters()).thenReturn(excludeFilters);

// When
ScanProject result = ScanProject.from(mockProject);

// Then - modifying original should not affect result
includeFilters.add("modified");
excludeFilters.add("modified");

assertThat(result.includeNamespaceFilters()).containsExactly("com.example");
assertThat(result.excludeNamespaceFilters()).containsExactly("com.test");
}

@Test
void from_should_throw_on_null_project() {
assertThatThrownBy(() -> ScanProject.from(null)).isInstanceOf(NullPointerException.class);
}

private Project createMinimalMockProject() {
Project mock = mock();
when(mock.id()).thenReturn("id");
when(mock.name()).thenReturn("name");
when(mock.organizationId()).thenReturn("org");
when(mock.language()).thenReturn("JAVA");
when(mock.includeNamespaceFilters()).thenReturn(List.of());
when(mock.excludeNamespaceFilters()).thenReturn(List.of());
return mock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,69 @@ void getScanProject_should_verify_project_has_required_fields() {
log.info(" - name: {}", response.data().name());
log.info(" - language: {}", response.data().language());
}

/**
* Regression test for bug AIML-343: get_scan_project returned {"data":{}} despite found:true.
*
* <p>The bug was caused by Jackson inability to serialize the SDK Project interface (which uses
* method-style accessors like id() instead of getId()). The fix creates a ScanProject record that
* Jackson can serialize properly.
*/
@Test
void getScanProject_regression_all_fields_should_be_populated() {
log.info("\n=== Regression Test: AIML-343 - Data should not be empty ===");
Comment thread
ChrisEdwards marked this conversation as resolved.
Resolved

var response = getSastProjectTool.getScanProject(testProjectName);

assertThat(response.found()).as("Project should be found").isTrue();
assertThat(response.data()).as("Data should not be null").isNotNull();

// THE BUG: data was serialized as {} (empty object)
// AFTER FIX: all fields should be populated
var project = response.data();

// Core identity fields (never null)
assertThat(project.id()).as("id field must be populated").isNotBlank();
assertThat(project.name()).as("name field must be populated").isNotBlank();
assertThat(project.organizationId()).as("organizationId field must be populated").isNotBlank();
assertThat(project.language()).as("language field must be populated").isNotBlank();

// Boolean field
assertThat(project.archived()).as("archived field accessible").isNotNull();

// Vulnerability counts (valid integers, may be zero)
assertThat(project.critical()).as("critical count accessible").isGreaterThanOrEqualTo(0);
assertThat(project.high()).as("high count accessible").isGreaterThanOrEqualTo(0);
assertThat(project.medium()).as("medium count accessible").isGreaterThanOrEqualTo(0);
assertThat(project.low()).as("low count accessible").isGreaterThanOrEqualTo(0);
assertThat(project.note()).as("note count accessible").isGreaterThanOrEqualTo(0);

// Scan counts
assertThat(project.completedScans()).as("completedScans accessible").isGreaterThanOrEqualTo(0);

// Namespace filters should be lists (possibly empty, never null)
assertThat(project.includeNamespaceFilters())
.as("includeNamespaceFilters should be a list")
.isNotNull();
assertThat(project.excludeNamespaceFilters())
.as("excludeNamespaceFilters should be a list")
.isNotNull();

log.info("✓ AIML-343 regression test passed - all fields populated:");
log.info(" - id: {}", project.id());
log.info(" - name: {}", project.name());
log.info(" - organizationId: {}", project.organizationId());
log.info(" - language: {}", project.language());
log.info(" - archived: {}", project.archived());
log.info(" - critical: {}", project.critical());
log.info(" - high: {}", project.high());
log.info(" - medium: {}", project.medium());
log.info(" - low: {}", project.low());
log.info(" - note: {}", project.note());
log.info(" - lastScanId: {}", project.lastScanId());
log.info(" - lastScanTime: {}", project.lastScanTime());
log.info(" - completedScans: {}", project.completedScans());
log.info(" - includeNamespaceFilters: {}", project.includeNamespaceFilters());
log.info(" - excludeNamespaceFilters: {}", project.excludeNamespaceFilters());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

# Test data configuration
# Set this to a real scan project name in your Contrast TeamServer for integration tests
test.scan.project-name=
test.scan.project-name=WebGoat
Comment thread
ChrisEdwards marked this conversation as resolved.
Resolved