Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ClamAvIntegrationApp.ts #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions ClamAvIntegrationApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -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
}
}

Expand Down