Merge branch '2143-rethrow-security-exceptions-when-opening-images' into 'master'

Rethrow SecurityExceptions when opening images

Closes #2143

See merge request briar/briar!1626
This commit is contained in:
Torsten Grote
2022-04-18 12:12:20 +00:00
2 changed files with 19 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ import static org.briarproject.bramble.util.IoUtils.tryToClose;
import static org.briarproject.bramble.util.LogUtils.logDuration; import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now; import static org.briarproject.bramble.util.LogUtils.now;
import static org.briarproject.briar.android.attachment.media.ImageCompressor.MIME_TYPE;
@NotNullByDefault @NotNullByDefault
class AttachmentCreationTask { class AttachmentCreationTask {
@@ -100,14 +101,17 @@ class AttachmentCreationTask {
if (!asList(getSupportedImageContentTypes()).contains(contentType)) { if (!asList(getSupportedImageContentTypes()).contains(contentType)) {
throw new UnsupportedMimeTypeException(contentType, uri); throw new UnsupportedMimeTypeException(contentType, uri);
} }
InputStream is = contentResolver.openInputStream(uri); InputStream is;
if (is == null) throw new IOException(); try {
is = imageCompressor is = contentResolver.openInputStream(uri);
.compressImage(is, contentType); if (is == null) throw new IOException();
} catch (SecurityException e) {
throw new IOException(e);
}
is = imageCompressor.compressImage(is, contentType);
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
AttachmentHeader h = messagingManager AttachmentHeader h = messagingManager.addLocalAttachment(groupId,
.addLocalAttachment(groupId, timestamp, timestamp, MIME_TYPE, is);
ImageCompressor.MIME_TYPE, is);
tryToClose(is, LOG, WARNING); tryToClose(is, LOG, WARNING);
logDuration(LOG, "Storing attachment", start); logDuration(LOG, "Storing attachment", start);
return h; return h;

View File

@@ -231,9 +231,14 @@ class SettingsViewModel extends DbViewModel implements EventListener {
if (!asList(getSupportedImageContentTypes()).contains(contentType)) { if (!asList(getSupportedImageContentTypes()).contains(contentType)) {
throw new UnsupportedMimeTypeException(contentType, uri); throw new UnsupportedMimeTypeException(contentType, uri);
} }
InputStream is = contentResolver.openInputStream(uri); InputStream is;
if (is == null) throw new IOException( try {
"ContentResolver returned null when opening InputStream"); is = contentResolver.openInputStream(uri);
if (is == null) throw new IOException(
"ContentResolver returned null when opening InputStream");
} catch (SecurityException e) {
throw new IOException(e);
}
InputStream compressed = imageCompressor.compressImage(is, contentType); InputStream compressed = imageCompressor.compressImage(is, contentType);
runOnDbThread(() -> { runOnDbThread(() -> {