You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: best-practices/MASTG-BEST-0029.md
+14-7Lines changed: 14 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,17 +10,23 @@ Apps should protect sensitive user interactions from overlay attacks by implemen
10
10
11
11
## Recommendation
12
12
13
-
Implement touch filtering to prevent touch events when the app's UI is obscured by another app. Use one or more of the following mechanisms:
13
+
Implement appropriate mechanisms to protect against overlay attacks. The following approaches are listed from most robust to least robust:
14
14
15
-
1.**Set the layout attribute `android:filterTouchesWhenObscured="true"`** for sensitive views such as login buttons, payment confirmations, or permission requests. This filters touch events when the view is obscured.
15
+
### Prevention Mechanisms
16
16
17
-
2.**Call `setFilterTouchesWhenObscured(true)`** programmatically on sensitive views to enable touch filtering at runtime.
17
+
These mechanisms prevent overlays from appearing or block touch events when overlays are detected:
18
18
19
-
3.**Call `setHideOverlayWindows(true)`**on the window (API level 31+)to hide all non-system overlay windows while the activity is in the foreground. This provides stronger protection by preventing overlays entirely rather than just filtering touch events.
19
+
1.**Use `HIDE_OVERLAY_WINDOWS` permission and `setHideOverlayWindows(true)`** (API level 31+): Declare the [`HIDE_OVERLAY_WINDOWS`](https://developer.android.com/reference/android/Manifest.permission#HIDE_OVERLAY_WINDOWS) permission in the manifest and call [`setHideOverlayWindows(true)`](https://developer.android.com/reference/android/view/Window#setHideOverlayWindows(boolean)) on the window to hide all non-system overlay windows while the activity is in the foreground. This is the most robust solution as it prevents overlays entirely rather than just filtering touch events.
20
20
21
-
4.**Override `onFilterTouchEventForSecurity`** for more granular control and to implement custom security policies based on your app's specific requirements.
21
+
2.**Set `android:filterTouchesWhenObscured="true"` or call `setFilterTouchesWhenObscured(true)`**: Set the layout attribute [`android:filterTouchesWhenObscured="true"`](https://developer.android.com/reference/android/view/View#attr_android:filterTouchesWhenObscured) in XML for sensitive views, or call [`setFilterTouchesWhenObscured(true)`](https://developer.android.com/reference/android/view/View#setFilterTouchesWhenObscured(boolean)) programmatically on sensitive views such as login buttons, payment confirmations, or permission requests. This filters touch events when the view is obscured by another visible window.
22
22
23
-
5.**Check motion event flags** such as `FLAG_WINDOW_IS_OBSCURED` (API level 9+) or `FLAG_WINDOW_IS_PARTIALLY_OBSCURED` (API level 29+) in touch event handlers to detect obscured windows and respond appropriately.
23
+
3.**Override `onFilterTouchEventForSecurity`**: Override the [`onFilterTouchEventForSecurity`](https://developer.android.com/reference/android/view/View#onFilterTouchEventForSecurity(android.view.MotionEvent)) method for more granular control and to implement custom security policies based on your app's specific requirements.
24
+
25
+
### Detection Mechanisms
26
+
27
+
These mechanisms detect when overlays are present but do not automatically prevent them. They allow the app to respond accordingly:
28
+
29
+
-**Check motion event flags** such as [`FLAG_WINDOW_IS_OBSCURED`](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_OBSCURED) (API level 9+) or [`FLAG_WINDOW_IS_PARTIALLY_OBSCURED`](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_PARTIALLY_OBSCURED) (API level 29+) in touch event handlers to detect obscured windows and respond appropriately. Note that this approach requires custom implementation to decide how to handle detected overlays.
24
30
25
31
Apply these protections selectively to security-sensitive UI elements where user confirmation is critical, such as:
26
32
@@ -53,8 +59,9 @@ Touch filtering mechanisms help ensure that user interactions occur with the int
Copy file name to clipboardExpand all lines: knowledge/android/MASVS-PLATFORM/MASTG-KNOW-0022.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,14 @@ There are several types of overlay attacks affecting different Android versions:
14
14
15
15
Android provides several defensive mechanisms that apps can use to protect against overlay attacks:
16
16
17
+
**Prevention Mechanisms:**
18
+
19
+
-[`HIDE_OVERLAY_WINDOWS`](https://developer.android.com/reference/android/Manifest.permission#HIDE_OVERLAY_WINDOWS) permission and [`setHideOverlayWindows`](https://developer.android.com/reference/android/view/Window#setHideOverlayWindows(boolean)) (since API level 31): Declare this permission in the manifest and call the method on the window to hide all non-system overlay windows while the activity is in the foreground. This provides the strongest protection by preventing overlays entirely.
20
+
-[`android:filterTouchesWhenObscured`](https://developer.android.com/reference/android/view/View#attr_android:filterTouchesWhenObscured) attribute and [`setFilterTouchesWhenObscured`](https://developer.android.com/reference/android/view/View#setFilterTouchesWhenObscured(boolean)) method: Set this layout attribute to `true` in XML or call the method programmatically to filter touch events when the view is obscured by another visible window.
17
21
-[`onFilterTouchEventForSecurity`](https://developer.android.com/reference/android/view/View#onFilterTouchEventForSecurity(android.view.MotionEvent)): Override this method for fine-grained control to implement custom security policies for views.
18
-
-[`android:filterTouchesWhenObscured`](https://developer.android.com/reference/android/view/View#attr_android:filterTouchesWhenObscured): Set this layout attribute to `true` or call [`setFilterTouchesWhenObscured`](https://developer.android.com/reference/android/view/View#setFilterTouchesWhenObscured(boolean)) to filter touch events when the view is obscured by another visible window.
19
-
-[`setHideOverlayWindows`](https://developer.android.com/reference/android/view/Window#setHideOverlayWindows(boolean)) (since API level 31): Call this method on the window to hide all non-system overlay windows while the activity is in the foreground. This provides a stronger protection by preventing overlays entirely rather than just filtering touch events.
22
+
23
+
**Detection Mechanisms:**
24
+
20
25
-[`FLAG_WINDOW_IS_OBSCURED`](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_OBSCURED) (since API level 9): Check this flag to detect if the window is obscured.
21
26
-[`FLAG_WINDOW_IS_PARTIALLY_OBSCURED`](https://developer.android.com/reference/android/view/MotionEvent#FLAG_WINDOW_IS_PARTIALLY_OBSCURED) (since API level 29): Check this flag to detect if the window is partially obscured.
0 commit comments