From 9ff33ec1fd836ebe35b50cc0346531fa983a59e3 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 23 Mar 2018 01:08:33 -0700 Subject: [PATCH] Fix IllegalArgumentException thrown by Base64.decode --- .../github/shadowsocks/database/Profile.kt | 71 ++++++++++--------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/mobile/src/main/java/com/github/shadowsocks/database/Profile.kt b/mobile/src/main/java/com/github/shadowsocks/database/Profile.kt index 43243d4a..9e8bfff6 100644 --- a/mobile/src/main/java/com/github/shadowsocks/database/Profile.kt +++ b/mobile/src/main/java/com/github/shadowsocks/database/Profile.kt @@ -43,41 +43,46 @@ class Profile : Serializable { fun findAll(data: CharSequence?) = pattern.findAll(data ?: "").map { val uri = Uri.parse(it.value) - if (uri.userInfo == null) { - val match = legacyPattern.matchEntire(String(Base64.decode(uri.host, Base64.NO_PADDING))) - if (match != null) { - val profile = Profile() - profile.method = match.groupValues[1].toLowerCase() - profile.password = match.groupValues[2] - profile.host = match.groupValues[3] - profile.remotePort = match.groupValues[4].toInt() - profile.plugin = uri.getQueryParameter(Key.plugin) - profile.name = uri.fragment - profile + try { + if (uri.userInfo == null) { + val match = legacyPattern.matchEntire(String(Base64.decode(uri.host, Base64.NO_PADDING))) + if (match != null) { + val profile = Profile() + profile.method = match.groupValues[1].toLowerCase() + profile.password = match.groupValues[2] + profile.host = match.groupValues[3] + profile.remotePort = match.groupValues[4].toInt() + profile.plugin = uri.getQueryParameter(Key.plugin) + profile.name = uri.fragment + profile + } else { + Log.e(TAG, "Unrecognized URI: ${it.value}") + null + } } else { - Log.e(TAG, "Unrecognized URI: ${it.value}") - null - } - } else { - val match = userInfoPattern.matchEntire(String(Base64.decode(uri.userInfo, - Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE))) - if (match != null) { - val profile = Profile() - profile.method = match.groupValues[1] - profile.password = match.groupValues[2] - // bug in Android: https://code.google.com/p/android/issues/detail?id=192855 - val javaURI = URI(it.value) - profile.host = javaURI.host ?: "" - if (profile.host.firstOrNull() == '[' && profile.host.lastOrNull() == ']') - profile.host = profile.host.substring(1, profile.host.length - 1) - profile.remotePort = javaURI.port - profile.plugin = uri.getQueryParameter(Key.plugin) - profile.name = uri.fragment ?: "" - profile - } else { - Log.e(TAG, "Unknown user info: ${it.value}") - null + val match = userInfoPattern.matchEntire(String(Base64.decode(uri.userInfo, + Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE))) + if (match != null) { + val profile = Profile() + profile.method = match.groupValues[1] + profile.password = match.groupValues[2] + // bug in Android: https://code.google.com/p/android/issues/detail?id=192855 + val javaURI = URI(it.value) + profile.host = javaURI.host ?: "" + if (profile.host.firstOrNull() == '[' && profile.host.lastOrNull() == ']') + profile.host = profile.host.substring(1, profile.host.length - 1) + profile.remotePort = javaURI.port + profile.plugin = uri.getQueryParameter(Key.plugin) + profile.name = uri.fragment ?: "" + profile + } else { + Log.e(TAG, "Unknown user info: ${it.value}") + null + } } + } catch (e: IllegalArgumentException) { + Log.e(TAG, "Invalid base64 detected: ${it.value}") + null } }.filterNotNull() }