Skip to content

Commit babbc7d

Browse files
committed
Added limitation for decryption files. | #118.
1 parent 889caf9 commit babbc7d

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/Constants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public class Constants {
4444

4545
public static final String PREFERENCES_KEY_TEMP_LAST_AUTH_CREDENTIALS =
4646
"preferences_key_temp_last_auth_credentials";
47+
48+
/**
49+
* The max total size off all attachment which can be send via the app.
50+
*/
51+
public static final int MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES = 1024 * 1024 * 3;
52+
53+
/**
54+
* The max size off an attachment which can be decrypted via the app.
55+
*/
56+
public static final int MAX_ATTACHMENT_SIZE_WHICH_CAN_BE_DECRYPTED = 1024 * 1024 * 3;
4757
}

FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentDownloadManagerService.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.widget.Toast;
2727

2828
import com.flowcrypt.email.BuildConfig;
29+
import com.flowcrypt.email.Constants;
2930
import com.flowcrypt.email.R;
3031
import com.flowcrypt.email.api.email.JavaEmailConstants;
3132
import com.flowcrypt.email.api.email.model.AttachmentInfo;
@@ -463,6 +464,8 @@ public void run() {
463464
AccountDao accountDao = new AccountDaoSource().getAccountInformation(context, attachmentInfo.getEmail());
464465

465466
try {
467+
checkMaxDecryptedFileSize();
468+
466469
Session session = OpenStoreHelper.getAttachmentSession(accountDao);
467470
Store store = OpenStoreHelper.openAndConnectToStore(context, accountDao, session);
468471
IMAPFolder imapFolder = (IMAPFolder) store.getFolder(new ImapLabelsDaoSource()
@@ -542,6 +545,23 @@ public void setOnDownloadAttachmentListener(OnDownloadAttachmentListener onDownl
542545
this.onDownloadAttachmentListener = onDownloadAttachmentListener;
543546
}
544547

548+
/**
549+
* Check is decrypted file has size not more than
550+
* {@link Constants#MAX_ATTACHMENT_SIZE_WHICH_CAN_BE_DECRYPTED}. If the file greater then
551+
* {@link Constants#MAX_ATTACHMENT_SIZE_WHICH_CAN_BE_DECRYPTED} we throw an exception. This is only for files
552+
* with the "pgp" extension.
553+
*/
554+
private void checkMaxDecryptedFileSize() {
555+
if ("pgp".equalsIgnoreCase(FilenameUtils.getExtension(attachmentInfo.getName()))) {
556+
if (attachmentInfo.getEncodedSize() > Constants.MAX_ATTACHMENT_SIZE_WHICH_CAN_BE_DECRYPTED) {
557+
throw new IllegalArgumentException(context.getString(R.string
558+
.template_warning_max_attachments_size_for_decryption,
559+
FileUtils.byteCountToDisplaySize(Constants
560+
.MAX_ATTACHMENT_SIZE_WHICH_CAN_BE_DECRYPTED)));
561+
}
562+
}
563+
}
564+
545565
/**
546566
* Do decryption of the downloaded file if it need.
547567
*
@@ -594,10 +614,12 @@ private File decryptFileIfNeed(Context context, File file) throws IOException {
594614
* @param attachmentFile The file which will be removed.
595615
*/
596616
private void removeNotCompleteDownloadFile(File attachmentFile) {
597-
if (!attachmentFile.delete()) {
598-
Log.d(TAG, "Cannot delete file: " + attachmentFile);
599-
} else {
600-
Log.d(TAG, "Canceled attachment \"" + attachmentFile + "\" was deleted");
617+
if (attachmentFile != null && attachmentFile.exists()) {
618+
if (!attachmentFile.delete()) {
619+
Log.d(TAG, "Cannot delete file: " + attachmentFile);
620+
} else {
621+
Log.d(TAG, "Canceled attachment \"" + attachmentFile + "\" was deleted");
622+
}
601623
}
602624
}
603625

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/CreateMessageFragment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.widget.TextView;
3232
import android.widget.Toast;
3333

34+
import com.flowcrypt.email.Constants;
3435
import com.flowcrypt.email.R;
3536
import com.flowcrypt.email.api.email.FoldersManager;
3637
import com.flowcrypt.email.api.email.model.AttachmentInfo;
@@ -78,8 +79,6 @@ public class CreateMessageFragment extends BaseGmailFragment implements View.OnF
7879
private static final int REQUEST_CODE_IMPORT_PUBLIC_KEY = 101;
7980
private static final int REQUEST_CODE_GET_CONTENT_FOR_SENDING = 102;
8081

81-
private static final int MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES = 1024 * 1024 * 3;
82-
8382
private Js js;
8483
private OnMessageSendListener onMessageSendListener;
8584
private OnChangeMessageEncryptedTypeListener onChangeMessageEncryptedTypeListener;
@@ -236,7 +235,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
236235
} else {
237236
showInfoSnackbar(getView(),
238237
getString(R.string.template_warning_max_total_attachments_size,
239-
FileUtils.byteCountToDisplaySize(MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES)),
238+
FileUtils.byteCountToDisplaySize(
239+
Constants.MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES)),
240240
Snackbar.LENGTH_LONG);
241241
}
242242
} else {
@@ -679,7 +679,7 @@ private boolean isAttachmentCanBeAdded(AttachmentInfo newAttachmentInfo) {
679679

680680
totalSizeOfAttachments += newAttachmentInfo.getEncodedSize();
681681

682-
return totalSizeOfAttachments < MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES;
682+
return totalSizeOfAttachments < Constants.MAX_TOTAL_ATTACHMENT_SIZE_IN_BYTES;
683683
}
684684

685685
/**

FlowCrypt/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,5 @@
201201
<string name="simulate_app_crash">Simulate app crash</string>
202202
<string name="attachments">Attachments</string>
203203
<string name="download_attachments_notification_channel">The download attachments notification channel</string>
204+
<string name="template_warning_max_attachments_size_for_decryption">The app cannot decrypt an attachment greater than %1$s</string>
204205
</resources>

0 commit comments

Comments
 (0)