Skip to content

Commit

Permalink
Merge main into feature/cw-proactive-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
aws-toolkit-automation authored Apr 9, 2024
2 parents f330346 + c6db2fe commit 16d3725
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "CodeWhisperer: handle exception when code scan service returns out of bounds line numbers"
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import java.time.Instant
import java.util.Base64
import java.util.UUID
import kotlin.coroutines.coroutineContext
import kotlin.math.max
import kotlin.math.min

class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
private val clientToken: UUID = UUID.randomUUID()
Expand Down Expand Up @@ -308,7 +310,8 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
runReadAction {
FileDocumentManager.getInstance().getDocument(file)
}?.let { document ->
val endCol = document.getLineEndOffset(it.endLine - 1) - document.getLineStartOffset(it.endLine - 1) + 1
val endLineInDocument = min(max(0, it.endLine - 1), document.lineCount - 1)
val endCol = document.getLineEndOffset(endLineInDocument) - document.getLineStartOffset(endLineInDocument) + 1
CodeWhispererCodeScanIssue(
startLine = it.startLine,
startCol = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ class CodeWhispererCodeScanTest : CodeWhispererCodeScanTestBase(PythonCodeInsigh
assertThat(res).hasSize(2)
}

@Test
fun `test mapToCodeScanIssues - handles index out of bounds`() {
val recommendations = listOf(
fakeListCodeScanFindingsOutOfBoundsIndexResponse.codeScanFindings(),
)
val res = codeScanSessionSpy.mapToCodeScanIssues(recommendations)
assertThat(res).hasSize(1)
}

@Test
fun `test run() - happypath`() {
assertNotNull(sessionConfigSpy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ open class CodeWhispererCodeScanTestBase(projectRule: CodeInsightTestFixtureRule
internal lateinit var fakeCreateCodeScanResponseFailed: CreateCodeScanResponse
internal lateinit var fakeCreateCodeScanResponsePending: CreateCodeScanResponse
internal lateinit var fakeListCodeScanFindingsResponse: ListCodeScanFindingsResponse
internal lateinit var fakeListCodeScanFindingsOutOfBoundsIndexResponse: ListCodeScanFindingsResponse
internal lateinit var fakeGetCodeScanResponse: GetCodeScanResponse
internal lateinit var fakeGetCodeScanResponsePending: GetCodeScanResponse
internal lateinit var fakeGetCodeScanResponseFailed: GetCodeScanResponse
Expand Down Expand Up @@ -109,52 +110,41 @@ open class CodeWhispererCodeScanTestBase(projectRule: CodeInsightTestFixtureRule
)
}

private fun setupCodeScanFindings(filePath: Path) = """
[
{
"filePath": "${filePath.systemIndependentPath}",
"startLine": 1,
"endLine": 2,
"title": "test",
"description": {
"text": "global variable",
"markdown": "### global variable"
},
"detectorId": "detectorId",
"detectorName": "detectorName",
"findingId": "findingId",
"relatedVulnerabilities": [],
"severity": "severity",
"remediation": {
"recommendation": {
"text": "recommendationText",
"url": "recommendationUrl"
},
"suggestedFixes": []
}
private fun setupCodeScanFinding(filePath: Path, startLine: Int, endLine: Int) = """
{
"filePath": "${filePath.systemIndependentPath}",
"startLine": $startLine,
"endLine": $endLine,
"title": "test",
"description": {
"text": "global variable",
"markdown": "### global variable"
},
{
"filePath": "${filePath.systemIndependentPath}",
"startLine": 1,
"endLine": 2,
"title": "test",
"description": {
"text": "global variable",
"markdown": "### global variable"
"detectorId": "detectorId",
"detectorName": "detectorName",
"findingId": "findingId",
"relatedVulnerabilities": [],
"severity": "severity",
"remediation": {
"recommendation": {
"text": "recommendationText",
"url": "recommendationUrl"
},
"detectorId": "detectorId",
"detectorName": "detectorName",
"findingId": "findingId",
"relatedVulnerabilities": [],
"severity": "severity",
"remediation": {
"recommendation": {
"text": "recommendationText",
"url": "recommendationUrl"
},
"suggestedFixes": []
}
"suggestedFixes": []
}
}
""".trimIndent()

private fun setupCodeScanFindings(filePath: Path) = """
[
${setupCodeScanFinding(filePath, 1, 2)},
${setupCodeScanFinding(filePath, 1, 2)}
]
"""

private fun setupCodeScanFindingsOutOfBounds(filePath: Path) = """
[
${setupCodeScanFinding(filePath, 99999, 99999)}
]
"""

Expand Down Expand Up @@ -188,6 +178,11 @@ open class CodeWhispererCodeScanTestBase(projectRule: CodeInsightTestFixtureRule
.responseMetadata(metadata)
.build() as ListCodeScanFindingsResponse

fakeListCodeScanFindingsOutOfBoundsIndexResponse = ListCodeScanFindingsResponse.builder()
.codeScanFindings(setupCodeScanFindingsOutOfBounds(filePath))
.responseMetadata(metadata)
.build() as ListCodeScanFindingsResponse

fakeGetCodeScanResponse = GetCodeScanResponse.builder()
.status(CodeScanStatus.COMPLETED)
.responseMetadata(metadata)
Expand Down

0 comments on commit 16d3725

Please sign in to comment.