|
1 | 1 | package com.launchdarkly.observability.replay |
2 | 2 |
|
| 3 | +import android.view.View |
| 4 | +import android.widget.ImageView |
3 | 5 | import com.launchdarkly.observability.replay.masking.MaskMatcher |
4 | 6 | import com.launchdarkly.observability.replay.masking.MaskTarget |
5 | 7 |
|
6 | 8 | /** |
7 | | - * [PrivacyProfile] encapsulates options and functionality related to privacy of session |
8 | | - * replay functionality. |
| 9 | + * [PrivacyProfile] controls what UI elements are masked in session replay. |
9 | 10 | * |
10 | | - * By default, session replay will apply an opaque mask to text inputs, text, and sensitive views. |
11 | | - * See [sensitiveMatcher] for specific details. |
| 11 | + * Masking is implemented as a list of [MaskMatcher]s that are evaluated against a [MaskTarget]. |
| 12 | + * Targets can represent native Android Views as well as Jetpack Compose semantics nodes. |
12 | 13 | * |
13 | | - * @param maskTextInputs set to false to turn off masking text inputs |
14 | | - * @param maskText set to false to turn off masking text |
15 | | - * @param maskSensitive set to false to turn off masking sensitive views |
16 | | - * @param maskAdditionalMatchers list of additional [com.launchdarkly.observability.replay.masking.MaskMatcher]s that will be masked when they match |
| 14 | + * |
| 15 | + * @param maskTextInputs Set to false to disable masking text input targets. |
| 16 | + * @param maskText Set to false to disable masking text targets. |
| 17 | + * @param maskSensitive Set to false to disable masking "sensitive" targets (password + keyword heuristics). |
| 18 | + * @param maskImageViews Set to true to mask [ImageView] targets by exact class match. |
| 19 | + * @param maskViews Additional Views to mask by exact class match (see [viewsMatcher]). |
| 20 | + * @param maskXMLViewIds Additional Views to mask by resource entry name (see [xmlViewIdsMatcher]). |
| 21 | + * Accepts `"@+id/foo"`, `"@id/foo"`, or `"foo"`. |
| 22 | + * @param maskAdditionalMatchers Additional custom matchers to apply. |
17 | 23 | **/ |
18 | 24 | data class PrivacyProfile( |
19 | 25 | val maskTextInputs: Boolean = true, |
20 | 26 | val maskText: Boolean = true, |
21 | 27 | val maskSensitive: Boolean = true, |
| 28 | + // only for XML ImageViews |
| 29 | + val maskImageViews: Boolean = false, |
| 30 | + val maskViews: List<MaskViewRef> = emptyList(), |
| 31 | + val maskXMLViewIds: List<String> = emptyList(), |
22 | 32 | val maskAdditionalMatchers: List<MaskMatcher> = emptyList(), |
23 | 33 | ) { |
| 34 | + private val viewClassSet = buildSet { |
| 35 | + addAll(maskViews.map { it.clazz }) |
| 36 | + if (maskImageViews) add(ImageView::class.java) |
| 37 | + } |
| 38 | + |
| 39 | + private val maskXMLViewIdSet = maskXMLViewIds.map { |
| 40 | + when { |
| 41 | + it.startsWith("@+id/") -> it.substring(5) |
| 42 | + it.startsWith("@id/") -> it.substring(4) |
| 43 | + else -> it |
| 44 | + } |
| 45 | + }.toSet() |
24 | 46 |
|
25 | 47 | /** |
26 | 48 | * Converts this [PrivacyProfile] into its equivalent [MaskMatcher] list. |
| 49 | + * |
| 50 | + * Note: matchers are evaluated with `any { ... }`, so ordering only affects performance |
| 51 | + * (earlier matchers can short-circuit later ones). |
27 | 52 | */ |
28 | 53 | internal fun asMatchersList(): List<MaskMatcher> = buildList { |
| 54 | + // Prefer cheaper checks first; heavier checks should be later. |
29 | 55 | if (maskTextInputs) add(textInputMatcher) |
30 | 56 | if (maskText) add(textMatcher) |
| 57 | + if (viewClassSet.isNotEmpty()) add(viewsMatcher) |
| 58 | + if (maskXMLViewIdSet.isNotEmpty()) add(xmlViewIdsMatcher) |
31 | 59 | if (maskSensitive) add(sensitiveMatcher) |
32 | 60 | addAll(maskAdditionalMatchers) |
33 | 61 | } |
34 | 62 |
|
35 | | - companion object { |
36 | | - /** |
37 | | - * This matcher will match most text inputs, but there may be special cases where it will |
38 | | - * miss as we can't account for all possible future semantic properties. |
39 | | - */ |
40 | | - val textInputMatcher: MaskMatcher = object : MaskMatcher { |
41 | | - override fun isMatch(target: MaskTarget): Boolean { |
42 | | - return target.isTextInput() |
43 | | - } |
| 63 | + /** |
| 64 | + * Matches targets whose underlying Android View has an exact class match with [maskViews]. |
| 65 | + * |
| 66 | + * Note: this uses `target.view.javaClass` equality; it does not match subclasses. |
| 67 | + */ |
| 68 | + val viewsMatcher: MaskMatcher = object : MaskMatcher { |
| 69 | + override fun isMatch(target: MaskTarget): Boolean { |
| 70 | + return viewClassSet.contains(target.view.javaClass) |
44 | 71 | } |
| 72 | + } |
45 | 73 |
|
46 | | - /** |
47 | | - * This matcher will match most text, but there may be special cases where it will |
48 | | - * miss as we can't account for all possible future semantic properties. |
49 | | - */ |
50 | | - val textMatcher: MaskMatcher = object : MaskMatcher { |
51 | | - override fun isMatch(target: MaskTarget): Boolean { |
52 | | - return target.isText() |
53 | | - } |
| 74 | + /** |
| 75 | + * Matches targets whose underlying Android View's resource entry name is included in |
| 76 | + * [maskXMLViewIds]. |
| 77 | + * |
| 78 | + * IDs are compared using `resources.getResourceEntryName(view.id)`, so this only applies to |
| 79 | + * Views with a non-[View.NO_ID] id that resolves to a resource entry. |
| 80 | + */ |
| 81 | + val xmlViewIdsMatcher: MaskMatcher = object : MaskMatcher { |
| 82 | + fun View.idNameOrNull(): String? = |
| 83 | + if (id == View.NO_ID) null |
| 84 | + else runCatching { resources.getResourceEntryName(id) }.getOrNull() |
| 85 | + |
| 86 | + override fun isMatch(target: MaskTarget): Boolean { |
| 87 | + val id = target.view.idNameOrNull() ?: return false |
| 88 | + |
| 89 | + return maskXMLViewIdSet.contains(id) |
54 | 90 | } |
| 91 | + } |
55 | 92 |
|
56 | | - /** |
57 | | - * This matcher will match all items having the semantic property [SemanticsProperties.Password] |
58 | | - * and all text or context descriptions that have substring matches with any of the [sensitiveKeywords] |
59 | | - */ |
60 | | - val sensitiveMatcher: MaskMatcher = object : MaskMatcher { |
61 | | - override fun isMatch(target: MaskTarget): Boolean { |
62 | | - return target.isSensitive(sensitiveKeywords) |
63 | | - } |
| 93 | + /** |
| 94 | + * This matcher will match most text inputs, but there may be special cases where it will |
| 95 | + * miss as we can't account for all possible future semantic properties. |
| 96 | + */ |
| 97 | + val textInputMatcher: MaskMatcher = object : MaskMatcher { |
| 98 | + override fun isMatch(target: MaskTarget): Boolean { |
| 99 | + return target.isTextInput() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * This matcher will match most text, but there may be special cases where it will |
| 105 | + * miss as we can't account for all possible future semantic properties. |
| 106 | + */ |
| 107 | + val textMatcher: MaskMatcher = object : MaskMatcher { |
| 108 | + override fun isMatch(target: MaskTarget): Boolean { |
| 109 | + return target.isText() |
64 | 110 | } |
| 111 | + } |
65 | 112 |
|
66 | | - // this list of sensitive keywords is used to detect sensitive content descriptions |
67 | | - private val sensitiveKeywords = listOf( |
68 | | - "sensitive", |
69 | | - "private", |
70 | | - "name", |
71 | | - "email", |
72 | | - "username", |
73 | | - "cell", |
74 | | - "mobile", |
75 | | - "phone", |
76 | | - "address", |
77 | | - "street", |
78 | | - "dob", |
79 | | - "birth", |
80 | | - "password", |
81 | | - "account", |
82 | | - "ssn", |
83 | | - "social", |
84 | | - "security", |
85 | | - "credit", |
86 | | - "debit", |
87 | | - "card", |
88 | | - "cvv", |
89 | | - "mm/yy", |
90 | | - "pin", |
91 | | - ) |
| 113 | + /** |
| 114 | + * This matcher will match all items having the semantic property |
| 115 | + * and all text or context descriptions that have substring matches with any of the [sensitiveKeywords] |
| 116 | + */ |
| 117 | + val sensitiveMatcher: MaskMatcher = object : MaskMatcher { |
| 118 | + override fun isMatch(target: MaskTarget): Boolean { |
| 119 | + return target.isSensitive(sensitiveKeywords) |
| 120 | + } |
92 | 121 | } |
| 122 | + |
| 123 | + // this list of sensitive keywords is used to detect sensitive content descriptions |
| 124 | + private val sensitiveKeywords = listOf( |
| 125 | + "sensitive", |
| 126 | + "private", |
| 127 | + "name", |
| 128 | + "email", |
| 129 | + "username", |
| 130 | + "cell", |
| 131 | + "mobile", |
| 132 | + "phone", |
| 133 | + "address", |
| 134 | + "street", |
| 135 | + "dob", |
| 136 | + "birth", |
| 137 | + "password", |
| 138 | + "account", |
| 139 | + "ssn", |
| 140 | + "social", |
| 141 | + "security", |
| 142 | + "credit", |
| 143 | + "debit", |
| 144 | + "card", |
| 145 | + "cvv", |
| 146 | + "mm/yy", |
| 147 | + "pin", |
| 148 | + ) |
93 | 149 | } |
| 150 | + |
0 commit comments