This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #9338 - Introduce SafeUrl to strip unwanted uri schemes
Interested clients can overwrite "mozac_url_schemes_blocklist" with a custom list of uri schemes that will be recursively removed from the front of the uri.
- Loading branch information
1 parent
e10fb53
commit b7fbb89
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
components/support/utils/src/main/java/mozilla/components/support/utils/SafeUrl.kt
This file contains 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,37 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.support.utils | ||
|
||
import android.content.Context | ||
|
||
/** | ||
* Collection of methods used for ensuring the validity and security of URLs. | ||
*/ | ||
object SafeUrl { | ||
/** | ||
* Remove recursively the schemes declared in [R.array.mozac_url_schemes_blocklist] | ||
* from the front of [unsafeText]. | ||
*/ | ||
fun stripUnsafeUrlSchemes(context: Context, unsafeText: CharSequence?): String? { | ||
val urlSchemesBlocklist = context.resources.getStringArray(R.array.mozac_url_schemes_blocklist) | ||
var safeUrl = unsafeText.toString() | ||
if (safeUrl.isEmpty()) { | ||
return safeUrl | ||
} | ||
|
||
@Suppress("ControlFlowWithEmptyBody", "EmptyWhileBlock") | ||
while (urlSchemesBlocklist.find { | ||
if (safeUrl.startsWith(it, true)) { | ||
safeUrl = safeUrl.replaceFirst(Regex(it, RegexOption.IGNORE_CASE), "") | ||
true | ||
} else { | ||
false | ||
} | ||
} != null) { | ||
} | ||
|
||
return safeUrl | ||
} | ||
} |
This file contains 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,10 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="yes"?> | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
<resources> | ||
<!-- | ||
List of url schemes used by methods of SafeUrl. | ||
--> | ||
<string-array name="mozac_url_schemes_blocklist" /> | ||
</resources> |
55 changes: 55 additions & 0 deletions
55
components/support/utils/src/test/java/mozilla/components/support/utils/SafeUrlTest.kt
This file contains 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,55 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.support.utils | ||
|
||
import android.content.Context | ||
import android.content.res.Resources | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.support.test.mock | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.doReturn | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SafeUrlTest { | ||
@Test | ||
fun `WHEN schemes blocklist is empty THEN stripUnsafeUrlSchemes should return the initial String`() { | ||
val resources = mock<Resources>() | ||
val context = mock<Context>() | ||
doReturn(resources).`when`(context).resources | ||
doReturn(emptyArray<String>()).`when`(resources).getStringArray(R.array.mozac_url_schemes_blocklist) | ||
|
||
val result = SafeUrl.stripUnsafeUrlSchemes(context, "unsafeText") | ||
|
||
assertEquals("unsafeText", result) | ||
} | ||
|
||
@Test | ||
fun `WHEN schemes blocklist contains items not found in the argument THEN stripUnsafeUrlSchemes should return the initial String`() { | ||
val resources = mock<Resources>() | ||
val context = mock<Context>() | ||
doReturn(resources).`when`(context).resources | ||
doReturn(arrayOf("alien")).`when`(resources).getStringArray(R.array.mozac_url_schemes_blocklist) | ||
|
||
val result = SafeUrl.stripUnsafeUrlSchemes(context, "thisIsAnOkText") | ||
|
||
assertEquals("thisIsAnOkText", result) | ||
} | ||
|
||
@Test | ||
fun `WHEN schemes blocklist contains items found in the argument THEN stripUnsafeUrlSchemes should recursively remove them from the front`() { | ||
val resources = mock<Resources>() | ||
val context = mock<Context>() | ||
doReturn(resources).`when`(context).resources | ||
doReturn(arrayOf("one", "two")).`when`(resources).getStringArray(R.array.mozac_url_schemes_blocklist) | ||
|
||
val result = SafeUrl.stripUnsafeUrlSchemes(context, "two" + "one" + "one" + "two" + "safeText") | ||
assertEquals("safeText", result) | ||
|
||
val result2 = SafeUrl.stripUnsafeUrlSchemes(context, "one" + "two" + "one" + "two" + "safeText" + "one") | ||
assertEquals("safeText" + "one", result2) | ||
} | ||
} |
This file contains 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