diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CreateCronJobSheet.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CreateCronJobSheet.tsx index 5715c020b99..3f36ac1fe8e 100644 --- a/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CreateCronJobSheet.tsx +++ b/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CreateCronJobSheet.tsx @@ -258,9 +258,11 @@ export const CreateCronJobSheet = ({ open, selectedCronJob, onClose }: CreateCro endpoint, snippet: '', }) - if (command) { - form.setValue('values.snippet', command) - } + if (!command) return + + // Keep the stored command until a field changes + const isStoredCommand = isEditing && command === buildCommand(cronJobValues) + form.setValue('values.snippet', isStoredCommand ? cronJobValues.snippet : command) // eslint-disable-next-line react-hooks/exhaustive-deps }, [ cronType, diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.test.ts b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.test.ts index 22873fd273e..fb6f222c7a6 100644 --- a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.test.ts +++ b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.test.ts @@ -65,6 +65,26 @@ describe('parseCronJobCommand', () => { case: 'an HTTP request in positional notation', command: `SELECT net.http_post( 'https://webhook.site/dacc2028-a588-462c-9597-c8968e61d0fa', '{"message":"Hello from Supabase"}'::jsonb, '{}'::jsonb, '{"Content-Type":"application/json"}'::jsonb );`, }, + { + case: 'an HTTP request whose header value is a SQL expression', + command: `select net.http_post( url:='https://random_project_ref.supabase.co/functions/v1/_', headers:=jsonb_build_object('Authorization', 'Bearer ' || (SELECT decrypted_secret FROM vault.decrypted_secrets WHERE name = 'secret')), body:='{}'::jsonb, timeout_milliseconds:=5000 );`, + }, + { + case: 'an HTTP request with concatenated header objects', + command: `select net.http_post( url:='https://example.com/api', headers:=jsonb_build_object('a', '1') || jsonb_build_object('b', to_char(now(), 'YYYY')), timeout_milliseconds:=5000 );`, + }, + { + case: 'an HTTP request with a non-literal body', + command: `select net.http_post( url:='https://example.com/api', body:=jsonb_build_object('at', now()), timeout_milliseconds:=5000 );`, + }, + { + case: 'an HTTP request with an argument the form does not support', + command: `select net.http_get( url:='https://example.com/api', params:=jsonb_build_object('a', '1') );`, + }, + { + case: 'an HTTP request followed by another statement', + command: `select net.http_get( url:='https://example.com/api' ); select 1;`, + }, ])('falls back to a sql snippet for $case', ({ command }) => { expect(parseCronJobCommand(command, REF)).toStrictEqual({ type: 'sql_snippet', diff --git a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.tsx b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.tsx index 7a6cd3a55a0..84ea14245a3 100644 --- a/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.tsx +++ b/apps/studio/components/interfaces/Integrations/CronJobs/CronJobs.utils.tsx @@ -28,27 +28,22 @@ const unwrapSqlLiteral = (token: string) => { } /** - * Splits the argument list of a `jsonb_build_object(...)` call into its individual - * values, honoring single-quoted SQL string literals (with '' escapes) and nested - * parentheses. A naive split on ',' corrupts a header name or value that legitimately - * contains a comma or parenthesis, which then gets persisted on save and no longer - * matches what the user entered. + * Splits a SQL argument list starting after its `(`, respecting quotes and nested parentheses. + * Returns the raw arguments and the index of the closing `)`, or undefined if unbalanced. */ -const parseJsonBuildObjectArgs = (command: string) => { - const match = command.match(/headers:=jsonb_build_object\s*\(/i) - if (!match || match.index === undefined) return [] - +const splitSqlArgs = (sql: string, start: number) => { const args: string[] = [] let current = '' let depth = 1 let inQuote = false let hasContent = false + let i = start - for (let i = match.index + match[0].length; i < command.length && depth > 0; i++) { - const char = command[i] + for (; i < sql.length; i++) { + const char = sql[i] if (inQuote) { - if (char === "'" && command[i + 1] === "'") { + if (char === "'" && sql[i + 1] === "'") { current += "''" i++ continue @@ -67,7 +62,8 @@ const parseJsonBuildObjectArgs = (command: string) => { current += char } else if (char === ')') { depth-- - if (depth > 0) current += char + if (depth === 0) break + current += char } else if (char === ',' && depth === 1) { args.push(current) current = '' @@ -77,12 +73,60 @@ const parseJsonBuildObjectArgs = (command: string) => { } } - // Unbalanced parentheses: bail rather than emit mangled fragments. - if (depth !== 0) return [] + if (depth !== 0) return undefined if (hasContent || args.length > 0) args.push(current) - return args.map(unwrapSqlLiteral) + return { args, end: i } +} + +const parseJsonBuildObjectArgs = (command: string) => { + const match = command.match(/headers:=jsonb_build_object\s*\(/i) + if (!match || match.index === undefined) return [] + + const result = splitSqlArgs(command, match.index + match[0].length) + return result ? result.args.map(unwrapSqlLiteral) : [] +} + +const SQL_STRING_LITERAL = /^e?'(?:''|[^'])*'$/i +const SQL_STRING_LITERAL_WITH_CAST = /^e?'(?:''|[^'])*'(?:\s*::\s*(?:jsonb|json|text))?$/i + +const isJsonBuildObjectOfLiterals = (value: string) => { + const match = value.match(/^jsonb_build_object\s*\(/i) + if (!match) return false + const result = splitSqlArgs(value, match[0].length) + if (!result || value.slice(result.end + 1).trim().length > 0) return false + return result.args.every((arg) => SQL_STRING_LITERAL.test(arg.trim())) +} + +// The HTTP form rebuilds the command from its fields, so anything else is edited as a SQL snippet +const isFormRepresentableHttpCommand = (command: string) => { + const match = command.match(/^select\s+net\.(http_get|http_post)\s*\(/i) + if (!match) return false + + const result = splitSqlArgs(command, match[0].length) + if (!result) return false + if (!/^\s*;?\s*$/.test(command.slice(result.end + 1))) return false + + return result.args.every((arg) => { + const namedArg = arg.trim().match(/^([a-z_]+)\s*:=\s*([\s\S]*)$/i) + if (!namedArg) return false + const name = namedArg[1].toLowerCase() + const value = namedArg[2].trim() + + switch (name) { + case 'url': + return SQL_STRING_LITERAL.test(value) + case 'body': + return SQL_STRING_LITERAL_WITH_CAST.test(value) + case 'headers': + return SQL_STRING_LITERAL_WITH_CAST.test(value) || isJsonBuildObjectOfLiterals(value) + case 'timeout_milliseconds': + return /^\d+$/.test(value) + default: + return false + } + }) } export function buildCronCreateQuery( @@ -142,7 +186,10 @@ export const parseCronJobCommand = ( ): CronJobType => { const command = originalCommand.replaceAll('$$', ' ').replaceAll(/\n/g, ' ').trim() - if (command.toLocaleLowerCase().match(/^select\s+net\./)) { + if ( + command.toLocaleLowerCase().match(/^select\s+net\./) && + isFormRepresentableHttpCommand(command) + ) { const methodMatch = command.match(/select\s+net\.([^']+)\(\s*url:=/i) const method = methodMatch?.[1] || ''