mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
Agent/auto scope rows are coverage-only (approved_by_operator=false): sandbox egress already filtered them, but the scoped asset reads (scopeTargetCTE behind QueryDSLInScope/GetByIDsInScope), the task's company list, and the lesson read scope still honoured them, so an agent could widen what it reads by calling add_task_scope (GHSA-53wc). Require approved_by_operator=true on those read paths. The coverage ledger keeps counting every scope row unchanged: only authorization reads are filtered, not the coverage denominator.
139 lines
4.6 KiB
Go
139 lines
4.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestAgentScopeDoesNotGrantReadsUntilOperatorApproved pins GHSA-53wc: an
|
|
// agent-written task_scope row is coverage-only, so the agent-facing scoped
|
|
// asset reads (list_assets / read-by-id) and the company-scoped lesson reads
|
|
// must ignore it. Promoting the same entry to operator-approved
|
|
// (approved_by_operator=true) grants those reads.
|
|
func TestAgentScopeDoesNotGrantReadsUntilOperatorApproved(t *testing.T) {
|
|
d, assets, _ := testSetup(t)
|
|
t.Cleanup(func() { d.Close() })
|
|
ctx := context.Background()
|
|
|
|
task, err := d.CreateTask("scope read approval", "goal", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) })
|
|
|
|
stamp := time.Now().UnixNano()
|
|
root := fmt.Sprintf("approval%d.invalid", stamp)
|
|
host := "a." + root
|
|
marker := fmt.Sprintf("appr%d", stamp)
|
|
var assetID int64
|
|
if err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain,page_title,last_seen)
|
|
VALUES ('subdomain',$1,$2,$3,now()) RETURNING id`, host, root, marker).Scan(&assetID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM assets WHERE id=$1`, assetID) })
|
|
|
|
// The agent widens the task's scope through add_task_scope: coverage-only.
|
|
ts, err := assets.AddAgentScope(task.ID, "subdomain", host, "planner widened scope", "agent")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ts.ApprovedByOperator {
|
|
t.Fatalf("agent row must not be operator-approved: %+v", ts)
|
|
}
|
|
if got, err := assets.QueryDSLInScope(marker, "", task.ID, 50, 0); err != nil {
|
|
t.Fatal(err)
|
|
} else if len(got) != 0 {
|
|
t.Fatalf("agent-written scope leaked %d asset(s) into QueryDSLInScope", len(got))
|
|
}
|
|
if got, err := assets.GetByIDsInScope(task.ID, []int64{assetID}); err != nil {
|
|
t.Fatal(err)
|
|
} else if len(got) != 0 {
|
|
t.Fatalf("agent-written scope leaked asset %d into GetByIDsInScope", assetID)
|
|
}
|
|
|
|
// The same drift via a company row: it must not expose that company's
|
|
// lessons or company list to the task.
|
|
companyID, _, err := d.Companies().UpsertCompany(fmt.Sprintf("ScopeApproval %d", stamp), "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { cleanupCompany(d, companyID) })
|
|
lessonCue := fmt.Sprintf("scope-lesson-%d", stamp)
|
|
var lessonID int64
|
|
if err := d.QueryRow(`INSERT INTO rsi_lessons(dedupe_key,kind,cue,action,company_id)
|
|
VALUES ($1,'positive',$2,'company lesson', $3) RETURNING id`, lessonCue, lessonCue, companyID).Scan(&lessonID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE id=$1`, lessonID) })
|
|
|
|
if _, err := assets.AddAgentScope(task.ID, "company", strconv.FormatInt(companyID, 10), "planner widened company", "agent"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lessons, err := d.TaskLessons(ctx, task.ID, 8)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, l := range lessons {
|
|
if l.ID == lessonID {
|
|
t.Fatalf("agent-written company scope exposed lesson %d", lessonID)
|
|
}
|
|
}
|
|
companyIDs, err := d.TaskCompanyIDs(task.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range companyIDs {
|
|
if id == companyID {
|
|
t.Fatalf("agent-written company scope exposed company %d through TaskCompanyIDs", companyID)
|
|
}
|
|
}
|
|
|
|
// The operator approves the very same entries: now the reads are granted.
|
|
if ts, err := assets.AddOperatorScope(task.ID, "subdomain", host, "operator approved"); err != nil || !ts.ApprovedByOperator {
|
|
t.Fatalf("AddOperatorScope subdomain: ts=%+v err=%v", ts, err)
|
|
}
|
|
if ts, err := assets.AddOperatorScope(task.ID, "company", strconv.FormatInt(companyID, 10), "operator approved"); err != nil || !ts.ApprovedByOperator {
|
|
t.Fatalf("AddOperatorScope company: ts=%+v err=%v", ts, err)
|
|
}
|
|
got, err := assets.QueryDSLInScope(marker, "", task.ID, 50, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].ID != assetID {
|
|
t.Fatalf("operator-approved scope did not surface asset %d: %+v", assetID, got)
|
|
}
|
|
if byID, err := assets.GetByIDsInScope(task.ID, []int64{assetID}); err != nil {
|
|
t.Fatal(err)
|
|
} else if len(byID) != 1 || byID[0].ID != assetID {
|
|
t.Fatalf("operator-approved scope did not surface asset %d by id: %+v", assetID, byID)
|
|
}
|
|
lessons, err = d.TaskLessons(ctx, task.ID, 8)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found := false
|
|
for _, l := range lessons {
|
|
if l.ID == lessonID {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("operator-approved company scope did not expose lesson %d: %+v", lessonID, lessons)
|
|
}
|
|
companyIDs, err = d.TaskCompanyIDs(task.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found = false
|
|
for _, id := range companyIDs {
|
|
if id == companyID {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("operator-approved company scope missing from TaskCompanyIDs: %v", companyIDs)
|
|
}
|
|
}
|