安全编程4j-12

规则8.3 防止用户调试应用程序

说明:为了防止用户调试应用程序,在发布的Android应用程序中,相应代码中android:: debuggable标签必须设置为Disable。否则,如果调试功能使能,即使没有源代码, 用户也能调试应用;并利用物理访问权限运行应用程序和访问应用数据。

错误示例:应用程序调试功能使能

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.awesome">

    <application android:label="@string/app_name">

        android:debuggable="true">

    </application>

</manifest>

推荐做法:android:debuggable选项设置为false,防止进行调试

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.awesome">

    <application android:label="@string/app_name">

        android:debuggable="false">

    </application>

</manifest>

规则8.4 保护私有数据文件防止被第三方应用程序访问

说明:避免暴露存储的个人数据和受保护数据给第三方应用程序。为了达到这样的目的,通常采用以下手段:

保护个人数据和请求权限访问数据的方法有:

--对数据文件,共享引用或数据库使用MODE_PRIVATE(在app的私有数据目录使用openFileOutput(),openSharedPreferences(), 和openOrCreateDatabase() 方法创建文件);

--外部存储介质属于共享存储,未经用户同意不要存储用户个人数据到外部存储。

对其他应用程序写入的文件采用不信任的态度:

--不要存储全局可写或者在外部存储上的代码库;

--不要在全局可写或外部存储上文件中存储代码库的路径;

--不要在本地代码中处理可写文件中的数据;

错误示例:数据访问模式设置不够安全

FileOutputStream fos = openFileOutput("private_data.txt",

    Context.MODE_WORLD_WRITEABLE);

SharedPreferences prefs = getSharedPreferences("data",

    Context.MODE_WORLD_READABLE);

推荐做法:访问模式设置为MODE_PRIVATE

FileOutputStream fos = openFileOutput("private_data.txt",

    Context.MODE_PRIVATE);

SharedPreferences prefs = getSharedPreferences("data",

    Context.MODE_PRIVATE);

规则8.5 使用WebView类时不能使能JavaScript运行时环境

说明:Android应用程序在调用WebView类时,如果使能JavaScript运行时环境,可能会导致对这种缺陷的XSS和CSRF攻击;如果调用者要使用addJavascriptInterface()危险接口,必须保证使用HTTPS协议,避免隐私数据和敏感数据给JavaScript接口调用。

推荐做法

WebView webview = new WebView(this);

setContentView(webview);

 

getWindow().requestFeature(Window.FEATURE_PROGRESS);

webview.getSettings().setJavaScriptEnabled(true);

URL url = new URL("https://www.Test.com/");

HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();

规则8.6 防止Touch输入数据被窃取

说明:Android中有口令登陆和支付的应用程序,进行PIN输入时,需要对密码进行保护,避免输入密码时被恶意程序获取,造成敏感数据泄漏。因此,在AndroidManifest.xml文件中,必须设置android:filterTouchesWhenObscured值为true。

错误示例:

<manifest xmlns:android=http://schemas.android.com/apk/res/androidpackage="com.example.awesome">

    <permission android:name="com.example.awesome.EXAMPLE_PERM" android:label="@string/example_perm_desc" android:filterTouchesWhenObscured="false"android:protectionLevel="signature"/>

推荐做法

<manifest xmlns:android=http://schemas.android.com/apk/res/androidpackage="com.example.awesome">

    <permission android:name="com.example.awesome.EXAMPLE_PERM" android:label="@string/example_perm_desc" android:filterTouchesWhenObscured="true"android:protectionLevel="signature"/>

建议8.1 避免请求危险的权限来实现相应的功能

说明:Android应用程序在开发时,通过申请提供者的权限,实现对提供者特定功能的访问,但是,有部分危险权限使用会导致安全风险,可以用其它适当的方法来实现相应的功能。

如:

拍照:android.permission.CAMERA

读取电话本:android.permission.READ_CONTACTS

发送短信:android.permission. SEND_SMS

获取设备ID:android.permission.READ_PHONE_STATE

这些权限都不是必要的,可以使用Intent行为来实现上述功能。

推荐做法:使用Intent行为来实现拍照

// create Intent to take a picture and return control to thecalling application

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// create a file to save the image

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

// set the image file name

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// start the image capture Intent

startActivityForResult(intent,MY_REQUEST_CODE);


你可能感兴趣的:(java,安全编程)