-
Notifications
You must be signed in to change notification settings - Fork 26
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
fix(message): fix in-message embedded link validator (WPB-3554) #2088
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
package com.wire.android.ui.common.dialogs | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.stringResource | ||
import com.wire.android.R | ||
import com.wire.android.ui.common.WireDialog | ||
import com.wire.android.ui.common.WireDialogButtonProperties | ||
import com.wire.android.ui.common.WireDialogButtonType | ||
import com.wire.android.ui.home.conversations.VisitLinkDialogState | ||
|
||
@Composable | ||
fun VisitLinkDialog(dialogState: VisitLinkDialogState, hideDialog: () -> Unit) { | ||
if (dialogState is VisitLinkDialogState.Visible) { | ||
WireDialog( | ||
title = stringResource(R.string.label_visit_link_title), | ||
text = stringResource(R.string.visit_link_dialog_body, dialogState.link), | ||
buttonsHorizontalAlignment = false, | ||
onDismiss = hideDialog, | ||
optionButton1Properties = WireDialogButtonProperties( | ||
text = stringResource(R.string.label_open), | ||
type = WireDialogButtonType.Primary, | ||
onClick = dialogState.openLink | ||
), | ||
optionButton2Properties = WireDialogButtonProperties( | ||
text = stringResource(R.string.label_cancel), | ||
type = WireDialogButtonType.Primary, | ||
onClick = hideDialog | ||
) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.util | ||
|
||
import java.net.URLDecoder | ||
|
||
fun containsSchema(url: String): Boolean { | ||
val regexPattern = Regex(".+://.+") | ||
|
||
return regexPattern.matches(url) | ||
} | ||
|
||
fun normalizeLink(url: String): String { | ||
val normalizedUrl = URLDecoder.decode(url, "UTF-8") // Decode URL to human-readable format | ||
|
||
return if (containsSchema(normalizedUrl)) { | ||
normalizedUrl | ||
} else { | ||
"https://$normalizedUrl" | ||
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. question: if the link already have something like, "/google.com" then it will turn into "https:///google.com" and it will fail to open, do we need to care about those edge cases ? 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. /google.com is not a URL 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. but what if we send 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. I'd blame whoever pasted the invalid URI there to be honest 😄. Would be a nice to have for the rare cases where people mistype that hard, but not sure if worth the effort |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android | ||
|
||
import kotlin.random.Random | ||
|
||
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9') | ||
|
||
fun Random.string(length: Int) = (1..length) | ||
.map { Random.nextInt(0, charPool.size).let { charPool[it] } } | ||
.joinToString("") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.util | ||
|
||
import com.wire.android.string | ||
import org.junit.jupiter.api.Test | ||
import kotlin.random.Random | ||
|
||
class UriUtilTest { | ||
@Test | ||
fun givenLink_whenTheLinkStartsWithHttps_thenReturnsTheSameLink() { | ||
val input = "https://google.com" | ||
val expected = "https://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkStartsWithHttp_thenReturnsTheSameLink() { | ||
val input = "http://google.com" | ||
val expected = "http://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkStartsWithRandomSchema_thenReturnsTheSameLink() { | ||
val randomString = Random.string(Random.nextInt(5)) | ||
val input = "$randomString://google.com" | ||
val expected = "$randomString://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkWithoutSchema_thenReturnsTheLinkWithHttps() { | ||
val input = Random.string(Random.nextInt(20)) | ||
val expected = "https://$input" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
} |
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.
Suggestion: leverage already existing functions in the JVM.
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.
this only validates against valid and known schemes, it's not passing against unknown schemes.
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.
@mchenani how so?