fix(webui): bind task requester to authenticated principal

This commit is contained in:
sam committed 2026-10-17 08:42:08 +11:00
1 parent 4217a50874
commit cbc4cf309d
7 files changed
+157 -124

No files matched your search

+9 -5
View File
@@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/status"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
@@ -50,7 +51,6 @@ func (s *Server) handleWorkflows(w http.ResponseWriter, r *http.Request) {
}
type workRequest struct {
Identity string `json:"identity"`
Repository string `json:"repository"`
Workflow string `json:"workflow"`
Title string `json:"title"`
@@ -70,6 +70,11 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
if !s.authorizeTaskMutation(w, r) {
return
}
principal, ok := access.PrincipalFromContext(r.Context())
if !ok || principal.IdentityID == "" {
http.Error(w, "authenticated requester required", http.StatusUnauthorized)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 32<<10)
var request workRequest
decoder := json.NewDecoder(r.Body)
@@ -86,13 +91,12 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "invalid work request", http.StatusBadRequest)
return
}
request.Identity = strings.TrimSpace(request.Identity)
request.Repository = strings.TrimSpace(request.Repository)
request.Workflow = strings.TrimSpace(request.Workflow)
request.Title = strings.TrimSpace(request.Title)
request.Instructions = strings.TrimSpace(request.Instructions)
if request.Identity == "" || request.Workflow == "" || request.Title == "" || request.Instructions == "" {
http.Error(w, "identity, workflow, title, and instructions are required", http.StatusBadRequest)
if request.Workflow == "" || request.Title == "" || request.Instructions == "" {
http.Error(w, "workflow, title, and instructions are required", http.StatusBadRequest)
return
}
if utf8.RuneCountInString(request.Title) > 80 {
@@ -113,7 +117,7 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), status)
return
}
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, request.Identity, "", request.Draft, request.Inputs)
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, string(principal.IdentityID), "", request.Draft, request.Inputs)
if err != nil {
s.logf("work request enqueue failed", "err", err)
http.Error(w, "work request rejected", http.StatusBadRequest)
+64 -2
View File
@@ -1,21 +1,30 @@
package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/grpc"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
)
func TestWorkRequestRejectsLongTitle(t *testing.T) {
for _, title := range []string{strings.Repeat("a", 81), strings.Repeat("界", 81)} {
t.Run(title[:3], func(t *testing.T) {
body, err := json.Marshal(workRequest{Identity: "operator", Workflow: "implement", Title: title, Instructions: "Full task description"})
body, err := json.Marshal(workRequest{Workflow: "implement", Title: title, Instructions: "Full task description"})
if err != nil {
t.Fatal(err)
}
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(string(body)))
request := httptest.NewRequestWithContext(access.WithPrincipal(t.Context(), access.Principal{IdentityID: "operator"}), http.MethodPost, "/api/work-requests", strings.NewReader(string(body)))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Archie-CSRF", "1")
response := httptest.NewRecorder()
@@ -26,3 +35,56 @@ func TestWorkRequestRejectsLongTitle(t *testing.T) {
})
}
}
type workRequestTasks struct {
storecontract.TaskStore
requester string
}
func (s *workRequestTasks) EnqueueChatTask(_ context.Context, _, _, _, _, _, requester, _, _ string, _ map[string]any) (*task.Task, error) {
s.requester = requester
return &task.Task{ID: 1}, nil
}
func (*workRequestTasks) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil }
type workRequestControl struct {
controlpb.ControlPlaneServiceClient
}
func (workRequestControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) {
value := []byte(`{}`)
if r.Kind == workflowDefinitionsKind {
value = []byte(`{"definitions":[{"id":"report","yaml":"id: report\nrepository: none\nidentity: worker\n"}]}`)
}
return &controlpb.QueryResponse{Resource: &controlpb.Resource{ValueJson: value}}, nil
}
func TestWorkRequestRequester(t *testing.T) {
for _, tt := range []struct {
name, claimed string
principal *access.Principal
status int
requester string
}{
{"authenticated requester", "", &access.Principal{IdentityID: "signed-in-human"}, http.StatusCreated, "signed-in-human"},
{"cannot claim another requester", `,"identity":"another-account"`, &access.Principal{IdentityID: "signed-in-human"}, http.StatusBadRequest, ""},
{"missing principal", "", nil, http.StatusUnauthorized, ""},
{"empty principal", "", &access.Principal{}, http.StatusUnauthorized, ""},
} {
t.Run(tt.name, func(t *testing.T) {
ctx := t.Context()
if tt.principal != nil {
ctx = access.WithPrincipal(ctx, *tt.principal)
}
req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/work-requests", strings.NewReader(`{"workflow":"report","title":"Report","instructions":"Summarise results"`+tt.claimed+`}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
tasks := &workRequestTasks{}
response := httptest.NewRecorder()
(&Server{Store: tasks, ControlPlane: workRequestControl{}}).handleWorkRequest(response, req)
if response.Code != tt.status || tasks.requester != tt.requester {
t.Fatalf("response = %d %s, requester = %q; want %d, %q", response.Code, response.Body.String(), tasks.requester, tt.status, tt.requester)
}
})
}
}