Skip to content

Commit

Permalink
Removed unnecessary file name sanitization that was causing issues wh…
Browse files Browse the repository at this point in the history
…en file names had spaces in them. Fixes #125
  • Loading branch information
kylefarris committed Jul 22, 2024
1 parent 685ade3 commit d8c42ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,11 @@ class NodeClam {
return hasCb ? cb(err, file, null, []) : reject(err);
}
// Clean file name
file = file.trim().replace(/\s+/g, ' ');
file = file.trim();

// This is the function used for scanning viruses using the clamd command directly
const localScan = () => {
// console.log("Doing local scan...");
if (self.settings.debugMode) console.log(`${this.debugLabel}: Scanning ${file}`);
if (self.settings.debugMode) console.log(`${this.debugLabel}: [Local Scan] Scanning ${file}`);
// Build the actual command to run
const args = self._buildClamArgs(file);
if (self.settings.debugMode)
Expand Down
2 changes: 2 additions & 0 deletions tests/eicargen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const goodScanDir = `${__dirname}/good_scan_dir`;
const badScanDir = `${__dirname}/bad_scan_dir`;
const mixedScanDir = `${__dirname}/mixed_scan_dir`
const badScanFile = `${badScanDir}/bad_file_1.txt`;
const spacedVirusFile = `${badScanDir}/bad file 1.txt`;

// prettier-ignore
const eicarByteArray = [
Expand Down Expand Up @@ -43,6 +44,7 @@ const EicarGen = {
unlinkSync(`${mixedScanDir}/folder2/bad_file_2.txt`);
},
getStream: () => Readable.from(eicarBuffer),
writeFileSpaced: () => writeFileSync(spacedVirusFile, eicarBuffer.toString()),
};

module.exports = EicarGen;
43 changes: 24 additions & 19 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const goodScanFile2 = `${goodScanDir}/good_file_2.txt`;
const goodFileList = `${__dirname}/good_files_list.txt`;
const badScanDir = `${__dirname}/bad_scan_dir`;
const badScanFile = `${badScanDir}/bad_file_1.txt`;
const spacedVirusFile = `${badScanDir}/bad file 1.txt`;
const badFileList = `${__dirname}/bad_files_list.txt`;
const mixedScanDir = `${__dirname}/mixed_scan_dir`;
const passthruFile = `${__dirname}/output`;
Expand Down Expand Up @@ -708,25 +709,6 @@ describe('isInfected', () => {
if (fs.existsSync(badScanFile)) fs.unlinkSync(badScanFile);
}
});

// it('should respond with properties: "file" (string), "isInfected" (boolean), and "viruses" (array) when scanning with remote host', async () => {
// const clamdScanOptions = Object.assign({}, config.clamdscan, {active: true, socket: false, host: 'localhost', port: 3310});
// const options = Object.assign({}, config, {clamdscan: clamdScanOptions});
//
// try {
// clamscan = await resetClam(options);
// const {viruses, isInfected, file} = await clamscan.isInfected(goodScanFile);
// expect(viruses).to.be.an('array');
// expect(viruses).to.have.length(0);
// expect(isInfected).to.be.a('boolean');
// expect(isInfected).to.eql(false);
// expect(viruses).to.be.an('array');
// expect(viruses).to.have.length(0);
// } catch (e) {
// // console.error("Annoying error: ", e);
// throw e;
// }
// });
});

describe('Edge Cases', () => {
Expand Down Expand Up @@ -759,6 +741,29 @@ describe('isInfected', () => {
});
}
});

it('should be okay when scanning a file with consecutive (or any) spaces in it while doing a local scan', async () => {
// Make sure we're forced to scan locally
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true });

// Write virus file with spaces in its name
eicarGen.writeFileSpaced();

// Check if infected
try {
const { file, isInfected, viruses } = await clamscan.isInfected(spacedVirusFile);
expect(isInfected, 'isInfected should be true').to.be.true;
expect(file, 'spaced file name should be the same').to.eql(spacedVirusFile);
expect(viruses, 'viruses found should be an array').to.be.an('array');
expect(viruses, 'viruses found should have 1 element').to.have.length(1);
expect(viruses[0], 'element should match eicar').to.match(eicarSignatureRgx);
// eslint-disable-next-line no-useless-catch
} catch (err) {
throw err;
} finally {
if (fs.existsSync(spacedVirusFile)) fs.unlinkSync(spacedVirusFile);
}
});
});
});

Expand Down

0 comments on commit d8c42ff

Please sign in to comment.