Wizard: Hostname validation

Hostname validation rules can be found in the hostname man pages (`man 5
hostname`). This commit tweaks the hostname validator function so it is
in line with these guidelines by limiting the length to 64 characters
and also updates the error message for invalid hostnames to provide
users with some additional guidance/context when an invalid hostname is
provided.
This commit is contained in:
Lucas Garfield 2024-12-11 12:25:55 -06:00 committed by Lucas Garfield
parent 5a514d1d04
commit b4932d6a44
2 changed files with 14 additions and 6 deletions

View file

@ -140,10 +140,14 @@ export function useFirstBootValidation(): StepValidation {
export function useHostnameValidation(): StepValidation {
const hostname = useAppSelector(selectHostname);
// Error message taken from hostname man page (`man 5 hostname`)
const errorMessage =
'Invalid hostname. The hostname should be composed of up to 64 7-bit ASCII lower-case alphanumeric characters or hyphens forming a valid DNS domain name. It is recommended that this name contains only a single label, i.e. without any dots.';
if (!isHostnameValid(hostname)) {
return {
errors: {
hostname: 'Invalid hostname',
hostname: errorMessage,
},
disabledNext: true,
};

View file

@ -92,10 +92,14 @@ export const isNtpServerValid = (ntpServer: string) => {
};
export const isHostnameValid = (hostname: string) => {
if (hostname !== '') {
return /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test(
hostname
);
switch (true) {
case hostname.length > 63:
return false;
case hostname !== '':
return /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test(
hostname
);
default:
return true;
}
return true;
};