|
| 1 | +/*! |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as assert from 'assert' |
| 7 | +import * as vscode from 'vscode' |
| 8 | +import * as codewhispererClient from '../../../codewhisperer/client/codewhisperer' |
| 9 | +import * as CodeWhispererConstants from '../../../codewhisperer/models/constants' |
| 10 | +import * as path from 'path' |
| 11 | +import * as testutil from '../../../test/testUtil' |
| 12 | +import * as fs from 'fs-extra' |
| 13 | +import { setValidConnection, skiptTestIfNoValidConn } from '../../util/codewhispererUtil' |
| 14 | +import { resetCodeWhispererGlobalVariables } from '../../../test/codewhisperer/testUtil' |
| 15 | +import { getTestWorkspaceFolder } from '../../integrationTestsUtilities' |
| 16 | +import { closeAllEditors } from '../../../test/testUtil' |
| 17 | +import { DependencyGraphFactory } from '../../../codewhisperer/util/dependencyGraph/dependencyGraphFactory' |
| 18 | +import { statSync } from 'fs' |
| 19 | +import { |
| 20 | + getPresignedUrlAndUpload, |
| 21 | + createScanJob, |
| 22 | + pollScanJobStatus, |
| 23 | + listScanResults, |
| 24 | +} from '../../../codewhisperer/service/securityScanHandler' |
| 25 | +import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities' |
| 26 | + |
| 27 | +const filePromptWithSecurityIssues = `from flask import app |
| 28 | +
|
| 29 | +def execute_input_noncompliant(): |
| 30 | + from flask import request |
| 31 | + module_version = request.args.get("module_version") |
| 32 | + # Noncompliant: executes unsanitized inputs. |
| 33 | + exec("import urllib%s as urllib" % module_version) |
| 34 | + |
| 35 | +def execute_input_compliant(): |
| 36 | + from flask import request |
| 37 | + module_version = request.args.get("module_version") |
| 38 | + # Compliant: executes sanitized inputs. |
| 39 | + exec("import urllib%d as urllib" % int(module_version))` |
| 40 | + |
| 41 | +const largePrompt = 'a'.repeat(CodeWhispererConstants.codeScanPythonPayloadSizeLimitBytes + 1) |
| 42 | + |
| 43 | +const javaPromptNoBuild = `class HelloWorld { |
| 44 | + public static void main(String[] args) { |
| 45 | + System.out.println("Hello World!"); |
| 46 | + } |
| 47 | +}` |
| 48 | + |
| 49 | +describe('CodeWhisperer security scan', async function () { |
| 50 | + let validConnection: boolean |
| 51 | + let tempFolder: string |
| 52 | + const client = new codewhispererClient.DefaultCodeWhispererClient() |
| 53 | + const workspaceFolder = getTestWorkspaceFolder() |
| 54 | + |
| 55 | + before(async function () { |
| 56 | + validConnection = await setValidConnection() |
| 57 | + }) |
| 58 | + |
| 59 | + beforeEach(function () { |
| 60 | + resetCodeWhispererGlobalVariables() |
| 61 | + //valid connection required to run tests |
| 62 | + skiptTestIfNoValidConn(validConnection, this) |
| 63 | + }) |
| 64 | + |
| 65 | + afterEach(async function () { |
| 66 | + if(tempFolder !== undefined){ |
| 67 | + await fs.remove(tempFolder) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + after(function () { |
| 72 | + closeAllEditors() |
| 73 | + }) |
| 74 | + |
| 75 | + const openTestFile = async (filePath: string) => { |
| 76 | + const doc = await vscode.workspace.openTextDocument(filePath) |
| 77 | + return await vscode.window.showTextDocument(doc, { |
| 78 | + selection: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 1)), |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + function getDependencyGraph(languageId: string) { |
| 83 | + return DependencyGraphFactory.getDependencyGraph(languageId) |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + securityJobSetup: combines steps 1 and 2 in startSecurityScan: |
| 88 | + |
| 89 | + Step 1: Generate context truncations |
| 90 | + Step 2: Get presigned Url, upload and clean up |
| 91 | +
|
| 92 | + returns artifactMap and projectPath |
| 93 | + */ |
| 94 | + async function securityJobSetup(editor: vscode.TextEditor) { |
| 95 | + const dependencyGraph = getDependencyGraph(editor.document.languageId) |
| 96 | + if (dependencyGraph === undefined) { |
| 97 | + throw new Error(`"${editor.document.languageId}" is not supported for security scan.`) |
| 98 | + } |
| 99 | + const uri = dependencyGraph.getRootFile(editor) |
| 100 | + |
| 101 | + if (dependencyGraph.reachSizeLimit(statSync(uri.fsPath).size)) { |
| 102 | + throw new Error( |
| 103 | + `Selected file larger than ${dependencyGraph.getReadableSizeLimit()}. Try a different file.` |
| 104 | + ) |
| 105 | + } |
| 106 | + const projectPath = dependencyGraph.getProjectPath(uri) |
| 107 | + const truncation = await dependencyGraph.generateTruncationWithTimeout( |
| 108 | + uri, |
| 109 | + CodeWhispererConstants.contextTruncationTimeoutSeconds |
| 110 | + ) |
| 111 | + |
| 112 | + let artifactMap |
| 113 | + try { |
| 114 | + artifactMap = await getPresignedUrlAndUpload(client, truncation) |
| 115 | + } finally { |
| 116 | + dependencyGraph.removeTmpFiles(truncation) |
| 117 | + } |
| 118 | + return { |
| 119 | + artifactMap: artifactMap, |
| 120 | + projectPath: projectPath, |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + it('codescan request with valid input params and no security issues completes scan and returns no recommendations', async function () { |
| 125 | + //set up file and editor |
| 126 | + const appRoot = path.join(workspaceFolder, 'python3.7-plain-sam-app') |
| 127 | + const appCodePath = path.join(appRoot, 'hello_world', 'app.py') |
| 128 | + const editor = await openTestFile(appCodePath) |
| 129 | + |
| 130 | + //run security scan |
| 131 | + const securityJobSetupResult = await securityJobSetup(editor) |
| 132 | + const artifactMap = securityJobSetupResult.artifactMap |
| 133 | + const projectPath = securityJobSetupResult.projectPath |
| 134 | + |
| 135 | + //get job status and result |
| 136 | + const scanJob = await createScanJob(client, artifactMap, editor.document.languageId) |
| 137 | + const jobStatus = await pollScanJobStatus(client, scanJob.jobId) |
| 138 | + const securityRecommendationCollection = await listScanResults( |
| 139 | + client, |
| 140 | + scanJob.jobId, |
| 141 | + CodeWhispererConstants.codeScanFindingsSchema, |
| 142 | + projectPath |
| 143 | + ) |
| 144 | + |
| 145 | + assert.deepStrictEqual(jobStatus, 'Completed') |
| 146 | + assert.ok(securityRecommendationCollection.length === 0) |
| 147 | + }) |
| 148 | + |
| 149 | + it('codescan request with valid input params and security issues completes scan and returns recommendations', async function () { |
| 150 | + //set up file and editor |
| 151 | + tempFolder = await makeTemporaryToolkitFolder() |
| 152 | + const tempFile = path.join(tempFolder, 'test.py') |
| 153 | + testutil.toFile(filePromptWithSecurityIssues, tempFile) |
| 154 | + const editor = await openTestFile(tempFile) |
| 155 | + |
| 156 | + //run security scan |
| 157 | + const securityJobSetupResult = await securityJobSetup(editor) |
| 158 | + const artifactMap = securityJobSetupResult.artifactMap |
| 159 | + const projectPath = securityJobSetupResult.projectPath |
| 160 | + const scanJob = await createScanJob(client, artifactMap, editor.document.languageId) |
| 161 | + |
| 162 | + //get job status and result |
| 163 | + const jobStatus = await pollScanJobStatus(client, scanJob.jobId) |
| 164 | + const securityRecommendationCollection = await listScanResults( |
| 165 | + client, |
| 166 | + scanJob.jobId, |
| 167 | + CodeWhispererConstants.codeScanFindingsSchema, |
| 168 | + projectPath |
| 169 | + ) |
| 170 | + |
| 171 | + assert.deepStrictEqual(jobStatus, 'Completed') |
| 172 | + assert.ok(securityRecommendationCollection.length === 1) |
| 173 | + }) |
| 174 | + |
| 175 | + it('codescan request on file that is too large causes scan job setup to fail', async function () { |
| 176 | + tempFolder = await makeTemporaryToolkitFolder() |
| 177 | + const tempFile = path.join(tempFolder, 'test2.py') |
| 178 | + testutil.toFile(largePrompt, tempFile) |
| 179 | + const editor = await openTestFile(tempFile) |
| 180 | + |
| 181 | + await assert.rejects(() => securityJobSetup(editor)) |
| 182 | + }) |
| 183 | + |
| 184 | + it('codescan request on java file with no build causes scan job setup to fail', async function () { |
| 185 | + tempFolder = await makeTemporaryToolkitFolder() |
| 186 | + const tempFile = path.join(tempFolder, 'test.java') |
| 187 | + testutil.toFile(javaPromptNoBuild, tempFile) |
| 188 | + const editor = await openTestFile(tempFile) |
| 189 | + |
| 190 | + await assert.rejects(() => securityJobSetup(editor)) |
| 191 | + }) |
| 192 | + |
| 193 | + it('codescan request for file in unsupported language fails to generate dependency graph and causes scan setup to fail', async function () { |
| 194 | + const appRoot = path.join(workspaceFolder, 'go1-plain-sam-app') |
| 195 | + const appCodePath = path.join(appRoot, 'hello-world', 'main.go') |
| 196 | + const editor = await openTestFile(appCodePath) |
| 197 | + const dependencyGraph = getDependencyGraph(editor.document.languageId) |
| 198 | + |
| 199 | + assert.strictEqual(dependencyGraph, undefined) |
| 200 | + await assert.rejects(() => securityJobSetup(editor)) |
| 201 | + }) |
| 202 | +}) |
0 commit comments