1- import { createLogger } from '@/lib/logs/console/logger'
1+ import { createLogger } from '@sim/logger'
2+ import { sanitizeSqlIdentifier } from '@/lib/core/security/input-validation'
23import type {
34 SupabaseColumnSchema ,
45 SupabaseIntrospectParams ,
@@ -150,14 +151,16 @@ SELECT json_build_object(
150151/**
151152 * SQL query filtered by specific schema
152153 */
153- const getSchemaFilteredSQL = ( schema : string ) => `
154+ const getSchemaFilteredSQL = ( schema : string ) => {
155+ const safeSchema = sanitizeSqlIdentifier ( schema )
156+ return `
154157WITH table_info AS (
155158 SELECT
156159 t.table_schema,
157160 t.table_name
158161 FROM information_schema.tables t
159162 WHERE t.table_type = 'BASE TABLE'
160- AND t.table_schema = '${ schema } '
163+ AND t.table_schema = '${ safeSchema } '
161164),
162165columns_info AS (
163166 SELECT
@@ -181,7 +184,7 @@ pk_info AS (
181184 ON tc.constraint_name = kcu.constraint_name
182185 AND tc.table_schema = kcu.table_schema
183186 WHERE tc.constraint_type = 'PRIMARY KEY'
184- AND tc.table_schema = '${ schema } '
187+ AND tc.table_schema = '${ safeSchema } '
185188),
186189fk_info AS (
187190 SELECT
@@ -197,7 +200,7 @@ fk_info AS (
197200 JOIN information_schema.constraint_column_usage ccu
198201 ON ccu.constraint_name = tc.constraint_name
199202 WHERE tc.constraint_type = 'FOREIGN KEY'
200- AND tc.table_schema = '${ schema } '
203+ AND tc.table_schema = '${ safeSchema } '
201204),
202205index_info AS (
203206 SELECT
@@ -207,7 +210,7 @@ index_info AS (
207210 CASE WHEN indexdef LIKE '%UNIQUE%' THEN true ELSE false END AS is_unique,
208211 indexdef
209212 FROM pg_indexes
210- WHERE schemaname = '${ schema } '
213+ WHERE schemaname = '${ safeSchema } '
211214)
212215SELECT json_build_object(
213216 'tables', (
@@ -285,6 +288,7 @@ SELECT json_build_object(
285288 )
286289) AS result;
287290`
291+ }
288292
289293/**
290294 * Tool for introspecting Supabase database schema
0 commit comments