首页

关于android开发中如何正确使用Private Content Provider安全用法及代码示例

标签:android,Private Content Provider,安全     发布时间:2017-10-31   

一、注意事项

1、不能在android2.2及更早的版本使用。

2、显示设置exported属性为false。

3、注意收到数据的真实性。

4、敏感数据可以在同一个应用中发送和接收。

二、原代码示例

1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>@b@    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jssec.android.provider.privateprovider">@b@ @b@    <application@b@        android:icon="@drawable/ic_launcher"@b@        android:label="@string/app_name" >@b@        <activity@b@            android:name=".PrivateUserActivity"@b@            android:label="@string/app_name"@b@            android:exported="true" >@b@            <intent-filter>@b@                <action android:name="android.intent.action.MAIN" />@b@                <category android:name="android.intent.category.LAUNCHER" />@b@            </intent-filter>@b@        </activity>@b@ @b@        <!-- *** POINT 2 *** Explicitly set the exported attribute to false. -->@b@        <provider@b@            android:name=".PrivateProvider"@b@            android:authorities="org.jssec.android.provider.privateprovider"@b@            android:exported="false" />@b@    </application>@b@</manifest>

2.PrivateProvider.java

package org.jssec.android.provider.privateprovider;@b@ @b@import android.content.ContentProvider;@b@import android.content.ContentUris;@b@import android.content.ContentValues;@b@import android.content.UriMatcher;@b@import android.database.Cursor;@b@import android.database.MatrixCursor;@b@import android.net.Uri;@b@ @b@public class PrivateProvider extends ContentProvider {@b@ @b@    public static final String AUTHORITY = "org.jssec.android.provider.privateprovider";@b@    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.org.jssec.contenttype";@b@    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.org.jssec.contenttype";@b@ @b@    // Expose the interface that the Content Provider provides.@b@    public interface Download {@b@        public static final String PATH = "downloads";@b@        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH);@b@    }@b@    public interface Address {@b@        public static final String PATH = "addresses";@b@        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH);@b@    }@b@ @b@    // UriMatcher@b@    private static final int DOWNLOADS_CODE = 1;@b@    private static final int DOWNLOADS_ID_CODE = 2;@b@    private static final int ADDRESSES_CODE = 3;@b@    private static final int ADDRESSES_ID_CODE = 4;@b@    private static UriMatcher sUriMatcher;@b@    static {@b@        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);@b@        sUriMatcher.addURI(AUTHORITY, Download.PATH, DOWNLOADS_CODE);@b@        sUriMatcher.addURI(AUTHORITY, Download.PATH + "/#", DOWNLOADS_ID_CODE);@b@        sUriMatcher.addURI(AUTHORITY, Address.PATH, ADDRESSES_CODE);@b@        sUriMatcher.addURI(AUTHORITY, Address.PATH + "/#", ADDRESSES_ID_CODE);@b@    }@b@ @b@    // Since this is a sample program,@b@    // query method returns the following fixed result always without using database.@b@    private static MatrixCursor sAddressCursor = new MatrixCursor(new String[] { "_id", "city" });@b@    static {@b@        sAddressCursor.addRow(new String[] { "1", "New York" });@b@        sAddressCursor.addRow(new String[] { "2", "Longon" });@b@        sAddressCursor.addRow(new String[] { "3", "Paris" });@b@    }@b@    private static MatrixCursor sDownloadCursor = new MatrixCursor(new String[] { "_id", "path" });@b@    static {@b@        sDownloadCursor.addRow(new String[] { "1", "/sdcard/downloads/sample.jpg" });@b@        sDownloadCursor.addRow(new String[] { "2", "/sdcard/downloads/sample.txt" }); }@b@ @b@    @Override@b@    public boolean onCreate() {@b@        return true;@b@    }@b@    @Override@b@    public String getType(Uri uri) {@b@        // *** POINT 3 *** Handle the received request data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Here, whether uri is within expectations or not, is verified by UriMatcher#match() and switch case. // Checking for other parameters are omitted here, due to sample.@b@        // Please refer to "3.2 Handle Input Data Carefully and Securely."@b@        // *** POINT 4 *** Sensitive information can be sent since it is sending and receiving all within the same app lication.@b@        // However, the result of getType rarely has the sensitive meaning.@b@        switch (sUriMatcher.match(uri)) {@b@        case DOWNLOADS_CODE:@b@        case ADDRESSES_CODE:@b@            return CONTENT_TYPE;@b@ @b@        case DOWNLOADS_ID_CODE:@b@        case ADDRESSES_ID_CODE:@b@            return CONTENT_ITEM_TYPE;@b@ @b@        default:@b@            throw new IllegalArgumentException("Invalid URI:" + uri);@b@        }@b@    }@b@ @b@    @Override@b@    public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {@b@ @b@        // *** POINT 3 *** Handle the received request data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Here, whether uri is within expectations or not, is verified by UriMatcher#match() and switch case. // Checking for other parameters are omitted here, due to sample.@b@        // Please refer to "3.2 Handle Input Data Carefully and Securely."@b@        // *** POINT 4 *** Sensitive information can be sent since it is sending and receiving all within the same app lication.@b@        // It depends on application whether the query result has sensitive meaning or not.@b@        switch (sUriMatcher.match(uri)) {@b@        case DOWNLOADS_CODE:@b@        case DOWNLOADS_ID_CODE:@b@            return sDownloadCursor;@b@ @b@        case ADDRESSES_CODE:@b@        case ADDRESSES_ID_CODE:@b@            return sAddressCursor;@b@ @b@        default:@b@            throw new IllegalArgumentException("Invalid URI:" + uri);@b@        }@b@    }@b@ @b@    @Override@b@    public Uri insert(Uri uri, ContentValues values) {@b@ @b@        // *** POINT 3 *** Handle the received request data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Here, whether uri is within expectations or not, is verified by UriMatcher#match() and switch case. // Checking for other parameters are omitted here, due to sample.@b@        // Please refer to "3.2 Handle Input Data Carefully and Securely."@b@        // *** POINT 4 *** Sensitive information can be sent since it is sending and receiving all within the same app lication.@b@        // It depends on application whether the issued ID has sensitive meaning or not.@b@        switch (sUriMatcher.match(uri)) {@b@        case DOWNLOADS_CODE:@b@            return ContentUris.withAppendedId(Download.CONTENT_URI, 3);@b@ @b@        case ADDRESSES_CODE:@b@            return ContentUris.withAppendedId(Address.CONTENT_URI, 4);@b@ @b@        default:@b@            throw new IllegalArgumentException("Invalid URI:" + uri);@b@        }@b@    }@b@ @b@    @Override@b@    public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {@b@ @b@        // *** POINT 3 *** Handle the received request data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Here, whether uri is within expectations or not, is verified by UriMatcher#match() and switch case. // Checking for other parameters are omitted here, due to sample.@b@        // Please refer to "3.2 Handle Input Data Carefully and Securely."@b@        // *** POINT 4 *** Sensitive information can be sent since it is sending and receiving all within the same app lication.@b@        // It depends on application whether the number of updated records has sensitive meaning or not.@b@        switch (sUriMatcher.match(uri)) {@b@        case DOWNLOADS_CODE:@b@            return 5; // Return number of updated records@b@ @b@        case DOWNLOADS_ID_CODE:@b@            return 1;@b@ @b@        case ADDRESSES_CODE:@b@            return 15;@b@ @b@        case ADDRESSES_ID_CODE:@b@            return 1;@b@ @b@        default:@b@            throw new IllegalArgumentException("Invalid URI:" + uri);@b@        }@b@    }@b@ @b@    @Override@b@    public int delete(Uri uri, String selection, String[] selectionArgs) {@b@ @b@        // *** POINT 3 *** Handle the received request data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Here, whether uri is within expectations or not, is verified by UriMatcher#match() and switch case. // Checking for other parameters are omitted here, due to sample.@b@        // Please refer to "3.2 Handle Input Data Carefully and Securely."@b@        // *** POINT 4 *** Sensitive information can be sent since it is sending and receiving all within the same app lication.@b@        // It depends on application whether the number of deleted records has sensitive meaning or not.@b@        switch (sUriMatcher.match(uri)) {@b@        case DOWNLOADS_CODE:@b@            return 10; // Return number of deleted records@b@ @b@        case DOWNLOADS_ID_CODE:@b@            return 1;@b@ @b@        case ADDRESSES_CODE:@b@            return 20;@b@ @b@        case ADDRESSES_ID_CODE:@b@            return 1;@b@ @b@        default:@b@            throw new IllegalArgumentException("Invalid URI:" + uri);@b@        }@b@    }@b@}

3.安全使用PrivateUserActivity.java - 敏感数据可以在同一个应用内发送 、处理收到的数据,确认其正确性。

package org.jssec.android.provider.privateprovider;@b@ @b@import android.app.Activity;@b@import android.database.Cursor;@b@import android.net.Uri;@b@import android.os.Bundle;@b@import android.view.View;@b@import android.widget.TextView;@b@ @b@public class PrivateUserActivity extends Activity {@b@   @b@        public void onQueryClick(View view) {@b@ @b@        logLine("[Query]");@b@ @b@        Cursor cursor = null;@b@        try {@b@ @b@            // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same applica@b@                cursor = getContentResolver().query( PrivateProvider.Download.CONTENT_URI, null, null, null, null);@b@ @b@            // *** POINT 6 *** Handle received result data carefully and securely,@b@            // even though the data comes from the same application.@b@            // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@            if (cursor == null) {@b@                logLine(" null cursor");@b@            } else {@b@                boolean moved = cursor.moveToFirst();@b@                while (moved) {@b@                    logLine(String.format(" %d, %s", cursor.getInt(0), cursor.getString(1)));@b@                    moved = cursor.moveToNext();@b@                }@b@            }@b@        }@b@        finally {@b@            if (cursor != null) cursor.close();@b@        }@b@    }@b@ @b@    public void onInsertClick(View view) {@b@ @b@        logLine("[Insert]");@b@ @b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application@b@ @b@        Uri uri = getContentResolver().insert(PrivateProvider.Download.CONTENT_URI, null);@b@ @b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@        logLine(" uri:" + uri);@b@    }@b@ @b@    public void onUpdateClick(View view) {@b@ @b@        logLine("[Update]");@b@ @b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application int count = getContentResolver().update(PrivateProvider.Download.CONTENT_URI, null, null, null);@b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely." logLine(String.format(" %s records updated", count));@b@ @b@    public void onDeleteClick(View view) {@b@ @b@        logLine("[Delete]");@b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application@b@ @b@        int count = getContentResolver().delete( PrivateProvider.Download.CONTENT_URI, null, null);@b@ @b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@        logLine(String.format(" %s records deleted", count));@b@    }@b@ @b@    private TextView mLogView;@b@ @b@    @Override@b@    public void onCreate(Bundle savedInstanceState) {@b@        super.onCreate(savedInstanceState);@b@        setContentView(R.layout.main);@b@        mLogView = (TextView)findViewById(R.id.logview);@b@    }@b@ @b@    private void logLine(String line) {@b@        mLogView.append(line);@b@        mLogView.append("¥n");@b@    }@b@}

三、安全代码示例

1、敏感数据可以在同一个应用内发送。@b@2、处理收到的数据,确认其正确性。
PrivateUserActivity.java@b@ @b@package org.jssec.android.provider.privateprovider;@b@ @b@import android.app.Activity;@b@import android.database.Cursor;@b@import android.net.Uri;@b@import android.os.Bundle;@b@import android.view.View;@b@import android.widget.TextView;@b@ @b@public class PrivateUserActivity extends Activity {@b@   @b@        public void onQueryClick(View view) {@b@ @b@        logLine("[Query]");@b@ @b@        Cursor cursor = null;@b@        try {@b@ @b@            // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same applica@b@                cursor = getContentResolver().query( PrivateProvider.Download.CONTENT_URI, null, null, null, null);@b@ @b@            // *** POINT 6 *** Handle received result data carefully and securely,@b@            // even though the data comes from the same application.@b@            // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@            if (cursor == null) {@b@                logLine(" null cursor");@b@            } else {@b@                boolean moved = cursor.moveToFirst();@b@                while (moved) {@b@                    logLine(String.format(" %d, %s", cursor.getInt(0), cursor.getString(1)));@b@                    moved = cursor.moveToNext();@b@                }@b@            }@b@        }@b@        finally {@b@            if (cursor != null) cursor.close();@b@        }@b@    }@b@ @b@    public void onInsertClick(View view) {@b@ @b@        logLine("[Insert]");@b@ @b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application@b@ @b@        Uri uri = getContentResolver().insert(PrivateProvider.Download.CONTENT_URI, null);@b@ @b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@        logLine(" uri:" + uri);@b@    }@b@ @b@    public void onUpdateClick(View view) {@b@ @b@        logLine("[Update]");@b@ @b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application int count = getContentResolver().update(PrivateProvider.Download.CONTENT_URI, null, null, null);@b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely." logLine(String.format(" %s records updated", count));@b@ @b@    public void onDeleteClick(View view) {@b@ @b@        logLine("[Delete]");@b@        // *** POINT 5 *** Sensitive information can be sent since the destination provider is in the same application@b@ @b@        int count = getContentResolver().delete( PrivateProvider.Download.CONTENT_URI, null, null);@b@ @b@        // *** POINT 6 *** Handle received result data carefully and securely,@b@        // even though the data comes from the same application.@b@        // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@        logLine(String.format(" %s records deleted", count));@b@    }@b@ @b@    private TextView mLogView;@b@ @b@    @Override@b@    public void onCreate(Bundle savedInstanceState) {@b@        super.onCreate(savedInstanceState);@b@        setContentView(R.layout.main);@b@        mLogView = (TextView)findViewById(R.id.logview);@b@    }@b@ @b@    private void logLine(String line) {@b@        mLogView.append(line);@b@        mLogView.append("¥n");@b@    }@b@}