Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't modify ciphertext in edit command if plaintext did not change #3888

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading