From b4932d6a44bb388c1cd1f166987f49310f392b56 Mon Sep 17 00:00:00 2001 From: Lucas Garfield Date: Wed, 11 Dec 2024 12:25:55 -0600 Subject: [PATCH] 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. --- .../CreateImageWizard/utilities/useValidation.tsx | 6 +++++- src/Components/CreateImageWizard/validators.ts | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Components/CreateImageWizard/utilities/useValidation.tsx b/src/Components/CreateImageWizard/utilities/useValidation.tsx index 04a20b57..2307c6d2 100644 --- a/src/Components/CreateImageWizard/utilities/useValidation.tsx +++ b/src/Components/CreateImageWizard/utilities/useValidation.tsx @@ -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, }; diff --git a/src/Components/CreateImageWizard/validators.ts b/src/Components/CreateImageWizard/validators.ts index 1e03f8e6..c2525d79 100644 --- a/src/Components/CreateImageWizard/validators.ts +++ b/src/Components/CreateImageWizard/validators.ts @@ -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; };