forked from longtao.wu/droboticflow
563 lines
31 KiB
Go
563 lines
31 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func runArtifactMembersPrincipal(organizationID string) *authenticatedPrincipal {
|
|
return &authenticatedPrincipal{
|
|
User: domain.UserRecord{ID: "member-admin", OrganizationID: organizationID, Role: domain.UserRoleAdmin},
|
|
Organization: domain.OrganizationRecord{ID: organizationID},
|
|
}
|
|
}
|
|
|
|
func runArtifactMembersRequest(method string, target string, principal *authenticatedPrincipal) *http.Request {
|
|
request := httptest.NewRequest(method, target, nil)
|
|
return request.WithContext(context.WithValue(request.Context(), authContextKey{}, principal))
|
|
}
|
|
|
|
func runArtifactMembersHandler(t *testing.T, dataStore store.Store, workspaceRoot string) http.Handler {
|
|
t.Helper()
|
|
return NewHandlerWithConfig(dataStore, nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
|
}
|
|
|
|
func TestRunArtifactMembersExposeNormalizedContentsAndSafeRangeDownload(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
runID := "run-members-normalized"
|
|
runRoot := filepath.Join(workspaceRoot, "system", "runs", runID)
|
|
videoPath := filepath.Join(runRoot, "nodes", "normalize", "outputs", "camera_left.mp4")
|
|
ptsPath := filepath.Join(filepath.Dir(videoPath), "camera_left_pts.csv")
|
|
if err := os.MkdirAll(filepath.Dir(videoPath), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(videoPath, []byte("video-member-content"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(ptsPath, []byte("timestamp_us\n100\n200\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "normalized-manifest", RunID: runID, NodeID: "normalize", Name: "orbbec-normalized-manifest.json", Type: domain.PortTypeJSON,
|
|
Data: map[string]any{
|
|
"schemaVersion": "droboticflow.orbbec.normalized/v1",
|
|
"items": []any{map[string]any{
|
|
"collectionId": "collection-left", "filename": "camera_left.mp4", "relativePath": "camera_left.mp4", "path": videoPath,
|
|
"sizeBytes": 20, "mimeType": "video/mp4", "cameraSide": "left",
|
|
"pairedFiles": map[string]any{"pts": "camera_left_pts.csv"},
|
|
"source": map[string]any{"previewURL": "https://expired.example.test/video.mp4?authorization=secret-must-not-leak"},
|
|
}},
|
|
},
|
|
}
|
|
run := domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-members", WorkflowID: "workflow-members", Status: domain.RunStatusCompleted, Artifacts: []domain.ExecutionArtifact{manifest}}
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertRun(context.Background(), run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
principal := runArtifactMembersPrincipal(domain.DefaultOrganizationID)
|
|
|
|
membersRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(membersRecorder, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members?limit=1", principal))
|
|
if membersRecorder.Code != http.StatusOK {
|
|
t.Fatalf("members status=%d body=%s", membersRecorder.Code, membersRecorder.Body.String())
|
|
}
|
|
var page struct {
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
Total int `json:"total"`
|
|
Recognized bool `json:"recognized"`
|
|
Schema string `json:"schemaVersion"`
|
|
Summary map[string]any `json:"summary"`
|
|
}
|
|
if err := json.Unmarshal(membersRecorder.Body.Bytes(), &page); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !page.Recognized || page.Schema != "droboticflow.orbbec.normalized/v1" || page.Total != 2 || len(page.Items) != 1 {
|
|
t.Fatalf("unexpected normalized member page: %#v", page)
|
|
}
|
|
if !page.Items[0].ContentAvailable || page.Items[0].Kind != "video" || page.Items[0].Details["cameraSide"] != "left" {
|
|
t.Fatalf("unexpected video member: %#v", page.Items[0])
|
|
}
|
|
if strings.Contains(membersRecorder.Body.String(), "authorization=") || strings.Contains(membersRecorder.Body.String(), workspaceRoot) {
|
|
t.Fatalf("member response leaked internal path or signed URL: %s", membersRecorder.Body.String())
|
|
}
|
|
|
|
allRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(allRecorder, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members", principal))
|
|
var all struct {
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(allRecorder.Body.Bytes(), &all); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(all.Items) != 2 || all.Items[0].ID != page.Items[0].ID {
|
|
t.Fatalf("member IDs must be stable across pagination: %#v", all.Items)
|
|
}
|
|
pts := all.Items[1]
|
|
if pts.Kind != "text" || !pts.ContentAvailable || pts.Name != "camera_left_pts.csv" {
|
|
t.Fatalf("paired PTS member=%#v", pts)
|
|
}
|
|
|
|
contentPath := "/api/runs/" + runID + "/artifacts/" + manifest.ID + "/members/" + page.Items[0].ID + "/content"
|
|
rangeRequest := runArtifactMembersRequest(http.MethodGet, contentPath, principal)
|
|
rangeRequest.Header.Set("Range", "bytes=0-4")
|
|
rangeRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(rangeRecorder, rangeRequest)
|
|
if rangeRecorder.Code != http.StatusPartialContent || rangeRecorder.Body.String() != "video" || !strings.HasPrefix(rangeRecorder.Header().Get("Content-Type"), "video/mp4") {
|
|
t.Fatalf("range content status=%d type=%q body=%q", rangeRecorder.Code, rangeRecorder.Header().Get("Content-Type"), rangeRecorder.Body.String())
|
|
}
|
|
headRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(headRecorder, runArtifactMembersRequest(http.MethodHead, contentPath, principal))
|
|
if headRecorder.Code != http.StatusOK || headRecorder.Body.Len() != 0 || !strings.HasPrefix(headRecorder.Header().Get("Content-Type"), "video/mp4") {
|
|
t.Fatalf("head content status=%d type=%q body=%q", headRecorder.Code, headRecorder.Header().Get("Content-Type"), headRecorder.Body.String())
|
|
}
|
|
downloadRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(downloadRecorder, runArtifactMembersRequest(http.MethodGet, contentPath+"?download=1", principal))
|
|
if downloadRecorder.Code != http.StatusOK || !strings.Contains(downloadRecorder.Header().Get("Content-Disposition"), "attachment") {
|
|
t.Fatalf("download status=%d disposition=%q", downloadRecorder.Code, downloadRecorder.Header().Get("Content-Disposition"))
|
|
}
|
|
}
|
|
|
|
func TestDirectoryIndexFilesystemMembersPermitInheritedParentDirectoryContent(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
parentRunID := "run-directory-parent"
|
|
childRunID := "run-directory-child"
|
|
root := filepath.Join(workspaceRoot, "system", "runs", parentRunID, "human-materializations", "item", "revision-000001")
|
|
videoPath := filepath.Join(root, "slices", "slice-000001", "left.mp4")
|
|
annotationPath := filepath.Join(filepath.Dir(videoPath), "annotation.json")
|
|
if err := os.MkdirAll(filepath.Dir(videoPath), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(videoPath, []byte("parent-video"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(annotationPath, []byte(`{"label":"鲜食处理"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "accepted-slice-directory", RunID: parentRunID, NodeID: "human", PortID: "acceptedSliceDirectory", PortName: "acceptedSliceDirectory",
|
|
Name: "revision-000001.directory.index", Type: domain.PortTypeFile, Path: root,
|
|
Data: map[string]any{"schemaVersion": "droboticflow.directory-index/v1", "datasetType": "directory", "presentation": "collection", "rootPath": root},
|
|
}
|
|
run := domain.RunRecord{ID: childRunID, ParentRunID: parentRunID, OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-directory-child", WorkflowID: "workflow-directory-child", Status: domain.RunStatusCompleted, Artifacts: []domain.ExecutionArtifact{manifest}}
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertRun(context.Background(), run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
principal := runArtifactMembersPrincipal(domain.DefaultOrganizationID)
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+childRunID+"/artifacts/"+manifest.ID+"/members", principal))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("members status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
var payload struct {
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
Summary map[string]any `json:"summary"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(payload.Items) != 2 || payload.Summary["contentAvailableCount"] != float64(2) {
|
|
t.Fatalf("inherited directory members=%#v summary=%#v", payload.Items, payload.Summary)
|
|
}
|
|
var video runArtifactManifestMember
|
|
for _, item := range payload.Items {
|
|
if item.Name == "left.mp4" {
|
|
video = item
|
|
}
|
|
}
|
|
if !video.ContentAvailable {
|
|
t.Fatalf("inherited parent video is not previewable: %#v", video)
|
|
}
|
|
contentRequest := runArtifactMembersRequest(http.MethodGet, "/api/runs/"+childRunID+"/artifacts/"+manifest.ID+"/members/"+video.ID+"/content", principal)
|
|
contentRequest.Header.Set("Range", "bytes=0-5")
|
|
content := httptest.NewRecorder()
|
|
handler.ServeHTTP(content, contentRequest)
|
|
if content.Code != http.StatusPartialContent || content.Body.String() != "parent" || !strings.HasPrefix(content.Header().Get("Content-Type"), "video/mp4") {
|
|
t.Fatalf("inherited content status=%d type=%q body=%q", content.Code, content.Header().Get("Content-Type"), content.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHumanItemEvidenceMembersPermitRoleScopedReviewWithoutRunMonitoringAccess(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
runID := "run-human-evidence-members"
|
|
projectID := "project-human-evidence-members"
|
|
root := filepath.Join(workspaceRoot, "system", "runs", runID, "human-materializations", "item", "revision-000001")
|
|
videoPath := filepath.Join(root, "slices", "slice-000001", "left.mp4")
|
|
if err := os.MkdirAll(filepath.Dir(videoPath), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(videoPath, []byte("role-scoped-video"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "materialized-slice-directory", RunID: runID, NodeID: "human", PortID: "materializedSliceDirectory",
|
|
Name: "revision-000001.directory.index", Type: domain.PortTypeFile, Path: root,
|
|
Data: map[string]any{"schemaVersion": "droboticflow.directory-index/v1", "datasetType": "directory", "presentation": "collection", "rootPath": root},
|
|
}
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertProject(context.Background(), domain.ProjectRecord{ID: projectID, OrganizationID: domain.DefaultOrganizationID, Name: "Human evidence"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertRun(context.Background(), domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, WorkflowID: "workflow-human-evidence", Status: domain.RunStatusWaitingForHuman, Artifacts: []domain.ExecutionArtifact{manifest}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
quality := domain.UserRecord{ID: "quality-human-evidence", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleQualityReviewer, SupplierID: domain.DefaultSupplierID(domain.DefaultOrganizationID)}
|
|
if err := dataStore.UpsertUser(context.Background(), quality); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertProjectAnnotationMembership(context.Background(), domain.ProjectAnnotationMembershipRecord{OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: quality.ID, WorkgroupID: "workgroup-human-evidence", AssignmentRole: domain.AnnotationRoleQualityReviewer}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertProjectMember(context.Background(), domain.ProjectMemberRecord{ID: "evidence-member", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: quality.ID, SupplierID: quality.SupplierID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleQualityReviewer}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "batch-human-evidence", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: projectID, WorkflowID: "workflow-human-evidence", RunID: runID, Stages: domain.DefaultFanyuV58HumanStages()}
|
|
if err := dataStore.UpsertHumanBatch(context.Background(), batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{
|
|
ID: "item-human-evidence", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", BatchID: batch.ID,
|
|
ProjectID: projectID, WorkflowID: batch.WorkflowID, RunID: runID, CurrentStageID: "quality_review", Status: domain.HumanItemClaimed,
|
|
AssignedUserID: quality.ID, WorkgroupID: "workgroup-human-evidence", Version: 2,
|
|
Subject: domain.HumanSubjectRef{Payload: map[string]any{"materialization": map[string]any{"runId": runID, "artifactId": manifest.ID}}},
|
|
}
|
|
if err := dataStore.UpsertHumanItem(context.Background(), item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
principal := &authenticatedPrincipal{User: quality, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID}}
|
|
|
|
scoped := httptest.NewRecorder()
|
|
handler.ServeHTTP(scoped, runArtifactMembersRequest(http.MethodGet, "/api/human-items/"+item.ID+"/evidence/"+manifest.ID+"/members", principal))
|
|
if scoped.Code != http.StatusOK {
|
|
t.Fatalf("scoped members=%d body=%s", scoped.Code, scoped.Body.String())
|
|
}
|
|
var page struct {
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(scoped.Body.Bytes(), &page); err != nil || len(page.Items) != 1 || !page.Items[0].ContentAvailable {
|
|
t.Fatalf("scoped members payload=%#v err=%v", page, err)
|
|
}
|
|
content := httptest.NewRecorder()
|
|
request := runArtifactMembersRequest(http.MethodGet, "/api/human-items/"+item.ID+"/evidence/"+manifest.ID+"/members/"+page.Items[0].ID+"/content", principal)
|
|
request.Header.Set("Range", "bytes=0-3")
|
|
handler.ServeHTTP(content, request)
|
|
if content.Code != http.StatusPartialContent || content.Body.String() != "role" {
|
|
t.Fatalf("scoped member content=%d body=%q", content.Code, content.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHumanItemEvidenceContentRejectsReferencedRunFromAnotherProject(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
artifactPath := filepath.Join(workspaceRoot, "foreign-evidence.json")
|
|
if err := os.WriteFile(artifactPath, []byte(`{"foreign":true}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dataStore := store.NewMemoryStore()
|
|
foreignRun := domain.RunRecord{
|
|
ID: "foreign-evidence-run", OrganizationID: domain.DefaultOrganizationID, ProjectID: "foreign-project", WorkflowID: "foreign-workflow", Status: domain.RunStatusCompleted,
|
|
Artifacts: []domain.ExecutionArtifact{{ID: "foreign-evidence-artifact", RunID: "foreign-evidence-run", NodeID: "node", Name: "foreign-evidence.json", Type: domain.PortTypeJSON, Path: artifactPath}},
|
|
}
|
|
if err := dataStore.UpsertRun(context.Background(), foreignRun); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{
|
|
ID: "project-scoped-evidence-item", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: "allowed-project", WorkflowID: "allowed-workflow", RunID: "allowed-run", Status: domain.HumanItemClaimed,
|
|
Subject: domain.HumanSubjectRef{Payload: map[string]any{"evidence": map[string]any{"runId": foreignRun.ID, "artifactId": foreignRun.Artifacts[0].ID}}},
|
|
}
|
|
if err := dataStore.UpsertHumanItem(context.Background(), item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
request := runArtifactMembersRequest(http.MethodGet, "/api/human-items/"+item.ID+"/evidence/"+foreignRun.Artifacts[0].ID+"/content", runArtifactMembersPrincipal(domain.DefaultOrganizationID))
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusNotFound || !strings.Contains(recorder.Body.String(), "Human evidence run not found") {
|
|
t.Fatalf("cross-project evidence content status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDeliveryManifestV5MembersUseDeliveryDirectoryContent(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
runID := "run-delivery-v5"
|
|
deliveryRoot := filepath.Join(workspaceRoot, "system", "runs", runID, "nodes", "package", "outputs", "delivery")
|
|
sliceRoot := filepath.Join(deliveryRoot, "clips", "slice-000001")
|
|
calibrationRoot := filepath.Join(deliveryRoot, "calibration")
|
|
if err := os.MkdirAll(sliceRoot, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(calibrationRoot, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files := []string{"left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "annotation.json", "synchronized-slice.json"}
|
|
manifestFiles := make([]any, 0, len(files))
|
|
for _, name := range files {
|
|
body := []byte("fixture-" + name)
|
|
if err := os.WriteFile(filepath.Join(sliceRoot, name), body, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifestFiles = append(manifestFiles, map[string]any{"name": name, "bytes": len(body), "sha256": "fixture"})
|
|
}
|
|
for _, name := range []string{"camera.yaml", "imu.yaml"} {
|
|
if err := os.WriteFile(filepath.Join(calibrationRoot, name), []byte("calibration"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "delivery-manifest-v5", RunID: runID, NodeID: "package", PortID: "deliveryManifest", PortName: "deliveryManifest", Name: "packages-manifest.json", Type: domain.PortTypeJSON,
|
|
Data: map[string]any{"schemaVersion": "droboticflow.delivery.manifest/v5", "items": []any{map[string]any{"sliceId": "slice-000001", "directory": "clips/slice-000001", "startSecond": 0.5, "endSecond": 2.0, "files": manifestFiles}}, "calibration": []any{map[string]any{"name": "camera.yaml", "path": "calibration/camera.yaml", "bytes": 11}, map[string]any{"name": "imu.yaml", "path": "calibration/imu.yaml", "bytes": 11}}},
|
|
}
|
|
directory := domain.ExecutionArtifact{ID: "delivery-directory-v5", RunID: runID, NodeID: "package", PortID: "deliveryDirectory", PortName: "deliveryDirectory", Name: "delivery.directory.index", Type: domain.PortTypeFile, Path: deliveryRoot, Data: map[string]any{"schemaVersion": "droboticflow.directory-index/v1", "datasetType": "directory", "presentation": "collection", "rootPath": deliveryRoot}}
|
|
run := domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-delivery-v5", WorkflowID: "workflow-delivery-v5", Status: domain.RunStatusCompleted, Artifacts: []domain.ExecutionArtifact{manifest, directory}}
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertRun(context.Background(), run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
principal := runArtifactMembersPrincipal(domain.DefaultOrganizationID)
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members", principal))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("members status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
var payload struct {
|
|
Recognized bool `json:"recognized"`
|
|
Schema string `json:"schemaVersion"`
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
Summary map[string]any `json:"summary"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !payload.Recognized || payload.Schema != "droboticflow.delivery.manifest/v5" || len(payload.Items) != 9 || payload.Summary["contentAvailableCount"] != float64(9) {
|
|
t.Fatalf("delivery v5 projection=%#v", payload)
|
|
}
|
|
if strings.Contains(response.Body.String(), workspaceRoot) {
|
|
t.Fatalf("delivery v5 members leaked workspace path: %s", response.Body.String())
|
|
}
|
|
var left runArtifactManifestMember
|
|
for _, item := range payload.Items {
|
|
if item.Name == "left.mp4" {
|
|
left = item
|
|
}
|
|
}
|
|
request := runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members/"+left.ID+"/content", principal)
|
|
request.Header.Set("Range", "bytes=0-6")
|
|
content := httptest.NewRecorder()
|
|
handler.ServeHTTP(content, request)
|
|
if content.Code != http.StatusPartialContent || content.Body.String() != "fixture" || !strings.HasPrefix(content.Header().Get("Content-Type"), "video/mp4") {
|
|
t.Fatalf("delivery v5 content status=%d type=%q body=%q", content.Code, content.Header().Get("Content-Type"), content.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestRunArtifactMembersRejectUnsafeMemberLocationsAndCrossTenantAccess(t *testing.T) {
|
|
workspaceRoot := t.TempDir()
|
|
runID := "run-members-security"
|
|
runRoot := filepath.Join(workspaceRoot, "system", "runs", runID)
|
|
if err := os.MkdirAll(runRoot, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outsidePath := filepath.Join(workspaceRoot, "outside-secret.mp4")
|
|
if err := os.WriteFile(outsidePath, []byte("outside"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
linkPath := filepath.Join(runRoot, "linked.mp4")
|
|
if err := os.Symlink(outsidePath, linkPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "unsafe-manifest", RunID: runID, NodeID: "normalize", Name: "orbbec-normalized-manifest.json", Type: domain.PortTypeJSON,
|
|
Data: map[string]any{
|
|
"schemaVersion": "droboticflow.orbbec.normalized/v1",
|
|
"items": []any{
|
|
map[string]any{"filename": "outside.mp4", "path": outsidePath, "mimeType": "video/mp4"},
|
|
map[string]any{"filename": "linked.mp4", "path": linkPath, "mimeType": "video/mp4"},
|
|
},
|
|
},
|
|
}
|
|
run := domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-security", WorkflowID: "workflow-security", Status: domain.RunStatusCompleted, Artifacts: []domain.ExecutionArtifact{manifest}}
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertRun(context.Background(), run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, workspaceRoot)
|
|
principal := runArtifactMembersPrincipal(domain.DefaultOrganizationID)
|
|
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members", principal))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("members status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
var payload struct {
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(payload.Items) != 2 || payload.Items[0].ContentAvailable || payload.Items[1].ContentAvailable {
|
|
t.Fatalf("unsafe locations must not be content-addressable: %#v", payload.Items)
|
|
}
|
|
if strings.Contains(response.Body.String(), outsidePath) || strings.Contains(response.Body.String(), linkPath) {
|
|
t.Fatalf("member response leaked local locations: %s", response.Body.String())
|
|
}
|
|
contentRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(contentRecorder, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members/"+payload.Items[0].ID+"/content", principal))
|
|
if contentRecorder.Code != http.StatusNotFound {
|
|
t.Fatalf("unsafe member content must be rejected, got %d", contentRecorder.Code)
|
|
}
|
|
|
|
crossTenantRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(crossTenantRecorder, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members", runArtifactMembersPrincipal("org-other")))
|
|
if crossTenantRecorder.Code != http.StatusForbidden && crossTenantRecorder.Code != http.StatusNotFound {
|
|
t.Fatalf("cross tenant member enumeration must not be readable, status=%d body=%s", crossTenantRecorder.Code, crossTenantRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestRunArtifactMembersReadsExternalizedManifestObjectRefWithoutLeakingLocator(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
runID := "run-members-ref"
|
|
connection := domain.StorageConnectionRecord{ID: "artifact-bos", OrganizationID: domain.DefaultOrganizationID, Provider: domain.StorageProviderBOS, Bucket: "datastorage", Endpoint: "https://bos.example.test"}
|
|
if err := dataStore.UpsertStorageConnection(ctx, connection); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifestKey := "artifacts/run-members-ref/normalize/orbbec-normalized-manifest.json"
|
|
browser := newFakeObjectStorageBrowser()
|
|
browser.objects["datastorage|"+manifestKey] = domain.ObjectStorageObjectSummary{Bucket: "datastorage", Key: manifestKey, Name: "orbbec-normalized-manifest.json", Kind: "json", SizeBytes: 256, ContentType: "application/json"}
|
|
browser.content["datastorage|"+manifestKey] = []byte(`{"schemaVersion":"droboticflow.orbbec.normalized/v1","items":[{"filename":"camera_left.mp4","path":"bos://datastorage/real/camera_left.mp4","mimeType":"video/mp4","cameraSide":"left"}]}`)
|
|
originalResolver := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return browser, func() {}, nil
|
|
}
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = originalResolver })
|
|
|
|
manifest := domain.ExecutionArtifact{
|
|
ID: "externalized-manifest", RunID: runID, NodeID: "normalize", Name: "orbbec-normalized-manifest.json", Type: domain.PortTypeJSON,
|
|
Path: "bos://datastorage/" + manifestKey,
|
|
Data: map[string]any{"schemaVersion": "droboticflow.orbbec.normalized/v1", "dataRef": map[string]any{"payloadPath": "/workspace/private.json"}},
|
|
Payload: &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeRef, Type: domain.PortTypeJSON, Ref: &domain.ArtifactReference{Provider: domain.StorageProviderBOS, ConnectionID: connection.ID, Bucket: "datastorage", Key: manifestKey, Kind: domain.ArtifactRefKindObject, SHA256: "manifest-hash", SizeBytes: 256}},
|
|
}
|
|
run := domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-members-ref", WorkflowID: "workflow-members-ref", Status: domain.RunStatusCompleted, Artifacts: []domain.ExecutionArtifact{manifest}}
|
|
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := runArtifactMembersHandler(t, dataStore, t.TempDir())
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, runArtifactMembersRequest(http.MethodGet, "/api/runs/"+runID+"/artifacts/"+manifest.ID+"/members", runArtifactMembersPrincipal(domain.DefaultOrganizationID)))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("members status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
var payload struct {
|
|
Recognized bool `json:"recognized"`
|
|
Summary map[string]any `json:"summary"`
|
|
Items []runArtifactManifestMember `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !payload.Recognized || len(payload.Items) != 1 || payload.Summary["transferMode"] != "ref" || payload.Summary["presentation"] != "collection" {
|
|
t.Fatalf("externalized manifest projection=%#v", payload)
|
|
}
|
|
for _, forbidden := range []string{"datastorage", manifestKey, connection.ID, "/workspace/private.json"} {
|
|
if strings.Contains(response.Body.String(), forbidden) {
|
|
t.Fatalf("members response leaked storage locator %q: %s", forbidden, response.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunArtifactManifestMemberSchemasAndUnknownFallback(t *testing.T) {
|
|
server := &server{workspaceMountPath: t.TempDir()}
|
|
run := &domain.RunRecord{ID: "run-schema", OrganizationID: domain.DefaultOrganizationID}
|
|
fixtures := []struct {
|
|
name string
|
|
data map[string]any
|
|
count int
|
|
}{
|
|
{
|
|
name: "directory", count: 1,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.directory-read/v1",
|
|
"entries": []any{map[string]any{"path": "input.csv", "storagePath": "bos://bucket/folder/input.csv", "mimeType": "text/csv"}},
|
|
},
|
|
},
|
|
{
|
|
name: "blurred", count: 1,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.video_face_blur.manifest/v1",
|
|
"items": []any{map[string]any{"filename": "blurred.mp4", "path": "bos://bucket/blurred.mp4", "mimeType": "video/mp4"}},
|
|
},
|
|
},
|
|
{
|
|
name: "roughcut", count: 1,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.qwen3vl.behavior-rough-cut/v1",
|
|
"items": []any{map[string]any{
|
|
"clipId": "clip-1", "clipName": "clip-1.mp4", "source": map[string]any{"path": "bos://bucket/source.mp4", "mimeType": "video/mp4"},
|
|
"startSecond": 1.0, "endSecond": 2.0, "behavior": "opens door",
|
|
"annotation": map[string]any{"actions": []any{map[string]any{"label": "热食加工与出餐"}}},
|
|
}},
|
|
},
|
|
},
|
|
{
|
|
name: "annotation", count: 1,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"items": []any{map[string]any{"clipId": "clip-1", "annotation": map[string]any{"actions": []any{map[string]any{"label": "热食加工与出餐"}}}}},
|
|
},
|
|
},
|
|
{
|
|
name: "package", count: 3,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.delivery.manifest/v2",
|
|
"items": []any{map[string]any{"packageName": "clip-1-package", "clipId": "clip-1", "clipName": "clip-1.mp4", "storagePath": "/not-servable/outside-run"}},
|
|
},
|
|
},
|
|
{
|
|
name: "synchronized-package-v4", count: 12,
|
|
data: map[string]any{
|
|
"schemaVersion": "droboticflow.delivery.manifest/v4",
|
|
"items": []any{map[string]any{
|
|
"packageName": "clip-1-package", "clipId": "clip-1", "clipName": "clip-1.mp4", "rightClipName": "clip-1_right.mp4", "storagePath": "/not-servable/outside-run",
|
|
"segmentManifest": map[string]any{
|
|
"video": map[string]any{"bytes": 100}, "videos": map[string]any{"right": map[string]any{"bytes": 110}},
|
|
"headpose": map[string]any{"file": "headpose.json"}, "handpose": map[string]any{"file": "handpose.json"},
|
|
},
|
|
}},
|
|
"calibration": []any{map[string]any{"name": "camera.yaml", "bytes": 10}, map[string]any{"name": "imu.yaml", "bytes": 11}},
|
|
},
|
|
},
|
|
}
|
|
for _, fixture := range fixtures {
|
|
t.Run(fixture.name, func(t *testing.T) {
|
|
manifest := &domain.ExecutionArtifact{ID: "manifest-" + fixture.name, RunID: run.ID, Name: fixture.name + ".json", Type: domain.PortTypeJSON, Data: fixture.data}
|
|
document := server.runArtifactManifestDocument(run, manifest)
|
|
if !document.Recognized || len(document.Members) != fixture.count {
|
|
t.Fatalf("schema %s document=%#v", fixture.name, document)
|
|
}
|
|
for _, member := range document.Members {
|
|
if !validRunArtifactMemberID(member.ID) || strings.Contains(member.Name, "/") || strings.Contains(fmt.Sprint(member.Details), "bos://") {
|
|
t.Fatalf("unsafe member projection: %#v", member)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
unknown := server.runArtifactManifestDocument(run, &domain.ExecutionArtifact{ID: "manifest-unknown", RunID: run.ID, Name: "unknown.json", Type: domain.PortTypeJSON, Data: map[string]any{"schemaVersion": "example.unknown/v1", "items": []any{map[string]any{"path": "/secret"}}}})
|
|
if unknown.Recognized || len(unknown.Members) != 0 || unknown.Message == "" {
|
|
t.Fatalf("unknown schema must fall back safely: %#v", unknown)
|
|
}
|
|
}
|