From 18b8c8fe1b370c47978e7ae0342139d556969f00 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 22 Mar 2018 23:45:11 +0100 Subject: Validate network names Signed-off-by: pacien --- .../pacien/tincapp/activities/ConfigureActivity.kt | 21 ++++++++++++++++----- app/src/main/res/values/strings.xml | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt index 64ed61d..e311415 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt @@ -19,14 +19,16 @@ import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.TincApp import org.pacien.tincapp.context.AppPaths import org.pacien.tincapp.extensions.Java.exceptionallyAccept +import java.util.regex.Pattern /** * @author pacien */ class ConfigureActivity : BaseActivity() { companion object { - val REQUEST_SCAN = 0 - val SCAN_PROVIDER = "com.google.zxing.client.android" + private const val REQUEST_SCAN = 0 + private const val SCAN_PROVIDER = "com.google.zxing.client.android" + private val NETWORK_NAME_PATTERN = Pattern.compile("^[^\\x00/]*$") } private var joinDialog: View? = null @@ -98,21 +100,24 @@ class ConfigureActivity : BaseActivity() { private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( R.string.message_generating_configuration, - Tinc.init(netName, nodeName) + validateNetName(netName) + .thenCompose { Tinc.init(netName, nodeName) } .thenCompose { TincApp.removeScripts(netName) } .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( R.string.message_joining_network, - Tinc.join(netName, url) + validateNetName(netName) + .thenCompose { Tinc.join(netName, url) } .thenCompose { TincApp.removeScripts(netName) } .thenCompose { TincApp.generateIfaceCfg(netName) } .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( R.string.message_encrypting_decrypting_private_keys, - TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) + validateNetName(netName) + .thenCompose { TincApp.setPassphrase(netName, currentPassphrase, newPassphrase) }) private fun execAction(@StringRes label: Int, action: CompletableFuture) { showProgressDialog(label).let { progressDialog -> @@ -122,4 +127,10 @@ class ConfigureActivity : BaseActivity() { .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } } } + + private fun validateNetName(netName: String): CompletableFuture = + if (NETWORK_NAME_PATTERN.matcher(netName).matches()) + CompletableFuture.completedFuture(Unit) + else + CompletableFuture.failedFuture(IllegalArgumentException(resources.getString(R.string.message_invalid_network_name))) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25bee44..da3fa10 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -88,6 +88,7 @@ Logging paused. Could not apply network interface configuration:\n\n%1$s Could not bind network interface. Is another VPN running? + Invalid network name. none yes -- cgit v1.2.3