From 4b7c8eb9a29c0167eb6aece3a4f8da44e37f59e6 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Sun, 26 Sep 2021 10:41:56 +0200 Subject: [PATCH] Throw error on invalid credentials or address --- source/libdnet/api/client.d | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/source/libdnet/api/client.d b/source/libdnet/api/client.d index fac3adc..0e533f2 100644 --- a/source/libdnet/api/client.d +++ b/source/libdnet/api/client.d @@ -37,12 +37,43 @@ public struct ConnectionDetails throw new DNetException("Address must be tuple of length 2"); } - - /* FIXME: Also throw for invalid address */ + /** + * Credentials must be valid + * + * 1. Must not both be empty + * 2. We must also strip the username of any whitespace on any side (FIXME) + */ + if(!(credentials[0].length > 0 && credentials[1].length > 0)) + { + throw new DNetException("Username or password has incorrect length"); + } + /* Save credentials */ this.username = credentials[0]; this.password = credentials[1]; - this.address = parseAddress(address[0], to!(ushort)(address[1])); + + /** + * Addresses must be valid + * + * 1. Must not be empty + */ + if(address[0].length > 0 && address[1].length > 0) + { + /* Make sure they pass address syntax check */ + try + { + /* Save address */ + this.address = parseAddress(address[0], to!(ushort)(address[1])); + } + catch(SocketException) + { + throw new DNetException("Invalid address format (error in port or address)"); + } + } + else + { + throw new DNetException("Address or port has incorrect length"); + } } }