-
-
Notifications
You must be signed in to change notification settings - Fork 13
Auth URL validation and normalization #108
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
Merged
Merged
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
Some comments aren't visible on the classic Files Changed page.
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
32 changes: 32 additions & 0 deletions
32
auth/src/main/java/com/android/swingmusic/auth/presentation/util/AuthUtils.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,32 @@ | ||
| package com.android.swingmusic.auth.presentation.util | ||
|
|
||
| object AuthUtils { | ||
| fun normalizeUrl(url: String?): String? { | ||
| val trimmed = url?.trim() | ||
| if (trimmed.isNullOrEmpty()) return null | ||
| // If it already has a scheme like http:// or https:// (or any scheme), leave it as-is | ||
| val hasScheme = Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://").containsMatchIn(trimmed) | ||
| return if (hasScheme) trimmed else "https://$trimmed" | ||
| } | ||
|
|
||
| /** | ||
| * Validates URLs for login input. | ||
| * | ||
| * Rules: | ||
| * - Allowed schemes: http, https, ftp | ||
| * - Host: domain (with subdomains), localhost, or IPv4 address | ||
| * - Optional port | ||
| * - Optional path/query/fragment | ||
| */ | ||
| fun validInputUrl(url: String?): Boolean { | ||
| val urlRegex = Regex( | ||
| pattern = "^(https?|ftp)://(" + | ||
| "localhost|" + | ||
| "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}|" + | ||
| "(?:(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3})" + | ||
| ")(?::\\d{1,5})?(?:/\\S*)?$", | ||
| options = setOf(RegexOption.IGNORE_CASE) | ||
| ) | ||
| return url?.matches(urlRegex) == true | ||
| } | ||
| } | ||
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
110 changes: 110 additions & 0 deletions
110
auth/src/test/java/com/android/swingmusic/auth/presentation/util/AuthUtilsTest.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,110 @@ | ||
| package com.android.swingmusic.auth.presentation.util | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class AuthUtilsTest { | ||
|
|
||
| // normalizeUrl | ||
| @Test | ||
| fun `normalizeUrl returns null for null input`() { | ||
| assertNull(AuthUtils.normalizeUrl(null)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl returns null for blank input`() { | ||
| assertNull(AuthUtils.normalizeUrl(" \t \n ")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl trims whitespace without scheme`() { | ||
| assertEquals("https://example.com", AuthUtils.normalizeUrl(" example.com ")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl trims whitespace with scheme`() { | ||
| assertEquals("https://example.com", AuthUtils.normalizeUrl(" https://example.com ")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl prepends https when scheme is missing`() { | ||
| assertEquals("https://example.com", AuthUtils.normalizeUrl("example.com")) | ||
| assertEquals("https://sub.domain.com", AuthUtils.normalizeUrl("sub.domain.com")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl handles ports and IPs when missing scheme`() { | ||
| assertEquals("https://example.com:8080", AuthUtils.normalizeUrl("example.com:8080")) | ||
| assertEquals("https://192.168.1.1", AuthUtils.normalizeUrl("192.168.1.1")) | ||
| assertEquals("https://192.168.1.1:8443", AuthUtils.normalizeUrl("192.168.1.1:8443")) | ||
| assertEquals("https://localhost:3000", AuthUtils.normalizeUrl("localhost:3000")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl leaves http and https unchanged`() { | ||
| assertEquals("http://example.com", AuthUtils.normalizeUrl("http://example.com")) | ||
| assertEquals("https://example.com", AuthUtils.normalizeUrl("https://example.com")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `normalizeUrl leaves other schemes unchanged`() { | ||
| assertEquals("ftp://example.com", AuthUtils.normalizeUrl("ftp://example.com")) | ||
| assertEquals("custom+scheme://host/path", AuthUtils.normalizeUrl("custom+scheme://host/path")) | ||
| } | ||
|
|
||
| // validInputUrl | ||
| @Test | ||
| fun `validInputUrl accepts http, https, and ftp`() { | ||
| assertTrue(AuthUtils.validInputUrl("http://example.com")) | ||
| assertTrue(AuthUtils.validInputUrl("https://example.com")) | ||
| assertTrue(AuthUtils.validInputUrl("ftp://example.com")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl accepts domains with ports`() { | ||
| assertTrue(AuthUtils.validInputUrl("https://example.com:8080")) | ||
| assertTrue(AuthUtils.validInputUrl("http://sub.example.co.uk:3000/path")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl accepts localhost and IPv4 with optional ports`() { | ||
| assertTrue(AuthUtils.validInputUrl("http://localhost")) | ||
| assertTrue(AuthUtils.validInputUrl("http://localhost:3000/health")) | ||
| assertTrue(AuthUtils.validInputUrl("https://192.168.1.1")) | ||
| assertTrue(AuthUtils.validInputUrl("https://192.168.1.1:8443/api")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl rejects malformed IPv4`() { | ||
| assertFalse(AuthUtils.validInputUrl("https://999.1.1.1")) | ||
| assertFalse(AuthUtils.validInputUrl("http://256.256.256.256")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl rejects missing scheme`() { | ||
| assertFalse(AuthUtils.validInputUrl("example.com")) | ||
| assertFalse(AuthUtils.validInputUrl("www.example.com")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl rejects blank or null`() { | ||
| assertFalse(AuthUtils.validInputUrl(null)) | ||
| assertFalse(AuthUtils.validInputUrl(" ")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl accepts typical paths and query`() { | ||
| assertTrue(AuthUtils.validInputUrl("https://example.com/path?query=1#frag")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `validInputUrl rejects obvious invalid urls and custom schemes`() { | ||
| assertFalse(AuthUtils.validInputUrl("https:///example.com")) | ||
| assertFalse(AuthUtils.validInputUrl("https://")) | ||
| assertFalse(AuthUtils.validInputUrl("not a url")) | ||
| assertFalse(AuthUtils.validInputUrl("custom+scheme://host/path")) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a mismatch between normalizeUrl and validInputUrl. The normalizeUrl function allows any scheme matching the pattern
[a-zA-Z][a-zA-Z0-9+.-]*://, which includes schemes likeftp://andcustom+scheme://. However, validInputUrl only acceptshttp://,https://, andftp://. This means if a user enters a URL likecustom://example.com, normalizeUrl will preserve it, but validInputUrl will reject it. While this may be intentional, it creates an inconsistency where normalizeUrl accepts more schemes than validInputUrl validates. Consider either restricting normalizeUrl to only preserve http/https/ftp schemes, or documenting this intentional behavior.