-
Notifications
You must be signed in to change notification settings - Fork 927
[PM-28157] Add string extension to prefix URIs with www #6183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,6 +239,34 @@ fun String.prefixHttpsIfNecessaryOrNull(): String? = | |
| fun String.prefixHttpsIfNecessary(): String = | ||
| prefixHttpsIfNecessaryOrNull() ?: this | ||
|
|
||
| /** | ||
| * If the given [String] is a valid URI, "www." will be prepended (or inserted after the scheme | ||
| * if present) if it is not already present. Otherwise `null` will be returned. | ||
| * | ||
| * Examples: | ||
| * - "example.com" -> "www.example.com" | ||
| * - "www.example.com" -> "www.example.com" | ||
| * - "https://example.com" -> "https://www.example.com" | ||
| * - "https://www.example.com" -> "https://www.example.com" | ||
| */ | ||
| fun String.prefixWwwIfNecessaryOrNull(): String? = | ||
| when { | ||
| this.isBlank() || !this.isValidUri() -> null | ||
| this.startsWith("www.") -> this | ||
| this.startsWith("http://") || this.startsWith("https://") -> { | ||
| if ("://www." in this) this else this.replaceFirst("://", "://www.") | ||
| } | ||
|
|
||
| else -> "www.$this" | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Excellent implementation The logic correctly handles all cases:
The check for Edge case verificationVerified the logic handles these correctly:
|
||
|
|
||
| /** | ||
| * If the given [String] is a valid URI, "www." will be prepended (or inserted after the scheme | ||
| * if present) if it is not already present. Otherwise the original [String] will be returned. | ||
| */ | ||
| fun String.prefixWwwIfNecessary(): String = | ||
| prefixWwwIfNecessaryOrNull() ?: this | ||
|
|
||
| /** | ||
| * Checks if a string is using base32 digits. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -281,4 +281,65 @@ class StringExtensionsTest { | |
| fun `orZeroWidthSpace returns the original value for a non-blank string`() { | ||
| assertEquals("test", "test".orZeroWidthSpace()) | ||
| } | ||
|
|
||
| @Suppress("MaxLineLength") | ||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should prefix www when URI is valid and no scheme and no www`() { | ||
| val uri = "example.com" | ||
| val expected = "www.$uri" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
|
|
||
| assertEquals(expected, actual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should return URI unchanged when starts with www`() { | ||
| val uri = "www.example.com" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
| assertEquals(uri, actual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should prefix www when scheme is http and no www`() { | ||
| val uri = "http://example.com" | ||
| val expected = "http://www.example.com" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
| assertEquals(expected, actual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should prefix www when scheme is https and no www`() { | ||
| val uri = "https://example.com" | ||
| val expected = "https://www.example.com" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
| assertEquals(expected, actual) | ||
| } | ||
|
|
||
| @Suppress("MaxLineLength") | ||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should return URI unchanged when scheme is http and www is present`() { | ||
| val uri = "http://www.example.com" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
| assertEquals(uri, actual) | ||
| } | ||
|
|
||
| @Suppress("MaxLineLength") | ||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should return URI unchanged when scheme is https and www is present`() { | ||
| val uri = "https://www.example.com" | ||
| val actual = uri.prefixWwwIfNecessaryOrNull() | ||
| assertEquals(uri, actual) | ||
| } | ||
|
|
||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should return null when URI is empty string`() { | ||
| val uri = "" | ||
| assertNull(uri.prefixWwwIfNecessaryOrNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `prefixWwwIfNecessaryOrNull should return null when URI is invalid`() { | ||
| val invalidUri = "invalid uri" | ||
| assertNull(invalidUri.prefixWwwIfNecessaryOrNull()) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Comprehensive test coverage for string extensions The test coverage for the new
All major edge cases are covered. Good work! |
||
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.
๐ญ Consider the security implications of automatic www prefixing
While the www-prefixing improves compatibility, it's worth considering the security implications:
Current behavior: If a site hosts their Digital Asset Links file at
https://www.example.com/.well-known/assetlinks.jsonbut NOT athttps://example.com/.well-known/assetlinks.json, this code will allow passkey authentication by automatically trying the www variant.Question: Is this the intended behavior? Should we:
From a security perspective, option 1 (current) seems reasonable since:
However, this should be documented as an intentional security decision, not just a compatibility fix.