-
Notifications
You must be signed in to change notification settings - Fork 688
fix(ssh): SSH agent IdentitiesOnly logic and public key parsing #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8843583
43fb6e8
4e8f8ae
8ca09fd
e999975
3d712a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -74,9 +74,9 @@ type ConnectionError struct { | |||||||||||||
|
|
||||||||||||||
| func (ce ConnectionError) Error() string { | ||||||||||||||
| if ce.CurrentClient == nil { | ||||||||||||||
| return fmt.Sprintf("Connecting to %s, Error: %v", ce.NextOpts, ce.Err) | ||||||||||||||
| return fmt.Sprintf("Connecting to %s, Error: %v", MaskString(ce.NextOpts.String()), ce.Err) | ||||||||||||||
| } | ||||||||||||||
| return fmt.Sprintf("Connecting from %v to %s (jump number %d), Error: %v", ce.CurrentClient, ce.NextOpts, ce.JumpNum, ce.Err) | ||||||||||||||
| return fmt.Sprintf("Connecting from client to %s (jump number %d), Error: %v", MaskString(ce.NextOpts.String()), ce.JumpNum, ce.Err) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func SimpleMessageFromPossibleConnectionError(err error) string { | ||||||||||||||
|
|
@@ -89,6 +89,116 @@ func SimpleMessageFromPossibleConnectionError(err error) string { | |||||||||||||
| return err.Error() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // logSSHKeywords logs SSH configuration in a sanitized way (DEBUG level) | ||||||||||||||
| func logSSHKeywords(ctx context.Context, sshKeywords *wconfig.ConnKeywords) { | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] User: %s\n", MaskString(utilfn.SafeDeref(sshKeywords.SshUser))) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] HostName: %s\n", MaskString(utilfn.SafeDeref(sshKeywords.SshHostName))) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] Port: %s\n", utilfn.SafeDeref(sshKeywords.SshPort)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] IdentityAgent: %s\n", filepath.Base(utilfn.SafeDeref(sshKeywords.SshIdentityAgent))) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] IdentitiesOnly: %v\n", utilfn.SafeDeref(sshKeywords.SshIdentitiesOnly)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] IdentityFile count: %d\n", len(sshKeywords.SshIdentityFile)) | ||||||||||||||
| // Log masked identity file paths for privacy | ||||||||||||||
| for i, f := range sshKeywords.SshIdentityFile { | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] IdentityFile[%d]: %s\n", i, maskIdentityFile(f)) | ||||||||||||||
| } | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] PubkeyAuthentication: %v\n", utilfn.SafeDeref(sshKeywords.SshPubkeyAuthentication)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] PasswordAuthentication: %v\n", utilfn.SafeDeref(sshKeywords.SshPasswordAuthentication)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] KbdInteractiveAuthentication: %v\n", utilfn.SafeDeref(sshKeywords.SshKbdInteractiveAuthentication)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] PreferredAuthentications: %v\n", sshKeywords.SshPreferredAuthentications) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] AddKeysToAgent: %v\n", utilfn.SafeDeref(sshKeywords.SshAddKeysToAgent)) | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] ProxyJump count: %d\n", len(sshKeywords.SshProxyJump)) | ||||||||||||||
| // Note: do not log PasswordSecretName value, only indicate if configured | ||||||||||||||
| if sshKeywords.SshPasswordSecretName != nil && *sshKeywords.SshPasswordSecretName != "" { | ||||||||||||||
| blocklogger.Debugf(ctx, "[ssh-config] PasswordSecretName: <configured>\n") | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // MaskString masks a string for privacy, showing only first 3 and last 3 characters. | ||||||||||||||
| // Uses rune-based slicing to properly handle multi-byte UTF-8 characters. | ||||||||||||||
| func MaskString(s string) string { | ||||||||||||||
| if s == "" { | ||||||||||||||
| return "<empty>" | ||||||||||||||
| } | ||||||||||||||
| runes := []rune(s) | ||||||||||||||
| if len(runes) <= 6 { | ||||||||||||||
| return "***" | ||||||||||||||
| } | ||||||||||||||
| return string(runes[:3]) + "***" + string(runes[len(runes)-3:]) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // maskIdentityFile masks an identity file path for privacy. | ||||||||||||||
| // It masks the username in home directory paths (/home/user/ or C:\Users\user\) | ||||||||||||||
| // and masks the filename while preserving .pub suffix if present. | ||||||||||||||
| func maskIdentityFile(path string) string { | ||||||||||||||
| if path == "" { | ||||||||||||||
| return "<empty>" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Normalize path separators for consistent handling | ||||||||||||||
| normalizedPath := filepath.ToSlash(path) | ||||||||||||||
|
|
||||||||||||||
| // Extract directory and filename | ||||||||||||||
| dir := filepath.Dir(path) | ||||||||||||||
| filename := filepath.Base(path) | ||||||||||||||
|
|
||||||||||||||
| // Check for .pub suffix | ||||||||||||||
| hasPubSuffix := strings.HasSuffix(filename, ".pub") | ||||||||||||||
| if hasPubSuffix { | ||||||||||||||
| filename = strings.TrimSuffix(filename, ".pub") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Mask the filename | ||||||||||||||
| maskedFilename := MaskString(filename) | ||||||||||||||
| if hasPubSuffix { | ||||||||||||||
| maskedFilename += ".pub" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Mask username in home directory paths | ||||||||||||||
| // Unix: /home/username/... or /Users/username/... | ||||||||||||||
| // Windows: C:\Users\username\... (normalized to C:/Users/username/...) | ||||||||||||||
| maskedDir := dir | ||||||||||||||
| if strings.HasPrefix(normalizedPath, "/home/") { | ||||||||||||||
| parts := strings.SplitN(normalizedPath, "/", 4) // ["", "home", "username", "rest..."] | ||||||||||||||
| if len(parts) >= 3 { | ||||||||||||||
| maskedUsername := MaskString(parts[2]) | ||||||||||||||
| if len(parts) >= 4 { | ||||||||||||||
| subDir := filepath.Dir(parts[3]) | ||||||||||||||
| if subDir == "." { | ||||||||||||||
| maskedDir = "/home/" + maskedUsername | ||||||||||||||
| } else { | ||||||||||||||
| maskedDir = "/home/" + maskedUsername + "/" + subDir | ||||||||||||||
| } | ||||||||||||||
| } else { | ||||||||||||||
| maskedDir = "/home/" + maskedUsername | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } else if strings.Contains(normalizedPath, "/Users/") { | ||||||||||||||
| // Handle both Unix /Users/ and Windows C:/Users/ | ||||||||||||||
| idx := strings.Index(normalizedPath, "/Users/") | ||||||||||||||
| prefix := normalizedPath[:idx] | ||||||||||||||
| rest := normalizedPath[idx+7:] // Skip "/Users/" | ||||||||||||||
| parts := strings.SplitN(rest, "/", 2) | ||||||||||||||
| if len(parts) >= 1 { | ||||||||||||||
| maskedUsername := MaskString(parts[0]) | ||||||||||||||
| if len(parts) >= 2 { | ||||||||||||||
| subDir := filepath.Dir(parts[1]) | ||||||||||||||
| if subDir == "." { | ||||||||||||||
| maskedDir = prefix + "/Users/" + maskedUsername | ||||||||||||||
| } else { | ||||||||||||||
| maskedDir = prefix + "/Users/" + maskedUsername + "/" + subDir | ||||||||||||||
| } | ||||||||||||||
| } else { | ||||||||||||||
| maskedDir = prefix + "/Users/" + maskedUsername | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Convert back to native path separators | ||||||||||||||
| maskedDir = filepath.FromSlash(maskedDir) | ||||||||||||||
|
|
||||||||||||||
| return filepath.Join(maskedDir, maskedFilename) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // This exists to trick the ssh library into continuing to try | ||||||||||||||
| // different public keys even when the current key cannot be | ||||||||||||||
| // properly parsed | ||||||||||||||
|
|
@@ -138,6 +248,78 @@ func createPublicKeyCallback(connCtx context.Context, sshKeywords *wconfig.ConnK | |||||||||||||
| // require pointer to modify list in closure | ||||||||||||||
| identityFilesPtr := &identityFiles | ||||||||||||||
|
|
||||||||||||||
| // If IdentitiesOnly is set, filter agent signers to only include those that match an IdentityFile | ||||||||||||||
| // This matches OpenSSH behavior where the agent can still be used, but only for keys explicitly listed | ||||||||||||||
| if utilfn.SafeDeref(sshKeywords.SshIdentitiesOnly) && len(authSockSignersExt) > 0 { | ||||||||||||||
| var identityPubKeys []ssh.PublicKey | ||||||||||||||
| for _, identityFile := range identityFiles { | ||||||||||||||
| pubKeyData := existingKeys[identityFile] | ||||||||||||||
|
|
||||||||||||||
| // 1. Try reading as OpenSSH authorized_key format (e.g., "ssh-rsa AAAA... comment") | ||||||||||||||
| // This is the most common format for .pub files including 1Password | ||||||||||||||
| pubKey, _, _, _, err := ssh.ParseAuthorizedKey(pubKeyData) | ||||||||||||||
| if err == nil { | ||||||||||||||
| identityPubKeys = append(identityPubKeys, pubKey) | ||||||||||||||
| blocklogger.Debugf(connCtx, "[ssh-agent] loaded public key from %s (authorized_key format)\n", maskIdentityFile(identityFile)) | ||||||||||||||
| continue | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // 2. Try reading as raw public key (binary format) | ||||||||||||||
| pubKey, err = ssh.ParsePublicKey(pubKeyData) | ||||||||||||||
| if err == nil { | ||||||||||||||
| identityPubKeys = append(identityPubKeys, pubKey) | ||||||||||||||
| blocklogger.Debugf(connCtx, "[ssh-agent] loaded public key from %s (raw format)\n", maskIdentityFile(identityFile)) | ||||||||||||||
| continue | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // 3. Try reading as unencrypted private key (to get public key) | ||||||||||||||
| privKey, err := ssh.ParseRawPrivateKey(pubKeyData) | ||||||||||||||
| if err == nil { | ||||||||||||||
| signer, err := ssh.NewSignerFromKey(privKey) | ||||||||||||||
| if err == nil { | ||||||||||||||
| identityPubKeys = append(identityPubKeys, signer.PublicKey()) | ||||||||||||||
| blocklogger.Debugf(connCtx, "[ssh-agent] loaded public key from %s (private key)\n", maskIdentityFile(identityFile)) | ||||||||||||||
| continue | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // 4. Handle encrypted private keys by looking for a corresponding .pub file | ||||||||||||||
| pubKeyPath, _ := wavebase.ExpandHomeDir(identityFile + ".pub") | ||||||||||||||
| if pubKeyPath != "" { | ||||||||||||||
| pubKeyData, err = os.ReadFile(pubKeyPath) | ||||||||||||||
| if err == nil { | ||||||||||||||
| pubKey, _, _, _, err = ssh.ParseAuthorizedKey(pubKeyData) | ||||||||||||||
|
Comment on lines
+289
to
+291
|
||||||||||||||
| pubKeyData, err = os.ReadFile(pubKeyPath) | |
| if err == nil { | |
| pubKey, _, _, _, err = ssh.ParseAuthorizedKey(pubKeyData) | |
| pubFileData, err := os.ReadFile(pubKeyPath) | |
| if err == nil { | |
| pubKey, _, _, _, err = ssh.ParseAuthorizedKey(pubFileData) |
Uh oh!
There was an error while loading. Please reload this page.