Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/http/routes/bucket/getAllBuckets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { bucketSchema } from '@storage/schemas'
import { ROUTE_OPERATIONS } from '../operations'
import { isPythonClientBefore } from '@storage/limits'

const successResponseSchema = {
type: 'array',
Expand Down Expand Up @@ -58,8 +59,15 @@ export default async function routes(fastify: FastifyInstance) {
},
async (request, response) => {
const { limit, offset, sortColumn, sortOrder, search } = request.query

// Detects user agents that support the type property in bucket list response
// storage-py < v0.12.1 throws fatal error if type property is present
// type property added in v0.12.1 -- https://github.com/supabase/storage-py/releases/tag/v0.12.1
const includeBucketType = !isPythonClientBefore(request.headers['user-agent'] || '', '0.12.1')

const results = await request.storage.listBuckets(
'id, name, type, public, owner, created_at, updated_at, file_size_limit, allowed_mime_types',
'id, name, public, owner, created_at, updated_at, file_size_limit, allowed_mime_types' +
(includeBucketType ? ', type' : ''),
{ limit, offset, sortColumn, sortOrder, search }
)

Expand Down
22 changes: 22 additions & 0 deletions src/storage/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ export function isUuid(value: string) {
export function isEmptyFolder(object: string) {
return object.endsWith('.emptyFolderPlaceholder')
}

/**
* Checks if the client is supabase-py and before the specified version
*
* @param userAgent user agent header string
* @param version semver to check against, must be in format '0.0.0'
*/
export function isPythonClientBefore(userAgent: string, version: string): boolean {
const [minMajor, minMinor, minPatch] = version.split('.').map(Number)
const match = userAgent.match(/supabase-py\/storage3 v(\d+)\.(\d+)\.(\d+)/i)
if (!match) {
return false
}

const [major, minor, patch] = match.slice(1).map(Number)

if (major < minMajor) return true
if (major > minMajor) return false
if (minor < minMinor) return true
if (minor > minMinor) return false
return patch < minPatch
}
28 changes: 28 additions & 0 deletions src/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,39 @@ describe('testing GET all buckets', () => {
expect(responseJSON[0]).toMatchObject({
id: expect.any(String),
name: expect.any(String),
type: expect.any(String),
public: expect.any(Boolean),
file_size_limit: null,
allowed_mime_types: null,
})
})

for (const [userAgent, shouldIncludeType] of [
['Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/138.0.0.0 Safari/537.36', true],
['supabase-py/storage3 v0.11.9', false],
['supabase-py/storage3 v0.12.0', false],
['supabase-py/storage3 v0.12.1', true],
['supabase-py/storage3 v0.12.2', true],
['supabase-py/storage3 v0.13.0', true],
['supabase-py/storage3 v1.0.0', true],
]) {
test(`Should ${
shouldIncludeType ? '' : 'NOT '
}include type for ${userAgent} client`, async () => {
const response = await appInstance.inject({
method: 'GET',
url: `/bucket`,
headers: {
authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`,
'user-agent': userAgent as string,
},
})
expect(response.statusCode).toBe(200)
const body = response.json()
expect(body[0].type).toBe(shouldIncludeType ? 'STANDARD' : undefined)
})
}

test('checking RLS: anon user is not able to get all buckets', async () => {
const response = await appInstance.inject({
method: 'GET',
Expand Down Expand Up @@ -159,6 +186,7 @@ describe('testing GET all buckets', () => {
expect(responseJSON[0]).toMatchObject({
id: 'bucket4',
name: 'bucket4',
type: expect.any(String),
public: false,
file_size_limit: null,
allowed_mime_types: null,
Expand Down
Loading