mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2026-01-27 03:04:21 +01:00
fix: problems with quotes from Host names and support SSH tokens (issue #32)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// ValidateHostname checks if a hostname is valid
|
||||
// Accepts regular hostnames, IP addresses, and SSH tokens like %h, %p, %r, %u, %n, %C, %d, %i, %k, %L, %l, %T
|
||||
func ValidateHostname(hostname string) bool {
|
||||
if len(hostname) == 0 || len(hostname) > 253 {
|
||||
return false
|
||||
@@ -22,7 +23,18 @@ func ValidateHostname(hostname string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
hostnameRegex := regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$`)
|
||||
// Check if hostname contains SSH tokens (e.g., %h, %p, %r, %u, %n, etc.)
|
||||
// SSH tokens are documented in ssh_config(5) man page
|
||||
sshTokenRegex := regexp.MustCompile(`%[hprunCdiklLT]`)
|
||||
if sshTokenRegex.MatchString(hostname) {
|
||||
// If it contains SSH tokens, it's a valid SSH config construct
|
||||
return true
|
||||
}
|
||||
|
||||
// RFC 1123: each label must start with alphanumeric, end with alphanumeric,
|
||||
// and contain only alphanumeric and hyphens. Labels are 1-63 chars.
|
||||
// A hostname is one or more labels separated by dots.
|
||||
hostnameRegex := regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]|[a-zA-Z0-9]{0,62})?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]|[a-zA-Z0-9]{0,62})?)*$`)
|
||||
return hostnameRegex.MatchString(hostname)
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,19 @@ func TestValidateHostname(t *testing.T) {
|
||||
{"hostname ending with dot", "example.com.", false},
|
||||
{"hostname with hyphen", "my-server.com", true},
|
||||
{"hostname starting with number", "1example.com", true},
|
||||
{"multiple hyphens and subdomains", "my-host-name-01.cwd.pub.domain.net", true},
|
||||
{"multiple hyphens", "my-host-name-01", true},
|
||||
{"complex hostname with hyphens", "server-01-prod.data-center.example.com", true},
|
||||
{"hostname with consecutive hyphens", "my--server.com", true},
|
||||
{"single char labels", "a.b.c.d.com", true},
|
||||
// SSH tokens support (issue #32 comment)
|
||||
{"SSH token %h", "%h.server.com", true},
|
||||
{"SSH token %p", "server.com:%p", true},
|
||||
{"SSH token %r", "%r@server.com", true},
|
||||
{"SSH token %u", "%u.example.com", true},
|
||||
{"SSH token %n", "%n.domain.net", true},
|
||||
{"SSH token %C", "host-%C.com", true},
|
||||
{"multiple SSH tokens", "%h.%u.server.com", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user