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
Expand Up @@ -5,6 +5,7 @@ import "github.com/checkmarx/ast-cli/internal/services/realtimeengine"
type SecretsRealtimeResult struct {
Title string `json:"Title"`
Description string `json:"Description"`
SecretValue string `json:"SecretValue"`
FilePath string `json:"FilePath"`
Severity string `json:"Severity"`
Locations []realtimeengine.Location `json:"Locations"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *SecretsRealtimeService) RunSecretsRealtimeScan(filePath, ignoredFilePat
}

results := convertToSecretsRealtimeResult(report)
resultsPerLineMap := createResultsPerLineMap(results)
resultsPerLineMap := createResultsPerLocationMap(results)
results = filterGenericAPIKeyVulIfNeeded(results, resultsPerLineMap)

if ignoredFilePath == "" {
Expand Down Expand Up @@ -163,6 +163,7 @@ func convertSecretToResult(secret *secrets.Secret) SecretsRealtimeResult {
return SecretsRealtimeResult{
Title: secret.RuleID,
Description: secret.RuleDescription,
SecretValue: secret.Value,
Severity: getSeverity(secret),
FilePath: secret.Source,
Locations: locations,
Expand All @@ -182,15 +183,17 @@ func getSeverity(secret *secrets.Secret) string {
}
}

func createResultsPerLineMap(results []SecretsRealtimeResult) map[string][]SecretsRealtimeResult {
resultsPerLine := make(map[string][]SecretsRealtimeResult)
func createResultsPerLocationMap(results []SecretsRealtimeResult) map[string][]SecretsRealtimeResult {
resultsPerLocation := make(map[string][]SecretsRealtimeResult)
for _, result := range results {
var locationKey string
for _, location := range result.Locations {
lineKey := fmt.Sprintf("%s:%d", result.FilePath, location.Line)
resultsPerLine[lineKey] = append(resultsPerLine[lineKey], result)
locationKey = fmt.Sprintf("%s:%d", locationKey, location.Line)
}
resultKey := fmt.Sprintf("%s%s", result.FilePath, locationKey)
resultsPerLocation[resultKey] = append(resultsPerLocation[resultKey], result)
}
return resultsPerLine
return resultsPerLocation
}

func filterGenericAPIKeyVulIfNeeded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,31 @@ func TestRunSecretsRealtimeScan_ValidFile_Success(t *testing.T) {

assert.NoError(t, err)
assert.NotNil(t, results)
// Note: The actual results depend on the 2ms scanner behavior
// This test mainly verifies that the function completes without error
}

func TestRunSecretsRealtimeScan_MultiLineResult_Success(t *testing.T) {
mock.Flag = wrappers.FeatureFlagResponseModel{Name: wrappers.OssRealtimeEnabled, Status: true}
value := "PRIVATE_KEY = \"\"\"\n-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA7v8wF+SECRETKEYEXAMPLE+QIDAQABAoIBAQC0\n-----END RSA PRIVATE KEY-----\n\"\"\""
// Create a temporary file for testing
tempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "test-secrets.txt")
testContent := value
err := os.WriteFile(tempFile, []byte(testContent), 0644)
assert.NoError(t, err)

service := &SecretsRealtimeService{
JwtWrapper: &mock.JWTMockWrapper{},
FeatureFlagWrapper: &mock.FeatureFlagsMockWrapper{},
}

results, err := service.RunSecretsRealtimeScan(tempFile, "")

assert.NoError(t, err)
assert.NotNil(t, results)
assert.Len(t, results, 1)
assert.Len(t, results[0].Locations, 3)
assert.NotEmpty(t, results[0].SecretValue)

}

func TestReadFile_ValidFile_Success(t *testing.T) {
Expand Down
Loading