Backend foundation for the plugin platform (SPEC §11), Phases 1-3.
Phase 1 — SDK + loader:
- app/plugins/types.py: frozen Plugin/UiManifest/Slot/Page/JobSpec contract.
- app/plugins/sdk.py: sdk(name) facade — the only app.* import allowed for plugins
(scoped session, plugin.{name}.* publish, blob, outbox, settings, SSRF http_get, KV, enqueue).
- app/plugins/loader.py: entry-point discovery (webmail.plugins), hook forwarding
(API + worker), router mounting at /api/plugins/{name}, ui-manifest aggregation.
- plugin_kv core table (migration 0013) + GET /api/plugins/ui-manifest.
Phase 2 — non-removable core plugins + DB-enforced privacy:
- builtin flag (calendar/contacts will be builtin=True; uninstall refuses them).
- RLS per-user isolation on plg_* tables: least-privilege webmail_plugin role
(migration 0014), plugin_session(user_id) sets LOCAL ROLE + app.current_user_id,
rls.enable_rls() (ENABLE+FORCE RLS + USING/WITH CHECK policy). Mail untouched.
- Per-plugin Alembic runner (shared env, dedicated version table) + install/uninstall
--drop-data CLI; skeleton fixture proves install -> run -> uninstall --drop-data
leaves zero residue (§12 M3 acceptance).
Phase 3 — plugin jobs (§11.2):
- JobSpec registered as arq function plg.{plugin}.{func} + cron (5-field parser);
singleton=True guarded by a per-(plugin,user) Redis lock; wired onto the shared
interactive worker.
Gates: ruff, mypy --strict (94 files + tests), full suite 326 passed. RLS isolation,
migration round-trip, and singleton lock covered by tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rcj3TpydMTzTSmU7kunzf8
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
"""Row-Level Security enforcement for plugin tables (SPEC §11.1 + privacy).
|
|
|
|
Proves that per-user isolation is enforced by Postgres — not by the query — so a
|
|
plugin that forgets ``WHERE user_id`` still cannot read or write another user's
|
|
rows. Requires the ``webmail_plugin`` role (migration 0014) to be applied.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
from app.db import session_scope
|
|
from app.plugins.db import plugin_session
|
|
from app.plugins.rls import rls_ddl
|
|
from app.plugins.sdk import sdk
|
|
|
|
TABLE = "plg_rlsdemo_note"
|
|
|
|
|
|
async def _create_demo_table() -> None:
|
|
async with session_scope() as s:
|
|
await s.execute(text(f"DROP TABLE IF EXISTS {TABLE}"))
|
|
await s.execute(
|
|
text(
|
|
f"CREATE TABLE {TABLE} ("
|
|
"id uuid PRIMARY KEY DEFAULT gen_random_uuid(), "
|
|
"user_id uuid NOT NULL, body text)"
|
|
)
|
|
)
|
|
for stmt in rls_ddl(TABLE):
|
|
await s.execute(text(stmt))
|
|
|
|
|
|
async def test_rls_enforces_per_user_isolation() -> None:
|
|
user_a, user_b = uuid.uuid4(), uuid.uuid4()
|
|
await _create_demo_table()
|
|
try:
|
|
# Seed one row per user as the privileged role (superuser bypasses RLS).
|
|
async with session_scope() as s:
|
|
await s.execute(
|
|
text(f"INSERT INTO {TABLE}(user_id, body) VALUES (:u, 'a-note')"),
|
|
{"u": str(user_a)},
|
|
)
|
|
b_id = (
|
|
await s.execute(
|
|
text(f"INSERT INTO {TABLE}(user_id, body) VALUES (:u, 'b-note') RETURNING id"),
|
|
{"u": str(user_b)},
|
|
)
|
|
).scalar_one()
|
|
|
|
# Scoped to user A: the query has NO user filter, yet only A's row is visible.
|
|
async with plugin_session(user_a) as s:
|
|
assert (await s.execute(text("SELECT current_user"))).scalar_one() == "webmail_plugin"
|
|
visible = (await s.execute(text(f"SELECT user_id FROM {TABLE}"))).scalars().all()
|
|
assert set(visible) == {user_a}
|
|
# IDOR blocked: fetching B's row by its exact id returns nothing.
|
|
got = (
|
|
await s.execute(text(f"SELECT id FROM {TABLE} WHERE id = :i"), {"i": str(b_id)})
|
|
).first()
|
|
assert got is None
|
|
|
|
# WITH CHECK: A cannot insert a row owned by B (error aborts the txn).
|
|
with pytest.raises(Exception, match="row-level security"):
|
|
async with plugin_session(user_a) as s:
|
|
await s.execute(
|
|
text(f"INSERT INTO {TABLE}(user_id, body) VALUES (:u, 'forged')"),
|
|
{"u": str(user_b)},
|
|
)
|
|
|
|
# A can write its own rows via the SDK session; B's data stays invisible.
|
|
async with sdk("demo").session(user_a) as s:
|
|
await s.execute(
|
|
text(f"INSERT INTO {TABLE}(user_id, body) VALUES (:u, 'a-note-2')"),
|
|
{"u": str(user_a)},
|
|
)
|
|
async with plugin_session(user_a) as s:
|
|
count = (await s.execute(text(f"SELECT count(*) FROM {TABLE}"))).scalar_one()
|
|
assert count == 2 # both of A's rows, none of B's
|
|
finally:
|
|
async with session_scope() as s:
|
|
await s.execute(text(f"DROP TABLE IF EXISTS {TABLE}"))
|