Skip to content

Commit c907b11

Browse files
authored
improvement(supabase): add Edge Functions tool; correct storage output shapes + harden tools (#5112)
* improvement(supabase): add Edge Functions tool; correct storage output shapes + harden tools - Add supabase_invoke_function tool (POST/GET/PUT/PATCH/DELETE /functions/v1/{name}) - upsert: support on_conflict; storage upload: support cache-control - Fix storage copy/move/upload/delete-bucket output properties to match live API - get_public_url: build URL via directExecution (no spurious network call) - text_search: validate column identifier - Strip non-TSDoc section-label comments * fix(supabase): harden rpc/text_search identifiers; drop unused get_public_url apiKey - rpc: validate + encode functionName (SSRF/injection parity with vector_search) - text_search: validate language config interpolated into the PostgREST operator - get_public_url: remove unused apiKey param + dead auth headers (public endpoint needs no auth) - create_bucket: tighten output description to match the {name}-only response * improvement(tavily): mark optional params advanced; fix empty content output - Mark 29 optional search/extract/crawl/map subBlocks as mode: advanced (keep query/urls/url/apiKey basic) - Fix search transformResponse: populate content from result.content (was result.snippet, always empty) - Guard data.results with ?? []; correct country placeholder to a lowercase name - Rewrite stale TavilySearch/Extract response interfaces; drop dead duplicate interfaces * fix(supabase): valid Cache-Control directive on upload; clarify functionName description - storage upload: expand a bare numeric cache-control to `max-age=<n>` (a raw number is not a valid Cache-Control header) - block: functionName input description now covers RPC, vector search, and Edge Function invoke * fix(supabase): edge-function error handling + reject array headers - invoke_function: drop unreachable !response.ok branch (executor throws on non-OK before transformResponse runs and surfaces the error body); document the success-only contract - invoke_function: ignore non-object (array) headers so JSON arrays can't produce numeric-index header names - block: reject array/non-object Edge Function headers with a clear error in config.params * fix(supabase): scope Edge Function method/body/headers to invoke_function Prevents a stale `method` value (e.g. from the Edge Function field) from leaking into other operations' params. The tool executor lets `params.method` override a tool's static verb (tools/utils.ts), so an unscoped value could turn a read into DELETE/POST against PostgREST. Now method/body/headers are only passed for the invoke_function operation. Adds a block-level regression test. * fix(supabase): only parse Edge Function body/headers for invoke_function Stale or invalid functionBody/functionHeaders left in the block (common when switching operations) were parsed and validated for every operation, so they could throw before unrelated tools ran even while hidden. Moved parsing and validation inside the invoke_function guard; added a regression test. * fix(supabase): include last_accessed_at as a storage list sort option The Storage list API accepts last_accessed_at for sortBy; add it to the tool description and the block dropdown so the surfaced options match the API.
1 parent 11e2313 commit c907b11

20 files changed

Lines changed: 490 additions & 162 deletions

File tree

apps/sim/app/api/tools/supabase/storage-upload/route.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
174174
'Content-Type': uploadContentType,
175175
}
176176

177+
if (validatedData.cacheControl) {
178+
const cacheControl = validatedData.cacheControl.trim()
179+
headers['cache-control'] = /^\d+$/.test(cacheControl)
180+
? `max-age=${cacheControl}`
181+
: cacheControl
182+
}
183+
177184
if (validatedData.upsert) {
178185
headers['x-upsert'] = 'true'
179186
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { describe, expect, it } from 'vitest'
5+
import { SupabaseBlock } from '@/blocks/blocks/supabase'
6+
7+
describe('SupabaseBlock', () => {
8+
const buildParams = SupabaseBlock.tools.config.params!
9+
const selectTool = SupabaseBlock.tools.config.tool!
10+
11+
it('maps each operation to its tool id', () => {
12+
expect(selectTool({ operation: 'query' })).toBe('supabase_query')
13+
expect(selectTool({ operation: 'invoke_function' })).toBe('supabase_invoke_function')
14+
expect(selectTool({ operation: 'delete' })).toBe('supabase_delete')
15+
})
16+
17+
it('does not leak the Edge Function method onto other operations', () => {
18+
// A stale `method` from the Edge Function field must never reach a tool with a
19+
// static verb — otherwise the executor would let it override e.g. GET with DELETE.
20+
const params = buildParams({
21+
operation: 'query',
22+
projectId: 'proj',
23+
apiKey: 'key',
24+
table: 'users',
25+
method: 'DELETE',
26+
})
27+
28+
expect(params).not.toHaveProperty('method')
29+
expect(params).not.toHaveProperty('body')
30+
expect(params).not.toHaveProperty('headers')
31+
})
32+
33+
it('ignores stale invalid Edge Function fields on other operations', () => {
34+
// Hidden Edge Function inputs left over from a prior selection must not be
35+
// parsed/validated (and must never throw) for unrelated operations.
36+
expect(() =>
37+
buildParams({
38+
operation: 'query',
39+
projectId: 'proj',
40+
apiKey: 'key',
41+
table: 'users',
42+
functionBody: '{not valid json',
43+
functionHeaders: '["a","b"]',
44+
})
45+
).not.toThrow()
46+
})
47+
48+
it('passes method, body, and headers through for invoke_function', () => {
49+
const params = buildParams({
50+
operation: 'invoke_function',
51+
projectId: 'proj',
52+
apiKey: 'key',
53+
functionName: 'hello-world',
54+
method: 'POST',
55+
functionBody: '{"name":"world"}',
56+
functionHeaders: '{"x-trace":"1"}',
57+
})
58+
59+
expect(params.method).toBe('POST')
60+
expect(params.body).toEqual({ name: 'world' })
61+
expect(params.headers).toEqual({ 'x-trace': '1' })
62+
})
63+
64+
it('rejects non-object Edge Function headers', () => {
65+
expect(() =>
66+
buildParams({
67+
operation: 'invoke_function',
68+
projectId: 'proj',
69+
apiKey: 'key',
70+
functionName: 'hello-world',
71+
functionHeaders: '["a","b"]',
72+
})
73+
).toThrow('Edge Function headers must be a JSON object')
74+
})
75+
})

0 commit comments

Comments
 (0)