mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2026-03-14 03:41:27 +01:00
fix: allow env vars and SSH tokens in IdentityFile validation (issue #33)
ValidateIdentityFile now accepts $VAR/${VAR} (expanded via os.Expand, undefined vars accepted as-is) and SSH tokens like %d, %h before falling back to os.Stat.
The raw value is preserved when writing to ssh_config.
This commit is contained in:
@@ -66,6 +66,25 @@ func ValidateIdentityFile(path string) bool {
|
||||
if path == "" {
|
||||
return true // Optional field
|
||||
}
|
||||
// SSH tokens (e.g. %d, %h, %r, %u) are resolved by SSH at connection time
|
||||
sshTokenRegex := regexp.MustCompile(`%[hprunCdiklLT]`)
|
||||
if sshTokenRegex.MatchString(path) {
|
||||
return true
|
||||
}
|
||||
// Expand environment variables ($VAR and ${VAR}); track undefined ones
|
||||
hasUndefined := false
|
||||
path = os.Expand(path, func(key string) string {
|
||||
val, ok := os.LookupEnv(key)
|
||||
if !ok {
|
||||
hasUndefined = true
|
||||
return "$" + key
|
||||
}
|
||||
return val
|
||||
})
|
||||
// If any variable was undefined, accept the path (SSH will report the error)
|
||||
if hasUndefined {
|
||||
return true
|
||||
}
|
||||
// Expand ~ to home directory
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
|
||||
Reference in New Issue
Block a user