Skip to content

Commit

Permalink
fix: skip empty context
Browse files Browse the repository at this point in the history
  • Loading branch information
kltk committed Jul 29, 2024
1 parent c6ff527 commit 273da48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions __tests__/rls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('rls extension', () => {
.resolves.toEqual([]);
});

test('denied with empty context', async () => {
const model = client.$rls([]).obj;
await expect(model.findMany()) //
.rejects.toThrow(/denied/);
});

test('allow with custom context', async () => {
const model = client.$rls(context).obj;
await expect(model.findMany()) //
Expand Down
7 changes: 5 additions & 2 deletions prisma/migrations/0000_init/migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ ALTER TABLE "Obj" ENABLE ROW LEVEL SECURITY;
-- Force Row Level Security for table owners
ALTER TABLE "Obj" FORCE ROW LEVEL SECURITY;

-- Create role for not exist
-- Create role for not exist (mock supabase)
-- CREATE ROLE authenticator WITH NOINHERIT LOGIN;
-- CREATE ROLE authenticated;
-- Grant `set role authenticated` perm to connected role(authenticator)
-- GRANT authenticated to authenticator;
-- GRANT ALL PRIVILEGES ON TABLE "Obj" TO authenticated;


-- Create row security policies
CREATE POLICY supabase_policy ON "Obj" TO "authenticated" USING ("uid" = (current_setting('request.jwt.claims', TRUE)::jsonb ->> 'sub')::uuid);
CREATE POLICY supabase_policy ON "Obj" TO "authenticated" USING ("uid" = (nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub')::uuid);
4 changes: 4 additions & 0 deletions src/prisma-extension-rls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function createContext(initial: ContextData) {
};

const merged = mergeContext(initial, context);
if (!merged.length) {
return query(rest);
}

const sqlParts = merged.map(
([k, v]) => Prisma.sql`SET_CONFIG(${k}, ${stringify(v)}, TRUE)`
);
Expand Down

0 comments on commit 273da48

Please sign in to comment.