StackExchange / blackbox

Safely store secrets in Git/Mercurial/Subversion
MIT License
6.69k stars 370 forks source link

featture request: blackbox_edit shouldn't re-encrypt a file if no changes were made #326

Open sudomain opened 3 years ago

sudomain commented 3 years ago

Sorry if this has veen discussed before, but I think if a file, testfile.gpg, is opened with blackbox_edit but no changes are made to the file, then the original testfile.gpg should be left alone rather than be overwritten by a newly encrypted version of the same file. Perhaps checking modification time or checksum the contents of a file when decrypting, then check it against the decrypted file when deciding to re-encrypt?

TomOnTime commented 3 years ago

I agree that it would be better if it didn't re-encrypt when the file hasn't changed.

Sadly gpg doesn't maintain timestamps in a way that makes this easy. When testfile.gpg is decrypted to testfile, the timestamp of testfile is when the decryption happened. Thus testfile is always "newer" than testfile.gpg, which is indistinguishable from when its been edited.

A way to make it work would be to take the hash of the file when decrypted, or decrypt to a temporary file to see if the file has changed right before deciding if it changed. However one would also have to take into account if files-admins.txt has changed.

Doing that in Bash seems difficult but the new Go version (which needs beta tests) should make that easier.

Tom

sudomain commented 3 years ago

However one would also have to take into account if files-admins.txt has changed.

Pardon my ignorance, but do you mean blackbox-admins.txt? I couldn't find info on "files-admins" during a GH search.'

Doing that in Bash seems difficult but the new Go version (which needs beta tests) should make that easier.

Having only superficially read the bash code, my naive approach would to take the decrypting functions such as https://github.com/StackExchange/blackbox/blob/a413affb566017d727233b045f27c9ee1b9640b7/bin/_blackbox_common.sh#L267 and https://github.com/StackExchange/blackbox/blob/a413affb566017d727233b045f27c9ee1b9640b7/bin/_blackbox_common.sh#L289

And change it to something like (some options omitted for clarity): filesig="$(gpg -d testfile.gpg | tee tempfile | sha1sum)"

This removes the --output option from gpg since tee will be writing the decrypted content. When deciding to re-encrypt or discard the file, we'll just check if the sha1sum matches $filesig

This approach ignores your comment on files-admin.txt (sorry) because I couldn't find info on it.

TomOnTime commented 3 years ago

Ah, yes, I did mean blackbox-admins.txt. If that file changes, then everything needs to be re-encrypted. Though, you could just depend on people using the blackbox_update_all_files

Sounds like you have some really smart ideas about how to fix this in Bash. If you'd like to give it a try, I'd love to get a PR!

sudomain commented 3 years ago

Does this project assume GNU coreutils is installed? (Needed for tee and sha1sum)

Any preference among md5sum, sha1sum, ... ?

TomOnTime commented 3 years ago

coreutils: Yes, assume that tee is installed.

hash algorithm: I don't have a preference as long as it is supported on macOS, Linux, FreeBSD and Windows.

Some background:

Let me know how I can help!