public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: Christoph Heiss <c.heiss@proxmox.com>
Cc: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Subject: Re: [pve-devel] [PATCH proxmox 2/2] network-types: add hostname type
Date: Tue, 1 Apr 2025 16:02:17 +0200	[thread overview]
Message-ID: <31921890-830e-446d-8ba9-1b473fae8fd8@proxmox.com> (raw)
In-Reply-To: <D8VCQAP1N3UA.2K8R8DPL9BA28@proxmox.com>



On 4/1/25 15:54, Christoph Heiss wrote:
> On Tue Apr 1, 2025 at 3:21 PM CEST, Stefan Hanreich wrote:
>> Add a type for representing Linux hostnames. These are the same
>> constraints as the installer enforces [1].
> 
> Actually, the regex is not *fully* complete, see parse_fqdn() in the
> same file in [1].
> 
> [..]
>> [1] https://git.proxmox.com/?p=pve-installer.git;a=blob;f=Proxmox/Sys/Net.pm;h=81cb15f0042b195461324fffeca53d732133629e;hb=HEAD#l11
>>
> [..]
>> diff --git a/proxmox-network-types/src/hostname.rs b/proxmox-network-types/src/hostname.rs
>> new file mode 100644
>> index 00000000..f183aecb
>> --- /dev/null
>> +++ b/proxmox-network-types/src/hostname.rs
> [..]
>> +/// Hostname of a Linux machine
>> +///
>> +/// A hostname is at most 63 characters long and must only contain lowercase alphanumeric
>> +/// characters as well as hyphens. It must not start or end with a hyphen.
> 
> This should probably also reject purely numeric hostnames, as mentioned
> in Bugzilla #1054 [0]. This isn't a requirement per any RFC, but seems
> to can cause subtle bugs. It's enforced in the installer too.
> 
> [0] https://bugzilla.proxmox.com/show_bug.cgi?id=1054
> 
>> +#[derive(Debug, Deserialize, Serialize, Clone, Eq, Hash, PartialOrd, Ord, PartialEq)]
>> +pub struct Hostname(String);
>> +
> [..]
>> +
>> +impl Hostname {
>> +    /// Constructs a new hostname from a string
>> +    ///
>> +    /// This function accepts characters in any case, but the resulting hostname will be
>> +    /// lowercased.
>> +    pub fn new(name: impl AsRef<str>) -> Result<Self, HostnameError> {
>> +        let name_ref = name.as_ref();
>> +
>> +        if !(1..63).contains(&name_ref.len()) {
> 
> This actually fails for hostnames exactly 63 characters long (which
> should be accepted), as `..` is non-inclusive :^)
> 
> E.g. the following tests, adapted from proxmox-installer-common/utils.rs
> in pve-installer:
> 
>   #[cfg(test)]
>   mod tests {
>       use super::*;
> 
>       #[test]
>       fn too_long() {
>           assert!(Hostname::new(format!("{}.com", "a".repeat(63))).is_ok());
>           assert!(Hostname::new(format!("{}.com", "a".repeat(64))).is_err());
>       }
>   }
> 
>> +            return Err(HostnameError::InvalidLength);
>> +        }
>> +
>> +        let host_name = name_ref.to_lowercase();
>> +
>> +        let mut characters = host_name.chars();
>> +
>> +        // first character must not be a hyphen
>> +        // SAFETY: ok because of length check
>> +        if !characters.next().unwrap().is_alphanumeric() {
> 
> This should rather use .is_ascii_alphanumeric(). Otherwise any
> alphanumeric Unicode character is accepted [1], but hostnames may be
> ASCII only, as per e.g. RFC 952 ("ASSUMPTIONS") and hostname(7) [2].
> 
> [1] https://doc.rust-lang.org/std/primitive.char.html#method.is_alphabetic
> [2] https://manpages.debian.org/bookworm/manpages/hostname.7.en.html
> 
>> +            return Err(HostnameError::InvalidSymbols);
>> +        }
>> +
>> +        if !characters.all(|c| c.is_alphanumeric() || c == '-') {
> 
> ^ same as above.
> 
>> +            return Err(HostnameError::InvalidSymbols);
>> +        }
>> +
>> +        // last character must not be a hyphen
>> +        // SAFETY: ok because of length check
>> +        if !host_name.chars().next_back().unwrap().is_alphanumeric() {
> 
> ^ same as above.
> 

Thanks! Will send a v3 with the suggested improvements


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  reply	other threads:[~2025-04-01 14:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-01 13:21 [pve-devel] [PATCH proxmox 1/2] network-types: initial commit Stefan Hanreich
2025-04-01 13:21 ` [pve-devel] [PATCH proxmox 2/2] network-types: add hostname type Stefan Hanreich
2025-04-01 13:28   ` Maximiliano Sandoval
2025-04-01 13:54   ` Christoph Heiss
2025-04-01 14:02     ` Stefan Hanreich [this message]
2025-04-01 13:21 ` [pve-devel] [PATCH proxmox-ve-rs 1/1] ve-config: move types to proxmox-network-types Stefan Hanreich
2025-04-01 13:21 ` [pve-devel] [PATCH proxmox-firewall 1/1] firewall: nftables: migrate " Stefan Hanreich
2025-04-01 13:36 ` [pve-devel] superseded: [PATCH proxmox 1/2] network-types: initial commit Stefan Hanreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=31921890-830e-446d-8ba9-1b473fae8fd8@proxmox.com \
    --to=s.hanreich@proxmox.com \
    --cc=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal