diff --git a/api/internal/edge/url.go b/api/internal/edge/url.go index 984eb421f..50ff44f40 100644 --- a/api/internal/edge/url.go +++ b/api/internal/edge/url.go @@ -19,6 +19,10 @@ func ParseHostForEdge(portainerURL string) (string, error) { portainerHost = parsedURL.Host } + if portainerHost == "" { + return "", errors.New("hostname cannot be empty") + } + if portainerHost == "localhost" { return "", errors.New("cannot use localhost as environment URL") } diff --git a/app/portainer/settings/edge-compute/AutomaticEdgeEnvCreation/AutoEnvCreationSettingsForm.tsx b/app/portainer/settings/edge-compute/AutomaticEdgeEnvCreation/AutoEnvCreationSettingsForm.tsx index 5cd77788a..0d9a045fb 100644 --- a/app/portainer/settings/edge-compute/AutomaticEdgeEnvCreation/AutoEnvCreationSettingsForm.tsx +++ b/app/portainer/settings/edge-compute/AutomaticEdgeEnvCreation/AutoEnvCreationSettingsForm.tsx @@ -23,11 +23,20 @@ const validation = yup.object({ EdgePortainerUrl: yup .string() .test( - 'not-local', - 'Cannot use localhost as environment URL', - (value) => !value?.includes('localhost') + 'url', + 'URL should be a valid URI and cannot include localhost', + (value) => { + if (!value) { + return false; + } + try { + const url = new URL(value); + return !!url.hostname && url.hostname !== 'localhost'; + } catch { + return false; + } + } ) - .url('URL should be a valid URI') .required('URL is required'), });