一、前言
Public Services存在以下风险:可以被多个应用使用,可能收到恶意的信息,例如恶意软件发送的。安全注意事项如下
1、显式设置exported属性为true。@b@2、处理收到的intent,确认真实性。@b@3、当返回结果时,不要包含敏感信息。
二、原代码示例
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.service.publicservice" >@b@ @b@ <application@b@ android:icon="@drawable/ic_launcher"@b@ android:label="@string/app_name"@b@ android:allowBackup="false" >@b@ @b@ <!-- Most standard Service -->@b@ <!-- *** POINT 1 *** Explicitly set the exported attribute to true. -->@b@ <service android:name=".PublicStartService" android:exported="true">@b@ <intent-filter>@b@ <action android:name="org.jssec.android.service.publicservice.action.startservice" />@b@ </intent-filter>@b@ </service>@b@ @b@ <!-- Public Service derived from IntentService class -->@b@ <!-- *** POINT 1 *** Explicitly set the exported attribute to true. -->@b@ <service android:name=".PublicIntentService" android:exported="true">@b@ <intent-filter>@b@ <action android:name="org.jssec.android.service.publicservice.action.intentservice" />@b@ </intent-filter>@b@ </service>@b@ @b@ </application>@b@ @b@</manifest>
2.PublicIntentService.java
package org.jssec.android.service.publicservice;@b@ @b@import android.app.IntentService;@b@import android.content.Intent;@b@import android.widget.Toast;@b@ @b@public class PublicIntentService extends IntentService{@b@ /**@b@ * Default constructor must be provided when a service extends IntentService class.@b@ * If it does not exist, an error occurs.@b@ */@b@ @b@ public PublicIntentService() {@b@ super("CreatingTypeBService");@b@ }@b@ @b@ // The onCreate gets called only one time when the Service starts.@b@ @Override@b@ public void onCreate() {@b@ super.onCreate();@b@ @b@ Toast.makeText(this, this.getClass().getSimpleName() + " - onCreate()", Toast.LENGTH_SHORT).show(); }@b@ @b@ // The onHandleIntent gets called each time after the startService gets called.@b@ @Override@b@ protected void onHandleIntent(Intent intent) {@b@ // *** POINT 2 *** Handle intent carefully and securely.@b@ // Since it's public service, the intent may come from malicious application.@b@ // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@ String param = intent.getStringExtra("PARAM");@b@ Toast.makeText(this, String.format("Recieved parameter ¥"%s¥"", param), Toast.LENGTH_LONG).show();@b@ }@b@ @b@ // The onDestroy gets called only one time when the service stops.@b@ @Override@b@ public void onDestroy() {@b@ Toast.makeText(this, this.getClass().getSimpleName() + " - onDestroy()", Toast.LENGTH_SHORT).show(); }@b@}
三、安全代码示例
1、不要发送敏感数据。@b@2、当接收结果时,处理结果数据,保证真实性和可用性。
AndroidManifest.xml@b@ @b@<?xml version="1.0" encoding="utf-8"?>@b@<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jssec.android.service.publicserviceuser" >@b@ @b@ <application@b@ android:icon="@drawable/ic_launcher"@b@ android:label="@string/app_name"@b@ android:allowBackup="false" >@b@ <activity@b@ android:name=".PublicUserActivity"@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@ </application>@b@ @b@</manifest>
PublicUserActivity.java@b@ @b@package org.jssec.android.service.publicserviceuser;@b@ @b@import android.app.Activity;@b@import android.content.Intent;@b@import android.os.Bundle;@b@import android.view.View;@b@ @b@public class PublicUserActivity extends Activity {@b@ @b@ // Using Service Info@b@ private static final String TARGET_PACKAGE = "org.jssec.android.service.publicservice";@b@ private static final String TARGET_START_CLASS = "org.jssec.android.service.publicservice.PublicStartService";@b@ private static final String TARGET_INTENT_CLASS = "org.jssec.android.service.publicservice.PublicIntentService";@b@ @b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ @b@ setContentView(R.layout.publicservice_activity);@b@ }@b@ @b@ // --- StartService control ---@b@ @b@ public void onStartServiceClick(View v) {@b@ Intent intent = new Intent("org.jssec.android.service.publicservice.action.startservice");@b@ @b@ // *** POINT 4 *** Call service by Explicit Intent@b@ intent.setClassName(TARGET_PACKAGE, TARGET_START_CLASS);@b@ // *** POINT 5 *** Do not send sensitive information.@b@ intent.putExtra("PARAM", "Not sensitive information");@b@ @b@ startService(intent);@b@ // *** POINT 6 *** When receiving a result, handle the result data carefully and securely.@b@ // This sample code uses startService(), so receiving no result.@b@ }@b@ @b@ public void onStopServiceClick(View v) {@b@ doStopService();@b@ }@b@ @b@ // --- IntentService control ---@b@ @b@ public void onIntentServiceClick(View v) {@b@ Intent intent = new Intent("org.jssec.android.service.publicservice.action.intentservice");@b@ @b@ // *** POINT 4 *** Call service by Explicit Intent@b@ intent.setClassName(TARGET_PACKAGE, TARGET_INTENT_CLASS);@b@ @b@ // *** POINT 5 *** Do not send sensitive information.@b@ intent.putExtra("PARAM", "Not sensitive information");@b@ @b@ startService(intent);@b@ }@b@ @b@ @Override@b@ public void onStop(){@b@ super.onStop();@b@ // Stop service if the service is running.@b@ doStopService();@b@ }@b@ @b@ // Stop service@b@ private void doStopService() {@b@ Intent intent = new Intent("org.jssec.android.service.publicservice.action.startservice");@b@ @b@ // *** POINT 4 *** Call service by Explicit Intent@b@ intent.setClassName(TARGET_PACKAGE, TARGET_START_CLASS);@b@ @b@ stopService(intent);@b@ }@b@}