ensure downloaded file does not exceed Content-Length reported by HEAD (Daniel Gultsch)

This commit is contained in:
12aw 2022-04-07 22:47:47 +02:00
parent a64526b45a
commit d9311b747d
2 changed files with 21 additions and 3 deletions

View file

@ -117,11 +117,15 @@ public class HttpDownloadConnection implements Transferable {
if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
//TODO add auth tag size to knownFileSize
final Long knownFileSize = message.getFileParams().size;
Log.d(Config.LOGTAG, "knownFileSize: " + knownFileSize + ", body=" + message.getBody());
if (knownFileSize != null && interactive) {
this.file.setExpectedSize(knownFileSize);
if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL
&& this.file.getKey() != null) {
this.file.setExpectedSize(knownFileSize + 16);
} else {
this.file.setExpectedSize(knownFileSize);
}
download(true);
} else {
checkFileSize(interactive);
@ -235,6 +239,8 @@ public class HttpDownloadConnection implements Transferable {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
} else if (e instanceof FileWriterException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
} else if (e instanceof InvalidFileException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_invalid_file);
} else {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
}
@ -448,9 +454,12 @@ public class HttpDownloadConnection implements Transferable {
transmitted += count;
try {
outputStream.write(buffer, 0, count);
} catch (IOException e) {
} catch (final IOException e) {
throw new FileWriterException(file);
}
if (transmitted > expected) {
throw new InvalidFileException(String.format("File exceeds expected size of %d", expected));
}
updateProgress(Math.round(((double) transmitted / expected) * 100));
}
outputStream.flush();
@ -478,4 +487,12 @@ public class HttpDownloadConnection implements Transferable {
throw new IOException(String.format(Locale.ENGLISH, "HTTP Status code was %d", code));
}
}
private static class InvalidFileException extends IOException {
private InvalidFileException(final String message) {
super(message);
}
}
}

View file

@ -1201,4 +1201,5 @@
<string name="verifying_omemo_keys_trusted_source_account">You are about to verify the OMEMO keys of your own account. This is only secure if you followed this link from a trusted source where only you could have published this link.</string>
<string name="continue_btn">Continue</string>
<string name="participants">Participants</string>
<string name="download_failed_invalid_file">Download failed: Invalid file</string>
</resources>