cmd/age: delay output creation until payload authentication

Reported by Joe Doyle of Trail of Bits.
This commit is contained in:
Filippo Valsorda
2026-08-29 19:30:10 +02:00
parent dbe5773864
commit 7a8f500567
2 changed files with 29 additions and 2 deletions
+5 -2
View File
@@ -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 {
+24
View File
@@ -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