Commit 4e5b464
authored
## Summary
- Implements platform-independent row-level security in the Migration
framework — single YAML schema produces native `CREATE POLICY` on
Postgres + trigger-based emulation on SQLite
- Closes NAP Tier 1 blockers: declarative RLS (#32), raw-SQL escape
hatch for SECURITY DEFINER functions (#36), `FORCE ROW LEVEL SECURITY`
(#37)
- 73 new RLS tests passing locally including **9 EXTREME end-to-end
NAP-shape tests** that prove tenant isolation, idempotent re-apply,
drift detection with rename, and 100-row cross-tenant stress against a
real Postgres testcontainer
## What ships
**Core (`Nimblesite.DataProvider.Migration.Core`)**
- `RlsPolicySetDefinition` — `Enabled`, `Forced`, `Policies`
- `RlsPolicyDefinition` — LQL via `UsingLql`/`WithCheckLql`, raw-SQL
escape hatch via `UsingSql`/`WithCheckSql` (#36), `IsPermissive`,
`Operations`, `Roles`
- `RlsOperation` enum — `All`/`Select`/`Insert`/`Update`/`Delete`
- 6 schema operations: `EnableRlsOperation`, `EnableForceRlsOperation`
(#37), `CreateRlsPolicyOperation`, `DropRlsPolicyOperation`,
`DisableRlsOperation`, `DisableForceRlsOperation` — last three
destructive
- `RlsPredicateTranspiler` — substitutes `current_user_id()` per
platform; for `exists(pipeline)` LQL, delegates the inner pipeline to
`LqlStatementConverter` and wraps the transpiled SQL with `EXISTS (...)`
- `SchemaDiff` extended for RLS — emits Enable/Force ops for new RLS
state, `CreatePolicy` for new policies, drops/disable when
`allowDestructive=true`. Forward-only by default; orphan drift cleanup
is opt-in
- YAML round-trippable with `[YamlMember]` aliases for
`using`/`withCheck`/`usingSql`/`withCheckSql`/`permissive`/`forced`
- Error codes: `MIG-E-RLS-EMPTY-PREDICATE`, `-EMPTY-CHECK`,
`-LQL-PARSE`, `-LQL-TRANSPILE`, `-MSSQL-UNSUPPORTED`,
`-RAW-SQL-UNSUPPORTED-ON-PLATFORM`, `-FORCE-UNSUPPORTED-ON-PLATFORM`
**Postgres (`Nimblesite.DataProvider.Migration.Postgres`)**
- Native `CREATE POLICY` with `PERMISSIVE`/`RESTRICTIVE`, `FOR
ALL|SELECT|INSERT|UPDATE|DELETE`, `TO PUBLIC|roles`, `USING`/`WITH
CHECK`
- Inspector reads `pg_class.relrowsecurity` +
`pg_class.relforcerowsecurity` + `pg_policies.qual`/`with_check` into
`RlsPolicySetDefinition`. Predicates round-trip as raw SQL (we don't
attempt SQL→LQL reverse mapping)
**SQLite (`Nimblesite.DataProvider.Migration.SQLite`)**
- `__rls_context` single-row context table auto-generated on first
`EnableRlsOperation`
- Trigger emulation: `BEFORE INSERT/UPDATE/DELETE` triggers with
`RAISE(ABORT, 'RLS-SQLITE: ...')` evaluating predicate against
`NEW`/`OLD` rows
- `{Tbl}_secure` view filtering `SELECT` (SQLite triggers don't
intercept `SELECT`)
- RESTRICTIVE policies emit `MIG-W-RLS-SQLITE-RESTRICTIVE-APPROX`
warning comment
- Inspector reverse-maps `rls_*_{Table}` triggers →
`RlsPolicySetDefinition`
## Test plan
**73 new RLS tests, all green locally:**
- [x] 8 YAML round-trip (`RlsYamlSerializerTests`)
- [x] 13 transpiler unit — per-platform `current_user_id` substitution,
`exists()` subquery wrapping, parse/transpile error paths
(`RlsPredicateTranspilerTests`)
- [x] 7 error code shape (`RlsErrorCodesTests`)
- [x] 11 Postgres DDL string-shape (`PostgresRlsDdlTests`)
- [x] 8 SchemaDiff RLS unit (`SchemaDiffRlsTests`)
- [x] 8 SQLite RLS DDL + inspector E2E (`SqliteRlsMigrationTests`)
- [x] 6 Postgres RLS E2E with real testcontainer + NOBYPASSRLS app role
(`PostgresRlsE2ETests`) — cross-user blocked, INSERT WITH CHECK
enforced, inspector round-trip, schema diff add/drop
- [x] **9 EXTREME NAP-shape E2E** (`PostgresRlsNapShapeTests`) — 4
tenant tables × 2 policies all FORCE'd, cross-tenant isolation, admin
role sees everything, idempotent re-apply emits ZERO ops, drift rename
drops 4 + creates 4, OR-combination predicates round-trip,
`DropForceRls` requires `allowDestructive`, 100-row stress with exact
per-tenant counts
**Local CI dry-run all green:**
- [x] `make fmt-check` (csharpier 434 files + cargo fmt)
- [x] `make lint` (analyzers + clippy + eslint)
- [x] `dotnet build DataProvider.sln -c Debug` — 0 warnings, 0 errors
- [x] `make _test_dotnet` for Migration tests — 363/363 pass, coverage
77.99% (ratcheted from 74% → 77%)
- [x] `cd Lql/lql-lsp-rust && cargo build`
## NAP integration
NapSupport2 has been notified via TMC. Local nupkgs packed at
`/tmp/nap-rls-nuget` as `0.1.0-rls.preview1` so NAP can pin against
their feed while this lands on main. Comments posted on issues #32 / #36
/ #37.
## Out of scope
- CREATE FUNCTION / CREATE ROLE / GRANT (#33/#34/#35) — NAP's bootstrap
retains these
- SQL Server implementation — `MIG-E-RLS-MSSQL-UNSUPPORTED` until
`Nimblesite.DataProvider.Migration.SqlServer` ships
🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 84f5b2a commit 4e5b464
32 files changed
Lines changed: 5626 additions & 80 deletions
File tree
- Lql
- Nimblesite.Lql.Core/Parsing
- Nimblesite.Lql.Tests
- Migration
- DataProviderMigrate
- Nimblesite.DataProvider.Migration.Core
- Nimblesite.DataProvider.Migration.Postgres
- Nimblesite.DataProvider.Migration.SQLite
- Nimblesite.DataProvider.Migration.Tests
- docs/plans
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
358 | 371 | | |
359 | 372 | | |
360 | 373 | | |
| |||
706 | 719 | | |
707 | 720 | | |
708 | 721 | | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
709 | 817 | | |
710 | 818 | | |
711 | 819 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
0 commit comments