測試和偵錯功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
OWASP 類別:MASVS-CODE:程式碼品質
總覽
發布包含測試或偵錯功能的正式版本,可能會對應用程式的安全防護機制造成負面影響。這些功能有助於開發人員在新版本發布之前或之後,找出並識別應用程式預期用途中的錯誤,因此不應開放公開存取。
舉例來說,測試/偵錯功能包括:
- 隱藏的選單
- 啟用偵錯記錄檔的選項
- 修改應用程式流程的選項
- 規避付款或訂閱程序的選項
- 規避驗證程序的選項
- 應用程式專屬活動的測試
惡意使用者可能會利用以上所有方法,修改應用程式的預期流程或擷取系統資訊,依需求設計進一步攻擊。
公開測試或偵錯功能所帶來的風險,可能會因偵錯功能本身相關聯的動作而異。
應用程式面臨的另一個風險,是在 AndroidManifest.xml 元素 <application>
內設定的 android:debuggable 屬性。如 android:debuggable 文章所述,在設定上述值的情況下部署正式版應用程式,會讓惡意使用者得以存取以下原本不開放存取的管理資源。
影響
惡意使用者在正式版中與測試或偵錯功能互動,可能會導致非預期的結果。任何動作所造成的影響,都與指派給功能的權限直接相關。權限越高,漏洞攻擊帶來的影響就越大。應用程式內的這類功能可用於規避多項保護措施、略過付費牆、擷取系統/使用者相關資訊,或是觸發測試活動。
因應措施
避免使用偵錯元件
測試或偵錯功能一律不應在正式版應用程式元件 (例如活動、廣播接收器、服務或內容供應器) 中實作,因為這些功能一旦匯出之後,即可由裝置上的任何其他程序執行。不過,將偵錯元件設為未匯出 (android:exported="false"),也不表示功能已獲得實質保護,因為在啟用偵錯選項的情況下,任何已解鎖的裝置只要使用 Android Debug Bridge (ADB) 工具,就還是能執行偵錯元件。
限制只能在測試環境版本中使用偵錯或測試功能
應用程式中的任何測試或偵錯功能,都應只在一組受限的測試環境版本中執行,這樣才能限制開發人員僅在受控環境中對應用程式功能進行偵錯或測試。為此,您可以建立應用程式的專屬測試或偵錯版本,並對其進行進階檢測設備測試,確保任何測試或偵錯功能都能在獨立版本上執行。
實作自動化 UI 測試
在應用程式上執行測試時,請選擇採用自動化 UI 測試,因為這類可重複的測試可在獨立環境中執行,較不容易出現人為錯誤。
資源
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2024-01-05 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2024-01-05 (世界標準時間)。"],[],[],null,["# Test and debug features\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\n\nOverview\n--------\n\nReleasing production builds that include Testing or Debug features can\nnegatively impact the security posture of the application. These functionalities\nare used to help developers discover and identify bugs within intended\napplication use cases prior to, or after a new version release, and shouldn't\nbe publicly accessible.\n\nExamples of Testing/ Debug features are:\n\n- Hidden menus\n- Options to enable debug logs\n- Options to alter application flow\n- Options to circumvent payment or subscription processes\n- Options to circumvent authentication\n- Tests for application-specific activities\n\nAll the preceding can be leveraged by a malicious user in order to alter the\napplication's intended flow or retrieve system information to tailor further\nattacks.\n\nThe risk introduced by leaving exposed Testing or Debug features may vary\naccording to the action associated with the debug capabilities itself.\n\nAnother area of risk for the application is the attribute **android:debuggable**\nset within the AndroidManifest.xml element **`\u003capplication\u003e`** . As reported in\nthe [**android:debuggable**](/topic/security/risks/android-debuggable) article,\ndeploying a production application with the aforementioned value set, allows\nmalicious users to access administrative resources that are otherwise\ninaccessible.\n\nImpact\n------\n\nA malicious user interacting with a Testing or Debug feature in a production\nbuild can lead to unexpected results. The impact of any action is directly\nconnected with the permissions assigned to the feature. The higher the\npermissions, the higher the impact that an active exploitation can have. Such\nfunctionalities within an application can be used to circumvent a number of\nprotections, bypass paywalls, retrieve system or user related information, or\ntrigger testing activities.\n\nMitigations\n-----------\n\n### Avoid using debug components\n\nTest or debug functionalities should never be implemented within production\napplication components such as activities, broadcast receivers, services or\ncontent providers since, if exported, can be run by any other process on the\ndevice.\nSetting the debug component as not exported\n([**android:exported=\"false\"**](/topic/security/risks/android-exported)) does\nnot constitute a valid protection for the capabilities since any rooted device\ncan still execute it through the Android Debug Bridge (ADB) tool if the debug\noption is enabled.\n\n### Limit debug or test features to staging builds\n\nThe execution of any test or debug function within applications should be\nlimited only to a restricted set of Staging builds to allow only developers to\ndebug or test application's features in a controlled environment.\nThis can be obtained by creating a dedicated test or debug build of the\napplication and advanced instrumented tests for it in order to ensure that any\ntest or debug feature is run on an isolated version.\n\n### Implement automated UI tests\n\nWhen running tests on an application, opt for automated UI tests since they are\nrepeatable, can be executed in a separated environment and are not prone to\nhuman errors.\n\nResources\n---------\n\n- [Dev guidance on advanced testing setups](/studio/test/advanced-test-setup)\n- [Dev guidance on automating UI tests](/training/testing/instrumented-tests/ui-tests)\n- [android:debuggable](/topic/security/risks/android-debuggable)\n- [android:exported](/topic/security/risks/android-exported)\n- [Debuggable Apps in Android Market](https://labs.withsecure.com/publications/debuggable-apps-in-android-market)\n- [Can debug code cause security vulnerabilities?](https://www.coderskitchen.com/an-debug-code-cause-security-vulnerabilities/)"]]