Skip to content

fix(rls): resolve policy table matching and fix Vitest test suite (#71) - #318

Open
MilindChatrabhuji wants to merge 1 commit into
outerbase:mainfrom
MilindChatrabhuji:fix/rls-table-matching-and-tests-71
Open

MilindChatrabhuji wants to merge 1 commit into
outerbase:mainfrom
MilindChatrabhuji:fix/rls-table-matching-and-tests-71

Conversation

@MilindChatrabhuji

Copy link
Copy Markdown

Purpose

Fixes #71 (Bounty claim: @algora-pbc /claim #71)

Currently on main, running pnpm test fails with 4 broken tests in src/rls/index.test.ts due to schema-qualified policy table matching and nested subquery traversal limitations.

This PR resolves the RLS policy table matching and repairs the test suite so that all 19 Vitest test suites across the repository pass 100% cleanly.

Key Changes

  1. Schema & Identifier Matching (src/rls/index.ts):

    • Added tableNamesMatch helper to match schema-qualified policy tables (e.g. public.users) against query table references (users), respecting stripped or explicit schemas.
    • Formats condition left.table appropriately based on single-table vs multi-table queries (ensuring single table queries omit redundant table prefixes while multi-table/JOIN queries preserve table/alias qualification).
  2. Nested Subquery Traversal:

    • Traverses nested subqueries in from, join, and columns by inspecting expr.ast (the actual AST node generated by node-sql-parser).
  3. Test Suite Isolation & Expectations (src/rls/index.test.ts):

    • Ensured mockConfig.role = 'client' in beforeEach to prevent test-order mutation pollution from admin edge case tests.
    • Verified individual assertions for SELECT, DELETE, UPDATE, INSERT, multi-table JOINs, aliased JOINs, and subqueries.

Verification

  • npx vitest run executes all 19 test files with 155 of 155 tests passing:
    Test Files  19 passed (19)
    Tests       155 passed (155)
    
  • Formatted with Prettier (npx prettier --check "src/rls/index.ts" "src/rls/index.test.ts").

…terbase#71)

- Added `tableNamesMatch` helper in `src/rls/index.ts` to accurately match schema-qualified policy tables (e.g. `public.users`) against query table references (`users`).
- Support nested subqueries in `from`, `join`, and `columns` by inspecting `expr.ast`.
- Fixed multi-table query condition formatting and test isolation in `src/rls/index.test.ts`.
- All 19 Vitest test suites (155 tests) now pass 100% cleanly.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve test coverage with Vitest

1 participant