mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-18 16:17:03 +00:00
Other: Consider vault corrupt if it cannot be unmarshaled
This commit is contained in:
@ -223,9 +223,13 @@ func newVault(path, gluonDir string, gcm cipher.AEAD) (*Vault, bool, error) {
|
|||||||
|
|
||||||
var corrupt bool
|
var corrupt bool
|
||||||
|
|
||||||
if _, err := decrypt(gcm, enc); err != nil {
|
if dec, err := decrypt(gcm, enc); err != nil {
|
||||||
corrupt = true
|
corrupt = true
|
||||||
|
} else if err := msgpack.Unmarshal(dec, new(Data)); err != nil {
|
||||||
|
corrupt = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if corrupt {
|
||||||
newEnc, err := initVault(path, gluonDir, gcm)
|
newEnc, err := initVault(path, gluonDir, gcm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
|||||||
@ -18,13 +18,15 @@
|
|||||||
package vault_test
|
package vault_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
|
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestVaultCorrupt(t *testing.T) {
|
func TestVault_Corrupt(t *testing.T) {
|
||||||
vaultDir, gluonDir := t.TempDir(), t.TempDir()
|
vaultDir, gluonDir := t.TempDir(), t.TempDir()
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -46,6 +48,35 @@ func TestVaultCorrupt(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVault_Corrupt_JunkData(t *testing.T) {
|
||||||
|
vaultDir, gluonDir := t.TempDir(), t.TempDir()
|
||||||
|
|
||||||
|
{
|
||||||
|
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, corrupt)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, corrupt)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
f, err := os.OpenFile(filepath.Join(vaultDir, "vault.enc"), os.O_WRONLY, 0o600)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer f.Close() //nolint:errcheck
|
||||||
|
|
||||||
|
_, err = f.Write([]byte("junk data"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, corrupt, err := vault.New(vaultDir, gluonDir, []byte("my secret key"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, corrupt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newVault(t *testing.T) *vault.Vault {
|
func newVault(t *testing.T) *vault.Vault {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user