Check granted URI permisions

pull/212/head
M66B 3 years ago
parent 241778348a
commit c7a8855cd7

@ -398,6 +398,8 @@ public class ActivitySignature extends ActivityBase {
NoStreamException.check(uri, this); NoStreamException.check(uri, this);
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!Helper.isPersisted(this, uri, true, false))
throw new IllegalStateException("No permission granted to access selected image " + uri);
int start = etText.getSelectionStart(); int start = etText.getSelectionStart();
if (etText.isRaw()) if (etText.isRaw())

@ -551,6 +551,8 @@ public class FragmentAnswer extends FragmentBase {
NoStreamException.check(uri, getContext()); NoStreamException.check(uri, getContext());
getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!Helper.isPersisted(getContext(), uri, true, false))
throw new IllegalStateException("No permission granted to access selected image " + uri);
int start = etText.getSelectionStart(); int start = etText.getSelectionStart();
SpannableStringBuilder ssb = new SpannableStringBuilderEx(etText.getText()); SpannableStringBuilder ssb = new SpannableStringBuilderEx(etText.getText());

@ -797,6 +797,9 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
if ("content".equals(uri.getScheme())) { if ("content".equals(uri.getScheme())) {
try { try {
getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!Helper.isPersisted(getContext(), uri, true, false))
Log.unexpectedError(getParentFragmentManager(),
new IllegalStateException("No permission granted to access selected sound " + uri));
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }

@ -817,6 +817,9 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
if ("content".equals(uri.getScheme())) { if ("content".equals(uri.getScheme())) {
try { try {
getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); getContext().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!Helper.isPersisted(getContext(), uri, true, false))
Log.unexpectedError(getParentFragmentManager(),
new IllegalStateException("No permission granted to access selected sound " + uri));
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }

@ -41,6 +41,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.UriPermission;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -2611,6 +2612,23 @@ public class Helper {
return (Looper.myLooper() == Looper.getMainLooper()); return (Looper.myLooper() == Looper.getMainLooper());
} }
static boolean isPersisted(Context context, Uri uri, boolean read, boolean write) {
try {
List<UriPermission> uperms = context.getContentResolver().getPersistedUriPermissions();
for (UriPermission uperm : uperms)
if (uperm.getUri().equals(uri)) {
boolean canRead = uperm.isReadPermission();
boolean canWrite = uperm.isWritePermission();
Log.i(uri + " read=" + read + "/" + canRead + " write=" + write + "/" + canWrite);
return (!read || canRead) && (!write || canWrite);
}
return false;
} catch (Throwable ex) {
Log.e(ex);
return !BuildConfig.DEBUG;
}
}
// Cryptography // Cryptography
static String sha256(String data) throws NoSuchAlgorithmException { static String sha256(String data) throws NoSuchAlgorithmException {

Loading…
Cancel
Save