Skip to content

Commit

Permalink
Handle and catch size limit exceeded error from clamd server.
Browse files Browse the repository at this point in the history
  • Loading branch information
anttivi-solita committed Aug 22, 2016
1 parent 98e33bb commit 4466548
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ services:
- docker

before_install:
- docker pull mkodockx/docker-clamav
- docker run -d -p 3310:3310 mkodockx/docker-clamav
- docker pull lokori/clamav-java
- docker run -d -p 3310:3310 lokori/clamav-java

10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mkodockx/docker-clamav

MAINTAINER Antti Virtanen <[email protected]>

# update maximum stream length to much smaller than default
# automated tests run smoother this way
RUN sed -i 's/^StreamMaxLength .*$/StreamMaxLength 50100/g' /etc/clamav/clamd.conf

# av daemon bootstrapping
CMD ["/bootstrap.sh"]
10 changes: 9 additions & 1 deletion src/main/java/fi/solita/clamav/ClamAVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public byte[] scan(InputStream is) throws IOException {

// read reply
try (InputStream clamIs = s.getInputStream()) {
return readAll(clamIs);
return assertSizeLimit(readAll(clamIs));
}
}
}
Expand All @@ -128,6 +128,14 @@ public static boolean isCleanReply(byte[] reply) {
String r = new String(reply, StandardCharsets.US_ASCII);
return (r.contains("OK") && !r.contains("FOUND"));
}


private byte[] assertSizeLimit(byte[] reply) {
String r = new String(reply, StandardCharsets.US_ASCII);
if (r.startsWith("INSTREAM size limit exceeded."))
throw new ClamAVSizeLimitException("Clamd size limit exceeded. Full reply from server: " + r);
return reply;
}

// byte conversion based on ASCII character set regardless of the current system locale
private static byte[] asBytes(String s) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/fi/solita/clamav/ClamAVSizeLimitException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fi.solita.clamav;

/**
* Thrown if clamd size limit is exceeded during scanning.
* <p>
* See <a href="http://linux.die.net/man/5/clamd.conf">clamd man page</a> for further information.
*/
public class ClamAVSizeLimitException extends RuntimeException {
private static final long serialVersionUID = 1L;

public ClamAVSizeLimitException(String msg) {
super(msg);
}
}
6 changes: 6 additions & 0 deletions src/test/java/fi/solita/clamav/InstreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public void testZeroBytes() throws UnknownHostException, IOException {
byte[] r = scan(new byte[]{});
assertTrue(ClamAVClient.isCleanReply(r));
}

@Test(expected = ClamAVSizeLimitException.class)
public void testSizeLimit() throws UnknownHostException, IOException {
byte[] overlyLarge = new byte[60000];
scan(overlyLarge);
}
}

0 comments on commit 4466548

Please sign in to comment.