Skip to content

Commit 01ecd53

Browse files
authored
Merge pull request #97 from usetrmnl/96-45-cleartext-com
[ADDED] Allow non-secure HTTP based BYOS URL
2 parents 0dec928 + 0ee50f1 commit 01ecd53

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
android:roundIcon="@mipmap/ic_launcher_round"
1515
android:supportsRtl="true"
1616
android:theme="@style/Theme.App"
17+
android:networkSecurityConfig="@xml/network_security_config"
1718
tools:targetApi="35"
1819
android:enableOnBackInvokedCallback="true"
1920
tools:replace="android:appComponentFactory">

app/src/main/java/ink/trmnl/android/util/InputValidator.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ package ink.trmnl.android.util
33
import java.util.regex.Pattern
44

55
/**
6-
* Validates if the provided string is a valid HTTPS URL.
6+
* Validates if the provided string is a valid HTTP or HTTPS URL.
77
*
88
* The function checks that the URL:
9-
* - Starts with https:// (HTTP is not accepted for security reasons)
9+
* - Starts with http:// or https://
1010
* - Contains valid URL characters in the domain and path components
1111
*
1212
* @param url The string to validate as a URL
13-
* @return true if the string is a valid HTTPS URL, false otherwise
13+
* @return true if the string is a valid HTTP or HTTPS URL, false otherwise
1414
*/
1515
internal fun isValidUrl(url: String): Boolean {
16-
val httpsRegex =
16+
val httpRegex =
1717
Pattern.compile(
18-
"^(https)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]",
18+
"^(http|https)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]",
1919
Pattern.CASE_INSENSITIVE,
2020
)
21-
return httpsRegex.matcher(url).matches()
21+
return httpRegex.matcher(url).matches()
2222
}
2323

2424
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
<!-- Allow cleartext traffic for all domains -->
4+
<base-config cleartextTrafficPermitted="true">
5+
<trust-anchors>
6+
<certificates src="system" />
7+
</trust-anchors>
8+
</base-config>
9+
10+
<!-- Specific configuration for localhost/emulator addresses -->
11+
<domain-config cleartextTrafficPermitted="true">
12+
<domain includeSubdomains="true">localhost</domain>
13+
<domain includeSubdomains="true">127.0.0.1</domain>
14+
<domain includeSubdomains="true">10.0.2.2</domain> <!-- Android emulator's localhost -->
15+
</domain-config>
16+
</network-security-config>

app/src/test/java/ink/trmnl/android/util/InputValidatorTest.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import org.junit.Test
88
*/
99
class InputValidatorTest {
1010
@Test
11-
fun `isValidUrl should return true for valid HTTPS URLs`() {
11+
fun `isValidUrl should return true for valid HTTP and HTTPS URLs`() {
1212
val validUrls =
1313
listOf(
14+
// HTTPS URLs
1415
"https://localhost:2443",
1516
"https://example.com",
1617
"https://example.com/path",
@@ -21,6 +22,16 @@ class InputValidatorTest {
2122
"https://example.com/path/to/resource",
2223
"https://example.com/path-with-dash",
2324
"https://example.com/path_with_underscore",
25+
// HTTP URLs
26+
"http://example.com",
27+
"http://localhost",
28+
"http://localhost:8080",
29+
"http://subdomain.example.com",
30+
"http://example.com/path",
31+
"http://example.com/path?query=value&another=true",
32+
"http://127.0.0.1",
33+
"http://10.0.2.2",
34+
"http://example-domain.com:9000/api",
2435
)
2536

2637
validUrls.forEach { url ->
@@ -29,15 +40,12 @@ class InputValidatorTest {
2940
}
3041

3142
@Test
32-
fun `isValidUrl should return false for invalid URLs and HTTP URLs`() {
43+
fun `isValidUrl should return false for invalid URLs`() {
3344
val invalidUrls =
3445
listOf(
3546
"",
3647
"example.com",
3748
"www.example.com",
38-
"http://example.com", // HTTP is now invalid - HTTPS only
39-
"http://subdomain.example.com", // HTTP is now invalid - HTTPS only
40-
"http://example.com/path", // HTTP is now invalid - HTTPS only
4149
"ftp://example.com",
4250
"file:///path/to/file",
4351
"http:/example.com",

0 commit comments

Comments
 (0)