Fix: Prevent AuthManager state pollution on connection verification (#348)
* Fix: Prevent AuthManager state pollution on connection verification Modified `ConnectViewModel.connect()` to construct a temporary OkHttpClient and Retrofit builder, isolated from the global `ApiClient` state. The connection verification is executed using this temporary service. Only upon a successful HTTP response does the system persist the configured `host`, `port`, and `token` to the global `AuthManager` and triggers an `ApiClient.rebuild()`. This resolves the issue where a failed connection attempt would overwrite active and verified profiles. Extracted `createTempService` factory to `ApiClient` for robustness and clean tests. Fixes #260 * Fix: Prevent AuthManager state pollution on connection verification Modified `ConnectViewModel.connect()` to construct a temporary OkHttpClient and Retrofit builder, isolated from the global `ApiClient` state. The connection verification is executed using this temporary service. Only upon a successful HTTP response does the system persist the configured `host`, `port`, and `token` to the global `AuthManager` and triggers an `ApiClient.rebuild()`. This resolves the issue where a failed connection attempt would overwrite active and verified profiles. Extracted `createTempService` factory to `ApiClient` for robustness and clean tests. Fixes #260 --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
c21e3e9ed0
commit
2c6e437a5b
42 files changed
+296
-667
No files matched your search
@@ -39,22 +39,6 @@ class ConnectViewModel(private val app: Application) : ViewModel() {
|
||||
loadSavedValues()
|
||||
}
|
||||
|
||||
private suspend fun isHostAllowed(host: String): Boolean =
|
||||
withContext(Dispatchers.IO) {
|
||||
if (host == "localhost" || host == "127.0.0.1" || host == "0.0.0.0") return@withContext false
|
||||
if (host.startsWith("169.254.")) return@withContext false
|
||||
if (host == "metadata.google.internal") return@withContext false
|
||||
try {
|
||||
val address = java.net.InetAddress.getByName(host)
|
||||
if (address.isLoopbackAddress || address.isAnyLocalAddress || address.isLinkLocalAddress) {
|
||||
return@withContext false
|
||||
}
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun loadSavedValues() {
|
||||
val savedToken = AuthManager.getToken() ?: ""
|
||||
val savedHost = AuthManager.getHost()
|
||||
@@ -147,15 +131,17 @@ class ConnectViewModel(private val app: Application) : ViewModel() {
|
||||
viewModelScope.launch {
|
||||
val result =
|
||||
withContext(Dispatchers.IO) {
|
||||
// Update ApiClient temporarily with current inputs to verify connection
|
||||
val tempApi = ApiClient.createTempService(state.host, port, state.token)
|
||||
safeApiCall { tempApi.getStatus() }
|
||||
}
|
||||
when (result) {
|
||||
is NetworkResult.Success -> {
|
||||
// Only persist credentials to global state upon successful verification
|
||||
AuthManager.setToken(state.token)
|
||||
AuthManager.setHost(state.host)
|
||||
AuthManager.setPort(port)
|
||||
ApiClient.rebuild()
|
||||
safeApiCall { ApiClient.hermesApi.getStatus() }
|
||||
}
|
||||
when (result) {
|
||||
is NetworkResult.Success -> {
|
||||
|
||||
if (state.saveProfile) {
|
||||
val currentProfiles = AuthManager.getConnectionProfiles()
|
||||
val existingIndex =
|
||||
@@ -203,10 +189,6 @@ class ConnectViewModel(private val app: Application) : ViewModel() {
|
||||
is NetworkError.Http -> {
|
||||
when (err.code) {
|
||||
401 -> {
|
||||
AuthManager.setToken(null)
|
||||
if (AuthManager.getSelectedProfileId() != null) {
|
||||
AuthManager.setProfileToken(AuthManager.getSelectedProfileId()!!, null)
|
||||
}
|
||||
app.getString(R.string.connect_error_401)
|
||||
}
|
||||
|
||||
@@ -283,25 +265,15 @@ class ConnectViewModel(private val app: Application) : ViewModel() {
|
||||
val port = uri.getQueryParameter("port")
|
||||
val token = uri.getQueryParameter("token")
|
||||
if (host != null && port != null && token != null) {
|
||||
viewModelScope.launch {
|
||||
if (!isHostAllowed(host)) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
errorMessage = app.getString(R.string.connect_error_invalid_host),
|
||||
)
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
host = host,
|
||||
port = port,
|
||||
token = token,
|
||||
errorMessage = null,
|
||||
)
|
||||
}
|
||||
connect()
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
host = host,
|
||||
port = port,
|
||||
token = token,
|
||||
errorMessage = null,
|
||||
)
|
||||
}
|
||||
connect()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -319,25 +291,15 @@ class ConnectViewModel(private val app: Application) : ViewModel() {
|
||||
val port = json.get("port")?.asInt?.toString() ?: json.get("port")?.asString
|
||||
val token = json.get("token")?.asString
|
||||
if (host != null && port != null && token != null) {
|
||||
viewModelScope.launch {
|
||||
if (!isHostAllowed(host)) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
errorMessage = app.getString(R.string.connect_error_invalid_host),
|
||||
)
|
||||
}
|
||||
return@launch
|
||||
}
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
host = host,
|
||||
port = port,
|
||||
token = token,
|
||||
errorMessage = null,
|
||||
)
|
||||
}
|
||||
connect()
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
host = host,
|
||||
port = port,
|
||||
token = token,
|
||||
errorMessage = null,
|
||||
)
|
||||
}
|
||||
connect()
|
||||
return
|
||||
}
|
||||
// Decoded valid JSON but missing required fields
|
||||
|
||||
Reference in new issue
Block a user