use std::time::Duration;
use futures_util::future::BoxFuture;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use super::Upload;
use super::pipeline::{StepOutcome, UploadContext, UploadStep};
use crate::Result;
#[derive(Clone, Debug)]
enum Address {
Tcp(String),
#[cfg(unix)]
Unix(std::path::PathBuf),
}
#[derive(Clone, Debug)]
pub struct ClamAv {
address: Address,
timeout: Duration,
fail_open: bool,
}
impl ClamAv {
pub fn tcp(address: impl Into<String>) -> Self {
Self::at(Address::Tcp(address.into()))
}
#[cfg(unix)]
pub fn unix(path: impl Into<std::path::PathBuf>) -> Self {
Self::at(Address::Unix(path.into()))
}
fn at(address: Address) -> Self {
Self {
address,
timeout: Duration::from_secs(10),
fail_open: false,
}
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn fail_open(mut self) -> Self {
self.fail_open = true;
self
}
async fn scan(&self, bytes: &[u8]) -> std::io::Result<String> {
match &self.address {
Address::Tcp(address) => {
let stream = tokio::net::TcpStream::connect(address).await?;
instream(stream, bytes).await
}
#[cfg(unix)]
Address::Unix(path) => {
let stream = tokio::net::UnixStream::connect(path).await?;
instream(stream, bytes).await
}
}
}
}
#[derive(Debug, PartialEq)]
enum Verdict {
Clean,
Infected(String),
Failed(String),
}
fn verdict(reply: &str) -> Verdict {
let reply = reply.trim_matches(|c: char| c == '\0' || c.is_whitespace());
if reply.ends_with(" OK") {
Verdict::Clean
} else if let Some(found) = reply.strip_suffix(" FOUND") {
let name = found.rsplit_once(": ").map_or(found, |(_, name)| name);
Verdict::Infected(name.to_owned())
} else {
Verdict::Failed(reply.to_owned())
}
}
async fn instream<S>(mut stream: S, bytes: &[u8]) -> std::io::Result<String>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let sent = async {
stream.write_all(b"zINSTREAM\0").await?;
for chunk in bytes.chunks(64 * 1024) {
stream
.write_all(&(chunk.len() as u32).to_be_bytes())
.await?;
stream.write_all(chunk).await?;
}
stream.write_all(&0u32.to_be_bytes()).await?;
stream.flush().await
}
.await;
let mut reply = Vec::new();
let read = stream.read_to_end(&mut reply).await;
if reply.is_empty() {
sent?;
read?;
}
Ok(String::from_utf8_lossy(&reply).into_owned())
}
impl UploadStep for ClamAv {
fn process<'a>(
&'a self,
file: &'a mut Upload,
ctx: &'a UploadContext,
) -> BoxFuture<'a, Result<StepOutcome>> {
Box::pin(async move {
let reply = tokio::time::timeout(self.timeout, self.scan(file.bytes())).await;
let failure = match reply {
Ok(Ok(reply)) => match verdict(&reply) {
Verdict::Clean => return Ok(StepOutcome::Keep),
Verdict::Infected(name) => {
tracing::warn!(
field = ctx.field(),
file = file.file_name(),
signature = name,
"clamd found malware in an upload"
);
return Ok(StepOutcome::Reject(
"The file was refused: it contains malware.".into(),
));
}
Verdict::Failed(reply) => format!("clamd answered `{reply}`"),
},
Ok(Err(err)) => format!("clamd could not be reached: {err}"),
Err(_) => format!("clamd didn't answer within {:?}", self.timeout),
};
tracing::warn!(
field = ctx.field(),
file = file.file_name(),
"the upload was not scanned: {failure}"
);
if self.fail_open {
Ok(StepOutcome::Keep)
} else {
Ok(StepOutcome::Reject(
"The file could not be checked for malware. Try again later.".into(),
))
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_clamds_replies() {
assert_eq!(verdict("stream: OK\0"), Verdict::Clean);
assert_eq!(
verdict("stream: Eicar-Test-Signature FOUND\0"),
Verdict::Infected("Eicar-Test-Signature".into())
);
assert_eq!(
verdict("INSTREAM size limit exceeded. ERROR\0"),
Verdict::Failed("INSTREAM size limit exceeded. ERROR".into())
);
assert_eq!(verdict(""), Verdict::Failed(String::new()));
}
}