mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
applyMoves and rollbackFiles validated the staging journal and then ran os.MkdirAll/os.Rename on host paths. Both the journal Source and the payload Relative resolve under the data directory, so a component swapped for a symlink after validation could move data into or out of the data directory through the staging rename. Open one os.OpenRoot(dataDir) per call and run root.Lstat, root.MkdirAll and root.Rename with names relative to the data directory, rejecting any name that is not strictly beneath it. validateArchiveStageJournal stays unchanged. The restore path is intentionally untouched: its sources live under the per-archive extracted root, which a single Root anchored at dataDir cannot span. Tests make dataDir/tasks, and separately the payload files directory, symlinks that escape the data dir; both applyMoves directions and the rollback escape are refused after the change, while a normal stage and rollback still apply.
127 lines
5 KiB
Go
127 lines
5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func newRootStageFixture(t *testing.T, dataDir string, archiveID int64, taskID string) *taskArchiveFileStage {
|
|
t.Helper()
|
|
root := filepath.Join(taskArchiveRoot(dataDir), ".staging", fmt.Sprintf("%d", archiveID))
|
|
payload := filepath.Join(root, "payload")
|
|
if err := os.MkdirAll(filepath.Join(payload, "files"), archiveDirMode); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &taskArchiveFileStage{
|
|
dataDir: dataDir,
|
|
root: root,
|
|
payload: payload,
|
|
journal: archiveStageJournal{
|
|
ArchiveID: archiveID,
|
|
TaskID: taskID,
|
|
Moves: []archiveFileMove{
|
|
{Source: filepath.Join(dataDir, "tasks", taskID), Relative: filepath.Join("files", "tasks", taskID)},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// TestTaskArchiveStageMovesRootConfinement verifies the staging renames are
|
|
// anchored to os.OpenRoot(dataDir): a journal Source or Destination that leaves
|
|
// the data dir through a symlinked component is refused before any rename, and
|
|
// a normal journal still stages and rolls back.
|
|
func TestTaskArchiveStageMovesRootConfinement(t *testing.T) {
|
|
t.Run("source_parent_symlink_escape_refused", func(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
outsideDir := t.TempDir()
|
|
victimDir := filepath.Join(outsideDir, "tasks", "7")
|
|
mustWriteFile(t, filepath.Join(victimDir, "victim.txt"), "KEEP_OUTSIDE")
|
|
// dataDir/tasks is a symlink to outside/tasks, so the journal's
|
|
// server-derived Source dataDir/tasks/7 resolves outside dataDir.
|
|
mustSymlink(t, filepath.Join(outsideDir, "tasks"), filepath.Join(dataDir, "tasks"))
|
|
|
|
stage := newRootStageFixture(t, dataDir, 51, "7")
|
|
if err := stage.applyMoves(); err == nil {
|
|
t.Fatal("applyMoves moved a source through a symlink escaping the data dir")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(victimDir, "victim.txt")); err != nil {
|
|
t.Fatalf("applyMoves moved data outside the data dir through a symlink: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("destination_parent_symlink_escape_refused", func(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
outsideDir := t.TempDir()
|
|
taskDir := filepath.Join(dataDir, "tasks", "7")
|
|
mustWriteFile(t, filepath.Join(taskDir, "ok.txt"), "VALID")
|
|
stage := newRootStageFixture(t, dataDir, 52, "7")
|
|
// Replace the payload files directory with a symlink to outside, so the
|
|
// journal Relative resolves the rename destination outside dataDir.
|
|
if err := os.RemoveAll(filepath.Join(stage.payload, "files")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mustSymlink(t, outsideDir, filepath.Join(stage.payload, "files"))
|
|
|
|
if err := stage.applyMoves(); err == nil {
|
|
t.Fatal("applyMoves moved a source to a destination through a symlink escaping the data dir")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outsideDir, "tasks", "7")); !os.IsNotExist(err) {
|
|
t.Fatalf("applyMoves staged files outside the data dir through a symlink: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(taskDir, "ok.txt")); err != nil {
|
|
t.Fatalf("applyMoves moved the source despite the escaping destination: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("rollback_parent_symlink_escape_refused", func(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
outsideDir := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(outsideDir, "tasks"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stage := newRootStageFixture(t, dataDir, 53, "7")
|
|
stagedFile := filepath.Join(stage.payload, "files", "tasks", "7", "ok.txt")
|
|
mustWriteFile(t, stagedFile, "VALID")
|
|
mustSymlink(t, filepath.Join(outsideDir, "tasks"), filepath.Join(dataDir, "tasks"))
|
|
|
|
if err := stage.rollbackFiles(); err == nil {
|
|
t.Fatal("rollbackFiles restored a file through a symlink escaping the data dir")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outsideDir, "tasks", "7")); !os.IsNotExist(err) {
|
|
t.Fatalf("rollbackFiles wrote outside the data dir through a symlink: %v", err)
|
|
}
|
|
if _, err := os.Stat(stagedFile); err != nil {
|
|
t.Fatalf("rollbackFiles removed the staged file despite the refusal: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("normal_stage_and_rollback_apply", func(t *testing.T) {
|
|
dataDir := t.TempDir()
|
|
taskDir := filepath.Join(dataDir, "tasks", "7")
|
|
mustWriteFile(t, filepath.Join(taskDir, "ok.txt"), "VALID")
|
|
stage := newRootStageFixture(t, dataDir, 54, "7")
|
|
|
|
if err := stage.applyMoves(); err != nil {
|
|
t.Fatalf("applyMoves rejected a normal journal: %v", err)
|
|
}
|
|
stagedFile := filepath.Join(stage.payload, "files", "tasks", "7", "ok.txt")
|
|
if got, err := os.ReadFile(stagedFile); err != nil || string(got) != "VALID" {
|
|
t.Fatalf("staged file mismatch: got=%q err=%v", got, err)
|
|
}
|
|
if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
|
|
t.Fatalf("source directory still present after staging: %v", err)
|
|
}
|
|
|
|
if err := stage.rollbackFiles(); err != nil {
|
|
t.Fatalf("rollbackFiles rejected a normal journal: %v", err)
|
|
}
|
|
if got, err := os.ReadFile(filepath.Join(taskDir, "ok.txt")); err != nil || string(got) != "VALID" {
|
|
t.Fatalf("restored file mismatch: got=%q err=%v", got, err)
|
|
}
|
|
if _, err := os.Stat(stagedFile); !os.IsNotExist(err) {
|
|
t.Fatalf("staged file still present after rollback: %v", err)
|
|
}
|
|
})
|
|
}
|