Skip to content

Commit 9392a9a

Browse files
authored
Merge pull request #5386 from ctlai95/fix-zip-class-files
fix: add class files from local file ## Problem When zipping the project workspace, binary files are getting decoded/added to the zip artifacts as a utf-8 string which results in .class files being invalid and causing scans to fail. ## Solution Process binary files by adding them to the zip using the local file path instead of a buffer.
2 parents 2a4ff55 + 71fffd3 commit 9392a9a

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Security Scan: Fixed an issue scans were not able to succeed on Java projects with .class files"
4+
}

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface ZipMetadata {
3030
export const ZipConstants = {
3131
newlineRegex: /\r?\n/,
3232
gitignoreFilename: '.gitignore',
33+
knownBinaryFileExts: ['.class'],
3334
}
3435

3536
export class ZipUtil {
@@ -149,10 +150,15 @@ export class ZipUtil {
149150
this.getProjectScanPayloadSizeLimitInBytes()
150151
)
151152
for (const file of sourceFiles) {
152-
const isFileOpenAndDirty = this.isFileOpenAndDirty(file.fileUri)
153-
const fileContent = isFileOpenAndDirty ? await this.getTextContent(file.fileUri) : file.fileContent
154153
const zipEntryPath = this.getZipEntryPath(file.workspaceFolder.name, file.zipFilePath)
155-
this.processFile(zip, file.fileUri, fileContent, languageCount, zipEntryPath)
154+
155+
if (ZipConstants.knownBinaryFileExts.includes(path.extname(file.fileUri.fsPath))) {
156+
await this.processBinaryFile(zip, file.fileUri, zipEntryPath)
157+
} else {
158+
const isFileOpenAndDirty = this.isFileOpenAndDirty(file.fileUri)
159+
const fileContent = isFileOpenAndDirty ? await this.getTextContent(file.fileUri) : file.fileContent
160+
this.processTextFile(zip, file.fileUri, fileContent, languageCount, zipEntryPath)
161+
}
156162
}
157163
}
158164

@@ -161,11 +167,11 @@ export class ZipUtil {
161167
.filter((document) => document.uri.scheme === 'file')
162168
.filter((document) => vscode.workspace.getWorkspaceFolder(document.uri) === undefined)
163169
.forEach((document) =>
164-
this.processFile(zip, document.uri, document.getText(), languageCount, document.uri.fsPath)
170+
this.processTextFile(zip, document.uri, document.getText(), languageCount, document.uri.fsPath)
165171
)
166172
}
167173

168-
protected processFile(
174+
protected processTextFile(
169175
zip: admZip,
170176
uri: vscode.Uri,
171177
fileContent: string,
@@ -188,6 +194,21 @@ export class ZipUtil {
188194
zip.addFile(zipEntryPath, Buffer.from(fileContent, 'utf-8'))
189195
}
190196

197+
protected async processBinaryFile(zip: admZip, uri: vscode.Uri, zipEntryPath: string) {
198+
const fileSize = (await fs.stat(uri.fsPath)).size
199+
200+
if (
201+
this.reachSizeLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
202+
this.willReachSizeLimit(this._totalSize, fileSize)
203+
) {
204+
throw new ProjectSizeExceededError()
205+
}
206+
this._pickedSourceFiles.add(uri.fsPath)
207+
this._totalSize += fileSize
208+
209+
zip.addLocalFile(uri.fsPath, path.dirname(zipEntryPath))
210+
}
211+
191212
protected incrementCountForLanguage(uri: vscode.Uri, languageCount: Map<CodewhispererLanguage, number>) {
192213
const fileExtension = path.extname(uri.fsPath).slice(1)
193214
const language = runtimeLanguageContext.getLanguageFromFileExtension(fileExtension)

0 commit comments

Comments
 (0)