Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8559e656 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Better volume name argument checking.

Bug: 117932814
Test: atest MediaProviderTests
Test: atest cts/tests/tests/media/src/android/media/cts/MediaScanner*
Test: atest cts/tests/tests/provider/src/android/provider/cts/MediaStore*
Change-Id: I799d04a30e5328c1b9266357f192a3e65e24a28d
parent 1eda2caa
Loading
Loading
Loading
Loading
+45 −12
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Environment;
import android.os.FileUtils;
import android.os.OperationCanceledException;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
@@ -86,8 +87,10 @@ public final class MediaStore {
    /** A content:// style uri to the authority for the media provider */
    public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);

    private static final String VOLUME_INTERNAL = "internal";
    private static final String VOLUME_EXTERNAL = "external";
    /** {@hide} */
    public static final String VOLUME_INTERNAL = "internal";
    /** {@hide} */
    public static final String VOLUME_EXTERNAL = "external";

    /**
     * The method name used by the media scanner and mtp to tell the media provider to
@@ -1213,6 +1216,7 @@ public final class MediaStore {

    /** {@hide} */
    public static @NonNull String getVolumeName(@NonNull File path) {
        if (FileUtils.contains(Environment.getStorageDirectory(), path)) {
            final StorageManager sm = AppGlobals.getInitialApplication()
                    .getSystemService(StorageManager.class);
            final StorageVolume sv = sm.getStorageVolume(path);
@@ -1220,8 +1224,10 @@ public final class MediaStore {
                if (sv.isPrimary()) {
                    return VOLUME_EXTERNAL;
                } else {
                return sv.getUuid();
                    return checkArgumentVolumeName(sv.getUuid());
                }
            }
            throw new IllegalStateException("Unknown volume at " + path);
        } else {
            return VOLUME_INTERNAL;
        }
@@ -2928,14 +2934,41 @@ public final class MediaStore {
        if (uri.getAuthority().equals(AUTHORITY) && segments != null && segments.size() > 0) {
            return segments.get(0);
        } else {
            throw new IllegalArgumentException("Not a media Uri: " + uri);
            throw new IllegalArgumentException("Missing volume name: " + uri);
        }
    }

    /** {@hide} */
    public static @NonNull String checkArgumentVolumeName(@NonNull String volumeName) {
        if (TextUtils.isEmpty(volumeName)) {
            throw new IllegalArgumentException();
        }

        if (VOLUME_INTERNAL.equals(volumeName)) {
            return volumeName;
        } else if (VOLUME_EXTERNAL.equals(volumeName)) {
            return volumeName;
        }

        // When not one of the well-known values above, it must be a hex UUID
        for (int i = 0; i < volumeName.length(); i++) {
            final char c = volumeName.charAt(i);
            if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
                    || ('0' <= c && c <= '9') || (c == '-')) {
                continue;
            } else {
                throw new IllegalArgumentException("Invalid volume name: " + volumeName);
            }
        }
        return volumeName;
    }

    /** {@hide} */
    public static @NonNull File getVolumePath(@NonNull String volumeName)
            throws FileNotFoundException {
        Objects.requireNonNull(volumeName);
        if (TextUtils.isEmpty(volumeName)) {
            throw new IllegalArgumentException();
        }

        if (VOLUME_INTERNAL.equals(volumeName)) {
            return Environment.getDataDirectory();