diff --git a/clamd/instream_test.go b/clamd/instream_test.go index 0898f58d..2a097ddd 100644 --- a/clamd/instream_test.go +++ b/clamd/instream_test.go @@ -24,7 +24,7 @@ func TestClamdInstream(t *testing.T) { name: "eicarcom2.zip", filepath: "testdata/eicarcom2.zip", expectedError: &clamd.VirusFoundError{ - Name: "Eicar-Signature", + Name: "Win.Test.EICAR_HDB-1", }, }, } diff --git a/clamd/version.go b/clamd/version.go index 3660f381..eb10ac79 100644 --- a/clamd/version.go +++ b/clamd/version.go @@ -10,11 +10,7 @@ type Version struct { } func parseVersion(response []byte) (Version, error) { - parts := strings.Split(string(response), " ") - if len(parts) != 2 { //nolint:gomnd - return Version{}, fmt.Errorf("failed to parse version: %s", string(response)) //nolint:goerr113 - } - + parts := strings.SplitN(string(response), " ", 2) //nolint:gomnd return Version{ Version: parts[1], }, nil diff --git a/client/update_file_test.go b/client/update_file_test.go index 8fcf9b90..9367c1b9 100644 --- a/client/update_file_test.go +++ b/client/update_file_test.go @@ -115,10 +115,10 @@ func TestUpdateFile(t *testing.T) { expectedErr: &client.APIResponseError{ StatusCode: http.StatusForbidden, ErrorResponse: &controller.ErrorResponse{ - Message: `virus found: Eicar-Signature`, + Message: `virus found: Win.Test.EICAR_HDB-1`, Data: map[string]any{ "file": "eicarcom2.zip", - "virus": "Eicar-Signature", + "virus": "Win.Test.EICAR_HDB-1", }, }, }, diff --git a/client/upload_file_test.go b/client/upload_file_test.go index c849c145..a3f2eeb8 100644 --- a/client/upload_file_test.go +++ b/client/upload_file_test.go @@ -127,10 +127,10 @@ func TestUploadFile(t *testing.T) { expectedErr: &client.APIResponseError{ StatusCode: http.StatusForbidden, ErrorResponse: &controller.ErrorResponse{ - Message: `virus found: Eicar-Signature`, + Message: `virus found: Win.Test.EICAR_HDB-1`, Data: map[string]any{ "file": "eicarcom2.zip", - "virus": "Eicar-Signature", + "virus": "Win.Test.EICAR_HDB-1", }, }, Response: &controller.UploadFileResponse{ @@ -159,10 +159,10 @@ func TestUploadFile(t *testing.T) { }, }, Error: &controller.ErrorResponse{ - Message: `virus found: Eicar-Signature`, + Message: `virus found: Win.Test.EICAR_HDB-1`, Data: map[string]any{ "file": "eicarcom2.zip", - "virus": "Eicar-Signature", + "virus": "Win.Test.EICAR_HDB-1", }, }, }, diff --git a/controller/update_file.go b/controller/update_file.go index 3e694b67..d7da4407 100644 --- a/controller/update_file.go +++ b/controller/update_file.go @@ -97,15 +97,9 @@ func (ctrl *Controller) updateFile(ctx *gin.Context) (FileMetadata, *APIError) { } defer fileContent.Close() - if err := ctrl.av.ScanReader(fileContent); err != nil { - err.SetData("file", file.Name) - - if err := ctrl.reportVirus( - ctx, file.ID, file.Name, err.GetDataString("virus"), ctx.Request.Header, - ); err != nil { - return FileMetadata{}, err - } - + if err := ctrl.scanAndReportVirus( + ctx, fileContent, file.ID, file.Name, ctx.Request.Header, + ); err != nil { return FileMetadata{}, err } diff --git a/controller/upload_file.go b/controller/upload_file.go index 0ef0531a..d4def0cf 100644 --- a/controller/upload_file.go +++ b/controller/upload_file.go @@ -62,20 +62,26 @@ func (ctrl *Controller) getMultipartFile(file fileData) (multipart.File, string, return fileContent, mt.String(), nil } -func (ctrl *Controller) reportVirus( +func (ctrl *Controller) scanAndReportVirus( ctx context.Context, + fileContent multipart.File, fileID string, filename string, - virus string, headers http.Header, ) *APIError { - userSession := GetUserSession(headers) + if err := ctrl.av.ScanReader(fileContent); err != nil { + err.SetData("file", filename) + + userSession := GetUserSession(headers) + + if err := ctrl.metadataStorage.InsertVirus( + ctx, fileID, filename, err.GetDataString("virus"), userSession, + http.Header{"x-hasura-admin-secret": []string{ctrl.hasuraAdminSecret}}, + ); err != nil { + err := err.ExtendError("problem inserting virus into database") + return err + } - if err := ctrl.metadataStorage.InsertVirus( - ctx, fileID, filename, virus, userSession, - http.Header{"x-hasura-admin-secret": []string{ctrl.hasuraAdminSecret}}, - ); err != nil { - err := err.ExtendError("problem inserting virus into database") return err } @@ -104,15 +110,9 @@ func (ctrl *Controller) processFile( return FileMetadata{}, err } - if err := ctrl.av.ScanReader(fileContent); err != nil { - err.SetData("file", file.Name) - - if err := ctrl.reportVirus( - ctx, file.ID, file.Name, err.GetDataString("virus"), headers, - ); err != nil { - return FileMetadata{}, err - } - + if err := ctrl.scanAndReportVirus( + ctx, fileContent, file.ID, file.Name, headers, + ); err != nil { return FileMetadata{}, err }