Improved instance name validation

This commit is contained in:
Thorsten Sommer 2024-07-03 14:45:26 +02:00
parent 88e3e46334
commit 71176cedaf
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -193,27 +193,40 @@ public partial class ProviderDialog : ComponentBase
return null; return null;
} }
[GeneratedRegex("^[a-zA-Z0-9 ]+$")] [GeneratedRegex(@"^[a-zA-Z0-9\-_. ]+$")]
private static partial Regex InstanceNameRegex(); private static partial Regex InstanceNameRegex();
private static readonly string[] RESERVED_NAMES = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
private string? ValidatingInstanceName(string instanceName) private string? ValidatingInstanceName(string instanceName)
{ {
if(string.IsNullOrWhiteSpace(instanceName)) if (string.IsNullOrWhiteSpace(instanceName))
return "Please enter an instance name."; return "Please enter an instance name.";
if(instanceName.StartsWith(' ')) if (instanceName.StartsWith(' ') || instanceName.StartsWith('.'))
return "The instance name must not start with a space."; return "The instance name must not start with a space or a dot.";
if(instanceName.EndsWith(' ')) if (instanceName.EndsWith(' ') || instanceName.EndsWith('.'))
return "The instance name must not end with a space."; return "The instance name must not end with a space or a dot.";
if (instanceName.StartsWith('-') || instanceName.StartsWith('_'))
return "The instance name must not start with a hyphen or an underscore.";
if (instanceName.Length > 255)
return "The instance name must not exceed 255 characters.";
// The instance name must only contain letters, numbers, and spaces:
if (!InstanceNameRegex().IsMatch(instanceName)) if (!InstanceNameRegex().IsMatch(instanceName))
return "The instance name must only contain letters, numbers, and spaces."; return "The instance name must only contain letters, numbers, spaces, hyphens, underscores, and dots.";
if(instanceName.Contains(" ")) if (instanceName.Contains(" "))
return "The instance name must not contain consecutive spaces."; return "The instance name must not contain consecutive spaces.";
if (RESERVED_NAMES.Contains(instanceName.ToUpperInvariant()))
return "This name is reserved and cannot be used.";
if (instanceName.Any(c => Path.GetInvalidFileNameChars().Contains(c)))
return "The instance name contains invalid characters.";
// The instance name must be unique: // The instance name must be unique:
var lowerInstanceName = instanceName.ToLowerInvariant(); var lowerInstanceName = instanceName.ToLowerInvariant();
if (lowerInstanceName != this.dataEditingPreviousInstanceName && this.UsedInstanceNames.Contains(lowerInstanceName)) if (lowerInstanceName != this.dataEditingPreviousInstanceName && this.UsedInstanceNames.Contains(lowerInstanceName))