Merge pull request #6095 from vector-im/michaelk/correct_well_known_behaviour

Well known lookups should be to port 443 only.
This commit is contained in:
Michael Kaye 2022-05-18 16:50:43 +01:00 committed by GitHub
commit 5260e729ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 34 additions and 29 deletions

1
changelog.d/6095.bugfix Normal file
View file

@ -0,0 +1 @@
Correct .well-known/matrix/client handling for server_names which include ports.

View file

@ -177,7 +177,7 @@ object MatrixPatterns {
* - "@alice:domain.org".getDomain() will return "domain.org"
* - "@bob:domain.org:3455".getDomain() will return "domain.org:3455"
*/
fun String.getDomain(): String {
fun String.getServerName(): String {
if (BuildConfig.DEBUG && !isUserId(this)) {
// They are some invalid userId localpart in the wild, but the domain part should be there anyway
Timber.w("Not a valid user ID: $this")

View file

@ -20,7 +20,7 @@ import android.net.Uri
import dagger.Lazy
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.data.Credentials
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
@ -381,7 +381,7 @@ internal class DefaultAuthenticationService @Inject constructor(
return getWellknownTask.execute(
GetWellknownTask.Params(
domain = matrixId.getDomain(),
domain = matrixId.getServerName().substringBeforeLast(":"),
homeServerConnectionConfig = homeServerConnectionConfig.orWellKnownDefaults()
)
)

View file

@ -17,7 +17,7 @@
package org.matrix.android.sdk.internal.session.homeserver
import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
import org.matrix.android.sdk.api.extensions.orFalse
@ -93,10 +93,14 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
}
}.getOrNull()
// Domain may include a port (eg, matrix.org:8080)
// Per https://spec.matrix.org/latest/client-server-api/#well-known-uri we should extract the hostname from the server name
// So we take everything before the last : as the domain for the well-known task.
// NB: This is not always the same endpoint as capabilities / mediaConfig uses.
val wellknownResult = runCatching {
getWellknownTask.execute(
GetWellknownTask.Params(
domain = userId.getDomain(),
domain = userId.getServerName().substringBeforeLast(":"),
homeServerConnectionConfig = homeServerConnectionConfig
)
)

View file

@ -16,7 +16,7 @@
package org.matrix.android.sdk.internal.session.permalinks
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel
@ -55,9 +55,9 @@ internal class ViaParameterFinder @Inject constructor(
}
fun computeViaParams(userId: String, roomId: String, max: Int): List<String> {
val userHomeserver = userId.getDomain()
val userHomeserver = userId.getServerName()
return getUserIdsOfJoinedMembers(roomId)
.map { it.getDomain() }
.map { it.getServerName() }
.groupBy { it }
.mapValues { it.value.size }
.toMutableMap()
@ -92,7 +92,7 @@ internal class ViaParameterFinder @Inject constructor(
.orEmpty()
.toSet()
return userThatCanInvite.map { it.getDomain() }
return userThatCanInvite.map { it.getServerName() }
.groupBy { it }
.mapValues { it.value.size }
.toMutableMap()

View file

@ -16,7 +16,7 @@
package org.matrix.android.sdk.internal.session.room.alias
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
import org.matrix.android.sdk.internal.di.UserId
@ -65,6 +65,6 @@ internal class RoomAliasAvailabilityChecker @Inject constructor(
}
companion object {
internal fun String.toFullLocalAlias(userId: String) = "#" + this + ":" + userId.getDomain()
internal fun String.toFullLocalAlias(userId: String) = "#" + this + ":" + userId.getServerName()
}
}

View file

@ -36,7 +36,7 @@ import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.ensureTrailingSlash
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
@ -607,7 +607,7 @@ class LoginViewModel @AssistedInject constructor(
identityServerUri = wellKnownPrompt.identityServerUrl?.let { Uri.parse(it) }
)
?: HomeServerConnectionConfig(
homeServerUri = Uri.parse("https://${action.username.getDomain()}"),
homeServerUri = Uri.parse("https://${action.username.getServerName()}"),
homeServerUriBase = Uri.parse(wellKnownPrompt.homeServerUrl),
identityServerUri = wellKnownPrompt.identityServerUrl?.let { Uri.parse(it) }
)

View file

@ -38,7 +38,7 @@ import im.vector.app.features.login.LoginMode
import im.vector.app.features.login.ReAuthHelper
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
@ -640,7 +640,7 @@ class LoginViewModel2 @AssistedInject constructor(
}
viewEvent?.let { _viewEvents.post(it) }
val urlFromUser = action.username.getDomain()
val urlFromUser = action.username.getServerName()
setState {
copy(
isLoading = false,

View file

@ -20,7 +20,7 @@ import im.vector.app.R
import im.vector.app.core.extensions.andThen
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.onboarding.OnboardingAction.LoginOrRegister
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
@ -75,7 +75,7 @@ class DirectLoginUseCase @Inject constructor(
)
private fun fallbackConfig(action: LoginOrRegister, wellKnownPrompt: WellknownResult.Prompt) = HomeServerConnectionConfig(
homeServerUri = uriFactory.parse("https://${action.username.getDomain()}"),
homeServerUri = uriFactory.parse("https://${action.username.getServerName()}"),
homeServerUriBase = uriFactory.parse(wellKnownPrompt.homeServerUrl),
identityServerUri = wellKnownPrompt.identityServerUrl?.let { uriFactory.parse(it) }
)

View file

@ -16,14 +16,14 @@
package im.vector.app.features.raw.wellknown
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.data.SessionParams
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService
suspend fun RawService.getElementWellknown(sessionParams: SessionParams): ElementWellKnown? {
// By default we use the domain of the userId to retrieve the .well-known data
val domain = sessionParams.userId.getDomain()
val domain = sessionParams.userId.getServerName()
return tryOrNull { getWellknown(domain) }
?.let { ElementWellKnownMapper.from(it) }
}

View file

@ -35,7 +35,7 @@ import im.vector.app.features.raw.wellknown.getElementWellknown
import im.vector.app.features.raw.wellknown.isE2EByDefault
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService
@ -98,7 +98,7 @@ class CreateRoomViewModel @AssistedInject constructor(
private fun initHomeServerName() {
setState {
copy(
homeServerName = session.myUserId.getDomain()
homeServerName = session.myUserId.getServerName()
)
}
}

View file

@ -20,7 +20,7 @@ import im.vector.app.R
import im.vector.app.core.resources.StringArrayProvider
import im.vector.app.features.roomdirectory.RoomDirectoryData
import im.vector.app.features.roomdirectory.RoomDirectoryServer
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.thirdparty.ThirdPartyProtocol
import javax.inject.Inject
@ -37,7 +37,7 @@ class RoomDirectoryListCreator @Inject constructor(
val protocols = ArrayList<RoomDirectoryData>()
// Add user homeserver name
val userHsName = session.myUserId.getDomain()
val userHsName = session.myUserId.getServerName()
// Add default protocol
protocols.add(

View file

@ -31,7 +31,7 @@ import im.vector.app.features.powerlevel.PowerLevelsFlowFactory
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType
@ -96,7 +96,7 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
private fun initHomeServerName() {
setState {
copy(
homeServerName = session.myUserId.getDomain()
homeServerName = session.myUserId.getServerName()
)
}
}

View file

@ -35,7 +35,7 @@ import im.vector.app.core.resources.StringProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.IdentityServiceListener
import org.matrix.android.sdk.api.session.room.AliasAvailabilityResult
@ -66,7 +66,7 @@ class CreateSpaceViewModel @AssistedInject constructor(
val identityServerUrl = identityService.getCurrentIdentityServerUrl()
setState {
copy(
homeServerName = session.myUserId.getDomain(),
homeServerName = session.myUserId.getServerName(),
canInviteByMail = identityServerUrl != null
)
}

View file

@ -27,7 +27,7 @@ import kotlinx.coroutines.test.runTest
import org.amshove.kluent.should
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.data.WellKnown
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
@ -38,7 +38,7 @@ private val A_WELLKNOWN_FAILED_WITH_CONTENT_RESULT = WellknownResult.FailPrompt(
private val A_WELLKNOWN_FAILED_WITHOUT_CONTENT_RESULT = WellknownResult.FailPrompt(null, null)
private val NO_HOMESERVER_CONFIG: HomeServerConnectionConfig? = null
private val A_FALLBACK_CONFIG: HomeServerConnectionConfig = HomeServerConnectionConfig(
homeServerUri = FakeUri("https://${A_LOGIN_OR_REGISTER_ACTION.username.getDomain()}").instance,
homeServerUri = FakeUri("https://${A_LOGIN_OR_REGISTER_ACTION.username.getServerName()}").instance,
homeServerUriBase = FakeUri(A_WELLKNOWN_SUCCESS_RESULT.homeServerUrl).instance,
identityServerUri = null
)