From 78b3557635efb43acbfae56a6b65c0fe4eb44a65 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 17 Nov 2021 21:54:34 +0100 Subject: [PATCH] Validate ip address and port --- .../ViewModels/AddConnectionViewModel.cs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Xamarin.Neo4j/Xamarin.Neo4j/Xamarin.Neo4j/ViewModels/AddConnectionViewModel.cs b/Xamarin.Neo4j/Xamarin.Neo4j/Xamarin.Neo4j/ViewModels/AddConnectionViewModel.cs index 2af12e2..a75fc29 100644 --- a/Xamarin.Neo4j/Xamarin.Neo4j/Xamarin.Neo4j/ViewModels/AddConnectionViewModel.cs +++ b/Xamarin.Neo4j/Xamarin.Neo4j/Xamarin.Neo4j/ViewModels/AddConnectionViewModel.cs @@ -9,7 +9,9 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; using Acr.UserDialogs; using Xamarin.Essentials; using Xamarin.Forms; @@ -41,7 +43,7 @@ namespace Xamarin.Neo4j.ViewModels Commands.Add("Test", new Command(async () => { - // TODO: Validate input. + if (!ValidateInput()) return; var connectionString = BuildConnectionString(); @@ -52,7 +54,7 @@ namespace Xamarin.Neo4j.ViewModels Commands.Add("Save", new Command(async () => { - // TODO: Validate input. + if (!ValidateInput()) return; var connectionString = BuildConnectionString(); @@ -70,7 +72,7 @@ namespace Xamarin.Neo4j.ViewModels Commands.Add("Connect", new Command(async () => { - // TODO: Validate input. + if (!ValidateInput()) return; var connectionString = BuildConnectionString(); @@ -103,6 +105,28 @@ namespace Xamarin.Neo4j.ViewModels }; } + private bool ValidateInput() + { + // Username and Password not checked because username- and passwordless connections are allowed + + var ipRegex = new Regex(@"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b"); + var portRegex = new Regex(@"^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"); + + if (!ipRegex.IsMatch(Host)) + { + UserDialogs.Instance.AlertAsync("Incorrect host format"); + return false; + } + + if (!portRegex.IsMatch(Port.ToString())) + { + UserDialogs.Instance.AlertAsync("Incorrect port format"); + return false; + } + + return true; + } + #region Bindable Properties public bool Encrypted