-
Notifications
You must be signed in to change notification settings - Fork 81
RUM-16634: Add wildcard host pattern matching to WebViewTracking
#3540
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
Draft
kikoveiga
wants to merge
1
commit into
develop
Choose a base branch
from
kikoveiga/rum-16634/webview-host-patterns
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...droid-core/src/main/kotlin/com/datadog/android/core/configuration/HostPatternValidator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.android.core.configuration | ||
|
|
||
| import com.datadog.android.api.InternalLogger | ||
| import com.datadog.android.core.internal.utils.unboundInternalLogger | ||
| import com.datadog.android.lint.InternalApi | ||
| import java.util.Locale | ||
|
|
||
| /** | ||
| * Utility class to validate wildcard host patterns. | ||
| * | ||
| * A host pattern may contain at most one `*` wildcard and may only use the characters `a-z`, `0-9`, | ||
| * `.`, `-` and `*`. Plain host names (without a wildcard) are valid patterns too. This is the | ||
| * counterpart of [HostsSanitizer] for entries that are allowed to carry a wildcard. | ||
| */ | ||
| class HostPatternValidator { | ||
|
|
||
| /** | ||
| * Validates the given host patterns, returning the valid ones lowercased and in their original | ||
| * order. Entries containing characters outside `[a-z0-9.*-]` or more than one `*` wildcard are | ||
| * dropped and a warning is logged. | ||
| * | ||
| * @param patterns Host patterns to validate. | ||
| * @param feature SDK feature requesting the validation. | ||
| */ | ||
| @InternalApi | ||
| fun validate( | ||
| patterns: List<String>, | ||
| feature: String | ||
| ): List<String> { | ||
| return patterns.mapNotNull { validatePattern(it, feature) } | ||
| } | ||
|
|
||
| private fun validatePattern(pattern: String, feature: String): String? { | ||
| val lowercased = pattern.lowercase(Locale.US) | ||
| return when { | ||
| lowercased.contains(INVALID_CHARACTER) -> { | ||
| unboundInternalLogger.log( | ||
| InternalLogger.Level.WARN, | ||
| InternalLogger.Target.USER, | ||
| { ERROR_INVALID_CHARACTERS.format(Locale.US, pattern, feature) } | ||
| ) | ||
| null | ||
| } | ||
| lowercased.count { it == WILDCARD } > 1 -> { | ||
| unboundInternalLogger.log( | ||
| InternalLogger.Level.WARN, | ||
| InternalLogger.Target.USER, | ||
| { ERROR_MULTIPLE_WILDCARDS.format(Locale.US, pattern, feature) } | ||
| ) | ||
| null | ||
| } | ||
| else -> lowercased | ||
| } | ||
| } | ||
|
|
||
| internal companion object { | ||
| private const val WILDCARD = '*' | ||
|
|
||
| private val INVALID_CHARACTER = Regex("[^a-z0-9.*-]") | ||
|
|
||
| internal const val ERROR_INVALID_CHARACTERS: String = | ||
| "You are using a malformed host pattern \"%s\" to setup %s tracking. It will be dropped. " + | ||
| "A host pattern may only contain lowercase letters, digits, '.', '-' and a single '*' wildcard." | ||
|
|
||
| internal const val ERROR_MULTIPLE_WILDCARDS: String = | ||
| "You are using a host pattern \"%s\" with more than one wildcard to setup %s tracking. " + | ||
| "It will be dropped. A host pattern may contain at most one '*' wildcard." | ||
| } | ||
| } | ||
209 changes: 209 additions & 0 deletions
209
...d-core/src/test/kotlin/com/datadog/android/core/configuration/HostPatternValidatorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.android.core.configuration | ||
|
|
||
| import com.datadog.android.api.InternalLogger | ||
| import com.datadog.android.utils.config.InternalLoggerTestConfiguration | ||
| import com.datadog.tools.unit.annotations.TestConfigurationsProvider | ||
| import com.datadog.tools.unit.extensions.TestConfigurationExtension | ||
| import com.datadog.tools.unit.extensions.config.TestConfiguration | ||
| import fr.xgouchet.elmyr.annotation.StringForgery | ||
| import fr.xgouchet.elmyr.annotation.StringForgeryType | ||
| import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.junit.jupiter.api.extension.Extensions | ||
| import org.mockito.junit.jupiter.MockitoExtension | ||
| import org.mockito.kotlin.argumentCaptor | ||
| import org.mockito.kotlin.eq | ||
| import org.mockito.kotlin.isNull | ||
| import org.mockito.kotlin.times | ||
| import org.mockito.kotlin.verify | ||
| import java.util.Locale | ||
|
|
||
| @Extensions( | ||
| ExtendWith(MockitoExtension::class), | ||
| ExtendWith(ForgeExtension::class), | ||
| ExtendWith(TestConfigurationExtension::class) | ||
| ) | ||
| internal class HostPatternValidatorTest { | ||
|
|
||
| lateinit var testedValidator: HostPatternValidator | ||
|
|
||
| @StringForgery(type = StringForgeryType.ALPHABETICAL) | ||
| lateinit var fakeFeature: String | ||
|
|
||
| @BeforeEach | ||
| fun `set up`() { | ||
| testedValidator = HostPatternValidator() | ||
| } | ||
|
|
||
| @Test | ||
| fun `M keep valid wildcard patterns W validate()`() { | ||
| // Given | ||
| val patterns = listOf("*.example.com", "preview-*.shopist.io", "example.net") | ||
|
|
||
| // When | ||
| val result = testedValidator.validate(patterns, fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).isEqualTo(patterns) | ||
| } | ||
|
|
||
| @Test | ||
| fun `M keep plain hosts W validate { no wildcard }`( | ||
| @StringForgery( | ||
| regex = "(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\\.)+([a-z]|[a-z][a-z0-9-]*[a-z0-9])" | ||
| ) hosts: List<String> | ||
| ) { | ||
| // When | ||
| val result = testedValidator.validate(hosts, fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).isEqualTo(hosts) | ||
| } | ||
|
|
||
| @Test | ||
| fun `M keep single wildcard W validate { star }`() { | ||
| // When | ||
| val result = testedValidator.validate(listOf("*"), fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).containsExactly("*") | ||
| } | ||
|
|
||
| @Test | ||
| fun `M keep empty string W validate { blank }`() { | ||
| // When | ||
| val result = testedValidator.validate(listOf(""), fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).containsExactly("") | ||
| } | ||
|
|
||
| @Test | ||
| fun `M lowercase patterns W validate { uppercase }`() { | ||
| // When | ||
| val result = testedValidator.validate( | ||
| listOf("*.SHOPIST.IO", "Preview-*.Example.COM"), | ||
| fakeFeature | ||
| ) | ||
|
|
||
| // Then | ||
| assertThat(result).containsExactly("*.shopist.io", "preview-*.example.com") | ||
| } | ||
|
|
||
| @Test | ||
| fun `M drop and warn W validate { more than one wildcard }`() { | ||
| // Given | ||
| val pattern = "*.foo.*.bar" | ||
|
|
||
| // When | ||
| val result = testedValidator.validate(listOf(pattern), fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).isEmpty() | ||
| val expectedMessage = HostPatternValidator.ERROR_MULTIPLE_WILDCARDS.format( | ||
| Locale.US, | ||
| pattern, | ||
| fakeFeature | ||
| ) | ||
| argumentCaptor<() -> String> { | ||
| verify(logger.mockInternalLogger).log( | ||
| eq(InternalLogger.Level.WARN), | ||
| eq(InternalLogger.Target.USER), | ||
| capture(), | ||
| isNull(), | ||
| eq(false), | ||
| eq(null) | ||
| ) | ||
| assertThat(firstValue()).isEqualTo(expectedMessage) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `M drop and warn W validate { invalid characters }`() { | ||
| // Given | ||
| val patterns = listOf("foo'bar.com", "back\\slash.com", "https://foo.com") | ||
|
|
||
| // When | ||
| val result = testedValidator.validate(patterns, fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).isEmpty() | ||
| val expectedMessages = patterns.map { | ||
| HostPatternValidator.ERROR_INVALID_CHARACTERS.format(Locale.US, it, fakeFeature) | ||
| } | ||
| argumentCaptor<() -> String> { | ||
| verify(logger.mockInternalLogger, times(patterns.size)).log( | ||
| eq(InternalLogger.Level.WARN), | ||
| eq(InternalLogger.Target.USER), | ||
| capture(), | ||
| isNull(), | ||
| eq(false), | ||
| eq(null) | ||
| ) | ||
| assertThat(allValues.map { it() }) | ||
| .containsExactlyInAnyOrderElementsOf(expectedMessages) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `M warn with original pattern W validate { uppercase invalid characters }`() { | ||
| // Given | ||
| val pattern = "FOO'BAR.com" | ||
|
|
||
| // When | ||
| testedValidator.validate(listOf(pattern), fakeFeature) | ||
|
|
||
| // Then | ||
| val expectedMessage = HostPatternValidator.ERROR_INVALID_CHARACTERS.format( | ||
| Locale.US, | ||
| pattern, | ||
| fakeFeature | ||
| ) | ||
| argumentCaptor<() -> String> { | ||
| verify(logger.mockInternalLogger).log( | ||
| eq(InternalLogger.Level.WARN), | ||
| eq(InternalLogger.Target.USER), | ||
| capture(), | ||
| isNull(), | ||
| eq(false), | ||
| eq(null) | ||
| ) | ||
| assertThat(firstValue()).isEqualTo(expectedMessage) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `M keep valid and drop invalid W validate { mixed }`() { | ||
| // Given | ||
| val validWildcard = "*.shopist.io" | ||
| val plainHost = "example.com" | ||
| val multiWildcard = "*.foo.*.bar" | ||
| val invalidChars = "foo'bar.com" | ||
| val patterns = listOf(validWildcard, multiWildcard, plainHost, invalidChars) | ||
|
|
||
| // When | ||
| val result = testedValidator.validate(patterns, fakeFeature) | ||
|
|
||
| // Then | ||
| assertThat(result).containsExactly(validWildcard, plainHost) | ||
| } | ||
|
|
||
| companion object { | ||
| val logger = InternalLoggerTestConfiguration() | ||
|
|
||
| @TestConfigurationsProvider | ||
| @JvmStatic | ||
| fun getTestConfigurations(): List<TestConfiguration> { | ||
| return listOf(logger) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| object com.datadog.android.webview.WebViewTracking | ||
| fun enable(android.webkit.WebView, List<String>, Float = 100f, com.datadog.android.api.SdkCore = Datadog.getInstance()) | ||
| fun enableWithPatterns(android.webkit.WebView, List<String>, Float = 100f, com.datadog.android.api.SdkCore = Datadog.getInstance()) | ||
| class _InternalWebViewProxy | ||
| constructor(com.datadog.android.api.SdkCore, String? = null) | ||
| fun consumeWebviewEvent(String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.