FiloSottile / age

A simple, modern and secure encryption tool (and Go library) with small explicit keys, no config options, and UNIX-style composability.
https://age-encryption.org
BSD 3-Clause "New" or "Revised" License
16.9k stars 495 forks source link

Encrypted file is destroyed when decrypting with same input and output filename #150

Closed penkovski closed 3 years ago

penkovski commented 3 years ago

Environment

What were you trying to do

Decrypt a file with passphrase but mistakenly enter the output filename to be the same as the encrypted filename. See below:

age -d test.txt.age > test.txt.age

What happened

age prints an error, but the encrypted file is destroyed (has size of 0 bytes).

~/agetest » age -d test.txt.age > test.txt.age                                                                            penkovski@mbpro-2
Error: failed to read header: parsing age header: failed to read intro: EOF
[ Did age not do what you expected? Could an error be more useful? Tell us: https://filippo.io/age/report ]
--------------------------------------------------------------------------------------------------------------------------------------------
~/agetest » ls -la                                                                                                        
total 0
drwxr-xr-x   3 penkovski  staff    96 Oct 13 18:53 .
drwxr-xr-x+ 94 penkovski  staff  3008 Oct 13 18:54 ..
-rw-r--r--   1 penkovski  staff     0 Oct 13 18:53 test.txt.age

I would suggest in case the user has accidentally made such a mistake (i.e. autocomplete) to have the option to try again with different output name. Currently, the encrypted file is destroyed after the first attempt.

wgslr commented 3 years ago

The > output redirection is the responsibility of the shell (e.g. bash) and not age itself. I don't think it's possible for age to detect what is the file after > and prevent such user mistakes as in your example.

penkovski commented 3 years ago

Agree. I just saw that there's an option to specify output name, so if it's used in that way the file won't be destroyed.

age -d -o test.txt.age test.txt.age

Thank you for answering, I'm closing it.