Stop vault creation when a vault's metadata cannot be read (closes #140)
check / check (push) Waiting to run

GetNextDerivationIndex passed over a vault whose vault-metadata.json could
not be read or parsed, so the next vault made from the same mnemonic could
get that vault's derivation index and so the same long-term key. It now
returns an error naming the file, and vault create, vault import and init
stop before writing anything. A vault directory with no metadata file is
still passed over.

The regression test corrupts one vault's metadata and requires CreateVault
with the same mnemonic to fail, naming the file, and leave no new vault.

Unverified by test: an unreadable metadata file; checked by hand only.

Model: opus-5-5
This commit is contained in:
2026-10-14 20:57:24 +00:00
parent fe94e99801
commit 75599f1b9d
4 changed files with 53 additions and 7 deletions
+30
View File
@@ -6,6 +6,9 @@ import (
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sneak.berlin/go/secret/internal/secret"
"sneak.berlin/go/secret/internal/vault"
"sneak.berlin/go/secret/pkg/agehd"
)
@@ -163,6 +166,33 @@ func testGetNextDerivationIndex(t *testing.T, fs afero.Fs) {
}
}
// TestCreateVaultWithCorruptMetadataFails checks that creating a vault from
// a mnemonic fails, naming the file and writing nothing, when another
// vault's metadata cannot be parsed. That vault's derivation index is then
// unknown, so the new vault could get the same one and the same key.
func TestCreateVaultWithCorruptMetadataFails(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
_, err := vault.CreateVault(fs, testStateDir, "work", testMnemonicBuffer(t), nil)
require.NoError(t, err)
vaultsDir := filepath.Join(testStateDir, "vaults.d")
metadataPath := filepath.Join(vaultsDir, "work", "vault-metadata.json")
err = afero.WriteFile(fs, metadataPath, []byte("{"), secret.FilePerms)
require.NoError(t, err)
_, err = vault.CreateVault(fs, testStateDir, "other", testMnemonicBuffer(t), nil)
require.ErrorContains(t, err, metadataPath)
entries, err := afero.ReadDir(fs, vaultsDir)
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "work", entries[0].Name())
}
func testMetadataPersistence(t *testing.T, fs afero.Fs) {
t.Helper()