Secure Sudo Password Handling in SSH Adapter
Overviewβ
The SSH adapter supports multiple methods for providing sudo passwords, with varying levels of security. This document describes best practices for secure sudo password handling.
Password Methodsβ
1. secure-askpass
(Recommended)β
The most secure method that creates a temporary askpass script on the remote machine:
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'secure-askpass'
}
});
Advantages:
- Password is never visible in process listings
- Temporary script is automatically cleaned up
- Works across all platforms
How it works:
- Creates a temporary shell script on the remote machine
- Sets appropriate permissions (700)
- Uses
SUDO_ASKPASS
environment variable - Automatically cleans up after execution
2. secure
(Local Secure Handler)β
Uses a local secure password handler with temporary files:
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'secure'
}
});
Advantages:
- Password stored in memory-only temporary file
- Automatic cleanup
- No command-line exposure
3. askpass
(Standard Askpass)β
Uses the system's askpass mechanism:
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'askpass'
}
});
4. stdin
(Default - Less Secure)β
Pipes password via stdin:
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'stdin' // or omit for default
}
});
Warnings:
- Password may be visible in process listings
- Uses
printf
instead ofecho
for better compatibility - Should only be used when secure methods are not available
5. echo
(Deprecated - Least Secure)β
β οΈ NOT RECOMMENDED - Only for backward compatibility:
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'echo'
}
});
Security Considerationsβ
- Process Listing Exposure: Methods like
stdin
andecho
can expose passwords in process listings (ps aux
) - Shell History: Passwords may be logged in shell history files
- Memory Safety: Secure methods minimize time passwords spend in memory
- Cleanup: Always ensure temporary files/scripts are cleaned up
Best Practicesβ
- Always use
secure-askpass
when sudo passwords are required - Store passwords securely - Use environment variables or secure credential stores
- Rotate passwords regularly
- Use SSH keys instead of passwords when possible
- Enable
NOPASSWD
in sudoers for automation scenarios when appropriate
Example: Secure Sudo with Environment Variablesβ
// Store password in environment variable (still visible to process)
process.env.SUDO_PASSWORD = 'mypassword';
// Use secure-askpass method
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: process.env.SUDO_PASSWORD,
method: 'secure-askpass'
}
});
// Clean up
delete process.env.SUDO_PASSWORD;
Example: Custom Secure Handlerβ
import { SecurePasswordHandler } from '@xec-sh/core';
const secureHandler = new SecurePasswordHandler();
try {
await $.ssh('sudo apt update', {
host: 'server.example.com',
username: 'user',
sudo: {
password: 'mypassword',
method: 'secure',
secureHandler
}
});
} finally {
await secureHandler.cleanup();
}
Migration Guideβ
If you're currently using insecure methods, migrate to secure methods:
// Old (insecure)
sudo: {
password: 'mypassword'
// defaults to stdin
}
// New (secure)
sudo: {
password: 'mypassword',
method: 'secure-askpass'
}
Troubleshootingβ
- "sudo: no askpass program specified": Ensure
SUDO_ASKPASS
is supported on the target system - Permission denied on askpass script: Check that
/tmp
is executable and writable - Cleanup failures: Temporary scripts are cleaned up automatically, but you can manually check
/tmp/askpass-*.sh
Future Improvementsβ
- Support for credential helpers (like git-credential)
- Integration with system keychains
- Support for MFA/2FA sudo authentication
- Encrypted password transmission options
See Alsoβ
- SSH Authentication - SSH authentication methods
- SSH Setup - Basic SSH configuration
- SSH Tunneling - Port forwarding and tunnels
- Security Best Practices - General security guidance