fix(traffic): keep store directories out of host deletion

DeleteHostsExact built a directory path from every requested host. A name that sanitizes to _index, _blobs, _ca, or _delete_staging is the store itself, so the batch delete moved those directories out with the host trees. Index rows for the host are still removed.
This commit is contained in:
Coff0xc
2026-11-26 11:29:48 +00:00
parent 3cb06383fd
commit 27b4db5fbe
3 changed files with 94 additions and 2 deletions
+1
View File
@@ -14,6 +14,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Traffic host delete leaves the store directories in place**: a requested host whose sanitized name is `_index`, `_blobs`, `_ca`, `_delete_staging`, `.`, or `..` is not used as a directory. Those paths are the capture index, the blob store, the MITM CA, the delete staging area, or the traffic directory itself. Index rows for that host are still removed.
- **Missing Docker buildx is reported before the build**: without the buildx plugin, Compose only warns and falls back to the legacy builder, which fails on the Dockerfile's first `FROM --platform=$BUILDPLATFORM` with `failed to parse platform`. `install.sh` (all-Docker) and `update.sh` (Docker update) now check `docker buildx version` first and stop with an install hint, and the automatic Docker install on apt-based distros not covered by get.docker.com installs the buildx package as well. The README Docker section names buildx as a requirement.
- **A goal URL followed by punctuation is still seeded**: a goal such as `Assess http://juiceshop:3000.` no longer logs `could not parse a target host/IP` and skips the site. Trailing punctuation, including full-width `。` and a closing bracket that has no matching opener, is trimmed from the URL before it is parsed, so `http://[::1]` is left alone. The scheme the goal names is kept and a final period does not end up in the host.
- **Task timeout triggers fire for every task**: `on_task_timeout` used the task id as a watermark, but tasks time out in deadline order. Once a newer task timed out first, an older task that timed out later was never fired for. Fired timeouts are now kept in a persisted set, like met goals; on first start the set is seeded with the tasks already timed out, so nothing replays.
+34 -2
View File
@@ -1071,6 +1071,34 @@ func sanitize(s string) string {
return out
}
// storeDir reports whether name is one of the traffic store's own directories.
// Host trees never use these names. DeleteAll skips every underscore-prefixed
// entry for the same reason.
func storeDir(name string) bool {
switch name {
case "_index", "_blobs", "_ca", "_delete_staging":
return true
default:
return false
}
}
// hostDir is the legacy on-disk tree for one recorded host. A sanitized name
// that is the store itself, one of its own directories, or a path outside the
// traffic root is not a host tree. Callers still delete the host's index rows.
func hostDir(root, host string) (string, bool) {
name := sanitize(host)
if name == "." || name == ".." || storeDir(name) {
return "", false
}
dir := filepath.Join(root, name)
rel, err := filepath.Rel(root, dir)
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", false
}
return dir, true
}
var blobHashRe = regexp.MustCompile(`^[0-9a-f]{64}$`)
// blobPath locates a stored blob. Current writes use one bucket level; blobs
@@ -1719,7 +1747,9 @@ func (t *Traffic) hostTrees(where string, args ...any) ([]string, error) {
if err := rows.Scan(&h); err != nil {
return nil, err
}
dirs = append(dirs, filepath.Join(t.dir, sanitize(h)))
if dir, ok := hostDir(t.dir, h); ok {
dirs = append(dirs, dir)
}
}
return dirs, rows.Err()
}
@@ -1952,7 +1982,9 @@ func (t *Traffic) stageDeleteHostsExact(hosts []string, archiveID, taskID int64)
// own an orphaned directory that this deletion should take with it.
legacy := make([]string, 0, len(unique))
for _, h := range unique {
legacy = append(legacy, filepath.Join(t.dir, sanitize(h)))
if dir, ok := hostDir(t.dir, h); ok {
legacy = append(legacy, dir)
}
}
tx, err := t.db.Begin()
+59
View File
@@ -180,6 +180,65 @@ VALUES(?,?,?,?,?,?,?,?,?,?,?)`,
}
}
func TestHostDirSkipsStorePaths(t *testing.T) {
root := t.TempDir()
for _, name := range []string{"_index", "_blobs", "_ca", "_delete_staging", ".", ".."} {
if _, ok := hostDir(root, name); ok {
t.Fatalf("hostDir(%q) was accepted", name)
}
}
dir, ok := hostDir(root, "api.example.com")
if !ok || dir != filepath.Join(root, "api.example.com") {
t.Fatalf("hostDir(api.example.com)=%q ok=%v", dir, ok)
}
}
func TestDeleteHostsExactSkipsStoreDirs(t *testing.T) {
dir := t.TempDir()
tr, err := Open(dir, "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer tr.Close()
host := "api.example.com"
exDir := filepath.Join(dir, host, "GET", "1")
if err := os.MkdirAll(exDir, 0o755); err != nil {
t.Fatal(err)
}
if _, err := tr.DB().Exec(`INSERT INTO exchanges(id,ts,host,method,url_template,url,status,content_type,req_len,resp_len,path)
VALUES(?,?,?,?,?,?,?,?,?,?,?)`,
"1", 1, host, "GET", "/", "http://"+host+"/", 200, "text/html", 0, 0, host+"/GET/1"); err != nil {
t.Fatal(err)
}
keep := filepath.Join(dir, "_delete_staging", "keep.txt")
if err := os.MkdirAll(filepath.Dir(keep), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(keep, []byte("keep"), 0o600); err != nil {
t.Fatal(err)
}
n, err := tr.DeleteHostsExact([]string{host, "_index", "_blobs", "_ca", "_delete_staging", ".", ".."})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("deleted=%d, want 1", n)
}
for _, path := range []string{
filepath.Join(dir, "_index", "index.sqlite"),
filepath.Join(dir, "_blobs"),
filepath.Join(dir, "_ca"),
keep,
} {
if _, err := os.Stat(path); err != nil {
t.Fatalf("store path %s gone: %v", path, err)
}
}
if _, err := os.Stat(exDir); !os.IsNotExist(err) {
t.Fatalf("host tree still exists: %v", err)
}
}
func TestDeleteHostsExactReportsTreeRemovalFailure(t *testing.T) {
dir := t.TempDir()
tr, err := Open(dir, "127.0.0.1:0")