From d8c42ff3439e1c7d84b93e82e524b4faff61d502 Mon Sep 17 00:00:00 2001
From: Kyle Farris <kylefarris@gmail.com>
Date: Mon, 22 Jul 2024 13:40:50 -0400
Subject: [PATCH] Removed unnecessary file name sanitization that was causing
 issues when file names had spaces in them. Fixes #125

---
 index.js          |  5 ++---
 tests/eicargen.js |  2 ++
 tests/index.js    | 43 ++++++++++++++++++++++++-------------------
 3 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/index.js b/index.js
index 01bd355..900cbf8 100755
--- a/index.js
+++ b/index.js
@@ -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)
diff --git a/tests/eicargen.js b/tests/eicargen.js
index efc1cf6..2388c83 100644
--- a/tests/eicargen.js
+++ b/tests/eicargen.js
@@ -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 = [
@@ -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;
diff --git a/tests/index.js b/tests/index.js
index dff5e86..47075f6 100755
--- a/tests/index.js
+++ b/tests/index.js
@@ -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`;
@@ -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', () => {
@@ -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);
+            }
+        });
     });
 });