Skip to content

supabase db diff migration files does not include view with (security_invoker=on) clause  #792

Closed

Description

@simbas

Bug report

Describe the bug

the diff tool does not add the with (security_invoker=on) clause for views created with this clause, this omission can cause security issues.

To Reproduce

in the supabase studio sql editor, create a table with RLS enabled, insert some data, and create a view with security invoker on:

create table test (
  name text
);

alter table "test" enable row level security;

insert into test values ('test1');
insert into test values ('test2');

create view view_with_security_invoker_on with (security_invoker=on) as select
name from test

you get the following result when calling the view with an anon key:

[]

then run migration and reset:

supabase db diff -f create_ view_with_security_invoker_on
supabase db reset

insert back the data (deleted during the reset) in the sql editor:

insert into test values ('test1');
insert into test values ('test2');

you get the following result when calling again the view with an anon key:

[{"name":"test1"}, 
 {"name":"test2"}]

the creation of the view in the migration file is done without the with (security_invoker=on) clause:

create table "public"."test" (
    "name" text
);


alter table "public"."test" enable row level security;

create or replace view "public"."view_with_security_invoker_on" as  SELECT test.name
   FROM test;



Expected behavior

migration file should include the with (security_invoker=on) clause for views created with with (security_invoker=on).

System information

  • OS: macOS
  • Version of supabase cli: 1.33.0
  • Version of Node.js: 16.19.0

Activity

  1. added
    bug
    on Jan 14, 2023
  2. gilbert commented on May 11, 2023

    @gilbert

    Just ran into this. Glad I checked, this is a subtle security hazard.

  3. SamuraiT commented on Feb 2, 2024

    @SamuraiT

    The same thing happens to me

  4. Palabola commented on Feb 21, 2024

    @Palabola

    Same here. The update almost got live. Fix would be much appreciated.

  5. daroczig commented on Feb 21, 2024

    @daroczig

    We have also encountered this and accidentally caught the problem while applying the auto-generated migration files to staging. Note that this is not only a bug, but a potentially serious security risk if a view gets into prod without security_invoker=on unintentionally 😞

  6. gabriel-jones commented on May 14, 2024

    @gabriel-jones

    We also encountered this. It's annoying as it means we have to replace all our views after migrations run. Please look into this as it's a high priority security issue.

  7. yannis216 commented on Jul 26, 2024

    @yannis216

    Also (still) running into this security risk :-/

  8. tvogel commented on Aug 7, 2024

    @tvogel

    As the supabase linter currently recommends WITH (security_invoker = on) as a remedy for implicit SECURITY DEFINER views, see https://supabase.com/docs/guides/database/database-advisors?lint=0010_security_definer_view#how-to-resolve, I think, supabase db diff should really support this now.

    What is the priority / timeline of this?

  9. sweatybridge commented on Aug 7, 2024

    @sweatybridge
    Contributor

    What is the priority / timeline of this?

    It's hard to say... we are not really keen to fork and maintain our own version of migra, as it may end up being abandoned like pgadmin-schema-diff.

    If anyone has the time, I'd recommend trying out and contributing to a new diff tool from stripe which is more actively maintained https://github.com/stripe/pg-schema-diff.

    It takes a lot of work build a perfect schema diff tool. But if we can focus on one, I think it will be a huge win for the postgres community.

  10. How-Sustainable commented on Aug 29, 2024

    @How-Sustainable

    If you have large number of views to invoke the security on, running this script on your db will find all views with the schema 'public' and invoke the security.

    DO $$ 
    DECLARE 
        view_rec RECORD;
        view_def TEXT;
    BEGIN
        -- Loop through all views in the 'public' schema
        FOR view_rec IN 
            SELECT n.nspname as table_schema,
                   c.relname as table_name,
                   pg_get_viewdef(c.oid) as view_definition
            FROM pg_class c
            JOIN pg_namespace n ON n.oid = c.relnamespace
            JOIN pg_roles r ON r.oid = c.relowner
            WHERE c.relkind = 'v'  -- Only views
              AND n.nspname = 'public'  -- Only in the 'public' schema
              AND r.rolname = current_user  -- Only process views owned by the current user
        LOOP
            -- Recreate the view with security_invoker=on
            EXECUTE format('
                CREATE OR REPLACE VIEW %I.%I WITH (security_invoker=on) AS
                %s', 
                view_rec.table_schema, 
                view_rec.table_name, 
                view_rec.view_definition);
        END LOOP;
    END $$;
    
  11. alon-z commented on May 18, 2025

    @alon-z

    Bug still exists

  12. teamclouday commented on Jun 18, 2025

    @teamclouday

    This bug still exists. I think it would be helpful to add a warning about view not being fully supported. Like how it's currently doing for drop commands

  13. peterHoburg commented on Nov 17, 2025

    @peterHoburg

    We would really love a fix for this.

  14. hardchor commented on Nov 25, 2025

    @hardchor

    Weirdly, this just started resurfacing for me with an update to supabase CLI (thankfully we caught it in time).

    @sweatybridge have you recently switched to pg-schema-diff? Or what might've caused this?

  15. jstettner commented on Dec 11, 2025

    @jstettner

    Does anyone have a workaround for this so that I don't have to continuously remove the drop / recreate incorrectly from the generated diffs?

  16. jgaull commented on Jan 10, 2026

    @jgaull

    I have run into this issue as well. I understand that the fix is to manually edit the generated migration file with with (security_invoker = on). However, if I apply that migration to the database and then run the diff tool again it is detecting a difference between the schema in my schemas folder and what's actually in the database and will generate another migration without with (security_invoker = on). This gets me into a place where I have to manually delete the create or replace view from every subsequent migration.

    Is anybody else seeing this? What is the solution?

  17. sweatybridge commented on Jan 10, 2026

    @sweatybridge
    Contributor

    The solution is to try using a different diff tool.

    supabase db diff --use-pg-delta

    This newly added flag is highly experimental and only works with the local database at the moment.

  18. jgaull commented on Jan 13, 2026

    @jgaull

    The solution is to try using a different diff tool.

    supabase db diff --use-pg-delta
    This newly added flag is highly experimental and only works with the local database at the moment.

    Thank you @sweatybridge! This worked for me. I look forward to being a tester 🙂

  19. sweatybridge commented on Jan 13, 2026

    @sweatybridge
    Contributor

    Glad to hear that. If you run into any issues, feel free to report them on here https://github.com/supabase/pg-delta

    Our team will look into them promptly.

  20. 2754github commented on Feb 10, 2026

    @2754github

    Honestly, this issue became the deciding factor for me to move away from Supabase, which I've loved for years. I'm deeply disappointed.

    Despite your documentation recommending RLS and Views as a security pattern, this critical security issue remains unresolved.

    This raises serious concerns about Supabase's development priorities and makes me question whether it's safe to trust my production infrastructure to this platform.

  21. sweatybridge commented on Feb 10, 2026

    @sweatybridge
    Contributor

    Hi @2754github, I'm sorry to hear about your experiences. We have actually shipped a new diff tool that captures RLS on views #792 (comment)

    It is not the default option currently because we are still improving its stability. But eventually it will replace migra as the default option.

    Have you given pg-delta a try? Let me know if the command works for you. We are committed to making your local development environment secure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmigration

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions