fix: verify NKey file has secure permissions before reading
Reject NKey files that are readable by group or others (permissions more permissive than 0600). This prevents accidental exposure of private keys through overly permissive file permissions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,15 @@ type Client struct {
|
||||
|
||||
// Connect establishes a connection to NATS using NKey authentication.
|
||||
func Connect(cfg Config) (*Client, error) {
|
||||
// Verify NKey file has secure permissions (no group/other access)
|
||||
info, err := os.Stat(cfg.NKeyFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to stat nkey file: %w", err)
|
||||
}
|
||||
if perm := info.Mode().Perm(); perm&0o077 != 0 {
|
||||
return nil, fmt.Errorf("nkey file has insecure permissions %04o: must not be accessible by group or others", perm)
|
||||
}
|
||||
|
||||
seed, err := os.ReadFile(cfg.NKeyFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read nkey file: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user