2222import java .io .FileNotFoundException ;
2323import java .io .IOException ;
2424import java .io .InputStream ;
25- import java .util .Collections ;
26- import java .util .HashSet ;
27- import java .util .Set ;
2825
2926import android .content .ContentResolver ;
3027import android .content .Context ;
3128import android .content .Intent ;
32- import android .content .pm .PackageInfo ;
33- import android .content .pm .PackageManager ;
34- import android .content .pm .PackageManager .NameNotFoundException ;
35- import android .content .pm .ProviderInfo ;
3629import android .content .res .AssetFileDescriptor ;
3730import android .net .Uri ;
38- import android .os .Bundle ;
3931import android .os .ParcelFileDescriptor ;
4032import android .support .annotation .NonNull ;
4133import android .support .annotation .Nullable ;
8072 * </p>
8173 */
8274public abstract class SafeContentResolver {
83- private static final String META_DATA_KEY_ALLOW_INTERNAL_ACCESS =
84- "de.cketti.safecontentresolver.ALLOW_INTERNAL_ACCESS" ;
85-
86-
8775 private final ContentResolver contentResolver ;
88- private final Set < String > blacklistedAuthorities ;
76+ private final Blacklist blacklist ;
8977
9078
9179 /**
@@ -107,7 +95,7 @@ public static SafeContentResolver newInstance(@NonNull Context context) {
10795
10896 protected SafeContentResolver (@ NonNull Context context ) {
10997 this .contentResolver = context .getContentResolver ();
110- this .blacklistedAuthorities = getBlacklistedContentProviderAuthorities (context );
98+ this .blacklist = new Blacklist (context );
11199 }
112100
113101 /**
@@ -136,7 +124,7 @@ public InputStream openInputStream(@NonNull Uri uri) throws FileNotFoundExceptio
136124 String scheme = uri .getScheme ();
137125 if (ContentResolver .SCHEME_CONTENT .equals (scheme )) {
138126 String authority = uri .getAuthority ();
139- if (blacklistedAuthorities . contains (authority )) {
127+ if (blacklist . isBlacklisted (authority )) {
140128 throw new FileNotFoundException ("content URI is owned by the application itself. " +
141129 "Content provider is not whitelisted: " + authority );
142130 }
@@ -164,37 +152,4 @@ public InputStream openInputStream(@NonNull Uri uri) throws FileNotFoundExceptio
164152 }
165153
166154 protected abstract int getFileUidOrThrow (@ NonNull FileDescriptor fileDescriptor ) throws FileNotFoundException ;
167-
168- private Set <String > getBlacklistedContentProviderAuthorities (Context context ) {
169- ProviderInfo [] providers = getProviderInfo (context );
170-
171- Set <String > blacklistedAuthorities = new HashSet <>(providers .length );
172- for (ProviderInfo providerInfo : providers ) {
173- if (!isContentProviderWhitelisted (providerInfo )) {
174- String [] authorities = providerInfo .authority .split (";" );
175- Collections .addAll (blacklistedAuthorities , authorities );
176- }
177- }
178-
179- return blacklistedAuthorities ;
180- }
181-
182- private ProviderInfo [] getProviderInfo (Context context ) {
183- try {
184- PackageManager packageManager = context .getPackageManager ();
185- String packageName = context .getPackageName ();
186- PackageInfo packageInfo = packageManager .getPackageInfo (packageName ,
187- PackageManager .GET_PROVIDERS | PackageManager .GET_META_DATA );
188-
189- ProviderInfo [] providers = packageInfo .providers ;
190- return providers != null ? providers : new ProviderInfo [0 ];
191- } catch (NameNotFoundException e ) {
192- throw new RuntimeException (e );
193- }
194- }
195-
196- private boolean isContentProviderWhitelisted (ProviderInfo providerInfo ) {
197- Bundle metaData = providerInfo .metaData ;
198- return metaData != null && metaData .getBoolean (META_DATA_KEY_ALLOW_INTERNAL_ACCESS , false );
199- }
200155}
0 commit comments