Skip to content

Fix scan-type filtering in cx results exit-code command(AST-186514) - #1632

Open
cx-sumit-morchhale wants to merge 4 commits into
stagingfrom
bug/AST-AST-186514-exitCode
Open

cx-sumit-morchhale wants to merge 4 commits into
stagingfrom
bug/AST-AST-186514-exitCode

Conversation

@cx-sumit-morchhale

Copy link
Copy Markdown
Contributor

Summary

  • Fix: Handle scan-type name mapping and validation for exit-code command
  • Issue: When using --scan-types filter with iac-security or �pi-security, results were not returned correctly
  • Root cause: User-friendly names were not mapped to actual API engine names (iac-security → kics, api-security → apisec)

Changes

  • Map user-friendly scan type names to API engine names in sanitizeScannerNames()
  • Add validation for invalid scan types with clear error messages in �alidateExitCodeScanTypes()
  • Return overall status + failed engines when no filter is provided
  • Return only requested engine details when --scan-types filter is provided
  • Use existing constants from commonParams for consistency

Test plan

  • Test with --scan-types sast - returns only SAST status
  • Test with --scan-types iac-security - returns KICS status
  • Test with --scan-types api-security - returns API Security status
  • Test with --scan-types sast,sca,iac-security,api-security,aisc - returns all matching engines
  • Test with invalid scan type - returns error message
  • Test without --scan-types flag - returns overall status + all failed engines

🤖 Generated with Claude Code

- Map user-friendly scan type names to API engine names (iac-security → kics, api-security → apisec)
- Add validation for invalid scan types with clear error messages
- Return overall status + failed engines when no filter is provided
- Return only requested engine details when --scan-types filter is provided
- Fixes AST-186514: Correctly filter results by scan type

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@cx-sumit-morchhale
cx-sumit-morchhale requested a review from a team as a code owner November 27, 2026 11:51
@cx-sumit-morchhale cx-sumit-morchhale changed the title AST-186514: Fix scan-type filtering in exit-code command Fix scan-type filtering in cx results exit-code command(AST-186514) Nov 27, 2026
cx-sumit-morchhale and others added 3 commits November 27, 2026 19:38
- Test exit-code with individual scan types (sast, sca, iac-security, api-security, aisc)
- Test exit-code with multiple scan types combined
- Test exit-code with invalid scan type (should return error)
- Test exit-code without scan-type filter (returns overall status + failed engines)
- Verify correct engine name mapping (iac-security → kics, api-security → apisec)
- Fixes AST-186514: Validates scan-type filtering functionality

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
- Updated test expectations to match implementation behavior:
  * No filter + Canceled status returns overall status + failed engines
  * Filtered request returns only requested engine details
- All three test cases now passing:
  * TestResultsExitCode_OnCanceledScan_PrintOnlyScanIDAndStatusCanceledToConsole
  * TestResultsExitCode_OnCanceledScanWithRequestedSuccessfulScanner_PrintOnlyScanIDAndStatusCanceledToConsole
  * TestResultsExitCode_OnCanceledScanWithRequestedFailedScanner_PrintOnlyScanIDAndStatusCanceledToConsole

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
…-exitCode

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>

if scanTypesFlagValue == "" {
results = createAllFailedScannersResponse(scanResponseModel)
if scanResponseModel.Status == wrappers.ScanCanceled ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A status-only response is no longer returned when --scan-types is set, so a pending scan can look empty

The scan-level status early return (Canceled, Running, Queued, Partial, Completed) now runs only when there is no filter. With a filter, output comes only from StatusDetails. For a Queued, Running or Canceled scan the requested engine may be absent from StatusDetails. Then results is empty, and runGetExitCodeCommand returns nil without printing (len(results) == 0). A script gating on --scan-types sast sees empty output and exit 0. Before this PR it got Running or Canceled.

Suggested fix: If the filtered result is empty and the scan status is not a final success, return the scan-level ScannerResponse (ID and status). That keeps the old behavior for non-terminal scans.

Evidence: The removed unconditional status block, the else branch that calls only createRequestedScannersResponse, and the empty-result early return in runGetExitCodeCommand.

return scanTypeMap
}

func validateExitCodeScanTypes(scanTypes string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new validator rejects engine names that were accepted before (kics, apisec, containers, scs)
Line: 520-537 (validateExitCodeScanTypes)

Before this PR the filter matched raw engine names in StatusDetails, so --scan-types kics worked. The whitelist now contains only the five friendly names. Existing pipelines using kics or apisec fail with an error. Engines that the repo defines but the whitelist omits (containers, scs) can no longer be queried, even though they appear in StatusDetails.

Suggested fix: Accept the raw engine names as aliases (the keys already in the mapping, plus kics, apisec, containers, scs if they are valid statuses). Otherwise state in the PR and release notes that this is a breaking change.

Evidence: commonParams.KicsType, APISecType, ContainersType and ScsType exist in flags.go. They are absent from validTypes.

commonParams.AiscType: true,
}

scanTypeSlice := strings.Split(scanTypes, ",")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty list entries ("sast," or "sast,,sca") fail with a blank, confusing error

Line: 529-535

An empty segment yields Invalid scan-type: . Valid types are….

Suggested fix: Skip empty segments after TrimSpace, or report that the value is empty.

Evidence: The loop validates every split element, including empty ones.

@@ -493,14 +496,46 @@ func createAllFailedScannersResponse(scanResponseModel *wrappers.ScanResponseMod
func sanitizeScannerNames(scanTypes string) map[string]string {
scanTypeSlice := strings.Split(scanTypes, ",")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The valid-types list is defined three times (whitelist map, mapping map, error string)

Line: 497-537

Adding an engine means editing three places. The error text can drift from the whitelist. A single package-level map would serve both functions and generate the message.

Suggested fix: Use one exitCodeScanTypeToEngine map, validate against its keys, and build the error message from them.

Evidence: engineNameMapping, validTypes and the hard-coded message string.

@@ -299,9 +299,11 @@ func TestResultsExitCode_OnCanceledScan_PrintOnlyScanIDAndStatusCanceledToConsol

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The PR's central fix (the iac-security → kics and api-security → apisec mapping) has no unit test, and the validator has none either

Line: 299-345

The edited tests cover "", sast and kics only. Nothing exercises iac-security, api-security, case or whitespace handling, or validateExitCodeScanTypes rejection. The test names still say "PrintOnlyScanIDAndStatusCanceled", but the assertions now check different behavior. The PR checklist relies on manual testing.

Suggested fix: Add table-driven tests for sanitizeScannerNames, validateExitCodeScanTypes and getScannerResponse("iac-security", …). Rename the three tests to describe what they now assert.

Evidence: The diff touches only three existing tests. Nothing in it calls sanitizeScannerNames or validateExitCodeScanTypes directly.

@@ -52,11 +52,11 @@ func TestResultsExitCode_OnSuccessfulScan_ShouldReturnStatusCompleted(t *testing
scansWrapper := wrappers.NewHTTPScansWrapper(scansPath)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion depends on the order and count of engines returned by the API

Line: 52-60

The test asserts len == 2 and results[0].Name == "sast" for "sast,sca". StatusDetails order is not guaranteed. It also fails if the test scan has no SCA engine. Before this PR it asserted len == 1, and now it asserts a different count for the same scan, so the earlier assumption was wrong or the scan data differs.

Suggested fix: Assert by name (for example, find the sast entry by name) and assert that both names are present, without relying on the index.

Evidence: results[0] index access, and the changed 1 → 2 count.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants