droboticflow/internal/api/storage_scope_test.go

133 lines
6.6 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestScopedBOSRejectsBeforeNetworkOrSigning(t *testing.T) {
// A nil client makes any missed guard fail immediately; no remote service or
// credential policy can accidentally hide a missing platform check.
b := &bosObjectStorageBrowser{connection: &domain.StorageConnectionRecord{Bucket: "testxyz", RootPrefix: "ubt/"}}
ctx := context.Background()
checks := map[string]func() error{
"list": func() error { _, e := b.ListObjects(ctx, "testxyz", "other"); return e },
"recursive": func() error { _, e := b.ListObjectsRecursive(ctx, "testxyz", "other"); return e },
"page": func() error { _, e := b.ListObjectsPage(ctx, "testxyz", "other", "", 10); return e },
"get": func() error { _, _, e := b.ReadObject(ctx, "testxyz", "other/x"); return e },
"range": func() error { _, _, e := b.ReadObjectRange(ctx, "testxyz", "other/x", 0, 1); return e },
"preview": func() error { _, _, e := b.ReadObjectPreview(ctx, "testxyz", "other/x", 10); return e },
"head": func() error { _, e := b.GetObjectSummary(ctx, "testxyz", "other/x"); return e },
"put": func() error { _, e := b.PutObject(ctx, "testxyz", "other/x", nil, ""); return e },
"presign put": func() error { _, e := b.PresignPutObject(ctx, "testxyz", "other/x", "", 60); return e },
"presign get": func() error { _, e := b.BuildPreviewURL(ctx, "testxyz", "other/x"); return e },
"initiate": func() error { _, e := b.InitiateMultipartUpload(ctx, "testxyz", "other/x", "", 60); return e },
"part": func() error { _, e := b.PresignMultipartUploadPart(ctx, "testxyz", "other/x", "id", 1, 60); return e },
"complete": func() error { _, e := b.CompleteMultipartUpload(ctx, "testxyz", "other/x", "id", nil); return e },
"abort": func() error { return b.AbortMultipartUpload(ctx, "testxyz", "other/x", "id") },
"delete": func() error { return b.DeleteObject(ctx, "testxyz", "other/x") },
"delete prefix": func() error { return b.DeletePrefix(ctx, "testxyz", "other") },
"rename source": func() error { _, e := b.RenameObject(ctx, "testxyz", "other/x", "ubt/x"); return e },
"rename target": func() error { _, e := b.RenameObject(ctx, "testxyz", "ubt/x", "other/x"); return e },
"move": func() error { _, e := b.MovePrefix(ctx, "testxyz", "ubt/x", "other"); return e },
}
for name, fn := range checks {
t.Run(name, func(t *testing.T) {
if e := fn(); !errors.Is(e, domain.ErrStorageScope) {
t.Fatalf("expected scope denial, got %v", e)
}
})
}
w := httptest.NewRecorder()
renderError(w, 500, domain.ErrStorageScope)
if w.Code != http.StatusForbidden {
t.Fatal(w.Code)
}
}
func TestScopedConnectionCannotBeWidenedByOrganizationAdmin(t *testing.T) {
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
data := store.NewMemoryStore()
h := NewHandler(data, nil)
root := bootstrapSuperAdminSession(t, h)
org := adminCreateOrganization(t, h, root, "UBT", "ubt-scope")
user := adminCreateUser(t, h, root, org.ID, "UBT Admin", "ubt-scope@example.com", "admin", "fixture-password")
cookie := loginSession(t, h, user.Email, "fixture-password")
c := domain.StorageConnectionRecord{ID: "scoped", OrganizationID: org.ID, Name: "UBT", Provider: domain.StorageProviderBOS, Bucket: "testxyz", RootPrefix: "ubt/", Endpoint: "https://bj.bcebos.com", AccessKeyID: "fixture", SecretAccessKey: "fixture"}
if err := data.UpsertStorageConnection(t.Context(), c); err != nil {
t.Fatal(err)
}
for _, body := range []string{`{"rootPrefix":""}`, `{"rootPrefix":"other/"}`, `{"bucket":"other"}`, `{"endpoint":"https://example.com"}`, `{"accessKeyId":"broad"}`, `{"secretAccessKey":"broad"}`} {
r := serveJSONWithCookie(h, http.MethodPut, "/api/storage/connections/scoped", []byte(body), cookie)
if r.Code != 403 {
t.Fatalf("mutation %s: %d %s", body, r.Code, r.Body.String())
}
}
for _, target := range []string{
"/api/object-storage/objects?connectionId=scoped&bucket=testxyz&prefix=other",
"/api/object-storage/content?connectionId=scoped&bucket=testxyz&key=other/x",
} {
r := serveJSONWithCookie(h, http.MethodGet, target, nil, cookie)
if r.Code != 403 {
t.Fatalf("scope escape: %d %s", r.Code, r.Body.String())
}
}
r := serveJSONWithCookie(h, http.MethodPut, "/api/storage/connections/scoped", []byte(`{"name":"UBT Storage"}`), cookie)
if r.Code != 200 {
t.Fatalf("rename: %d %s", r.Code, r.Body.String())
}
got, _ := data.GetStorageConnection(t.Context(), "scoped")
if got.RootPrefix != "ubt/" {
t.Fatal("scope lost")
}
}
func TestScopedListingUsesAuthorizedRoot(t *testing.T) {
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("prefix") != "ubt/" {
t.Errorf("unexpected prefix: %s", r.URL.String())
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"name":"testxyz","prefix":"ubt/","contents":[{"key":"ubt/file.txt","size":3}],"isTruncated":false}`))
}))
defer endpoint.Close()
b := bosContextTestBrowser(t, endpoint.URL)
b.connection = &domain.StorageConnectionRecord{Bucket: "testxyz", RootPrefix: "ubt/"}
page, err := b.ListObjectsPage(t.Context(), "testxyz", "", "", 10)
if err != nil || len(page.Items) != 1 || page.Items[0].Key != "ubt/file.txt" {
t.Fatalf("page=%+v err=%v", page, err)
}
}
func TestScopedSyncReserveUploadsDirectlyInsideRoot(t *testing.T) {
data, _, session, request := cancellationRaceFixture(t, domain.ObjectSyncSessionPlanned)
c, _ := data.GetStorageConnection(t.Context(), "unused")
c.RootPrefix = "ubt/"
c.Bucket = "fixture"
if err := data.UpsertStorageConnection(t.Context(), *c); err != nil {
t.Fatal(err)
}
space, _ := data.GetObjectSyncSpace(t.Context(), session.SyncSpaceID)
space.RootPrefix = "ubt"
if err := data.UpsertObjectSyncSpace(t.Context(), *space); err != nil {
t.Fatal(err)
}
item := domain.ObjectSyncItemRecord{ID: "scoped-item", OrganizationID: session.OrganizationID, SessionID: session.ID, TargetKey: "ubt/file", RelativePath: "file", Status: domain.ObjectSyncItemPlanned, Action: "create"}
if err := data.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{item}); err != nil {
t.Fatal(err)
}
r := request("items/scoped-item/reserve", map[string]any{}, "scoped-reserve")
if r.Code != 200 {
t.Fatalf("reserve: %d %s", r.Code, r.Body.String())
}
got, _ := data.GetObjectSyncItem(t.Context(), item.ID)
if got.StagingKey != item.TargetKey {
t.Fatalf("upload key differs from formal target: %s", got.StagingKey)
}
}