Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safely handle bad URIs when tracking deep links #767

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,17 @@ private void trackDeepLink(Activity activity) {

Properties properties = new Properties();
Uri uri = intent.getData();
for (String parameter : uri.getQueryParameterNames()) {
String value = uri.getQueryParameter(parameter);
if (value != null && !value.trim().isEmpty()) {
properties.put(parameter, value);
try {
for (String parameter : uri.getQueryParameterNames()) {
String value = uri.getQueryParameter(parameter);
if (value != null && !value.trim().isEmpty()) {
properties.put(parameter, value);
}
}
} catch (Exception e) {
analytics
.logger("LifecycleCallbacks")
.error(e, "failed to get uri params for %s", uri.toString());
}

properties.put("url", uri.toString());
Expand Down
71 changes: 71 additions & 0 deletions analytics/src/test/java/com/segment/analytics/AnalyticsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,77 @@ open class AnalyticsTest {
)
}

@Test
fun trackDeepLinks_bad_uri() {
Analytics.INSTANCES.clear()

val callback =
AtomicReference<ActivityLifecycleCallbacks>()

doNothing()
.whenever(application)
.registerActivityLifecycleCallbacks(
argThat<ActivityLifecycleCallbacks>(
object : NoDescriptionMatcher<ActivityLifecycleCallbacks>() {
override fun matchesSafely(item: ActivityLifecycleCallbacks): Boolean {
callback.set(item)
return true
}
})
)

analytics = Analytics(
application,
networkExecutor,
stats,
traitsCache,
analyticsContext,
defaultOptions,
Logger.with(Analytics.LogLevel.NONE),
"qaz", listOf(factory),
client,
Cartographer.INSTANCE,
projectSettingsCache,
"foo",
DEFAULT_FLUSH_QUEUE_SIZE,
DEFAULT_FLUSH_INTERVAL.toLong(),
analyticsExecutor,
true,
CountDownLatch(0),
false,
false,
optOut,
Crypto.none(), emptyList(), emptyMap(),
jsMiddleware,
ValueMap(),
lifecycle,
false,
true,
DEFAULT_API_HOST
)

val expectedURL = "wc:foo-bar-jk@1bridge=https%3A%2F%2Fbridge.walletconnect.org&key=1234"

val activity = Mockito.mock(Activity::class.java)
val intent = Mockito.mock(Intent::class.java)
val uri = Uri.parse(expectedURL)

whenever(intent.data).thenReturn(uri)
whenever(activity.intent).thenReturn(intent)

verify(integration, Mockito.never())
.track(
argThat<TrackPayload>(
object : NoDescriptionMatcher<TrackPayload>() {
override fun matchesSafely(payload: TrackPayload): Boolean {
return payload.event() == "Deep Link Opened" &&
payload.properties()
.getString("url") == expectedURL
}
})
)
}

@Test
fun trackDeepLinks_null() {
Analytics.INSTANCES.clear()
Expand Down