-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(amazonq): duplicate security issues on same line #4895
Problem Security issues appear multiple times in the same line if the ListCodeScanFindings response is paginated. Solution Create the issues list from issues map only once per scan instead of on every page response.
- Loading branch information
Showing
3 changed files
with
136 additions
and
25 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/amazonq/.changes/next-release/Bug Fix-f3dc220b-64f4-4e50-990a-9c825baf14e2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "Bug Fix", | ||
"description": "Security Scan: Addresses a bug where security issues sometimes appear multiple times" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/core/src/test/codewhisperer/service/securityScanHandler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { PromiseResult } from 'aws-sdk/lib/request' | ||
import { DefaultCodeWhispererClient, ListCodeScanFindingsResponse } from '../../../codewhisperer/client/codewhisperer' | ||
import { listScanResults } from '../../../codewhisperer/service/securityScanHandler' | ||
import { Stub, stub } from '../../utilities/stubber' | ||
import { AWSError, HttpResponse } from 'aws-sdk' | ||
import { RawCodeScanIssue } from '../../../codewhisperer/models/model' | ||
import { CodeAnalysisScope } from '../../../codewhisperer/models/constants' | ||
import assert from 'assert' | ||
import sinon from 'sinon' | ||
import fs from 'fs' | ||
|
||
const mockCodeScanFindings = JSON.stringify([ | ||
{ | ||
filePath: '/workspaceFolder/python3.7-plain-sam-app/hello_world/app.py', | ||
startLine: 1, | ||
endLine: 1, | ||
title: 'title', | ||
description: { | ||
text: 'text', | ||
markdown: 'markdown', | ||
}, | ||
detectorId: 'detectorId', | ||
detectorName: 'detectorName', | ||
findingId: 'findingId', | ||
relatedVulnerabilities: [], | ||
severity: 'High', | ||
remediation: { | ||
recommendation: { | ||
text: 'text', | ||
url: 'url', | ||
}, | ||
suggestedFixes: [], | ||
}, | ||
} satisfies RawCodeScanIssue, | ||
]) | ||
|
||
const mockListCodeScanFindingsResponse: Awaited<Promise<PromiseResult<ListCodeScanFindingsResponse, AWSError>>> = { | ||
$response: { | ||
hasNextPage: () => false, | ||
nextPage: () => undefined, | ||
data: undefined, | ||
error: undefined, | ||
requestId: '', | ||
redirectCount: 0, | ||
retryCount: 0, | ||
httpResponse: new HttpResponse(), | ||
}, | ||
codeScanFindings: mockCodeScanFindings, | ||
} | ||
|
||
// eslint-disable-next-line id-length | ||
const mockListCodeScanFindingsPaginatedResponse: Awaited< | ||
Promise<PromiseResult<ListCodeScanFindingsResponse, AWSError>> | ||
> = { | ||
...mockListCodeScanFindingsResponse, | ||
nextToken: 'nextToken', | ||
} | ||
|
||
describe('securityScanHandler', function () { | ||
describe('listScanResults', function () { | ||
let mockClient: Stub<DefaultCodeWhispererClient> | ||
beforeEach(function () { | ||
mockClient = stub(DefaultCodeWhispererClient) | ||
sinon.stub(fs, 'existsSync').returns(true) | ||
sinon.stub(fs, 'statSync').returns({ isFile: () => true } as fs.Stats) | ||
}) | ||
|
||
afterEach(function () { | ||
sinon.restore() | ||
}) | ||
|
||
it('should make ListCodeScanFindings request and aggregate findings by file path', async function () { | ||
mockClient.listCodeScanFindings.resolves(mockListCodeScanFindingsResponse) | ||
|
||
const aggregatedCodeScanIssueList = await listScanResults( | ||
mockClient, | ||
'jobId', | ||
'codeScanFindingsSchema', | ||
'projectPath', | ||
CodeAnalysisScope.PROJECT | ||
) | ||
|
||
assert.equal(aggregatedCodeScanIssueList.length, 1) | ||
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 1) | ||
}) | ||
|
||
it('should handle ListCodeScanFindings request with paginated response', async function () { | ||
mockClient.listCodeScanFindings | ||
.onFirstCall() | ||
.resolves(mockListCodeScanFindingsPaginatedResponse) | ||
.onSecondCall() | ||
.resolves(mockListCodeScanFindingsPaginatedResponse) | ||
.onThirdCall() | ||
.resolves(mockListCodeScanFindingsResponse) | ||
|
||
const aggregatedCodeScanIssueList = await listScanResults( | ||
mockClient, | ||
'jobId', | ||
'codeScanFindingsSchema', | ||
'projectPath', | ||
CodeAnalysisScope.PROJECT | ||
) | ||
|
||
assert.equal(aggregatedCodeScanIssueList.length, 1) | ||
assert.equal(aggregatedCodeScanIssueList[0].issues.length, 3) | ||
}) | ||
}) | ||
}) |