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

Commit 5b1bba2a authored by Felipe Leme's avatar Felipe Leme
Browse files

Fixed ScopedAccessProvider.getPackagesCursor().

It was returning packages with any granted URI permission, not just to
scoped access directories.

Bug: 72055774
Fixes: 110867799

Test: atest CtsAppSecurityHostTestCases:ScopedDirectoryAccessTest#testResetDoNotAskAgain,testResetGranted
Test: manual verification

Change-Id: Id72fb591fcef47c2140fdbf5303976818cfb5237
parent 94785ef3
Loading
Loading
Loading
Loading
+44 −1
Original line number Diff line number Diff line
@@ -162,7 +162,11 @@ public class ScopedAccessProvider extends ContentProvider {

        final List<GrantedUriPermission> amPkgs = am.getGrantedUriPermissions(null).getList();
        if (!amPkgs.isEmpty()) {
            amPkgs.forEach((perm) -> pkgs.add(perm.packageName));
            amPkgs.forEach((perm) -> {
                if (isScopedAccessPermission(perm)) {
                    pkgs.add(perm.packageName);
                }
            });
        }

        if (ArrayUtils.isEmpty(pkgs)) {
@@ -372,6 +376,45 @@ public class ScopedAccessProvider extends ContentProvider {
        return permissions;
    }

    private boolean isScopedAccessPermission(GrantedUriPermission uriPermission) {
        // TODO(b/72055774): we should query AUTHORITY_STORAGE or call DocumentsContract instead of
        // hardcoding the logic here.
        final Uri uri = uriPermission.uri;
        final String authority = uri.getAuthority();
        if (!Providers.AUTHORITY_STORAGE.equals(authority)) {
            return false;
        }
        final List<String> pathSegments = uri.getPathSegments();
        if (pathSegments.size() < 2) {
            return false;
        }
        // TODO(b/72055774): make PATH_TREE private again if not used anymore
        if (!DocumentsContract.PATH_TREE.equals(pathSegments.get(0))) {
            return false;
        }

        final String[] uuidAndDir = pathSegments.get(1).split(":");
        // uuid and dir are either UUID:DIR (for scoped directory) or UUID: (for full volume)
        if (uuidAndDir.length != 1 && uuidAndDir.length != 2) {
            return false;
        }
        final String uuid, dir;
        if (Providers.ROOT_ID_HOME.equals(uuidAndDir[0])) {
            uuid = null;
            dir = Environment.DIRECTORY_DOCUMENTS;
        } else {
            uuid = Providers.ROOT_ID_DEVICE.equals(uuidAndDir[0])
                    ? null // primary
                    : uuidAndDir[0]; // external volume
            dir = uuidAndDir.length == 1 ? null : uuidAndDir[1];
        }
        if ((dir == null && uuid != null) || !Environment.isStandardDirectory(dir)) {
            return false;
        }

        return true;
    }

    @Override
    public String getType(Uri uri) {
        return null;