2024-07-08 13:52:20 +02:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Preloaded File - node ace make:preload rules/fileScan
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|*/
|
|
|
|
import { FieldContext } from '@vinejs/vine/types';
|
|
|
|
import vine, { errors } from '@vinejs/vine';
|
|
|
|
import { VineMultipartFile, isBodyParserFile } from '#providers/vinejs_provider';
|
|
|
|
import type { MultipartFile } from '@adonisjs/core/bodyparser';
|
|
|
|
import ClamScan from 'clamscan';
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
removeInfected: boolean;
|
|
|
|
host?: string;
|
|
|
|
port?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function fileScan(file: VineMultipartFile | unknown, options: Options, field: FieldContext) {
|
|
|
|
// if (typeof value !== 'string' && typeof value != 'number') {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
if (!isBodyParserFile(file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const validatedFile = file as MultipartFile;
|
2025-01-29 11:26:21 +01:00
|
|
|
|
2024-07-08 13:52:20 +02:00
|
|
|
try {
|
2025-01-29 11:26:21 +01:00
|
|
|
await scanFileForViruses(validatedFile.tmpPath, options);
|
2024-07-08 13:52:20 +02:00
|
|
|
} catch (error) {
|
|
|
|
// If the file is infected or there's an error scanning the file, throw a validation exception
|
|
|
|
// throw error;
|
|
|
|
field.report(`Upload error. Code: ${error.code} message: ${error.messages.uploadError}`, 'fileScan', field);
|
|
|
|
}
|
|
|
|
}
|
2025-01-29 11:26:21 +01:00
|
|
|
|
|
|
|
async function scanFileForViruses(filePath: string | undefined, options: Options): Promise<void> {
|
|
|
|
if (!filePath) {
|
|
|
|
throw new errors.E_VALIDATION_ERROR({ uploadError: 'File path is undefined!' });
|
|
|
|
}
|
2024-07-08 13:52:20 +02:00
|
|
|
const opts: ClamScan.Options = {
|
2025-01-29 11:26:21 +01:00
|
|
|
removeInfected: options.removeInfected, // If true, removes infected files
|
|
|
|
debugMode: false, // If true, deep scan folders recursively
|
2024-07-08 13:52:20 +02:00
|
|
|
scanRecursively: true, // If true, deep scan folders recursively
|
|
|
|
clamdscan: {
|
|
|
|
active: true, // If true, this module will consider using the clamdscan binary
|
2025-01-29 11:26:21 +01:00
|
|
|
host: options.host,
|
|
|
|
port: options.port,
|
2024-07-08 13:52:20 +02:00
|
|
|
multiscan: true, // Scan using all available cores! Yay!
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-01-29 11:26:21 +01:00
|
|
|
const clamscan = await new ClamScan().init(opts);
|
2024-07-08 13:52:20 +02:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
// You can re-use the `clamscan` object as many times as you want
|
|
|
|
// const version = await clamscan.getVersion();
|
|
|
|
// console.log(`ClamAV Version: ${version}`);
|
2025-01-29 11:26:21 +01:00
|
|
|
const result = await clamscan.isInfected(filePath);
|
|
|
|
if (!result || typeof result.isInfected === 'undefined') {
|
|
|
|
reject(new errors.E_VALIDATION_ERROR({ uploadError: 'Unexpected response from virus scan!' }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { file, isInfected, viruses } = result;
|
2024-07-08 13:52:20 +02:00
|
|
|
if (isInfected) {
|
2025-01-29 11:26:21 +01:00
|
|
|
console.log(`${file} is infected with ${viruses}!`); // reject(new ValidationException(true, { 'upload error': `File ${file} is infected!` }));
|
|
|
|
reject(new errors.E_VALIDATION_ERROR({ uploadError: `File ${file} is infected with ${viruses}!` }));
|
2024-07-08 13:52:20 +02:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// If there's an error scanning the file, throw a validation exception
|
|
|
|
// reject(new ValidationException(true, { 'upload error': `${error.message}` }));
|
|
|
|
reject(new errors.E_VALIDATION_ERROR({ uploadError: `${error.message}!` }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fileScanRule = vine.createRule(fileScan);
|
|
|
|
|
|
|
|
declare module '#providers/vinejs_provider' {
|
|
|
|
interface VineMultipartFile {
|
|
|
|
fileScan(options: Options): this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VineMultipartFile.macro('fileScan', function (this: VineMultipartFile, options: Options) {
|
|
|
|
return this.use(fileScanRule(options));
|
|
|
|
});
|