[pve-devel] [PATCH installer] tui: fix FQDN validation

Christoph Heiss c.heiss at proxmox.com
Thu Jun 22 13:05:39 CEST 2023


Add checks to ensure that:
* It actually has a hostname, not just a domain name
* Properly check if the hostname is purely numeric, by verifying each
  FQDN part

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 proxmox-tui-installer/src/main.rs  |  8 ++++++--
 proxmox-tui-installer/src/utils.rs | 12 +++++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/proxmox-tui-installer/src/main.rs b/proxmox-tui-installer/src/main.rs
index d2a5fcf..b49f2be 100644
--- a/proxmox-tui-installer/src/main.rs
+++ b/proxmox-tui-installer/src/main.rs
@@ -587,10 +587,14 @@ fn network_dialog(siv: &mut Cursive) -> InstallerView {
                     Err("host and gateway IP address version must not differ".to_owned())
                 } else if address.addr().is_ipv4() != dns_server.is_ipv4() {
                     Err("host and DNS IP address version must not differ".to_owned())
-                } else if fqdn.to_string().chars().all(|c| c.is_ascii_digit()) {
+                } else if fqdn
+                    .parts()
+                    .iter()
+                    .any(|p| p.chars().all(|c| c.is_ascii_digit() || c == '.'))
+                {
                     // Not supported/allowed on Debian
                     Err("hostname cannot be purely numeric".to_owned())
-                } else if fqdn.to_string().ends_with(".invalid") {
+                } else if !fqdn.is_valid() || fqdn.to_string().ends_with(".invalid") {
                     Err("hostname does not look valid".to_owned())
                 } else {
                     Ok(NetworkOptions {
diff --git a/proxmox-tui-installer/src/utils.rs b/proxmox-tui-installer/src/utils.rs
index 3245fac..08521f3 100644
--- a/proxmox-tui-installer/src/utils.rs
+++ b/proxmox-tui-installer/src/utils.rs
@@ -122,7 +122,7 @@ impl Fqdn {
             .map(ToOwned::to_owned)
             .collect::<Vec<String>>();

-        if !parts.iter().all(&Self::validate_single) {
+        if parts.is_empty() || !parts.iter().all(&Self::validate_single) {
             Err(())
         } else {
             Ok(Self { parts })
@@ -143,6 +143,16 @@ impl Fqdn {
         parts.join(".")
     }

+    pub fn parts(&self) -> &[String] {
+        &self.parts
+    }
+
+    pub fn is_valid(&self) -> bool {
+        // It's a valid FQDN if it has at least a hostname and a TLD name, the
+        // latter is ensured by the constructor.
+        self.has_host()
+    }
+
     fn has_host(&self) -> bool {
         self.parts.len() > 1
     }
--
2.40.1






More information about the pve-devel mailing list