rlsparity 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rlsparity-0.1.0/.github/workflows/tests.yml +79 -0
- rlsparity-0.1.0/.gitignore +13 -0
- rlsparity-0.1.0/CHANGELOG.md +43 -0
- rlsparity-0.1.0/DESIGN.md +172 -0
- rlsparity-0.1.0/LICENSE +21 -0
- rlsparity-0.1.0/PKG-INFO +245 -0
- rlsparity-0.1.0/README.md +219 -0
- rlsparity-0.1.0/pyproject.toml +47 -0
- rlsparity-0.1.0/src/rlsparity/__init__.py +64 -0
- rlsparity-0.1.0/src/rlsparity/compare.py +123 -0
- rlsparity-0.1.0/src/rlsparity/labels.py +11 -0
- rlsparity-0.1.0/src/rlsparity/plugin.py +157 -0
- rlsparity-0.1.0/src/rlsparity/preconditions.py +159 -0
- rlsparity-0.1.0/src/rlsparity/probes/__init__.py +24 -0
- rlsparity-0.1.0/src/rlsparity/probes/policy.py +38 -0
- rlsparity-0.1.0/src/rlsparity/probes/view.py +117 -0
- rlsparity-0.1.0/src/rlsparity/roles.py +92 -0
- rlsparity-0.1.0/tests/conftest.py +18 -0
- rlsparity-0.1.0/tests/demo/__init__.py +1 -0
- rlsparity-0.1.0/tests/demo/conftest.py +78 -0
- rlsparity-0.1.0/tests/demo/models.py +16 -0
- rlsparity-0.1.0/tests/demo/test_parity.py +49 -0
- rlsparity-0.1.0/tests/demo/views.py +27 -0
- rlsparity-0.1.0/tests/settings.py +25 -0
- rlsparity-0.1.0/tests/test_compare.py +101 -0
- rlsparity-0.1.0/tests/test_plugin.py +406 -0
- rlsparity-0.1.0/tests/test_policy_probe.py +109 -0
- rlsparity-0.1.0/tests/test_preconditions.py +189 -0
- rlsparity-0.1.0/tests/test_roles.py +123 -0
- rlsparity-0.1.0/tests/test_smoke.py +42 -0
- rlsparity-0.1.0/tests/test_view_probe.py +293 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
name: tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches: [main]
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
lint:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v7
|
|
16
|
+
|
|
17
|
+
- uses: actions/setup-python@v7
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.12"
|
|
20
|
+
|
|
21
|
+
- name: Install ruff
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip
|
|
24
|
+
pip install ruff==0.17.6
|
|
25
|
+
|
|
26
|
+
- name: Run ruff
|
|
27
|
+
run: |
|
|
28
|
+
ruff format --check src tests
|
|
29
|
+
ruff check src tests
|
|
30
|
+
|
|
31
|
+
test:
|
|
32
|
+
runs-on: ubuntu-latest
|
|
33
|
+
|
|
34
|
+
strategy:
|
|
35
|
+
fail-fast: false
|
|
36
|
+
matrix:
|
|
37
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
38
|
+
django-version: ["4.2", "5.2"]
|
|
39
|
+
exclude:
|
|
40
|
+
# Django 4.2 does not support Python 3.13.
|
|
41
|
+
- python-version: "3.13"
|
|
42
|
+
django-version: "4.2"
|
|
43
|
+
|
|
44
|
+
services:
|
|
45
|
+
postgres:
|
|
46
|
+
image: postgres:17
|
|
47
|
+
env:
|
|
48
|
+
POSTGRES_USER: postgres
|
|
49
|
+
POSTGRES_PASSWORD: postgres
|
|
50
|
+
POSTGRES_DB: rlsparity_test
|
|
51
|
+
ports:
|
|
52
|
+
- 5432:5432
|
|
53
|
+
options: >-
|
|
54
|
+
--health-cmd "pg_isready -U postgres -d rlsparity_test"
|
|
55
|
+
--health-interval 10s
|
|
56
|
+
--health-timeout 5s
|
|
57
|
+
--health-retries 5
|
|
58
|
+
|
|
59
|
+
env:
|
|
60
|
+
RLSPARITY_DB_NAME: rlsparity_test
|
|
61
|
+
RLSPARITY_DB_USER: postgres
|
|
62
|
+
RLSPARITY_DB_PASSWORD: postgres
|
|
63
|
+
RLSPARITY_DB_HOST: localhost
|
|
64
|
+
RLSPARITY_DB_PORT: "5432"
|
|
65
|
+
|
|
66
|
+
steps:
|
|
67
|
+
- uses: actions/checkout@v7
|
|
68
|
+
|
|
69
|
+
- uses: actions/setup-python@v7
|
|
70
|
+
with:
|
|
71
|
+
python-version: ${{ matrix.python-version }}
|
|
72
|
+
|
|
73
|
+
- name: Install the package and the matrix Django
|
|
74
|
+
run: |
|
|
75
|
+
python -m pip install --upgrade pip
|
|
76
|
+
pip install -e ".[dev]" "Django~=${{ matrix.django-version }}.0"
|
|
77
|
+
|
|
78
|
+
- name: Run the test suite
|
|
79
|
+
run: python -m pytest -q
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-11-28
|
|
9
|
+
|
|
10
|
+
First usable release. This is an early alpha: install it from git, not PyPI, and
|
|
11
|
+
expect the API to change before 1.0.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- A pytest plugin that compares a Django view with PostgreSQL RLS. The view runs
|
|
16
|
+
with RLS bypassed, the policy runs with RLS enforced. A view that returns a row
|
|
17
|
+
the policy hides is a **GAP** and fails the test; a view stricter than the
|
|
18
|
+
policy is reported as **STRICTER** but does not fail.
|
|
19
|
+
- `ensure_roles`, `Principal` and `Case`, plus the `rlsparity` fixture with
|
|
20
|
+
`check` for a single case and `check_all` for several.
|
|
21
|
+
- A session-setting `context` callable, applied after switching roles, so policies
|
|
22
|
+
can key on the current principal instead of the database user.
|
|
23
|
+
- `PolicyUsesDatabaseUser`: a precondition error when a policy uses `current_user`
|
|
24
|
+
or `session_user`. This inspects the policy expression only, not functions the
|
|
25
|
+
policy calls.
|
|
26
|
+
- View probes for GET, POST, PUT, PATCH and DELETE, selected with `Case.method`
|
|
27
|
+
and an optional `Case.data`. Non-GET requests are rolled back.
|
|
28
|
+
- A demo notes app under `tests/demo/` with a deliberately leaky view and
|
|
29
|
+
end-to-end parity tests.
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- Policy comparison is still SELECT visibility of the named row, not `WITH CHECK`
|
|
34
|
+
or insert, update and delete policies. Write-policy coverage is not claimed.
|
|
35
|
+
- Precondition checks run before each comparison, and the end-of-run summary now
|
|
36
|
+
includes errored cases alongside GAP and STRICTER findings.
|
|
37
|
+
- CI runs `ruff check` and `ruff format --check` with a pinned ruff.
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
|
|
41
|
+
- The plugin registers once when it is already installed.
|
|
42
|
+
- The test client is logged out after every case, including failures, so no case
|
|
43
|
+
inherits the previous principal's session.
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# rlsparity: design
|
|
2
|
+
|
|
3
|
+
Status: draft. Nothing here is implemented yet.
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Apps that use PostgreSQL row-level security (RLS) as a second layer have two places
|
|
8
|
+
that decide who may see a row: the Django view (or serializer, queryset, permission
|
|
9
|
+
class) and the RLS policy. They are written at different times, in different
|
|
10
|
+
languages, by possibly different people. They drift.
|
|
11
|
+
|
|
12
|
+
The dangerous drift is when the view is **weaker** than the policy. Example, with
|
|
13
|
+
made-up names:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
# view: any logged-in user may open a photo whose owner is active
|
|
17
|
+
photo = get_object_or_404(Photo, pk=pk, owner__is_active=True)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```sql
|
|
21
|
+
-- policy: the owner, or anyone if visibility = 'public',
|
|
22
|
+
-- or a contact of the owner if visibility = 'contacts'
|
|
23
|
+
CREATE POLICY photo_read ON photo FOR SELECT USING (...);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
A logged-in stranger requesting a `contacts` photo gets a 404 in production,
|
|
27
|
+
because RLS filters the row out. The view never decided that. With RLS off (a
|
|
28
|
+
superuser script, a wrong role, a test database set up as the table owner) the same
|
|
29
|
+
request returns 200. No test fails, because the test database usually bypasses RLS
|
|
30
|
+
while production enforces it, so neither environment exercises the gap.
|
|
31
|
+
|
|
32
|
+
The mirror case (view stricter than the policy) is not a leak, but it is often a
|
|
33
|
+
bug: the feature silently does less than the policy allows.
|
|
34
|
+
|
|
35
|
+
## Prior art and what is missing
|
|
36
|
+
|
|
37
|
+
| Tool | What it covers | Gap for this problem |
|
|
38
|
+
|---|---|---|
|
|
39
|
+
| [pgrls](https://github.com/pgrls/pgrls) | Static lint of policies (68 rules), Z3 tenant-isolation proof, pytest plugin for SQL-level RLS tests, policy diff | Works at the SQL layer. Never looks at what a Django view returns. |
|
|
40
|
+
| [Supabase splinter](https://github.com/supabase/splinter) | SQL lints: RLS disabled, `USING (true)`, missing policies | Policy hygiene only. |
|
|
41
|
+
| [pgTAP RLS tests](https://supabase.com/docs/guides/database/postgres/testing-row-level-security) | Hand-written SQL assertions per role | No application layer. |
|
|
42
|
+
| [django-rls](https://github.com/kdpisda/django-rls), [django-boundary](https://github.com/icvoss/django-boundary) | Define policies from Django, set session context, multi-tenancy | Generate and run RLS. Do not compare it with the view logic. |
|
|
43
|
+
| [authzprobe](https://github.com/gl1tchdust/api-authz-testing-playbook), Autorize, RoleBreaker | Black-box HTTP access matrices, user A vs user B | Compare principals with each other, not application logic with the database policy. No notion of RLS. |
|
|
44
|
+
|
|
45
|
+
Nobody checks the seam: "does the view, taken alone, enforce what the policy
|
|
46
|
+
enforces?" That is the whole scope of this project.
|
|
47
|
+
|
|
48
|
+
## Approach
|
|
49
|
+
|
|
50
|
+
Considered:
|
|
51
|
+
|
|
52
|
+
- **Static**: read `pg_policies` and migrations, read view code, compare. Rejected for
|
|
53
|
+
the MVP. Policies are arbitrary SQL (subqueries, functions), views are arbitrary
|
|
54
|
+
Python (querysets, permission classes, helpers). A faithful static comparison is a
|
|
55
|
+
research project and would produce either noise or silence.
|
|
56
|
+
- **Differential (chosen)**: ask the real database and the real view, and compare
|
|
57
|
+
the answers. The database already knows how to evaluate its own policies, and
|
|
58
|
+
Django already knows how to run its own views. We only orchestrate.
|
|
59
|
+
|
|
60
|
+
For every declared case (principal, endpoint, row), inside one test transaction:
|
|
61
|
+
|
|
62
|
+
1. **Seed** rows as the test connection's normal (privileged) user.
|
|
63
|
+
2. **Policy probe**: switch to `rlsparity_enforced` (`SET LOCAL ROLE`, a role without
|
|
64
|
+
`BYPASSRLS`, not the table owner), apply the principal's session context
|
|
65
|
+
(for example `set_config('app.user_id', ...)`), and check whether the row is
|
|
66
|
+
visible with a direct query on the model's table.
|
|
67
|
+
3. **View probe**: switch to `rlsparity_bypass` (a role with `BYPASSRLS`), apply the
|
|
68
|
+
same context, request the endpoint through Django's test client as that
|
|
69
|
+
principal, and classify the response (allowed or denied).
|
|
70
|
+
4. **Compare** and report.
|
|
71
|
+
|
|
72
|
+
| View (RLS bypassed) | Row visible under RLS | Verdict |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| allowed | no | **GAP**: the view relies on RLS. Fails the test. |
|
|
75
|
+
| denied | yes | **STRICTER**: view hides what policy allows. Reported, not failed by default. |
|
|
76
|
+
| allowed | yes | ok |
|
|
77
|
+
| denied | no | ok |
|
|
78
|
+
|
|
79
|
+
Precondition checks (a failed precondition is an error, never a pass):
|
|
80
|
+
|
|
81
|
+
- the row exists when seen by the bypass role (negative control),
|
|
82
|
+
- `row_security_active(table)` is true for the enforced role,
|
|
83
|
+
- the enforced role really has no `BYPASSRLS` and does not own the table.
|
|
84
|
+
|
|
85
|
+
This also catches the quiet failure where a test passes only because the connection
|
|
86
|
+
was a superuser.
|
|
87
|
+
|
|
88
|
+
Why a pytest plugin: it runs in the user's existing suite and fixtures, inside
|
|
89
|
+
`TestCase`/`pytest-django` transactions, so no extra database or process is needed.
|
|
90
|
+
|
|
91
|
+
### User-facing shape (sketch, will change)
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# conftest.py
|
|
95
|
+
from rlsparity import Principal, Case
|
|
96
|
+
|
|
97
|
+
def set_context(cursor, user):
|
|
98
|
+
cursor.execute("SELECT set_config('app.user_id', %s, true)", [str(user.pk)])
|
|
99
|
+
|
|
100
|
+
RLSPARITY = dict(context=set_context, allowed_statuses={200}, denied_statuses={403, 404})
|
|
101
|
+
|
|
102
|
+
def test_photo_detail_parity(rlsparity, alice, bob, photo_of_alice):
|
|
103
|
+
rlsparity.check(
|
|
104
|
+
Case(principal=Principal(bob, label="bob"), url=f"/photos/{photo_of_alice.pk}/",
|
|
105
|
+
model=Photo, pk=photo_of_alice.pk)
|
|
106
|
+
)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`check` raises with a message such as:
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
GAP GET /photos/12/ as bob
|
|
113
|
+
view returned 200 with RLS bypassed; policy hides photo.12 from bob under RLS
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Scope
|
|
117
|
+
|
|
118
|
+
### MVP
|
|
119
|
+
|
|
120
|
+
- PostgreSQL only, Django >= 4.2, pytest.
|
|
121
|
+
- Read access (GET) on a single object endpoint, plus list endpoints compared by the
|
|
122
|
+
set of ids returned.
|
|
123
|
+
- Principals are Django users (or anonymous); session context via a user-supplied
|
|
124
|
+
callable.
|
|
125
|
+
- Explicitly declared cases. No discovery.
|
|
126
|
+
- Roles and grants are created by a helper the user calls from a fixture.
|
|
127
|
+
|
|
128
|
+
### Not in the MVP
|
|
129
|
+
|
|
130
|
+
- Writes (INSERT/UPDATE/DELETE, `WITH CHECK`).
|
|
131
|
+
- Automatic enumeration of URLs and objects.
|
|
132
|
+
- Django REST framework specifics (plain responses work already).
|
|
133
|
+
- Policies that depend on `current_user` instead of session settings.
|
|
134
|
+
- Any static analysis.
|
|
135
|
+
|
|
136
|
+
### Known limits
|
|
137
|
+
|
|
138
|
+
- It proves nothing about cases you do not declare. It is a regression net, not a
|
|
139
|
+
verifier.
|
|
140
|
+
- Only observable outputs are compared: status class and returned ids. A view that
|
|
141
|
+
returns the row's existence through a side channel is not detected.
|
|
142
|
+
- `SET ROLE` does not change `session_user`; policies written against
|
|
143
|
+
`session_user` need a different setup.
|
|
144
|
+
|
|
145
|
+
## Implementation plan
|
|
146
|
+
|
|
147
|
+
Each step is meant to stay under about 250 changed lines and five files.
|
|
148
|
+
|
|
149
|
+
1. **Package skeleton.** `src/rlsparity/` with `__init__`, `plugin` (registers the
|
|
150
|
+
fixture), the `Principal` and `Case` dataclasses, a smoke test, and the Postgres
|
|
151
|
+
test settings.
|
|
152
|
+
2. **Roles.** `roles.py`: create/grant helper for the enforced and bypass roles;
|
|
153
|
+
context manager `as_role(connection, role, context, user)` using `SET LOCAL ROLE`.
|
|
154
|
+
Tests against a real database.
|
|
155
|
+
3. **Preconditions.** `preconditions.py`: checks for `BYPASSRLS`, ownership,
|
|
156
|
+
`row_security_active`, and the negative control; clear error types.
|
|
157
|
+
4. **Policy probe.** `probes.py`: `row_visible(model, pk, principal)` under the
|
|
158
|
+
enforced role; list variant returning visible ids.
|
|
159
|
+
5. **View probe.** `probes.py`: request via the test client as the principal under
|
|
160
|
+
the bypass role; classify by configured status sets; list variant extracts ids
|
|
161
|
+
through a user-supplied callable.
|
|
162
|
+
6. **Comparison.** `compare.py`: `Finding` dataclass and the verdict table above,
|
|
163
|
+
pure functions, table-driven tests.
|
|
164
|
+
7. **Fixture and report.** `plugin.py`: the `rlsparity` fixture with `check` and
|
|
165
|
+
`check_all`, failure messages, optional summary at the end of the run.
|
|
166
|
+
8. **Demo app.** `tests/demo/`: a tiny generic notes/photos app with an RLS policy,
|
|
167
|
+
one deliberately leaky view and one correct view; an end-to-end test asserting
|
|
168
|
+
the GAP and the pass.
|
|
169
|
+
9. **CI and docs.** GitHub Actions with a PostgreSQL service for Django 4.2, 5.x
|
|
170
|
+
and Python 3.10 to 3.13; usage section in the README.
|
|
171
|
+
|
|
172
|
+
After the MVP: write operations, discovery from the URL conf, DRF helpers.
|
rlsparity-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 driftwright
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
rlsparity-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: rlsparity
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pytest plugin that checks Django views against PostgreSQL row-level security: finds endpoints that would leak rows if RLS were off.
|
|
5
|
+
Project-URL: Homepage, https://github.com/driftwright/rlsparity
|
|
6
|
+
Project-URL: Issues, https://github.com/driftwright/rlsparity/issues
|
|
7
|
+
Author: driftwright
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: authorization,django,postgresql,pytest,rls,row-level-security
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: Framework :: Pytest
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Security
|
|
17
|
+
Classifier: Topic :: Software Development :: Testing
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: django>=4.2
|
|
20
|
+
Requires-Dist: psycopg[binary]>=3.1
|
|
21
|
+
Requires-Dist: pytest>=7
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest-django>=4.5; extra == 'dev'
|
|
24
|
+
Requires-Dist: ruff==0.17.6; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# rlsparity
|
|
28
|
+
|
|
29
|
+
> **Status: early alpha (0.1.0).** The library works and is usable from git, but
|
|
30
|
+
> this is not a 1.0: the API may still change. Nothing is on PyPI. See
|
|
31
|
+
> [CHANGELOG.md](CHANGELOG.md) for release notes.
|
|
32
|
+
|
|
33
|
+
Many Django apps use PostgreSQL row-level security (RLS) as a second layer behind
|
|
34
|
+
their own permission checks. That works until the two layers disagree: a view
|
|
35
|
+
checks "logged in and owner", the RLS policy also checks a visibility flag and a
|
|
36
|
+
relationship, and nobody notices because RLS quietly hides the difference. Turn RLS
|
|
37
|
+
off (a superuser connection in a script, a misconfigured role, a migration) and the
|
|
38
|
+
view serves rows it never should have.
|
|
39
|
+
|
|
40
|
+
`rlsparity` is a pytest plugin that makes that gap visible. For each
|
|
41
|
+
(user, endpoint, row) you declare, it asks the same question two ways:
|
|
42
|
+
|
|
43
|
+
1. What does the **view** return when the database does not enforce RLS?
|
|
44
|
+
2. Can that user **see the row** when the database does enforce RLS?
|
|
45
|
+
|
|
46
|
+
If the view says yes where the policy says no, that endpoint depends on RLS to be
|
|
47
|
+
safe. The test fails and names the endpoint, the user and the row.
|
|
48
|
+
|
|
49
|
+
It runs inside your existing test database and transactions. It does not parse
|
|
50
|
+
policies or view code, so it works with whatever your policies and views actually do.
|
|
51
|
+
|
|
52
|
+
## What it is not
|
|
53
|
+
|
|
54
|
+
- Not a policy linter. For static checks of the policies themselves, see
|
|
55
|
+
[pgrls](https://github.com/pgrls/pgrls) and
|
|
56
|
+
[Supabase splinter](https://github.com/supabase/splinter).
|
|
57
|
+
- Not a policy generator. See [django-rls](https://github.com/kdpisda/django-rls) and
|
|
58
|
+
[django-boundary](https://github.com/icvoss/django-boundary).
|
|
59
|
+
- Not a black-box scanner. It needs your Django project and a PostgreSQL test database.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Install
|
|
64
|
+
|
|
65
|
+
`rlsparity` is not on PyPI. Install it from this repository, plus
|
|
66
|
+
[pytest-django](https://pytest-django.readthedocs.io/) and a PostgreSQL test
|
|
67
|
+
database:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install "git+https://github.com/driftwright/rlsparity.git"
|
|
71
|
+
pip install pytest-django
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
In your test settings, point `DATABASES` at PostgreSQL, and tell pytest which
|
|
75
|
+
settings module to use, for example in `pytest.ini`:
|
|
76
|
+
|
|
77
|
+
```ini
|
|
78
|
+
[pytest]
|
|
79
|
+
DJANGO_SETTINGS_MODULE = myproject.settings_test
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Settings and roles
|
|
83
|
+
|
|
84
|
+
The checks run as two PostgreSQL roles that `ensure_roles` creates:
|
|
85
|
+
`rlsparity_enforced` (no `BYPASSRLS`) and `rlsparity_bypass` (`BYPASSRLS`). Call
|
|
86
|
+
it from a fixture with an open transaction and a superuser connection:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
# conftest.py
|
|
90
|
+
import pytest
|
|
91
|
+
from django.db import connection
|
|
92
|
+
from rlsparity import ensure_roles
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@pytest.fixture
|
|
96
|
+
def rls_roles(db):
|
|
97
|
+
ensure_roles(connection)
|
|
98
|
+
with connection.cursor() as cursor:
|
|
99
|
+
# Enforced needs SELECT on the checked table. Bypass needs SELECT on
|
|
100
|
+
# that table and on every other table the view reads, e.g. auth_user.
|
|
101
|
+
cursor.execute(
|
|
102
|
+
"GRANT USAGE ON SCHEMA public "
|
|
103
|
+
"TO rlsparity_enforced, rlsparity_bypass"
|
|
104
|
+
)
|
|
105
|
+
cursor.execute("GRANT SELECT ON notes_note TO rlsparity_enforced")
|
|
106
|
+
cursor.execute(
|
|
107
|
+
"GRANT SELECT ON notes_note, auth_user TO rlsparity_bypass"
|
|
108
|
+
)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The roles and grants are transactional, so they disappear when the test
|
|
112
|
+
transaction rolls back. Depend on this fixture from each parity test, or make it
|
|
113
|
+
`autouse=True`.
|
|
114
|
+
|
|
115
|
+
Policies must key on a session setting that your `context` callable applies. It
|
|
116
|
+
is called as `context(cursor, user)` after switching to each role, and `user` is
|
|
117
|
+
`None` for an anonymous principal:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
# myproject/rls.py
|
|
121
|
+
def set_context(cursor, user):
|
|
122
|
+
cursor.execute(
|
|
123
|
+
"SELECT set_config('app.user_id', %s, true)",
|
|
124
|
+
["" if user is None else str(user.pk)],
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Configure the plugin in your test settings:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
# myproject/settings_test.py
|
|
132
|
+
from myproject.rls import set_context
|
|
133
|
+
|
|
134
|
+
RLSPARITY = {
|
|
135
|
+
"context": set_context,
|
|
136
|
+
# Optional; these are the defaults.
|
|
137
|
+
"allowed_statuses": {200},
|
|
138
|
+
"denied_statuses": {403, 404},
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
See [`tests/demo/conftest.py`](tests/demo/conftest.py) for a complete fixture,
|
|
143
|
+
including the table, policy and grants.
|
|
144
|
+
|
|
145
|
+
### A minimal check
|
|
146
|
+
|
|
147
|
+
Assume a `Note` model with an `owner` field and two detail views:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
# notes/views.py
|
|
151
|
+
from django.contrib.auth.decorators import login_required
|
|
152
|
+
from django.http import HttpResponse
|
|
153
|
+
from django.shortcuts import get_object_or_404
|
|
154
|
+
|
|
155
|
+
from .models import Note
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@login_required
|
|
159
|
+
def leaky_note(request, pk):
|
|
160
|
+
# GAP: relies on RLS to hide other users' notes.
|
|
161
|
+
note = get_object_or_404(Note, pk=pk)
|
|
162
|
+
return HttpResponse(note.title)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@login_required
|
|
166
|
+
def correct_note(request, pk):
|
|
167
|
+
note = get_object_or_404(Note, pk=pk, owner=request.user)
|
|
168
|
+
return HttpResponse(note.title)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
With URLs `/notes/<pk>/leaky/` and `/notes/<pk>/correct/`, and fixtures `bob`
|
|
172
|
+
and `note_of_alice`, the checks look like this:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
# tests/test_notes.py
|
|
176
|
+
import pytest
|
|
177
|
+
from notes.models import Note
|
|
178
|
+
from rlsparity import Case, ParityFailure, Principal
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_leaky_view_is_reported_as_a_gap(rlsparity, rls_roles, bob, note_of_alice):
|
|
182
|
+
url = f"/notes/{note_of_alice.pk}/leaky/"
|
|
183
|
+
with pytest.raises(ParityFailure) as info:
|
|
184
|
+
rlsparity.check(
|
|
185
|
+
Case(principal=Principal(bob, label="bob"), url=url,
|
|
186
|
+
model=Note, pk=note_of_alice.pk)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
message = str(info.value)
|
|
190
|
+
assert "GAP" in message
|
|
191
|
+
assert url in message
|
|
192
|
+
assert "bob" in message
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def test_correct_view_matches_the_policy(rlsparity, rls_roles, bob, note_of_alice):
|
|
196
|
+
finding = rlsparity.check(
|
|
197
|
+
Case(
|
|
198
|
+
principal=Principal(bob, label="bob"),
|
|
199
|
+
url=f"/notes/{note_of_alice.pk}/correct/",
|
|
200
|
+
model=Note,
|
|
201
|
+
pk=note_of_alice.pk,
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
assert finding.verdict == "OK"
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`check` returns a `Finding` and raises `ParityFailure` for a failing case.
|
|
209
|
+
`check_all([...])` runs several cases and raises once at the end.
|
|
210
|
+
|
|
211
|
+
`Case` defaults to `method="GET"`. For a write endpoint, set `method="POST"`
|
|
212
|
+
(or `PUT`, `PATCH`, `DELETE`) and pass optional `data={...}`; non-GET requests
|
|
213
|
+
are rolled back.
|
|
214
|
+
|
|
215
|
+
### GAP and STRICTER
|
|
216
|
+
|
|
217
|
+
- **GAP** — the view returns the row with RLS bypassed, but the policy hides it.
|
|
218
|
+
The test fails with `ParityFailure`. This is the dangerous case: the view is
|
|
219
|
+
relying on RLS.
|
|
220
|
+
- **STRICTER** — the view hides a row the policy would allow. It is reported,
|
|
221
|
+
including in the end-of-run summary, but does not fail the test.
|
|
222
|
+
- A matching allow or deny is **OK**.
|
|
223
|
+
|
|
224
|
+
### Current limits
|
|
225
|
+
|
|
226
|
+
- The view probe can issue `GET`, `POST`, `PUT`, `PATCH` and `DELETE`
|
|
227
|
+
(`Case.method`, with optional `Case.data`); non-GET requests are rolled back.
|
|
228
|
+
The policy side is still SELECT visibility of the named row, not `WITH CHECK`
|
|
229
|
+
or insert, update and delete policies, so write-policy coverage is not claimed.
|
|
230
|
+
- Policies must be keyed on session settings applied through `context`, not on
|
|
231
|
+
`current_user` or `session_user` (`SET ROLE` does not change `session_user`).
|
|
232
|
+
A policy that uses either raises a `PolicyUsesDatabaseUser` precondition error;
|
|
233
|
+
this is a hard error, not a supported mode. The check looks at the policy
|
|
234
|
+
expression itself, not at functions the policy calls.
|
|
235
|
+
- Only explicitly declared cases are checked. It proves nothing about cases you
|
|
236
|
+
do not declare.
|
|
237
|
+
- PostgreSQL, Django >= 4.2 and pytest are required.
|
|
238
|
+
|
|
239
|
+
## Design
|
|
240
|
+
|
|
241
|
+
See [DESIGN.md](DESIGN.md) for the approach, scope and planned milestones.
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
MIT
|