diff --git a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy index 6eaaa243..546e77fc 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy @@ -13,8 +13,9 @@ import java.security.NoSuchAlgorithmException class FileHasher { + public static final int MAX_PIECE_SIZE_POW2 = 37 /** max size of shared file is 128 GB */ - public static final long MAX_SIZE = 0x1L << 37 + public static final long MAX_SIZE = 0x1L << MAX_PIECE_SIZE_POW2 /** * @param size of the file to be shared @@ -26,7 +27,7 @@ class FileHasher { if (size <= 0x1 << 30) return 17 - for (int i = 31; i <= 37; i++) { + for (int i = 31; i <= MAX_PIECE_SIZE_POW2; i++) { if (size <= 0x1L << i) { return i-13 } diff --git a/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy b/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy index 44796210..467c1046 100644 --- a/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy +++ b/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy @@ -6,6 +6,7 @@ import javax.naming.directory.InvalidSearchControlsException import com.muwire.core.InfoHash import com.muwire.core.Persona +import com.muwire.core.files.FileHasher import com.muwire.core.util.DataUtil import net.i2p.data.Base64 @@ -30,12 +31,12 @@ class ResultsParser { private static parseV1(Persona p, UUID uuid, def json) { if (json.name == null) throw new InvalidSearchResultException("name missing") - if (json.size == null || json.size <= 0) - throw new InvalidSearchResultException("length missing") + if (json.size == null || json.size <= 0 || json.size > FileHasher.MAX_SIZE) + throw new InvalidSearchResultException("length missing or invalid, $json.size") if (json.infohash == null) throw new InvalidSearchResultException("infohash missing") - if (json.pieceSize == null || json.pieceSize <= 0) - throw new InvalidSearchResultException("pieceSize missing") + if (json.pieceSize == null || json.pieceSize <= 0 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) + throw new InvalidSearchResultException("pieceSize missing or invalid, $json.pieceSize") if (!(json.hashList instanceof List)) throw new InvalidSearchResultException("hashlist not a list") try { @@ -71,12 +72,12 @@ class ResultsParser { private static UIResultEvent parseV2(Persona p, UUID uuid, def json) { if (json.name == null) throw new InvalidSearchResultException("name missing") - if (json.size == null || json.size <= 0) - throw new InvalidSearchResultException("length missing") + if (json.size == null || json.size <= 0 || json.size > FileHasher.MAX_SIZE) + throw new InvalidSearchResultException("length missing or invalid $json.size") if (json.infohash == null) throw new InvalidSearchResultException("infohash missing") - if (json.pieceSize == null || json.pieceSize <= 0) - throw new InvalidSearchResultException("pieceSize missing") + if (json.pieceSize == null || json.pieceSize <= 0 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) + throw new InvalidSearchResultException("pieceSize missing or invalid, $json.pieceSize") if (json.hashList != null) throw new InvalidSearchResultException("V2 result with hashlist") try {