fix(server): count only the task's assets in the task report

GET /api/report renders one task's report, but its "Assets:" summary line
was built from QueryByType over the whole asset library, so it reported
every task's and every company's assets as this task's. It also loaded up
to 100000 full rows per type only to count them.

Use CountsByTypeForTask, the per-task count the assets tab already uses.
This commit is contained in:
Chaitanya Deshpande
2026-11-26 17:09:05 +05:30
parent 3cb06383fd
commit 1fceeb3479
2 changed files with 76 additions and 7 deletions
+75
View File
@@ -0,0 +1,75 @@
package server
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/db"
)
// The task report summarises the task's own assets, not every asset in the
// shared library.
func TestReportCountsOnlyTheTaskAssets(t *testing.T) {
m, err := NewManager(t.TempDir(), "")
if err != nil {
db.SkipPGUnavailable(t, err)
}
defer m.Close()
mine, err := m.CreateTask("report mine", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
other, err := m.CreateTask("report other", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
mineID, _ := strconv.ParseInt(mine.ID, 10, 64)
otherID, _ := strconv.ParseInt(other.ID, 10, 64)
defer func() {
_ = m.pg.DeleteTask(mineID)
_ = m.pg.DeleteTask(otherID)
}()
stamp := time.Now().UnixNano()
var ids []int64
for i, taskID := range []int64{mineID, otherID, otherID} {
root := fmt.Sprintf("r%d-%d.invalid", stamp, i)
var id int64
if err := m.pg.QueryRow(`INSERT INTO assets(type,domain,root_domain,task_ids)
VALUES ('subdomain',$1,$2,ARRAY[$3]::bigint[]) RETURNING id`, "a."+root, root, taskID).Scan(&id); err != nil {
t.Fatal(err)
}
ids = append(ids, id)
}
defer func() { _, _ = m.pg.Exec(`DELETE FROM assets WHERE id=ANY($1::bigint[])`, ids) }()
ctx, cancel := context.WithCancel(context.Background())
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
defer stopTestServer(t, s, cancel)
token, err := signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/api/report?task="+mine.ID, nil)
req.Header.Set("Authorization", "Bearer "+token)
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("report status=%d body=%s", rec.Code, rec.Body.String())
}
var line string
for _, l := range strings.Split(rec.Body.String(), "\n") {
if strings.HasPrefix(l, "- Assets: ") {
line = l
}
}
if line != "- Assets: subdomain 1" {
t.Fatalf("summary line %q, want %q", line, "- Assets: subdomain 1")
}
}
+1 -7
View File
@@ -4583,14 +4583,8 @@ func (s *Server) getReport(w http.ResponseWriter, r *http.Request) {
return
}
findings, _ := t.Store.ListByKind(db.KindFinding, 1000) // pure vulnerabilities (facts are a separate KindFact and never enter the report)
counts := map[string]int{}
for _, ty := range []string{"root_domain", "ip", "subdomain", "app", "service", "endpoint"} {
ns, _ := s.m.Assets().QueryByType(ty, 100000, 0)
if len(ns) > 0 {
counts[ty] = len(ns)
}
}
taskID, _ := strconv.ParseInt(t.ID, 10, 64)
counts, _ := s.m.Assets().CountsByTypeForTask(taskID)
var ledger *db.CoverageLedger
if t.CoverageEnabled {
ledger, _ = s.m.Assets().CoverageLedger(taskID)