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

Dont wipe vault on decryption errors #486

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions internal/app/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func WithVault(locations *locations.Locations, keychains *keychain.List, panicHa
// Create the encVault.
encVault, insecure, corrupt, err := newVault(locations, keychains, panicHandler)
if err != nil {
return fmt.Errorf("could not create vault: %w", err)
return fmt.Errorf("could not load/create vault: %w", err)
}

logrus.WithFields(logrus.Fields{
Expand Down Expand Up @@ -87,7 +87,7 @@ func newVault(locations *locations.Locations, keychains *keychain.List, panicHan

vault, corrupt, err := vault.New(vaultDir, gluonCacheDir, vaultKey, panicHandler)
if err != nil {
return nil, false, corrupt, fmt.Errorf("could not create vault: %w", err)
return nil, false, corrupt, err
}

return vault, insecure, corrupt, nil
Expand Down
4 changes: 4 additions & 0 deletions internal/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ func newVault(path, gluonDir string, gcm cipher.AEAD) (*Vault, error, error) {
var corrupt error

if err := unmarshalFile(gcm, enc, new(Data)); err != nil {
if errors.Is(err, ErrDecryptFailed) {
return nil, nil, err
}

corrupt = err
}

Expand Down
14 changes: 10 additions & 4 deletions internal/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,50 @@ import (
"github.com/stretchr/testify/require"
)

func TestVault_Corrupt(t *testing.T) {
func TestVault_DecryptFailed(t *testing.T) {
vaultDir, gluonDir := t.TempDir(), t.TempDir()

{
// Create
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"), async.NoopPanicHandler{})
require.NoError(t, err)
require.NoError(t, corrupt)
}

{
// Load
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"), async.NoopPanicHandler{})
require.NoError(t, err)
require.NoError(t, corrupt)
}

{
// Load with bad key
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("bad key"), async.NoopPanicHandler{})
require.NoError(t, err)
require.ErrorIs(t, corrupt, vault.ErrDecryptFailed)
require.ErrorIs(t, err, vault.ErrDecryptFailed)
require.NoError(t, corrupt)
}
}

func TestVault_Corrupt_JunkData(t *testing.T) {
func TestVault_Corrupt(t *testing.T) {
vaultDir, gluonDir := t.TempDir(), t.TempDir()

{
// Create
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"), async.NoopPanicHandler{})
require.NoError(t, err)
require.NoError(t, corrupt)
}

{
// Load
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"), async.NoopPanicHandler{})
require.NoError(t, err)
require.NoError(t, corrupt)
}

{
// Load with junk data
f, err := os.OpenFile(filepath.Join(vaultDir, "vault.enc"), os.O_WRONLY, 0o600)
require.NoError(t, err)
defer f.Close() //nolint:errcheck
Expand Down