diff --git a/components/buckets/new-form.tsx b/components/buckets/new-form.tsx index 231c3bdf..bcb7d88e 100644 --- a/components/buckets/new-form.tsx +++ b/components/buckets/new-form.tsx @@ -43,7 +43,7 @@ export function BucketNewForm({ show, onShowChange }: BucketNewFormProps) { const { t } = useTranslation() const message = useMessage() const { createBucket, putBucketVersioning, putObjectLockConfiguration, putBucketQuota } = useBucket() - const { canCapability, checkCapability } = usePermissions() + const { canCapability, mayCapability, checkCapability } = usePermissions() const [objectKey, setObjectKey] = React.useState("") const [version, setVersion] = React.useState(false) @@ -62,7 +62,11 @@ export function BucketNewForm({ show, onShowChange }: BucketNewFormProps) { const parsedRetentionPeriod = Math.max(1, Number.parseInt(retentionPeriod, 10) || 1) const isSubmitDisabled = creating || !trimmedBucketName || !!nameError const bucketContext = trimmedBucketName ? { bucket: trimmedBucketName } : {} - const canCreateBucket = !!trimmedBucketName && canCapability("bucket.create", bucketContext) + // Bucket names are user input. On legacy servers the browser cannot + // evaluate resource variables such as ${aws:userid}, so discoverability + // uses the action-potential check and the actual CreateBucket request remains + // the authorization boundary. + const canCreateBucket = !!trimmedBucketName && mayCapability("bucket.create", bucketContext) const canEditVersioning = canCapability("bucket.versioning.edit", bucketContext) const canEditObjectLock = canCapability("bucket.objectLock.edit", bucketContext) const canEditQuota = canCapability("bucket.quota.edit", bucketContext) diff --git a/hooks/use-permissions.tsx b/hooks/use-permissions.tsx index 4a8eab9e..911f95e8 100644 --- a/hooks/use-permissions.tsx +++ b/hooks/use-permissions.tsx @@ -339,6 +339,9 @@ export function PermissionsProvider({ children }: { children: React.ReactNode }) if (permissionClient.status === "unsupported") { const snapshot = legacyPolicy.current if (snapshot.client !== permissionClient) return false + if (capability === "bucket.create") { + return snapshot.isAdmin || hasConsoleActionPotential(snapshot.userPolicy, "s3:CreateBucket") + } if (capability === "objects.forceCleanup") return snapshot.isOwner || hasConsoleCapability(snapshot.userPolicy, capability, context) return snapshot.isAdmin || hasConsoleCapability(snapshot.userPolicy, capability, context) diff --git a/lib/effective-permissions.ts b/lib/effective-permissions.ts index d6d4af3d..067da24d 100644 --- a/lib/effective-permissions.ts +++ b/lib/effective-permissions.ts @@ -330,7 +330,7 @@ export class EffectivePermissionClient { const status = error && typeof error === "object" && "status" in error ? error.status : undefined if (this.active && generation === this.generation) this.status = - !this.supportedEndpoint && [404, 405, 501].includes(Number(status)) ? "unsupported" : "unavailable" + !this.supportedEndpoint && [404, 405, 426, 501].includes(Number(status)) ? "unsupported" : "unavailable" failed = true results = requests.map(() => UNKNOWN) } diff --git a/tests/hooks/effective-permissions.test.ts b/tests/hooks/effective-permissions.test.ts index bcef90ec..262ef684 100644 --- a/tests/hooks/effective-permissions.test.ts +++ b/tests/hooks/effective-permissions.test.ts @@ -136,6 +136,53 @@ test("an explicitly empty warehouse object retains the bucket ARN on a legacy se assert.ok(mounted.requests.some((request) => request.object === "")) }) +test("legacy bucket creation uses action potential when a resource variable cannot be resolved", async (t) => { + const mounted = await mount( + t, + () => { + throw Object.assign(new Error("Unsupported"), { status: 426 }) + }, + JSON.stringify({ + Version: "2012-10-17", + Statement: [ + { + Effect: "Allow", + Action: ["s3:CreateBucket"], + Resource: ["arn:aws:s3:::${aws:userid}", "arn:aws:s3:::${aws:userid}-*"], + }, + ], + }), + ) + await act(async () => { + await mounted.permissions.loadAction({ action: "s3:CreateBucket", context: { bucket: "fixture-alice-project" } }) + assert.equal(mounted.permissions.mayCapability("bucket.create", { bucket: "fixture-alice-project" }), true) + }) + let allowed = false + await act(async () => { + allowed = await mounted.permissions.checkCapability("bucket.create", { bucket: "fixture-alice-project" }) + }) + assert.equal(allowed, true) + assert.equal(mounted.permissions.mayCapability("bucket.create", { bucket: "other-user-project" }), true) +}) + +test("legacy bucket creation remains unavailable when the action is absent", async (t) => { + const mounted = await mount( + t, + () => { + throw Object.assign(new Error("Unsupported"), { status: 426 }) + }, + JSON.stringify({ + Version: "2012-10-17", + Statement: [{ Effect: "Allow", Action: ["s3:ListBucket"], Resource: ["arn:aws:s3:::${aws:userid}-*"] }], + }), + ) + await act(async () => { + await mounted.permissions.loadAction({ action: "s3:CreateBucket", context: { bucket: "fixture-alice-project" } }) + }) + assert.equal(mounted.permissions.mayCapability("bucket.create", { bucket: "fixture-alice-project" }), false) + assert.equal(await mounted.permissions.checkCapability("bucket.create", { bucket: "fixture-alice-project" }), false) +}) + test("a server-side session or condition denial overrides a broad identity projection", async (t) => { const mounted = await mount(t, () => ({ decision: "denied" })) let allowed = true diff --git a/tests/lib/effective-permissions.test.ts b/tests/lib/effective-permissions.test.ts index f233fbe6..dd59f757 100644 --- a/tests/lib/effective-permissions.test.ts +++ b/tests/lib/effective-permissions.test.ts @@ -306,6 +306,17 @@ for (const status of [401, 403, 500]) { }) } +test("HTTP 426 selects legacy compatibility for older permission endpoints", async () => { + const client = new EffectivePermissionClient( + async () => { + throw Object.assign(new Error("upgrade required"), { status: 426 }) + }, + () => {}, + ) + await client.load(check) + assert.equal(client.status, "unsupported") +}) + test("an absent endpoint selects explicit legacy compatibility", async () => { const client = new EffectivePermissionClient( async () => { diff --git a/tests/lib/form-interaction.test.ts b/tests/lib/form-interaction.test.ts index 00ca1c6d..8dd391d9 100644 --- a/tests/lib/form-interaction.test.ts +++ b/tests/lib/form-interaction.test.ts @@ -269,6 +269,7 @@ test("object lock and versioning change together, and a pending bucket creation useBucket: () => api, usePermissions: () => ({ canCapability: () => true, + mayCapability: () => true, checkCapability: async (...args: unknown[]) => { permissionChecks.push(args) return true @@ -323,7 +324,7 @@ test("enabling bucket retention explains lifecycle expiration limits", async (t) const switches = new Map void }>() const { container } = await mount(t, React.createElement(BucketNewForm, { show: true, onShowChange: () => {} }), { useBucket: () => ({}), - usePermissions: () => ({ canCapability: () => true }), + usePermissions: () => ({ canCapability: () => true, mayCapability: () => true }), useMessage: () => ({ error: () => {}, success: () => {} }), getOptionalBucketNameError: () => null, cn: (...values: unknown[]) => values.filter(Boolean).join(" "),