diff --git a/cmd/age/age.go b/cmd/age/age.go index cdca460..71e5d9b 100644 --- a/cmd/age/age.go +++ b/cmd/age/age.go @@ -521,10 +521,13 @@ func decrypt(identities []age.Identity, in io.Reader, out io.Writer) { } else if err != nil { errorf("%v", err) } - out.Write(nil) // trigger the lazyOpener even if r is empty if _, err := io.Copy(out, r); err != nil { errorf("%v", err) } + // Trigger the lazyOpener even if r is empty, if Copy succeeded. + if _, err := out.Write(nil); err != nil { + errorf("%v", err) + } } var lazyScryptIdentity = &LazyScryptIdentity{passphrasePromptForDecryption} @@ -590,7 +593,7 @@ func (l *lazyOpener) Close() error { if l.f != nil { return l.f.Close() } - return nil + return l.err } func absPath(name string) string { diff --git a/cmd/age/testdata/output_file.txt b/cmd/age/testdata/output_file.txt index 5b16d65..6633c3c 100644 --- a/cmd/age/testdata/output_file.txt +++ b/cmd/age/testdata/output_file.txt @@ -25,6 +25,22 @@ age -d -i key.txt -o new empty.age ! stderr . cmp new empty +# Don't create or truncate the output if the payload fails authentication. +! age -o test.out -d -i key.txt corrupt.age +stderr 'failed to decrypt and authenticate payload chunk' +! exists test.out +cp input existing +! age -o existing -d -i key.txt corrupt.age +stderr 'failed to decrypt and authenticate payload chunk' +cmp existing input + +# Report output creation errors for an empty plaintext. +! age -o missingdir/test.out -d -i key.txt empty.age +stderr 'missingdir/test.out' +! exists missingdir/test.out +! age -o missingdir/test.out -d -i key.txt test.age +stderr 'missingdir/test.out' + # https://github.com/FiloSottile/age/issues/491 cp input inputcopy ! age -r age1xmwwc06ly3ee5rytxm9mflaz2u56jjj36s0mypdrwsvlul66mv4q47ryef -o inputcopy inputcopy @@ -68,6 +84,14 @@ wrong -- input -- age -- empty -- +-- corrupt.age -- +-----BEGIN AGE ENCRYPTED FILE----- +YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBDMXYwVmsyU3dyc1NPdUF1 +S1ZKRXhVMXFKWDdyd2ZHajZyUkRDRHBad20wCk93WjZOS0tOZjBNSW5Ka2ErUFRz +K0gzU1pzWEdqS3EvZWlmbUE1ZE95V1EKLS0tIEZBTW81V3RhWk1xVVhoMnA3UDRl +YSs2QW5zeHp6ZFk4M1dSc0pmeld6VWcK2XikoRl4QMDKbFQRZXWCl/A6Z5nvCK3a +RaZyUlKtXBnSTaVCMKRb +-----END AGE ENCRYPTED FILE----- -- key.txt -- # created: 2021-02-02T13:09:43+01:00 # public key: age1xmwwc06ly3ee5rytxm9mflaz2u56jjj36s0mypdrwsvlul66mv4q47ryef