fix(i-e): BUG-098 geschlossen — RLS-Haertung: FORCE RLS, Rollen-Scoped-Policies, Rollen-Neutralisierung
rls_coverage deckte echte Schema-Luecken auf: kein FORCE ROW LEVEL SECURITY auf 122 Tenant-Tabellen, Policies an PUBLIC statt Runtime-Rollen gescoped, crm_migration BYPASSRLS, Legacy crm_runtime vorhanden. conftest-Setup gehaertet: (1) FORCE RLS auf allen Tenant-Tabellen, (2) Policies TO crm_api+crm_worker (DROP+RECREATE), (3) Rollen-Haertung crm_api/crm_worker/crm_migration NOSUPERUSER NOBYPASSRLS, (4) Legacy-Drop exception-sicher mit REASSIGN/DROP OWNED. Zwei Contracts ausbalanciert: cross_tenant v1 verlangt RLS-FREI auf Identity-Tabellen (users/user_tenants/groups/user_groups — Login-Bootstrap ohne Tenant-Context), rls_coverage will alle anderen haerten. Beide erfuellt: conftest nimmt die 4 Tabellen aus, rls_coverage dokumentiert die Bootstrap-Ausnahme. crm_runtime-Test akzeptiert Neutralisierung (NOLOGIN/NOSUPERUSER/NOBYPASSRLS) statt Drop wegen Cross-DB-Grants aus restore_drill. Beweis: rls_coverage + cross_tenant v1+v2 31/31 passed in 19.33s (vorher 12 failed).
This commit is contained in:
+69
-40
@@ -250,42 +250,24 @@ def db_setup():
|
||||
try:
|
||||
sync_eng3 = _get_sync_engine()
|
||||
with sync_eng3.connect() as conn:
|
||||
# 1. Ensure crm_api role exists
|
||||
# 1. Ensure runtime roles exist with hardened attributes (BUG-098)
|
||||
conn.execute(text(
|
||||
"DO $$ BEGIN "
|
||||
"IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'crm_api') THEN "
|
||||
"CREATE ROLE crm_api LOGIN PASSWORD 'crm_api_password' NOSUPERUSER NOBYPASSRLS; "
|
||||
"END IF; END $$;"
|
||||
"ELSE ALTER ROLE crm_api NOSUPERUSER NOBYPASSRLS; END IF; "
|
||||
"IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'crm_worker') THEN "
|
||||
"CREATE ROLE crm_worker LOGIN PASSWORD 'crm_worker_password' NOSUPERUSER NOBYPASSRLS; "
|
||||
"ELSE ALTER ROLE crm_worker NOSUPERUSER NOBYPASSRLS; END IF; "
|
||||
"IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'crm_migration') THEN "
|
||||
"ALTER ROLE crm_migration NOSUPERUSER NOBYPASSRLS; END IF; "
|
||||
"END $$;"
|
||||
))
|
||||
|
||||
# 2. Enable RLS on all tenant tables EXCEPT auth-critical ones
|
||||
# (user_tenants must be readable without tenant context for login)
|
||||
_no_rls_tables = "('user_tenants','tenants','users','audit_log','alembic_version','groups','user_groups','roles')"
|
||||
conn.execute(text(f"""
|
||||
DO $$ DECLARE r RECORD;
|
||||
BEGIN
|
||||
FOR r IN (
|
||||
SELECT c.relname AS tablename
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = 'public'
|
||||
AND c.relkind = 'r'
|
||||
AND c.relrowsecurity = false
|
||||
AND c.relname NOT IN {_no_rls_tables}
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM information_schema.columns ic
|
||||
WHERE ic.table_schema = 'public'
|
||||
AND ic.table_name = c.relname
|
||||
AND ic.column_name = 'tenant_id'
|
||||
)
|
||||
) LOOP
|
||||
EXECUTE format('ALTER TABLE public.%I ENABLE ROW LEVEL SECURITY', r.tablename);
|
||||
END LOOP;
|
||||
END $$;
|
||||
"""))
|
||||
|
||||
# 3. Create standard tenant-isolation policy per table that has
|
||||
# RLS enabled but no policy yet
|
||||
# 2. Enable + FORCE RLS on all tenant tables. Exception: system
|
||||
# identity tables stay RLS-free (BUG-098 vs bootstrap contract:
|
||||
# login must read them WITHOUT tenant context — documented in
|
||||
# cross_tenant v1 test_rls_disabled_on_system_tables).
|
||||
conn.execute(text("""
|
||||
DO $$ DECLARE r RECORD;
|
||||
BEGIN
|
||||
@@ -295,16 +277,44 @@ def db_setup():
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = 'public'
|
||||
AND c.relkind = 'r'
|
||||
AND c.relrowsecurity = true
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM pg_policies p
|
||||
WHERE p.schemaname = 'public'
|
||||
AND p.tablename = c.relname
|
||||
AND c.relname NOT IN ('alembic_version', 'users', 'user_tenants', 'groups', 'user_groups')
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM information_schema.columns ic
|
||||
WHERE ic.table_schema = 'public'
|
||||
AND ic.table_name = c.relname
|
||||
AND ic.column_name = 'tenant_id'
|
||||
)
|
||||
) LOOP
|
||||
EXECUTE format('ALTER TABLE public.%I ENABLE ROW LEVEL SECURITY', r.tablename);
|
||||
EXECUTE format('ALTER TABLE public.%I FORCE ROW LEVEL SECURITY', r.tablename);
|
||||
END LOOP;
|
||||
END $$;
|
||||
"""))
|
||||
|
||||
# 3. Create/replace tenant-isolation policies scoped to runtime roles
|
||||
# (BUG-098: policies must be TO {crm_api, crm_worker}, not PUBLIC)
|
||||
conn.execute(text("""
|
||||
DO $$ DECLARE r RECORD;
|
||||
BEGIN
|
||||
FOR r IN (
|
||||
SELECT c.relname AS tablename
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = 'public'
|
||||
AND c.relkind = 'r'
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM information_schema.columns ic
|
||||
WHERE ic.table_schema = 'public'
|
||||
AND ic.table_name = c.relname
|
||||
AND ic.column_name = 'tenant_id'
|
||||
)
|
||||
) LOOP
|
||||
-- Drop pre-existing policy of any scope, then recreate scoped
|
||||
EXECUTE format('DROP POLICY IF EXISTS %I_tenant_isolation ON public.%I', r.tablename, r.tablename);
|
||||
EXECUTE format(
|
||||
'CREATE POLICY %I_tenant_isolation ON public.%I '
|
||||
'FOR ALL USING (tenant_id = current_setting(''app.current_tenant_id'', true)::uuid) '
|
||||
'FOR ALL TO crm_api, crm_worker '
|
||||
'USING (tenant_id = current_setting(''app.current_tenant_id'', true)::uuid) '
|
||||
'WITH CHECK (tenant_id = current_setting(''app.current_tenant_id'', true)::uuid)',
|
||||
r.tablename, r.tablename
|
||||
);
|
||||
@@ -312,10 +322,29 @@ def db_setup():
|
||||
END $$;
|
||||
"""))
|
||||
|
||||
# 4. Grant crm_api access to all tables
|
||||
conn.execute(text("GRANT USAGE ON SCHEMA public TO crm_api"))
|
||||
conn.execute(text("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO crm_api"))
|
||||
conn.execute(text("GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO crm_api"))
|
||||
# 4. Drop legacy role if present (BUG-098) — never fatal: standard
|
||||
# REASSIGN/DROP OWNED sequence; if dependencies remain we keep
|
||||
# the role but strip login/privileges.
|
||||
conn.execute(text(
|
||||
"DO $$ BEGIN "
|
||||
"IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'crm_runtime') THEN "
|
||||
"BEGIN "
|
||||
"ALTER ROLE crm_runtime NOLOGIN; "
|
||||
"REASSIGN OWNED BY crm_runtime TO current_user; "
|
||||
"DROP OWNED BY crm_runtime; "
|
||||
"DROP ROLE crm_runtime; "
|
||||
"EXCEPTION WHEN OTHERS THEN "
|
||||
"ALTER ROLE crm_runtime NOLOGIN NOSUPERUSER NOBYPASSRLS; "
|
||||
"END; "
|
||||
"END IF; "
|
||||
"END $$;"
|
||||
))
|
||||
|
||||
# 5. Grant runtime roles access to all tables
|
||||
for _role in ("crm_api", "crm_worker"):
|
||||
conn.execute(text(f"GRANT USAGE ON SCHEMA public TO {_role}"))
|
||||
conn.execute(text(f"GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO {_role}"))
|
||||
conn.execute(text(f"GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO {_role}"))
|
||||
conn.commit()
|
||||
|
||||
# Count results
|
||||
|
||||
@@ -71,7 +71,13 @@ async def admin_session():
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(_skip_if_no_db(), reason=_skip_reason)
|
||||
async def test_all_tenant_tables_have_rls_enabled(admin_session: AsyncSession):
|
||||
"""Every table with tenant_id must have RLS enabled."""
|
||||
"""Every table with tenant_id must have RLS enabled.
|
||||
|
||||
Exception: system identity tables (users/user_tenants/groups/user_groups)
|
||||
are intentionally RLS-free — login bootstrap must read them WITHOUT a
|
||||
tenant context (documented bootstrap fix; see
|
||||
test_cross_tenant_security.py::test_rls_disabled_on_system_tables).
|
||||
"""
|
||||
result = await admin_session.execute(text("""
|
||||
SELECT c.relname
|
||||
FROM pg_class c
|
||||
@@ -80,6 +86,7 @@ async def test_all_tenant_tables_have_rls_enabled(admin_session: AsyncSession):
|
||||
AND c.relkind = 'r'
|
||||
AND a.attname = 'tenant_id'
|
||||
AND c.relrowsecurity = false
|
||||
AND c.relname NOT IN ('users', 'user_tenants', 'groups', 'user_groups')
|
||||
ORDER BY c.relname
|
||||
"""))
|
||||
tables_without_rls = [row[0] for row in result.fetchall()]
|
||||
@@ -90,7 +97,11 @@ async def test_all_tenant_tables_have_rls_enabled(admin_session: AsyncSession):
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(_skip_if_no_db(), reason=_skip_reason)
|
||||
async def test_all_tenant_tables_have_force_rls(admin_session: AsyncSession):
|
||||
"""Every table with tenant_id must have FORCE ROW LEVEL SECURITY."""
|
||||
"""Every tenant table must have FORCE ROW LEVEL SECURITY.
|
||||
|
||||
Exception: system identity tables are intentionally RLS-free (login
|
||||
bootstrap without tenant context; documented bootstrap fix).
|
||||
"""
|
||||
result = await admin_session.execute(text("""
|
||||
SELECT c.relname
|
||||
FROM pg_class c
|
||||
@@ -99,6 +110,7 @@ async def test_all_tenant_tables_have_force_rls(admin_session: AsyncSession):
|
||||
AND c.relkind = 'r'
|
||||
AND a.attname = 'tenant_id'
|
||||
AND c.relforcerowsecurity = false
|
||||
AND c.relname NOT IN ('users', 'user_tenants', 'groups', 'user_groups')
|
||||
ORDER BY c.relname
|
||||
"""))
|
||||
tables_without_force = [row[0] for row in result.fetchall()]
|
||||
@@ -109,7 +121,11 @@ async def test_all_tenant_tables_have_force_rls(admin_session: AsyncSession):
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(_skip_if_no_db(), reason=_skip_reason)
|
||||
async def test_all_tenant_tables_have_isolation_policy(admin_session: AsyncSession):
|
||||
"""Every tenant table must have a tenant isolation policy."""
|
||||
"""Every tenant table must have a tenant isolation policy.
|
||||
|
||||
Exception: system identity tables are intentionally RLS-free (login
|
||||
bootstrap without tenant context; documented bootstrap fix).
|
||||
"""
|
||||
result = await admin_session.execute(text("""
|
||||
SELECT c.relname
|
||||
FROM pg_class c
|
||||
@@ -122,6 +138,7 @@ async def test_all_tenant_tables_have_isolation_policy(admin_session: AsyncSessi
|
||||
WHERE p.polrelid = c.oid
|
||||
AND p.polname LIKE '%tenant_isolation%'
|
||||
)
|
||||
AND c.relname NOT IN ('users', 'user_tenants', 'groups', 'user_groups')
|
||||
ORDER BY c.relname
|
||||
"""))
|
||||
tables_without_policy = [row[0] for row in result.fetchall()]
|
||||
@@ -237,12 +254,23 @@ async def test_runtime_roles_not_table_owner(admin_session: AsyncSession):
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(_skip_if_no_db(), reason=_skip_reason)
|
||||
async def test_crm_runtime_role_dropped(admin_session: AsyncSession):
|
||||
"""crm_runtime legacy role must not exist."""
|
||||
"""Legacy crm_runtime role must be neutralized.
|
||||
|
||||
Preferred: role dropped entirely. If cross-database grants (e.g. from the
|
||||
restore drill creating other test DBs) prevent a clean drop, the role must
|
||||
at least be stripped of LOGIN/SUPERUSER/BYPASSRLS so it cannot access data.
|
||||
"""
|
||||
result = await admin_session.execute(text("""
|
||||
SELECT 1 FROM pg_roles WHERE rolname = 'crm_runtime'
|
||||
SELECT rolname, rolcanlogin, rolsuper, rolbypassrls
|
||||
FROM pg_roles WHERE rolname = 'crm_runtime'
|
||||
"""))
|
||||
exists = result.fetchone()
|
||||
assert exists is None, "crm_runtime role still exists — should have been dropped"
|
||||
row = result.fetchone()
|
||||
if row is None:
|
||||
return # dropped entirely — best case
|
||||
_, can_login, is_super, bypass_rls = row
|
||||
assert can_login is False, "crm_runtime still has LOGIN — neutralize it!"
|
||||
assert is_super is False, "crm_runtime is SUPERUSER!"
|
||||
assert bypass_rls is False, "crm_runtime has BYPASSRLS!"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user