移動應用安全開發指南(Android)--數據驗證

概述 html

移動應用每每經過數據的發送、接收和處理來完成一系列功能,一般狀況下,處理的數據絕大部分都來源於外部(好比網絡、內部或外部存儲和用戶輸入等),對這些數據處理不當會致使各類各樣的漏洞和風險,比代碼執行和信息泄漏等等。web

安全準則 正則表達式

A.      通常性原則:對全部外部數據進行數據驗證,數據驗證建議採用白名單的方式,即只容許指定的字符經過,其它字符一概過濾,同時驗證數據的長度和類型等。sql

B.      使用參數化查詢語句防止SQL注入(參考附錄3)。chrome

C.      使用WebViews時,將JavaScript和插件支持特性關掉(此爲默認值),若是必定要打開該特性,須要對輸出的內容進行html轉義。數據庫

D.      設置禁止WebViews對文件系統進行存取,以防止文件包含漏洞。瀏覽器

E.       activity內使用Intent Filteractiondata進行過濾。安全

詳細描述 網絡

A.      進行白名單數據驗證的經常使用方法是使用正則表達式,樣例可參考附錄6app

B.      預編譯查詢能夠有效防止應用把數據看成代碼來執行,樣例可參考附錄3

C.      Html輸出轉義規則以下(詳情可參考附錄7):

&à&

< à &lt;

> à &gt;

à &quot;

à &#x27;

/ à &#x2F;

D.      webview.getSettings().setAllowFileAccess(false)可用於關閉WebViews對文件系統進行存取的能力。

備註

這裏的外部數據包含但不限於如下來源的數據:網絡、存儲(特別注意外部存儲)、文件、數據庫、IPC和用戶輸入等。

如顯示格式不正確,建議使用chrome瀏覽器

附錄:

3SQLite防止SQL注入漏洞方案

11.1.1、使用SQLiteDatabase對象自帶的防SQL注入的方法,好比query(),insert(),updatedelete()

DatabaseHelper dbhelper = new DatabaseHelper(SqliteActivity.this,"sqliteDB");

SQLiteDatabase db = dbhelper.getReadableDatabase();

 

/*查詢操做,userInputIDuserInputName是用戶可控制的輸入數據 */

Cursor cur = db.query("user", new String[]{"id","name"}, "id=? and name=?", new String[]{userInputID,userInputName}, null, null, null);

 

/* 插入記錄操做*/

ContentValues val = new ContentValues();

val.put("id", userInputID);

val.put("name", userInputName);

db.insert("user", null, val);

 

/*更新記錄操做*/

ContentValues val = new ContentValues();

val.put("id", userInputName);

db.update("user", val, "id=?", new String[]{userInputID });

 

/*刪除記錄操做*/

db.delete("user", "id=? and name=?", new String[]{userInputID , userInputName });

 

11.1.2、正確地使用SQLiteDatabase對象的rawQuery()方法(僅以查詢爲例):

DatabaseHelper dbhelper = new DatabaseHelper(SqliteActivity.this,"sqliteDB");

SQLiteDatabase db = dbhelper.getReadableDatabase();

 

/* userInputIDuserInputName是用戶可控制的輸入數據*/

String[] selectionArgs = new String[]{userInputID , userInputName };

String sql = "select * from user where id=? and name=?";//正確!此處綁定變量

Cursor curALL = db.rawQuery(sql, selectionArgs);

 

11.1.3、如下爲錯誤案例!僅供參考:

DatabaseHelper dbhelper = new DatabaseHelper(SqliteActivity.this,"sqliteDB");

SQLiteDatabase db = dbhelper.getReadableDatabase();

 

/*案例1:錯誤地使用rawQuery(),未綁定參數*/

String sql = "select * from user where id='" + userInputID +"'";//錯誤!動態拼接,未綁定變量

Cursor curALL = db.rawQuery(sql, null);

 

/*案例2:使用execSQL()方法*/

String sql = "INSERT INTO user values('"+userInputID +"','"+userInputName +"')";//錯誤同上

db.execSQL(sql);

7HTML轉義輸出樣例:

7.1、使用Android自帶的html轉義函數:

         String str = TextUtils.htmlEncode(str);

7.2、自定義的html轉義函數:

         public static String htmlEncode(String str){

                   char c ;

                   StringBuilder sb = new StringBuilder();

                   int len = str.length();

                   for(int i=0 ;i<len; i++){

                            c = str.charAt(i);

                           

                            switch(c){

                            case '<':

                                     sb.append("&lt;");

                                     break;

                            case '>':

                                     sb.append("&gt;");

                                     break;

                            case '"':

                                     sb.append("&quot;");

                                     break;

                            case '\'':

                                     sb.append("&#x27;");

                                     break;

                            case '/':

                                     sb.append("&#x2F;");

                                     break;

                            case '&':

                                     sb.append("&amp;");

                                     break;

                            default:

                                     sb.append(c);

                            }                

                   }

                   return sb.toString();

}

相關文章
相關標籤/搜索