Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion internal/httpapi/project_write_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,44 @@ func (d deps) runWriteGuard() gin.HandlerFunc {
if issue, found := d.repo.Issue(run.ExecutionContext.IssueID); found {
issueProject = issue.ProjectID
}
if !d.requireProjectWriter(c, domain.RunProjectID(run, issueProject)) {
projectID := domain.RunProjectID(run, issueProject)
if strings.TrimSpace(projectID) == "" {
// Shared workflows can start without a project. An empty project
// must not mean "every signed-in user": scope the run controls to
// the responsible user instead.
if !d.requireRunOwner(c, run) {
c.Abort()
return
}
} else if !d.requireProjectWriter(c, projectID) {
c.Abort()
return
}
c.Next()
}
}

// requireRunOwner authorizes run lifecycle writes for a run without a
// project. Failing closed when the responsible user is unknown is deliberate:
// the alternative (an empty project means every authenticated user) lets any
// account approve or cancel another user's shared-workflow run.
func (d deps) requireRunOwner(c *gin.Context, run domain.Run) bool {
if !authRequired() || d.isPlatformAdmin(c.Request) {
return true
}
user, ok := d.requestUser(c.Request, true)
if !ok || user.ID <= 0 {
c.JSON(http.StatusForbidden, gin.H{"error": "请先登录"})
return false
}
owner := run.ExecutionContext.ResponsibleUserID
if owner > 0 && owner == user.ID {
return true
}
c.JSON(http.StatusForbidden, gin.H{"error": "只有该运行的负责人可以修改或执行该运行"})
return false
}

func (d deps) requirePlatformAdmin(c *gin.Context) bool {
if !authRequired() || d.isPlatformAdmin(c.Request) {
return true
Expand Down
74 changes: 74 additions & 0 deletions internal/httpapi/run_write_guard_projectless_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package httpapi

import (
"bytes"
"net/http"
"testing"

"agentflow/internal/domain"

"github.com/gin-gonic/gin"
)

// A shared workflow can run without a project. The empty project ID must not
// authorize every signed-in account to approve, cancel or delete the run.
func TestRunWriteGuardRequiresResponsibleUserWithoutProject(t *testing.T) {
t.Setenv("AGENTFLOW_AUTH_REQUIRED", "true")
h, owner := workbenchFixture(t)
if err := h.repo.PutUser(domain.User{LoginName: "outsider"}); err != nil {
t.Fatal(err)
}
outsider, _ := h.repo.UserByLoginName("outsider")

run := domain.Run{
ID: "projectless-run",
Status: "waiting",
ExecutionContext: domain.TelemetryContext{ResponsibleUserID: owner.ID},
Nodes: map[string]*domain.NodeRun{},
}
if err := h.repo.PutRun(run); err != nil {
t.Fatal(err)
}

router := gin.New()
router.Use(h.runWriteGuard())
router.POST("/api/runs/:id/approve", func(c *gin.Context) {
c.Status(http.StatusOK)
})

for _, tc := range []struct {
name string
user domain.User
status int
}{
{"responsible user", owner, http.StatusOK},
{"unrelated user", outsider, http.StatusForbidden},
} {
t.Run(tc.name, func(t *testing.T) {
c, rec := workbenchContext(tc.user, http.MethodPost, "/api/runs/projectless-run/approve", bytes.NewBufferString(`{}`))
router.ServeHTTP(rec, c.Request)
if rec.Code != tc.status {
t.Fatalf("status = %d, want %d: %s", rec.Code, tc.status, rec.Body.String())
}
})
}
}

// Unknown ownership fails closed rather than falling back to the empty-project
// "any authenticated user" rule.
func TestRunWriteGuardRejectsProjectlessRunWithoutOwner(t *testing.T) {
t.Setenv("AGENTFLOW_AUTH_REQUIRED", "true")
h, owner := workbenchFixture(t)
if err := h.repo.PutRun(domain.Run{ID: "orphan-run", Status: "waiting", Nodes: map[string]*domain.NodeRun{}}); err != nil {
t.Fatal(err)
}
router := gin.New()
router.Use(h.runWriteGuard())
router.POST("/api/runs/:id/approve", func(c *gin.Context) { c.Status(http.StatusOK) })

c, rec := workbenchContext(owner, http.MethodPost, "/api/runs/orphan-run/approve", bytes.NewBufferString(`{}`))
router.ServeHTTP(rec, c.Request)
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403: %s", rec.Code, rec.Body.String())
}
}