kylefarris / clamscan

A robust ClamAV virus scanning library supporting scanning files, directories, and streams with local sockets, local/remote TCP, and local clamscan/clamdscan binaries (with failover).
MIT License
230 stars 68 forks source link

scanDir do not return an array of files #87

Closed Annubis45 closed 2 years ago

Annubis45 commented 2 years ago

The result of doesn't return arrays of good files and bad files. It returns an array with the path scanned.

kylefarris commented 2 years ago

Please refer to the docs: https://github.com/kylefarris/clamscan#note

Relevant passage:

The goodFiles and badFiles parameters of the end_callback callback in this method will only contain the directories that were scanned in all but the following scenarios:

  • A file_callback callback is provided, and scanRecursively is set to true.
  • The scanner is set to clamdscan and scanRecursively is set to false.
Annubis45 commented 2 years ago

Hi,

Here my code:

        console.log('AnalyseFiles:');
        const clamscan = await new NodeClam().init({ scanRecursively: false});
        const version = await clamscan.getVersion();
        console.log(`ClamAV Version: ${version}`);

        clamscan.scanDir('./files/', (err, goodFiles, badFiles, viruses) => {
            if (err) return console.error(err);

            if (badFiles.length > 0) {
                console.log(`${path} was infected. The offending files (${badFiles.join (', ')}) have been quarantined.`);
                console.log(`Viruses Found: ${viruses.join(', ')}`);
            } else {
                console.log("Everything looks good! No problems here!.");
            }
        },
        (error, file, isInfected) =>{
            console.log(`${file} is ${isInfected ? 'infected' : 'clean'}`);
        });

and the result is :

ClamAV Version: ClamAV 0.103.5/26459/Sun Feb 20 10:22:37 2022
Attachment is infected
ContentVersion is infected
Everything looks good! No problems here!

files directory is : ---files ----- Attachment -----------file1 -----------file2 -----------file3 ----- ContentVersion -----------file4 -----------file5

kylefarris commented 2 years ago

You would need to set scanRecursively to true if you want to see the individual files that are infected.

Annubis45 commented 2 years ago

Thank you for your answser.

When I set scanRecursively to true, I get this error:

node_modules/clamscan/index.js:1529
    if (err) return hasCb ? endCb(err, goodFiles, badFiles,viruses, []) : reject(new NodeClamError({ badFiles }, err));
kylefarris commented 2 years ago

That can't be the entire error. That's just the stacktrace showing where the error was thrown. I'd need to know what error was actually thrown here as the could be any number of things.

Annubis45 commented 2 years ago

Hi,

I restarted eveything from scratch:

my code:

console.log('AnalyseFiles:');
const clamscan = await new NodeClam().init({ debugMode:true, scanRecursively: true});
const version = await clamscan.getVersion();
console.log(`ClamAV Version: ${version}`);
clamscan.scanDir('./filesdir/', (err, goodFiles, badFiles, viruses) => {
        if (err) return console.error(err);

        if (badFiles.length > 0) {
            console.log(`MYLOG -  it was infected. The offending files (${badFiles.join (', ')}) have been quarantined.`);
            console.log(`MYLOG - Viruses Found: ${viruses.join(', ')}`);
        } else {
            console.log("MYLOG - Everything looks good! No problems here!.");
        }
    },
    (error, file, isInfected) =>{
        console.log(`MYLOG - ${file} is ${isInfected ? 'infected' : 'clean'}`);
    });

My directory : ---filesdir ----- rep1 -----------file1 ----- rep2 -----------file1 -----------file2

And the result:

node-clam: DEBUG MODE ON
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan --version
ClamAV Version: ClamAV 0.103.5/26473/Sun Mar  6 10:27:13 2022

node-clam: Scanning a list of 6 passed files. [
  'filesdir',
  'filesdir/rep1',
  'filesdir/rep1/file1',
  'filesdir/rep2',
  'filesdir/rep2/file1',
  'filesdir/rep2/file2'
]
node-clam: Scanning filesdir/rep1/file1
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir/rep1/file1
node-clam: Scanning filesdir/rep2/file1
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir/rep2/file1
node-clam: Scanning filesdir/rep2/file2
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir/rep2/file2
node-clam: Scanning a list of 2 passed files. [ 'filesdir/rep1', 'filesdir/rep1/file1' ]
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir/rep1 filesdir/rep1/file1
node-clam: Scanning a list of 6 passed files. [
  'filesdir',
  'filesdir/rep1',
  'filesdir/rep1/file1',
  'filesdir/rep2',
  'filesdir/rep2/file1',
  'filesdir/rep2/file2'
]
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir filesdir/rep1 filesdir/rep1/file1 filesdir/rep2 filesdir/rep2/file1 filesdir/rep2/file2
node-clam: Scan Response:  /filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND
node-clam: File is INFECTED!
node-clam: File is OK!
node-clam: File is OK!
node-clam: Scanning a list of 3 passed files. [ 'filesdir/rep2', 'filesdir/rep2/file1', 'filesdir/rep2/file2' ]
node-clam: Configured clam command: /usr/bin/clamdscan --no-summary --fdpass --multiscan filesdir/rep2 filesdir/rep2/file1 filesdir/rep2/file2
node-clam: stdout: /filesdir/rep1: OK
/filesdir/rep1/file1: OK

node-clam: /filesdir/rep1 is OK!
node-clam: /filesdir/rep1/file1 is OK!
node-clam: stdout: /filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND
/filesdir/rep2/file1: OK
/filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND

node-clam: /filesdir/rep2/file2 is INFECTED!
node-clam: /filesdir/rep2/file1 is OK!
node-clam: /filesdir/rep2/file2 is INFECTED!
node-clam: stdout: /filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND
/filesdir/rep1: OK
/filesdir/rep1/file1: OK
/filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND
/filesdir/rep2/file1: OK
/filesdir/rep2/file2: Win.Test.EICAR_HDB-1 FOUND

node-clam: /filesdir/rep2/file2 is INFECTED!
node-clam: /filesdir/rep1 is OK!
node-clam: /filesdir/rep1/file1 is OK!
node-clam: /filesdir/rep2/file2 is INFECTED!
node-clam: /filesdir/rep2/file1 is OK!
node-clam: /filesdir/rep2/file2 is INFECTED!

And when I put breakpoints in callback functions, I never reach it.

I also tried the promise syntaxe and stay stuck in it. I got the same result and never reach the next line.

const {goodFiles, badFiles} = await clamscan.scanDir('./filesdir/');

I'm sorry if this thread has changed since the start. If you want I can open a new one.

Annubis45 commented 2 years ago

My analyse is : scandir need a resolve/reject result, but when I call it with my option, it does: scandir ==> scanFiles ==> doscan ==> ...

Scandir return a promise that make a "return scanfile(...)" and scanfile return a promise (line 1978) So, my guess is the promise of scandir is never resolved.

line 1978 : return this.scanFiles(files, endCb, fileCb);

IMO, it should be replaced by something like this:

const scanResult= await this.scanFiles(files, endCb, fileCb);
return hasCb
        ? endCb(null, [], [])
        : resolve(scanResult);
kylefarris commented 2 years ago

That's more like it. Yeah, from looking at your debug log, it all looks like it's working up to a point. I haven't had a chance to look at the source code yet but your analysis seems correct in theory. Why don't you see if you can fix the issue and run some tests? A PR would be really help since I'm quite busy this week.

Thanks for all your hard work.

Annubis45 commented 2 years ago

ok, I'll try !

kylefarris commented 2 years ago

FTR, this issue was fixed in v2.1.1.