The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: lint integration for pg_graphql introspection + SECURITY DEFINER functions (#45260)
samrose4afbe9c2b2ee
diff --git a/apps/studio/components/interfaces/Linter/Linter.utils.tsx b/apps/studio/components/interfaces/Linter/Linter.utils.tsxindex 50f809381ef..9abb5bdb6b6 100644--- a/apps/studio/components/interfaces/Linter/Linter.utils.tsx+++ b/apps/studio/components/interfaces/Linter/Linter.utils.tsx@@ -334,7 +334,7 @@ export const lintInfoMap: LintInfo[] = [},{name: 'pg_graphql_anon_table_exposed',- title: 'pg_graphql Anon Role Exposes Objects in Introspection',+ title: 'Public Can See Object in GraphQL Schema',icon: <Eye className="text-foreground-muted" size={15} strokeWidth={1.5} />,link: ({ projectRef, metadata }) =>`/project/${projectRef}/editor?schema=${metadata?.schema}&table=${metadata?.name}`,@@ -342,6 +342,36 @@ export const lintInfoMap: LintInfo[] = [docsLink: `${DOCS_URL}/guides/database/database-linter?lint=0026_pg_graphql_anon_table_exposed`,category: 'security',},+ {+ name: 'pg_graphql_authenticated_table_exposed',+ title: 'Signed-In Users Can See Object in GraphQL Schema',+ icon: <Eye className="text-foreground-muted" size={15} strokeWidth={1.5} />,+ link: ({ projectRef, metadata }) =>+ `/project/${projectRef}/editor?schema=${metadata?.schema}&table=${metadata?.name}`,+ linkText: 'View object',+ docsLink: `${DOCS_URL}/guides/database/database-linter?lint=0027_pg_graphql_authenticated_table_exposed`,+ category: 'security',+ },+ {+ name: 'anon_security_definer_function_executable',+ title: 'Public Can Execute SECURITY DEFINER Function',+ icon: <LockIcon className="text-foreground-muted" size={15} strokeWidth={1} />,+ link: ({ projectRef, metadata }) =>+ `/project/${projectRef}/database/functions?schema=${metadata?.schema}&search=${metadata?.name}`,+ linkText: 'View function',+ docsLink: `${DOCS_URL}/guides/database/database-linter?lint=0028_anon_security_definer_function_executable`,+ category: 'security',+ },+ {+ name: 'authenticated_security_definer_function_executable',+ title: 'Signed-In Users Can Execute SECURITY DEFINER Function',+ icon: <LockIcon className="text-foreground-muted" size={15} strokeWidth={1} />,+ link: ({ projectRef, metadata }) =>+ `/project/${projectRef}/database/functions?schema=${metadata?.schema}&search=${metadata?.name}`,+ linkText: 'View function',+ docsLink: `${DOCS_URL}/guides/database/database-linter?lint=0029_authenticated_security_definer_function_executable`,+ category: 'security',+ },]export const LintCTA = ({@@ -385,12 +415,7 @@ export const EntityTypeIcon = ({ type }: { type: string | undefined }) => {}export const LintEntity = ({ metadata }: { metadata: Lint['metadata'] }) => {- return (- (metadata &&- (metadata.entity ||- (metadata.schema && metadata.name && `${metadata.schema}.${metadata.name}`))) ??- undefined- )+ return getLintEntityString(metadata)}export const LintCategoryBadge = ({ category }: { category: string }) => {@@ -418,13 +443,7 @@ export const NoIssuesFound = ({ level }: { level: string }) => {export const createLintSummaryPrompt = (lint: Lint) => {const title = lintInfoMap.find((item) => item.name === lint.name)?.title ?? lint.title- const entity =- (lint.metadata &&- (lint.metadata.entity ||- (lint.metadata.schema &&- lint.metadata.name &&- `${lint.metadata.schema}.${lint.metadata.name}`))) ||- 'N/A'+ const entity = getLintEntityString(lint.metadata) || 'N/A'const schema = lint.metadata?.schema ?? 'N/A'const issue = lint.detail ? lint.detail.replace(/\\`/g, '`') : 'N/A'const description = lint.description ? lint.description.replace(/\\`/g, '`') : 'N/A'@@ -435,3 +454,22 @@ Schema: ${schema}Issue Details: ${issue}Description: ${description}`}++export const getLintEntityString = (metadata: Lint['metadata']) => {+ if (!metadata) {+ return undefined+ }++ if (metadata.entity) {+ return metadata.entity+ }++ if (metadata.schema && metadata.name) {+ const extendedMetadata = metadata as typeof metadata & { arguments?: string }+ const args =+ typeof extendedMetadata.arguments === 'string' ? extendedMetadata.arguments : undefined+ return `${metadata.schema}.${metadata.name}${args !== undefined ? `(${args})` : ''}`+ }++ return undefined+}diff --git a/apps/studio/components/ui/AdvisorPanel/AdvisorPanel.utils.ts b/apps/studio/components/ui/AdvisorPanel/AdvisorPanel.utils.tsindex 61e35f26298..dc0f8a9a765 100644--- a/apps/studio/components/ui/AdvisorPanel/AdvisorPanel.utils.ts+++ b/apps/studio/components/ui/AdvisorPanel/AdvisorPanel.utils.ts@@ -176,7 +176,10 @@ export const getLintEntityString = (lint: Lint | null): string | undefined => {}if (lint.metadata.schema && lint.metadata.name) {- return `${lint.metadata.schema}.${lint.metadata.name}`+ const extendedMetadata = lint.metadata as typeof lint.metadata & { arguments?: string }+ const args =+ typeof extendedMetadata.arguments === 'string' ? extendedMetadata.arguments : undefined+ return `${lint.metadata.schema}.${lint.metadata.name}${args !== undefined ? `(${args})` : ''}`}return undefineddiff --git a/packages/pg-meta/src/sql/studio/advisor/lints.ts b/packages/pg-meta/src/sql/studio/advisor/lints.tsindex 0dfa9d2ac38..e5309f7271f 100644--- a/packages/pg-meta/src/sql/studio/advisor/lints.ts+++ b/packages/pg-meta/src/sql/studio/advisor/lints.ts@@ -1522,31 +1522,39 @@ exposed_objects as (c.relkind as object_relkind,case c.relkindwhen 'r' then 'table'- when 'p' then 'table'when 'v' then 'view'when 'm' then 'materialized view'+ when 'f' then 'foreign table'end as object_typefrompg_catalog.pg_class cjoin pg_catalog.pg_namespace non c.relnamespace = n.oidwhere- c.relkind in ('r', 'p', 'v', 'm') -- tables, partitioned tables, views, materialized views- and pg_catalog.has_table_privilege('anon', c.oid, 'SELECT')+ c.relkind in ('r', 'v', 'm', 'f') -- tables, views, materialized views, foreign tables; matches pg_graphql+ and exists (+ -- Any selectable column (table-level or column-level grant)+ select 1+ from pg_catalog.pg_attribute a+ where a.attrelid = c.oid+ and a.attnum > 0+ and not a.attisdropped+ and pg_catalog.has_column_privilege('anon', c.oid, a.attnum, 'SELECT')+ )and exists (select 1 from graphql_installed)and n.nspname not in (- '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'pgtle', 'realtime', 'repack', 'storage', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'+ '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'realtime', 'repack', 'storage', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'))select'pg_graphql_anon_table_exposed' as name,- 'pg_graphql Anon Role Exposes Objects in Introspection' as title,+ 'Public Can See Object in GraphQL Schema' as title,'WARN' as level,'EXTERNAL' as facing,array['SECURITY'] as categories,- 'Detects tables and views whose schema is visible via the public \`/graphql/v1\` introspection endpoint. When \`pg_graphql\` is installed, any table, view, or materialized view the \`anon\` role has \`SELECT\` on is visible in introspection — names, columns, types, and relationships — even when RLS is enabled. Both forms of introspection are intentional behavior this lint surfaces the objects that are currently public so you can confirm each one is meant to be discoverable without authentication.' as description,+ 'Detects tables, views, materialized views, and foreign tables that are visible in the GraphQL schema to anyone using your public anon key. Revoke \`SELECT\` from \`anon\` for objects that should not be discoverable before sign-in, and check lint 0027 for the matching signed-in-user exposure.' as description,format(- 'Extension \`pg_graphql\` is installed and the \`anon\` role has \`SELECT\` on %s \`%s.%s\`. Its name, columns, and relationships are visible via the public \`/graphql/v1\` introspection endpoint.',+ '%s \`%s.%s\` is visible in the GraphQL schema because the \`anon\` role can \`SELECT\` it. Revoke \`SELECT\` from \`anon\` if it should not be discoverable without signing in.',object_type,schema_name,object_name@@ -1566,4 +1574,181 @@ fromexposed_objectsorder byschema_name,- object_name)`+ object_name)+union all+(+with graphql_installed as (+ select 1 as installed+ from pg_catalog.pg_extension+ where extname = 'pg_graphql'+),+exposed_objects as (+ select+ n.nspname as schema_name,+ c.relname as object_name,+ c.relkind as object_relkind,+ case c.relkind+ when 'r' then 'table'+ when 'v' then 'view'+ when 'm' then 'materialized view'+ when 'f' then 'foreign table'+ end as object_type+ from+ pg_catalog.pg_class c+ join pg_catalog.pg_namespace n+ on c.relnamespace = n.oid+ where+ c.relkind in ('r', 'v', 'm', 'f') -- tables, views, materialized views, foreign tables; matches pg_graphql+ and exists (+ -- Any selectable column (table-level or column-level grant)+ select 1+ from pg_catalog.pg_attribute a+ where a.attrelid = c.oid+ and a.attnum > 0+ and not a.attisdropped+ and pg_catalog.has_column_privilege('authenticated', c.oid, a.attnum, 'SELECT')+ )+ and exists (select 1 from graphql_installed)+ and n.nspname not in (+ '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'realtime', 'repack', 'storage', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'+ )+)+select+ 'pg_graphql_authenticated_table_exposed' as name,+ 'Signed-In Users Can See Object in GraphQL Schema' as title,+ 'WARN' as level,+ 'EXTERNAL' as facing,+ array['SECURITY'] as categories,+ 'Detects tables, views, materialized views, and foreign tables that are visible in the GraphQL schema to signed-in users. Revoke \`SELECT\` from \`authenticated\` for objects that signed-in users should not discover, and check lint 0026 for the matching public exposure.' as description,+ format(+ '%s \`%s.%s\` is visible in the GraphQL schema to signed-in users because the \`authenticated\` role can \`SELECT\` it. Revoke \`SELECT\` from \`authenticated\` if it should not be discoverable to every account.',+ object_type,+ schema_name,+ object_name+ ) as detail,+ ${literal(`${docsUrl}/guides/database/database-linter?lint=0027_pg_graphql_authenticated_table_exposed`)} as remediation,+ jsonb_build_object(+ 'schema', schema_name,+ 'name', object_name,+ 'type', object_type+ ) as metadata,+ format(+ 'pg_graphql_authenticated_table_exposed_%s_%s',+ schema_name,+ object_name+ ) as cache_key+from+ exposed_objects+order by+ schema_name,+ object_name)+union all+(+select+ 'anon_security_definer_function_executable' as name,+ 'Public Can Execute SECURITY DEFINER Function' as title,+ 'WARN' as level,+ 'EXTERNAL' as facing,+ array['SECURITY'] as categories,+ 'Detects \`SECURITY DEFINER\` functions that are callable without signing in. Revoke \`EXECUTE\`, switch the function to \`SECURITY INVOKER\`, or move it out of your exposed API schema if it is not meant to be public.' as description,+ format(+ 'Function \`%s.%s(%s)\` can be executed by the \`anon\` role as a \`SECURITY DEFINER\` function via \`/rest/v1/rpc/%s\`. Revoke \`EXECUTE\` or switch it to \`SECURITY INVOKER\` if that is not intentional.',+ schema_name,+ function_name,+ function_args,+ function_name+ ) as detail,+ ${literal(`${docsUrl}/guides/database/database-linter?lint=0028_anon_security_definer_function_executable`)} as remediation,+ jsonb_build_object(+ 'schema', schema_name,+ 'name', function_name,+ 'arguments', function_args,+ 'language', function_language,+ 'security_definer', true+ ) as metadata,+ format(+ 'anon_security_definer_function_executable_%s_%s_%s',+ schema_name,+ function_name,+ function_args+ ) as cache_key+from+ (+ select+ n.nspname as schema_name,+ p.proname as function_name,+ pg_catalog.pg_get_function_identity_arguments(p.oid) as function_args,+ l.lanname as function_language+ from+ pg_catalog.pg_proc p+ join pg_catalog.pg_namespace n+ on p.pronamespace = n.oid+ join pg_catalog.pg_language l+ on p.prolang = l.oid+ where+ p.prosecdef = true+ and pg_catalog.has_function_privilege('anon', p.oid, 'EXECUTE')+ and n.nspname = any(array(select trim(unnest(string_to_array(current_setting('pgrst.db_schemas', 't'), ',')))))+ and n.nspname not in (+ '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'realtime', 'repack', 'storage', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'+ )+ ) exposed_functions+order by+ schema_name,+ function_name,+ function_args)+union all+(+select+ 'authenticated_security_definer_function_executable' as name,+ 'Signed-In Users Can Execute SECURITY DEFINER Function' as title,+ 'WARN' as level,+ 'EXTERNAL' as facing,+ array['SECURITY'] as categories,+ 'Detects \`SECURITY DEFINER\` functions that are callable by signed-in users. Revoke \`EXECUTE\`, switch the function to \`SECURITY INVOKER\`, or move it out of your exposed API schema if signed-in users should not call it.' as description,+ format(+ 'Function \`%s.%s(%s)\` can be executed by the \`authenticated\` role as a \`SECURITY DEFINER\` function via \`/rest/v1/rpc/%s\`. Revoke \`EXECUTE\` or switch it to \`SECURITY INVOKER\` if that is not intentional.',+ schema_name,+ function_name,+ function_args,+ function_name+ ) as detail,+ ${literal(`${docsUrl}/guides/database/database-linter?lint=0029_authenticated_security_definer_function_executable`)} as remediation,+ jsonb_build_object(+ 'schema', schema_name,+ 'name', function_name,+ 'arguments', function_args,+ 'language', function_language,+ 'security_definer', true+ ) as metadata,+ format(+ 'authenticated_security_definer_function_executable_%s_%s_%s',+ schema_name,+ function_name,+ function_args+ ) as cache_key+from+ (+ select+ n.nspname as schema_name,+ p.proname as function_name,+ pg_catalog.pg_get_function_identity_arguments(p.oid) as function_args,+ l.lanname as function_language+ from+ pg_catalog.pg_proc p+ join pg_catalog.pg_namespace n+ on p.pronamespace = n.oid+ join pg_catalog.pg_language l+ on p.prolang = l.oid+ where+ p.prosecdef = true+ and pg_catalog.has_function_privilege('authenticated', p.oid, 'EXECUTE')+ and n.nspname = any(array(select trim(unnest(string_to_array(current_setting('pgrst.db_schemas', 't'), ',')))))+ and n.nspname not in (+ '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'realtime', 'repack', 'storage', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'+ )+ ) exposed_functions+order by+ schema_name,+ function_name,+ function_args)`