Limit zip file size

pull/207/head
M66B 3 years ago
parent 5576acfa9f
commit 1ac876fa1a

@ -1177,7 +1177,9 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
} }
}); });
tvUnzipHint.setText(getString(R.string.title_advanced_unzip_hint, MessageHelper.MAX_UNZIP)); tvUnzipHint.setText(getString(R.string.title_advanced_unzip_hint,
Integer.toString(MessageHelper.MAX_UNZIP_COUNT),
Helper.humanReadableByteCount(MessageHelper.MAX_UNZIP_SIZE)));
swAttachmentsAlt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { swAttachmentsAlt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override

@ -50,7 +50,6 @@ import com.sun.mail.util.FolderClosedIOException;
import com.sun.mail.util.MessageRemovedIOException; import com.sun.mail.util.MessageRemovedIOException;
import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
@ -99,8 +98,6 @@ import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.activation.FileDataSource; import javax.activation.FileDataSource;
@ -149,7 +146,8 @@ public class MessageHelper {
static final String HEADER_CORRELATION_ID = "X-Correlation-ID"; static final String HEADER_CORRELATION_ID = "X-Correlation-ID";
static final int MAX_SUBJECT_AGE = 48; // hours static final int MAX_SUBJECT_AGE = 48; // hours
static final int DEFAULT_THREAD_RANGE = 7; // 2^7 = 128 days static final int DEFAULT_THREAD_RANGE = 7; // 2^7 = 128 days
static final int MAX_UNZIP = 10; static final int MAX_UNZIP_COUNT = 20;
static final long MAX_UNZIP_SIZE = 1000 * 1000 * 1000L;
static final List<String> RECEIVED_WORDS = Collections.unmodifiableList(Arrays.asList( static final List<String> RECEIVED_WORDS = Collections.unmodifiableList(Arrays.asList(
"from", "by", "via", "with", "id", "for" "from", "by", "via", "with", "id", "for"
@ -3395,46 +3393,48 @@ public class MessageHelper {
Log.i("Gzipped attachment seq=" + local.sequence + " " + name + ":" + total); Log.i("Gzipped attachment seq=" + local.sequence + " " + name + ":" + total);
if (name == null && if (total <= MAX_UNZIP_SIZE) {
local.name != null && local.name.endsWith(".gz")) if (name == null &&
name = local.name.substring(0, local.name.length() - 3); local.name != null && local.name.endsWith(".gz"))
name = local.name.substring(0, local.name.length() - 3);
EntityAttachment attachment = new EntityAttachment();
attachment.message = local.message; EntityAttachment attachment = new EntityAttachment();
attachment.sequence = local.sequence; attachment.message = local.message;
attachment.subsequence = 1; attachment.sequence = local.sequence;
attachment.name = name; attachment.subsequence = 1;
attachment.type = Helper.guessMimeType(name); attachment.name = name;
if (total >= 0) attachment.type = Helper.guessMimeType(name);
attachment.size = total; if (total >= 0)
attachment.id = db.attachment().insertAttachment(attachment); attachment.size = total;
attachment.id = db.attachment().insertAttachment(attachment);
File efile = attachment.getFile(context);
Log.i("Gunzipping to " + efile); File efile = attachment.getFile(context);
Log.i("Gunzipping to " + efile);
int last = 0;
long size = 0; int last = 0;
try (OutputStream os = new FileOutputStream(efile)) { long size = 0;
byte[] buffer = new byte[Helper.BUFFER_SIZE]; try (OutputStream os = new FileOutputStream(efile)) {
for (int len = gzip.read(buffer); len != -1; len = gzip.read(buffer)) { byte[] buffer = new byte[Helper.BUFFER_SIZE];
size += len; for (int len = gzip.read(buffer); len != -1; len = gzip.read(buffer)) {
os.write(buffer, 0, len); size += len;
os.write(buffer, 0, len);
if (total > 0) {
int progress = (int) (size * 100 / total); if (total > 0) {
if (progress / 20 > last / 20) { int progress = (int) (size * 100 / total);
last = progress; if (progress / 20 > last / 20) {
db.attachment().setProgress(attachment.id, progress); last = progress;
db.attachment().setProgress(attachment.id, progress);
}
} }
} }
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
db.attachment().setAvailable(attachment.id, true); // unrecoverable
} }
} catch (Throwable ex) {
Log.e(ex);
db.attachment().setError(attachment.id, Log.formatThrowable(ex));
db.attachment().setAvailable(attachment.id, true); // unrecoverable
}
db.attachment().setDownloaded(attachment.id, efile.length()); db.attachment().setDownloaded(attachment.id, efile.length());
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
db.attachment().setWarning(local.id, Log.formatThrowable(ex)); db.attachment().setWarning(local.id, Log.formatThrowable(ex));
@ -3447,12 +3447,15 @@ public class MessageHelper {
int count = 0; int count = 0;
ArchiveEntry entry; ArchiveEntry entry;
while ((entry = ais.getNextEntry()) != null) while ((entry = ais.getNextEntry()) != null)
if (ais.canReadEntryData(entry) && !entry.isDirectory()) if (ais.canReadEntryData(entry) && !entry.isDirectory()) {
if (++count > MAX_UNZIP) if (entry.getSize() > MAX_UNZIP_SIZE)
count = MAX_UNZIP_COUNT;
if (++count > MAX_UNZIP_COUNT)
break; break;
}
Log.i("Zip entries=" + count); Log.i("Zip entries=" + count);
if (count <= MAX_UNZIP) { if (count <= MAX_UNZIP_COUNT) {
fis.getChannel().position(0); fis.getChannel().position(0);
ais = new ArchiveStreamFactory().createArchiveInputStream( ais = new ArchiveStreamFactory().createArchiveInputStream(

@ -808,7 +808,7 @@
<string name="title_advanced_monospaced_pre_hint">Plain text only messages will be considered as preformatted</string> <string name="title_advanced_monospaced_pre_hint">Plain text only messages will be considered as preformatted</string>
<string name="title_advanced_placeholders_hint">This applies to reformatted messages only</string> <string name="title_advanced_placeholders_hint">This applies to reformatted messages only</string>
<string name="title_advanced_inline_hint">Inline images are images included in the message</string> <string name="title_advanced_inline_hint">Inline images are images included in the message</string>
<string name="title_advanced_unzip_hint">The contents of zip files with up to %1$d files will automatically be shown</string> <string name="title_advanced_unzip_hint">The contents of zip files with more than %1$s files or with files larger than %2$s will not be shown</string>
<string name="title_advanced_parse_classes_hint">This will more accurately display messages, but possibly with a delay</string> <string name="title_advanced_parse_classes_hint">This will more accurately display messages, but possibly with a delay</string>
<string name="title_advanced_language_detection_hint">Language detection support depends on the device manufacturer</string> <string name="title_advanced_language_detection_hint">Language detection support depends on the device manufacturer</string>

Loading…
Cancel
Save