Why does IPAddress.TryParse allow anything after a ']'
-
26-09-2019 - |
Question
I'd like to use System.Net.IPAddress.TryParse to validate IPv6 addresses because I don't want to write my own reg exp :-)
However, this seems to allow strings such as "(validIPv6)](anythingatallhere)" - for example, "1234::5678:abcd]whargarbl".
Is there a reason for these being valid, or is this a fault?
This is further complicated by the fact that I actually want only strings of the form "[(validIPv6)]:(portnumber)" so I'm going to have to do a bit of validation myself.
Solution
I was looking through reflector and it seems that if your current OS does not support IPV6 a parse routine is called that will ignore everything past the final ']'
character.
This here seems to be the offending code, take notice of the second or condition as it relates to the preceding bit of code.
int length = ipString.Length;
fixed (char* str2 = ((char*) ipString))
{
char* name = str2;
if (IPv6AddressHelper.IsValid(name, start, ref length) || (length != ipString.Length))