Open the downloaded snapshot database read-only, on a private temp dir (closes #162)
check / check (pull_request) Successful in 1m21s
check / check (push) Successful in 3m4s

Restore and deep verify used to open the decrypted snapshot database read-write through the local-index constructor, which applied migrations against whatever the file carried, and left the decrypted file in the shared temp directory. A forged file could redefine what restore queries return, and an interrupted open left decrypted metadata on disk.

Add database.OpenReadOnly: opens the file read-only (mode=ro) with query_only and trusted_schema=OFF, never applies schema files, and refuses a file whose schema carries a trigger, view or virtual table or lacks an expected table. Restore and deep verify now both use it, each inside its own private (0700) temp directory removed on every return path. pickNextDownload returns (FileID, bool) so a genuine nil-UUID file is not mistaken for "nothing left".

Model: opus-4-8
This commit was merged in pull request #186.
This commit is contained in:
2026-09-22 12:45:53 +02:00
parent b4654f8e52
commit a6434de57f
7 changed files with 541 additions and 62 deletions
+88
View File
@@ -0,0 +1,88 @@
package vaultik //nolint:testpackage // inspects unexported restore plan internals
import (
"context"
"math"
"testing"
"github.com/stretchr/testify/require"
"sneak.berlin/go/vaultik/internal/database"
"sneak.berlin/go/vaultik/internal/types"
)
// TestPickNextDownloadReturnsNilUUIDFile proves a genuine pending file
// carrying the nil UUID is picked for download rather than mistaken for
// "nothing left" — the bug that could abandon every remaining file.
func TestPickNextDownloadReturnsNilUUIDFile(t *testing.T) {
t.Parallel()
var nilID types.FileID // zero value is the nil UUID
plan := &restorePlan{
fileBlobs: map[types.FileID]map[string]struct{}{
nilID: {"blobhash": {}},
},
}
id, ok := plan.pickNextDownload()
require.True(t, ok,
"pickNextDownload treated a pending nil-UUID file as nothing to do")
require.True(t, id.IsZero(), "expected the nil-UUID file to be picked")
}
// TestPickNextDownloadEmptyPlan confirms the second return value is false
// only when no file needs a download.
func TestPickNextDownloadEmptyPlan(t *testing.T) {
t.Parallel()
plan := &restorePlan{
fileBlobs: map[types.FileID]map[string]struct{}{},
}
_, ok := plan.pickNextDownload()
require.False(t, ok, "pickNextDownload reported work on an empty plan")
}
// TestRunRestoreLoopFailsOnAbandonedFiles proves the loop returns an
// error rather than silent success when files remain pending after it
// can make no further progress.
func TestRunRestoreLoopFailsOnAbandonedFiles(t *testing.T) {
t.Parallel()
ctx := context.Background()
db, err := database.NewTestDB()
require.NoError(t, err)
t.Cleanup(func() { _ = db.Close() })
repos := database.NewRepositories(db)
cache, err := newBlobDiskCache(math.MaxInt64)
require.NoError(t, err)
t.Cleanup(func() { _ = cache.Close() })
v := &Vaultik{ctx: ctx}
session := &restoreSession{
v: v,
ctx: ctx,
repos: repos,
sweeper: newRestoreSweeper(ctx, repos, cache, 1),
result: &RestoreResult{},
}
// A file that is still pending but whose uncached-blob set is empty
// and which was never queued as ready: the loop can neither restore
// nor download it. This is the abandonment the guard must catch.
var stuck types.FileID
plan := &restorePlan{
fileBlobs: map[types.FileID]map[string]struct{}{stuck: {}},
blobFiles: map[string]map[types.FileID]struct{}{},
cached: map[string]struct{}{},
}
err = v.runRestoreLoop(session, plan, map[types.FileID]*database.File{}, 0)
require.ErrorIs(t, err, errRestoreIncomplete)
}