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
236 stars 69 forks source link

Invalid information provided to connect to clamav service #59

Closed niftylettuce closed 4 years ago

niftylettuce commented 4 years ago

@kylefarris it seems like #57 and #58 does not resolve the issue I had described. Below is the stack trace:

Invalid information provided to connect to clamav service. A unix socket or port (+ optional host) is required!
Error: Invalid information provided to connect to clamav service. A unix socket or port (+ optional host) is required!
   at /var/www/production/source/node_modules/clamscan/index.js:1649:29
   at new Promise (<anonymous>)
   at NodeClam.scan_stream (/var/www/production/source/node_modules/clamscan/index.js:1636:16)
   at /var/www/production/source/node_modules/spamscanner/index.js:400:32
   at Array.map (<anonymous>)
   at SpamScanner.getVirusResults (/var/www/production/source/node_modules/spamscanner/index.js:392:26)
   at async Promise.all (index 4)
   at async SpamScanner.scan (/var/www/production/source/node_modules/spamscanner/index.js:1144:9)
   at async MessageSplitter.<anonymous> (/var/www/production/source/index.js:1087:18)

You can reference the source code at https://github.com/spamscanner/spamscanner to see how I implemented it, and let me know maybe if my usage is wrong? This to me seems like a bug with clamscan, since the config passed clearly has the socket defined, and the error message indicates it is instead not defined.

This is most likely from stuff happening in parallel to me it seems at least.

kylefarris commented 4 years ago

Actually, I think there might be an issue with your code...

In the following section, you're doing a Promise.all() but you don't need to since the map you're running doesn't return any unresolved promises. In fact, it doesn't return anything (which is invalid).

const clamscan = await this.clamscan.init(this.config.clamscan);
await Promise.all(
  mail.attachments.map(async (attachment, i) => {
    try {
      const stream = isStream(attachment.content)
        ? attachment.content
        : intoStream(attachment.content);
      const {
        is_infected: isInfected,
        viruses
      } = await clamscan.scan_stream(stream);
      const name = isSANB(attachment.filename)
        ? `"${attachment.filename}"`
        : `#${i + 1}`;
      if (isInfected)
        messages.push(
          `Attachment ${name} was infected with "${viruses}".`
        );
    } catch (err) {
      this.config.logger.error(err);
    }
  })
);

Something along these lines might be better...

const clamscan = await this.clamscan.init(this.config.clamscan);
try {
  const results = await Promise.all(
    mail.attachments.map((attachment, i) => {
      const stream = isStream(attachment.content)
        ? attachment.content
        : intoStream(attachment.content);
      return clamscan.scan_stream(stream);
    })
  );

  results.forEach((result, i) => {
    const attachment = mail.attachments[i];
    const name = isSANB(attachment.filename)
      ? `"${attachment.filename}"`
      : `#${i + 1}`;
    if (result.isInfected)
      messages.push(
        `Attachment ${name} was infected with "${result.viruses}".`
      );
  });
} catch (err) {
    this.config.logger.error(err);
}

Let me know if that helps at all.

kylefarris commented 4 years ago

Did this help at all? I'm inclined to close this ticket in a few days under the assumption that you've figured out what's going on.

niftylettuce commented 4 years ago

No, that did not help. You can use await Promise.all even if you don't return anything, that's totally valid.

kylefarris commented 4 years ago

You’re misunderstanding what I’m saying. It’s not the Promise.all that’s the problem, it’s you’re map. Nothing is actually going into your Promise.all method because the map isn’t retuning anything.

The way you currently have it, you might as well just run a forEach loop and skip the Promise.all altogether.

kylefarris commented 4 years ago

FWIW, I just wrote some tests to verify that the specific scenario you have is working and the tests pass. You can see the new tests here: https://github.com/kylefarris/clamscan/blob/7ea77aecbc28bf7f34dd64e8500adb2280e7b573/tests/index.js#L1262-L1302

There may be something else going on in your code but I'm not sure how to help beyond this. As far as I can test, the clamscan module is working fine. If you can provide a simplified repeatable test script showing that this module isn't working as expected, I'd be more than happy to address it (pull requests are also welcome as always).

niftylettuce commented 4 years ago

Thanks for following up here, will close, and follow up if I see it again.