Skip to content

Commit

Permalink
fix: Don't modify ciphertext in edit command if plaintext did not change
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 5, 2024
1 parent c0a8059 commit 73893c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/cmd/editcmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"log/slog"
"os"
"runtime"
Expand Down Expand Up @@ -87,6 +88,7 @@ func (c *Config) runEditCmd(cmd *cobra.Command, args []string) error {
type transparentlyDecryptedFile struct {
sourceAbsPath chezmoi.AbsPath
decryptedAbsPath chezmoi.AbsPath
preEditPlaintext []byte
}
var transparentlyDecryptedFiles []transparentlyDecryptedFile
TARGET_REL_PATH:
Expand Down Expand Up @@ -119,6 +121,7 @@ TARGET_REL_PATH:
transparentlyDecryptedFile := transparentlyDecryptedFile{
sourceAbsPath: c.SourceDirAbsPath.Join(sourceRelPath.RelPath()),
decryptedAbsPath: decryptedAbsPath,
preEditPlaintext: contents,
}
transparentlyDecryptedFiles = append(transparentlyDecryptedFiles, transparentlyDecryptedFile)
editorArgs = append(editorArgs, decryptedAbsPath.String())
Expand Down Expand Up @@ -165,6 +168,13 @@ TARGET_REL_PATH:

postEditFunc := func() error {
for _, transparentlyDecryptedFile := range transparentlyDecryptedFiles {
postEditPlaintext, err := c.baseSystem.ReadFile(transparentlyDecryptedFile.decryptedAbsPath)
if err != nil {
return err
}
if bytes.Equal(postEditPlaintext, transparentlyDecryptedFile.preEditPlaintext) {
return nil
}
contents, err := c.encryption.EncryptFile(transparentlyDecryptedFile.decryptedAbsPath)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/testdata/scripts/issue3887.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[!exec:age] skip 'age not found in path'

mkageconfig
mkgitconfig

# add an initial encrypted file
exec chezmoi init
exec chezmoi add --encrypt ${HOME}${/}.encrypted
exec chezmoi git add .
exec chezmoi git commit -- -m 'initial commit' .

# test that chezmoi edit on an encrypted file with no changes does not change the ciphertext
prependline ${CHEZMOICONFIGDIR}/chezmoi.toml 'edit.command = "true"'
exec chezmoi edit ${HOME}${/}.encrypted
exec chezmoi diff
! stdout .
exec chezmoi git diff
! stdout .

-- home/user/.encrypted --
plaintext

0 comments on commit 73893c1

Please sign in to comment.