为什么 Content Provider 没有权限并且 exported=true 任何应用程序都可以访问?

Why is Content Provider without permissions and with exported=true accessible to any app?

这是我运行了解Android内容提供者权限的测试:

App ProviderApp 清单:

<provider
    android:authorities="com.mycompany.myProviderApp"
    android:name="com.mycompany.myProviderApp.ContentProviderForMyOtherApps"
    android:exported="true"/>

我还实现了一个虚拟的 ContentProvider (ContentProviderForMyOtherApps) 和一个基本的 query 方法返回一个字符串 ProviderApp:

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    String[] cols = {"column1"};
    MatrixCursor cursor = new MatrixCursor(cols);
    MatrixCursor.RowBuilder builder = cursor.newRow();
    builder.add("HELLO!");
    return cursor;
}

App ClientApp 代码:

Cursor cursor = getContentResolver().query(Uri.parse("content://com.mycompany.myProviderApp"),null,null,null,null);
cursor.moveToFirst();
Log.d(TAG, cursor.getString(0)); // output: HELLO!

好的,所以一切正常,ClientApp 成功访问提供程序。

但根据以下摘录,我对文档的理解是 ClientApp 应该 拒绝 访问提供程序,因为:

文档摘录:

If a provider's application doesn't specify any permissions, then other applications have no access to the provider's data.

https://developer.android.com/guide/topics/providers/content-provider-basics.html#Permissions

android:exported

Whether the content provider is available for other applications to use: true: The provider is available to other applications. Any application can use the provider's content URI to access it, subject to the permissions specified for the provider.

https://developer.android.com/guide/topics/manifest/provider-element.html

为什么这段代码(供应商和客户声明无权限)实际上有效?

(我在文档中遗漏了什么?)

文档有错误。这个:

If a provider's application doesn't specify any permissions, then other applications have no access to the provider's data.

应读作:

If a provider's application doesn't specify any permissions, then other applications' access to the provider's data is determined solely by the android:exported value (true grants unlimited access to all applications; false blocks access by other applications) and android:grantUriPermissions value (which gets complicated).

恕我直言,整个部分需要重写。但是,关于您的测试,导出的 permission-less 提供程序是完全开放的,任何应用程序都可以不受惩罚地读写。