From 71cf1f39e89b76872ee86b94599b1173df9e3516 Mon Sep 17 00:00:00 2001 From: Leo Hike Date: Thu, 29 Oct 2026 22:15:12 +0000 Subject: [PATCH] ssh-tpm-keygen: respect "n" at the overwrite prompt Since e4a46e2 moved the check into checkFile, declining returned nil and keygen silently replaced the existing key. Return an error so it exits before writing. Extend keygen.txt to cover both declining and accepting the overwrite. Fixes: https://github.com/Foxboron/ssh-tpm-agent/issues/127 --- cmd/ssh-tpm-keygen/main.go | 2 +- cmd/ssh-tpm-keygen/testdata/script/keygen.txt | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/ssh-tpm-keygen/main.go b/cmd/ssh-tpm-keygen/main.go index 4002026..acf8c25 100644 --- a/cmd/ssh-tpm-keygen/main.go +++ b/cmd/ssh-tpm-keygen/main.go @@ -363,7 +363,7 @@ func checkFile(f string) error { return err } if !bytes.Equal(s, []byte("y")) { - return nil + return fmt.Errorf("not overwriting %s", f) } } return nil diff --git a/cmd/ssh-tpm-keygen/testdata/script/keygen.txt b/cmd/ssh-tpm-keygen/testdata/script/keygen.txt index 4eaa935..ee1cd74 100644 --- a/cmd/ssh-tpm-keygen/testdata/script/keygen.txt +++ b/cmd/ssh-tpm-keygen/testdata/script/keygen.txt @@ -2,7 +2,20 @@ exec ssh-tpm-keygen exists .ssh/id_ecdsa.pub exists .ssh/id_ecdsa.tpm -rm .ssh + +# Declining to overwrite keeps the existing key, "y" replaces it +cp .ssh/id_ecdsa.tpm orig.tpm +stdin no.txt +! exec ssh-tpm-keygen -f .ssh/id_ecdsa +stderr 'not overwriting .ssh/id_ecdsa.tpm' +cmp .ssh/id_ecdsa.tpm orig.tpm +# keygen asks once per existing file (a minor UX quirk: one key, two +# confirmations), and piped stdin only reaches the first prompt, so +# answer "y" via askpass, which runs for every prompt +chmod 755 askpass-yes +exec env SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=./askpass-yes ssh-tpm-keygen -f .ssh/id_ecdsa +! cmp .ssh/id_ecdsa.tpm orig.tpm +rm .ssh orig.tpm # Check that we can create RSA keys exec ssh-tpm-keygen -t rsa @@ -54,3 +67,8 @@ rm test -- save_name.txt -- .ssh/new_name +-- no.txt -- +n +-- askpass-yes -- +#!/bin/sh +echo y