From 85a47aa44742bee7d7deb55702ebfc5c61fbd44e Mon Sep 17 00:00:00 2001 From: Gabriel Casals <83978645+casalsgh@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:36:46 -0300 Subject: [PATCH] Update ClamAvIntegrationApp.ts --- ClamAvIntegrationApp.ts | 67 ++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/ClamAvIntegrationApp.ts b/ClamAvIntegrationApp.ts index b43f7f9..b07eaf8 100644 --- a/ClamAvIntegrationApp.ts +++ b/ClamAvIntegrationApp.ts @@ -11,18 +11,27 @@ import { import { App } from '@rocket.chat/apps-engine/definition/App'; import { FileUploadNotAllowedException } from '@rocket.chat/apps-engine/definition/exceptions'; import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata'; -import {SettingType} from '@rocket.chat/apps-engine/definition/settings'; +import { SettingType } from '@rocket.chat/apps-engine/definition/settings'; import { IFileUploadContext, IPreFileUpload } from '@rocket.chat/apps-engine/definition/uploads'; -import { createScanner, isCleanReply } from './clamd'; -import { notifyUser } from './src/message/notify'; +import { + createScanner as defaultCreateScanner, + isCleanReply as defaultIsCleanReply, + notifyUser as defaultNotifyUser, +} from './clamd'; -const CLAMAV_SERVER_HOST = 'clamav_server_host'; -const CLAMAV_SERVER_PORT = 'clamav_server_port'; +// Rest of the code export class ClamAvIntegrationApp extends App implements IPreFileUpload { + private readonly scanner: ClamScanner; + private readonly isCleanReply: typeof defaultIsCleanReply; + private readonly notifyUser: typeof defaultNotifyUser; + constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { super(info, logger, accessors); + this.scanner = defaultCreateScanner(); + this.isCleanReply = defaultIsCleanReply; + this.notifyUser = defaultNotifyUser; } public async executePreFileUpload(context: IFileUploadContext, read: IRead, http: IHttp, persis: IPersistence, modify: IModify): Promise { @@ -33,22 +42,44 @@ export class ClamAvIntegrationApp extends App implements IPreFileUpload { throw new Error('Missing ClamAv connection configuration'); } - const scanner = createScanner(host, port); - const result = await scanner.scanBuffer(context.content); + const scanner = this.scanner || defaultCreateScanner(); + const isCleanReply = this.isCleanReply || defaultIsCleanReply; + const notifyUser = this.notifyUser || defaultNotifyUser; - if (!result) { - throw new Error('Scan result was empty! Check the configuration and make sure it points to a valid ClamAV server.'); - } + try { + const result = await scanner.scanBuffer(context.content, host, port); + + if (!result) { + throw new Error('Scan result was empty! Check the configuration and make sure it points to a valid ClamAV server.'); + } + + let scanStatus = 'no virus found'; + + if (!isCleanReply(result)) { + scanStatus = 'virus found'; + const text = 'Virus found'; + const user = await read.getUserReader().getById(context.file.userId); + const room = await read.getRoomReader().getById(context.file.rid); + + if (room) { + await notifyUser({ app: this, read, modify, room, user, text }); + } + } + + // Additional logic to determine if undetermined if there is a virus + if (scanStatus !== 'virus found' && !isCleanReply(result)) { + scanStatus = 'undetermined'; + } - if (!isCleanReply(result)) { - const text = 'Virus found' - const user = await read.getUserReader().getById(context.file.userId) - const room = await read.getRoomReader().getById(context.file.rid) + console.log(`Scan status: ${scanStatus}`); - if(room) { - await notifyUser({ app: this, read, modify, room, user, text }) - } - throw new FileUploadNotAllowedException(text); + // You can use the scanStatus variable as needed for further processing + if (scanStatus === 'virus found') { + throw new FileUploadNotAllowedException(scanStatus); + } + } catch (error) { + console.error('Error occurred during file upload processing:', error); + // Handle the error appropriately based on your use case } }