Skip to content

Commit 92a1a7c

Browse files
Xinzhu Caifacebook-github-bot
Xinzhu Cai
authored andcommitted
6/n filter default sensitive params
Summary: **Context** In the future, we will have default sensitive parameters which are applied for all app events. We need to parse them and process separately. More details in: https://fburl.com/gdoc/41wgxcd6 **Change in this diff** - Parse default sensitive params - Drop the default sensitive params if there is any Reviewed By: KylinChang Differential Revision: D53879033 fbshipit-source-id: e2c791e1a4c2f38c1eb54d13ffedcd624c1d934c
1 parent 7164752 commit 92a1a7c

File tree

2 files changed

+123
-15
lines changed

2 files changed

+123
-15
lines changed

facebook-core/src/main/java/com/facebook/appevents/integrity/SensitiveParamsManager.kt

+35-8
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,42 @@ import kotlin.collections.HashSet
1818
@AutoHandleExceptions
1919
object SensitiveParamsManager {
2020
private var enabled = false
21+
22+
/* the parameters will be filtered out for all events */
23+
private var defaultSensitiveParameters: HashSet<String> = HashSet()
24+
25+
/* the parameters will be filtered out based on the event name */
2126
private var sensitiveParameters: MutableMap<String, HashSet<String>> = HashMap()
22-
private const val SENSITIVE_PARAMS_KEY = "_filteredKey"
27+
28+
private const val DEFAULT_SENSITIVE_PARAMS_KEY = "_MTSDK_Default_"
29+
private const val SENSITIVE_PARAMS_KEY = "_filteredKey" /* send back to Meta server */
2330

2431
@JvmStatic
2532
fun enable() {
2633
loadSensitiveParameters()
27-
if (!sensitiveParameters.isNullOrEmpty()) {
28-
enabled = true
34+
if (defaultSensitiveParameters.isNullOrEmpty() && sensitiveParameters.isNullOrEmpty()) {
35+
enabled = false
36+
return
2937
}
38+
39+
/* enable only when there is non empty default sensitive params or non empty specific
40+
* sensitive params
41+
*/
42+
enabled = true
3043
}
3144

3245
@JvmStatic
3346
fun disable() {
3447
enabled = false
3548
sensitiveParameters = HashMap()
49+
defaultSensitiveParameters = HashSet()
3650
}
3751

3852
private fun loadSensitiveParameters() {
3953
val settings = FetchedAppSettingsManager.queryAppSettings(FacebookSdk.getApplicationId(), false)
4054
?: return
4155
try {
56+
defaultSensitiveParameters = HashSet()
4257
sensitiveParameters = HashMap()
4358
val sensitiveParamsFromServer = settings.sensitiveParams
4459
if (sensitiveParamsFromServer != null && sensitiveParamsFromServer.length() != 0) {
@@ -47,12 +62,19 @@ object SensitiveParamsManager {
4762
val hasEventName = jsonObject.has("key")
4863
val hasSensitiveParams = jsonObject.has("value")
4964
if (hasEventName && hasSensitiveParams) {
50-
val eventName = jsonObject.getString("key")
65+
/* This indicates that the sensitive params are from the specific event
66+
* name or for all events which are the default sensitive params.
67+
*/
68+
val sensitiveParamsScope = jsonObject.getString("key")
5169
val sensitiveParams = jsonObject.getJSONArray("value")
52-
eventName?.let {
70+
sensitiveParamsScope?.let {
5371
sensitiveParams?.let {
5472
convertJSONArrayToHashSet(sensitiveParams)?.let {
55-
sensitiveParameters[eventName] = it
73+
if (sensitiveParamsScope.equals(DEFAULT_SENSITIVE_PARAMS_KEY)) {
74+
defaultSensitiveParameters = it
75+
} else {
76+
sensitiveParameters[sensitiveParamsScope] = it
77+
}
5678
}
5779
}
5880
}
@@ -69,7 +91,7 @@ object SensitiveParamsManager {
6991
if (!enabled) {
7092
return
7193
}
72-
if (!sensitiveParameters.containsKey(eventName)) {
94+
if (defaultSensitiveParameters.isNullOrEmpty() && !sensitiveParameters.containsKey(eventName)) {
7395
return
7496
}
7597

@@ -78,7 +100,7 @@ object SensitiveParamsManager {
78100
val sensitiveParamsForEvent = sensitiveParameters.get(key = eventName)
79101
val keys: List<String> = ArrayList(parameters.keys)
80102
for (key in keys) {
81-
if (!sensitiveParamsForEvent.isNullOrEmpty() && sensitiveParamsForEvent.contains(key)) {
103+
if (shouldFilterOut(key, sensitiveParamsForEvent)) {
82104
parameters.remove(key)
83105
filteredParamsJSON.put(key)
84106
}
@@ -91,4 +113,9 @@ object SensitiveParamsManager {
91113
parameters[SENSITIVE_PARAMS_KEY] = filteredParamsJSON.toString()
92114
}
93115
}
116+
117+
private fun shouldFilterOut(parameterKey: String, sensitiveParamsForEvent: HashSet<String>?) : Boolean {
118+
return defaultSensitiveParameters.contains(parameterKey)
119+
|| (!sensitiveParamsForEvent.isNullOrEmpty() && sensitiveParamsForEvent.contains(parameterKey))
120+
}
94121
}

facebook-core/src/test/kotlin/com/facebook/appevents/integrity/SensitiveParamsManagerTest.kt

+88-7
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,88 @@ class SensitiveParamsManagerTest : FacebookPowerMockTestCase() {
105105
@Test
106106
fun `test fetched sensitive params list is not null from the server and no params need to be filtered`() {
107107
initMockFetchedAppSettings(mockSensitiveParamsFromServer)
108+
109+
var mockInputParamsWithoutSensitiveParams = HashMap<String, String?>()
110+
mockInputParamsWithoutSensitiveParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
111+
108112
enable()
109-
processFilterSensitiveParams(mockInputParams, mockEventNameWithoutSensitiveParams)
113+
processFilterSensitiveParams(mockInputParamsWithoutSensitiveParams, mockEventNameWithoutSensitiveParams)
110114

111-
Assertions.assertThat(mockInputParams.containsKey(filteredParamsKey)).isFalse
112-
Assertions.assertThat(mockInputParams).isEqualTo(expectedFinalParamsWithoutChange)
115+
var expectedFinalParamsWithoutChange = mockInputParamsWithoutSensitiveParams.toMap()
116+
117+
Assertions.assertThat(mockInputParamsWithoutSensitiveParams.containsKey(filteredParamsKey)).isFalse
118+
Assertions.assertThat(mockInputParamsWithoutSensitiveParams).isEqualTo(expectedFinalParamsWithoutChange)
119+
}
120+
121+
@Test
122+
fun `test fetched sensitive params list has only default sensitive params from the server and need to filter the params`() {
123+
initMockFetchedAppSettings(mockSensitiveParamsFromServerDefaultOnly)
124+
enable()
125+
processFilterSensitiveParams(mockInputParams, mockEventWithSensitiveParam)
126+
127+
var expectedParams = HashMap<String, String?>()
128+
var filteredParams = JSONArray()
129+
filteredParams.put(mockSensitiveParam3)
130+
expectedParams[filteredParamsKey] = filteredParams.toString()
131+
expectedParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
132+
expectedParams[mockSensitiveParam1] = null
133+
expectedParams[mockSensitiveParam2] = null
134+
135+
Assertions.assertThat(mockInputParams.containsKey(filteredParamsKey)).isTrue
136+
Assertions.assertThat(mockInputParams).isEqualTo(expectedParams)
113137
}
114138

115139
@Test
116140
fun `test fetched sensitive params list is not null from the server and filter the params`() {
117141
initMockFetchedAppSettings(mockSensitiveParamsFromServer)
142+
enable()
143+
processFilterSensitiveParams(mockInputParams, mockEventWithSensitiveParam)
144+
145+
var expectedParams = HashMap<String, String?>()
146+
var filteredParams = JSONArray()
147+
filteredParams.put(mockSensitiveParam3) /* default sensitive param */
148+
filteredParams.put(mockSensitiveParam1) /* specific sensitive params */
149+
filteredParams.put(mockSensitiveParam2) /* specific sensitive params */
150+
expectedParams[filteredParamsKey] = filteredParams.toString()
151+
expectedParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
152+
153+
Assertions.assertThat(mockInputParams.containsKey(filteredParamsKey)).isTrue
154+
Assertions.assertThat(mockInputParams).isEqualTo(expectedParams)
155+
}
156+
157+
@Test
158+
fun `test fetched sensitive params list default only from the server and filter the params`() {
159+
initMockFetchedAppSettings(mockSensitiveParamsFromServerDefaultOnly)
160+
enable()
161+
processFilterSensitiveParams(mockInputParams, mockEventWithSensitiveParam)
118162

119163
var expectedParams = HashMap<String, String?>()
120164
var filteredParams = JSONArray()
121-
filteredParams.put(mockSensitiveParam1)
122-
filteredParams.put(mockSensitiveParam2)
165+
filteredParams.put(mockSensitiveParam3) /* default sensitive param */
123166
expectedParams[filteredParamsKey] = filteredParams.toString()
124167
expectedParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
168+
expectedParams[mockSensitiveParam1] = null
169+
expectedParams[mockSensitiveParam2] = null
170+
171+
Assertions.assertThat(mockInputParams.containsKey(filteredParamsKey)).isTrue
172+
Assertions.assertThat(mockInputParams).isEqualTo(expectedParams)
173+
}
125174

175+
@Test
176+
fun `test fetched sensitive params list specific only from the server and filter the params`() {
177+
initMockFetchedAppSettings(mockSensitiveParamsFromServerWithoutDefault)
126178
enable()
127179
processFilterSensitiveParams(mockInputParams, mockEventWithSensitiveParam)
128180

181+
var expectedParams = HashMap<String, String?>()
182+
var filteredParams = JSONArray()
183+
filteredParams.put(mockSensitiveParam1) /* specific sensitive params */
184+
filteredParams.put(mockSensitiveParam2) /* specific sensitive params */
185+
186+
expectedParams[filteredParamsKey] = filteredParams.toString()
187+
expectedParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
188+
expectedParams[mockSensitiveParam3] = null
189+
129190
Assertions.assertThat(mockInputParams.containsKey(filteredParamsKey)).isTrue
130191
Assertions.assertThat(mockInputParams).isEqualTo(expectedParams)
131192
}
@@ -135,22 +196,28 @@ class SensitiveParamsManagerTest : FacebookPowerMockTestCase() {
135196

136197
private const val configKey = "key"
137198
private const val configValue = "value"
199+
138200
private const val filteredParamsKey = "_filteredKey"
201+
private const val defaultSensitiveParametersKey = "_MTSDK_Default_"
139202

140203
private const val mockEventNameWithoutSensitiveParams = "install_app"
141204
private const val mockEventWithSensitiveParam = "sensitive_event_1"
142205
private const val mockSensitiveParam1 = "sensitive_param_1"
143206
private const val mockSensitiveParam2 = "sensitive_param_2"
207+
private const val mockSensitiveParam3 = "sensitive_param_3"
144208
private const val mockNonSensitiveParam = "non_sensitive_param"
145209
private const val mockNonSensitiveParamValue = "param_value"
146210

147211
private lateinit var emptyJSONArray: JSONArray
148212

149213
/* mock sensitive params with event name */
150-
private lateinit var mockSpecificSensitiveParams: JSONObject
214+
private lateinit var mockDefaultSensitiveParams: JSONObject /* default sensitive params */
215+
private lateinit var mockSpecificSensitiveParams: JSONObject /* non default sensitive params */
151216

152217
/* mock config fetched from server */
153-
private lateinit var mockSensitiveParamsFromServer: JSONArray
218+
private lateinit var mockSensitiveParamsFromServerDefaultOnly: JSONArray /* default only */
219+
private lateinit var mockSensitiveParamsFromServerWithoutDefault: JSONArray /* specific sensitive params only */
220+
private lateinit var mockSensitiveParamsFromServer: JSONArray /* specific sensitive params and default */
154221

155222
private lateinit var mockInputParams: HashMap<String, String?>
156223
private lateinit var expectedFinalParamsWithoutChange: Map<String, String?>
@@ -165,12 +232,26 @@ class SensitiveParamsManagerTest : FacebookPowerMockTestCase() {
165232
put(configKey, mockEventWithSensitiveParam)
166233
put(configValue, sensitiveParams)
167234
}
235+
mockSensitiveParamsFromServerWithoutDefault = JSONArray()
236+
mockSensitiveParamsFromServerWithoutDefault.put(mockSpecificSensitiveParams)
237+
238+
sensitiveParams = JSONArray()
239+
sensitiveParams.put(mockSensitiveParam3)
240+
mockDefaultSensitiveParams = JSONObject().apply {
241+
put("key", defaultSensitiveParametersKey)
242+
put("value", sensitiveParams)
243+
}
244+
mockSensitiveParamsFromServerDefaultOnly = JSONArray()
245+
mockSensitiveParamsFromServerDefaultOnly.put(mockDefaultSensitiveParams)
246+
168247
mockSensitiveParamsFromServer = JSONArray()
169248
mockSensitiveParamsFromServer.put(mockSpecificSensitiveParams)
249+
mockSensitiveParamsFromServer.put(mockDefaultSensitiveParams)
170250

171251
mockInputParams = HashMap()
172252
mockInputParams[mockSensitiveParam1] = null
173253
mockInputParams[mockSensitiveParam2] = null
254+
mockInputParams[mockSensitiveParam3] = null
174255
mockInputParams[mockNonSensitiveParam] = mockNonSensitiveParamValue
175256

176257
expectedFinalParamsWithoutChange = mockInputParams.toMap()

0 commit comments

Comments
 (0)