Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

fix(resources): pre-flight blob stats so a ZIP download can't truncate under 200

Sergey Kozyrenkoe1b7d8e5d973
diff --git a/backend/pkg/resources/resources.go b/backend/pkg/resources/resources.go
index 80dd545e..cfdced00 100644
--- a/backend/pkg/resources/resources.go
+++ b/backend/pkg/resources/resources.go
@@ -323,6 +323,15 @@ func CommitBlob(dataDir, hash, tmpPath string) error {
// ZipResources writes a ZIP archive to w containing all entries in files.
// Each ZipEntry maps a .blob file on disk to a path inside the archive.
func ZipResources(w io.Writer, entries []ZipEntry) (err error) {
+ // The streaming HTTP caller commits its 200 status on the first byte written,
+ // so a missing blob must be caught before then, or the client gets a truncated
+ // archive under 200. Stat all blobs up front; don't fold into the write loop.
+ for _, e := range entries {
+ if _, statErr := os.Lstat(e.BlobPath); statErr != nil {
+ return fmt.Errorf("failed to stat blob %s: %w", e.BlobPath, statErr)
+ }
+ }
+
zw := zip.NewWriter(w)
defer func() {
if cerr := zw.Close(); err == nil {
diff --git a/backend/pkg/resources/resources_test.go b/backend/pkg/resources/resources_test.go
index 328c7fe9..ad829b8f 100644
--- a/backend/pkg/resources/resources_test.go
+++ b/backend/pkg/resources/resources_test.go
@@ -251,6 +251,25 @@ func TestZipResourcesSkipsNonRegularBlobs(t *testing.T) {
assert.Empty(t, zr.File)
}
+// The valid blob must come first: with the missing one first, even a per-entry
+// check errors before writing anything, so the empty-buffer assertion below would
+// pass regardless. Valid-first forces output unless the missing blob is caught up front.
+func TestZipResourcesFailsCleanlyOnMissingBlob(t *testing.T) {
+ dataDir := t.TempDir()
+ present := BlobPath(dataDir, md5Hex("alpha"))
+ require.NoError(t, os.MkdirAll(filepath.Dir(present), 0755))
+ require.NoError(t, os.WriteFile(present, []byte("alpha"), 0644))
+ missing := BlobPath(dataDir, md5Hex("ghost"))
+
+ var buf bytes.Buffer
+ err := ZipResources(&buf, []ZipEntry{
+ {BlobPath: present, ZipPath: "a.txt"},
+ {BlobPath: missing, ZipPath: "b.txt"},
+ })
+ require.Error(t, err)
+ assert.Empty(t, buf.Bytes(), "no bytes must be written when a blob is missing")
+}
+
func TestZipDirectory(t *testing.T) {
src := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(src, "sub"), 0755))
diff --git a/backend/pkg/server/services/resources_test.go b/backend/pkg/server/services/resources_test.go
index a2cc52eb..4030e6a2 100644
--- a/backend/pkg/server/services/resources_test.go
+++ b/backend/pkg/server/services/resources_test.go
@@ -1288,6 +1288,19 @@ func TestResourceService_DownloadResourceScenarios(t *testing.T) {
privs: []string{"resources.download"},
wantStatus: http.StatusNotFound,
},
+ {
+ // DB row present but the blob file is gone, inside a multi-file ZIP: must
+ // fail with an error, not stream a truncated archive under 200. Present
+ // blob first so the pre-flight, not write order, is what prevents output.
+ name: "blob missing on disk in a zip batch fails cleanly, no truncated 200",
+ seeds: []seed{
+ {path: "a.txt", content: "alpha"},
+ {path: "b.txt", content: "beta", skipBlobWrite: true},
+ },
+ paths: []string{"a.txt", "b.txt"},
+ privs: []string{"resources.download"},
+ wantStatus: http.StatusInternalServerError,
+ },
}
for _, tt := range tests {