Files
leocrm/alembic/versions/0084_rls_fail_closed_reactivate.py
T
Agent Zero 7fbbe420bd
Check Cross-Plugin Imports / check (push) Has been cancelled
fix: comprehensive system audit fixes (55+ issues)
CRITICAL:
- Fix SQL injection in prestart.sh (parameterized query)
- Fix secret key validation (always validate, not just production)
- Fix workspace model partial index bug (func.text -> text)
- Fix HealthResponse schema (add checks field)
- Fix Tenant import in permissions.py (NameError on every auth request)
- Fix README tech stack (React instead of Alpine.js)
- Delete broken test_cross_tenant_security_v2.py
- Add fail-closed RLS migration 0084 (48 tenant tables)

HIGH:
- Add GeneralRateLimitMiddleware for all API routes
- Add file type blocklist for DMS and attachment uploads
- Fix guest auth: Pydantic schema, tenant_slug required, CSRF bypass
- Fix CSRF bypass path matching (in -> endswith)
- Add worker healthcheck in docker-compose.yml
- Add ARQ max_tries=3 for job retries
- Fix 28 bare pass in mail services (-> logger.debug)
- Fix print() -> logger in main.py and ai_assistant
- Fix duplicate email handling (catch IntegrityError -> 409)
- Add session revocation (invalidate_all_user_sessions)
- Add resource limits to all containers
- Fix CORS default (localhost -> production domain)
- Fix SameSite=Lax -> Strict
- Fix Redis password visibility in healthcheck
- Fix npm vulnerabilities (19 -> 9)
- Fix Sidebar OOM (wildcard lucide import -> curated ICON_MAP)

MEDIUM:
- Localize ErrorBoundary to German
- Wire Mail.tsx save/delete filter to API
- Document system_notif plugin (no routes needed)
- Fix datetime.utcnow() -> datetime.now(UTC)
- Pin litellm version (>=1.0,<2.0)
- Move CSRF token from sessionStorage to in-memory
- Fix restore_backup error handling and transaction
- Fix Dms.tsx useEffect cleanup
- Add skip-to-content link for accessibility
- Add selectinload imports to 3 services
- Add .env.example missing variables
- Fix AppShell/TopBar/Sidebar test mocks

NEW TESTS:
- test_guest_auth.py (6 tests)
- test_user_service.py (8 tests)
- test_backup_service.py (5 tests)

NEW SCHEMAS:
- saved_filter, saved_view, user_preference, workspace, entity_policy

Tests: 22/22 PASSED
2026-07-31 00:58:05 +02:00

106 lines
4.1 KiB
Python

"""Re-enable RLS fail-closed on all tenant tables.
This migration reverses the RLS disabling from migrations 0078-0081.
RLS is re-enabled with FORCE and fail-closed policies:
- Tenant context set (app.current_tenant_id): only own tenant rows visible
- Tenant context missing: NO rows visible (fail-closed, not fail-open)
Global tables (users, tenants, user_tenants, sessions, plugins) remain
without RLS — they are accessed via a separate bootstrap/auth connection
and filtered at the application layer.
Bootstrap and startup must use:
1. A separate connection (crm_auth/crm_bootstrap) for global tables
2. Per-tenant initialization with explicit tenant context:
SELECT set_config('app.current_tenant_id', :tenant_id, true);
Revision ID: 0084
Revises: 0083
""
from alembic import op
from sqlalchemy import text
revision = "0084"
down_revision = "0083"
branch_labels = None
depends_on = None
# Tables WITH tenant_id column — get fail-closed RLS
TENANT_TABLES = [
"groups", "roles", "system_settings", "currencies", "tax_rates", "sequences",
"saved_filters", "saved_views", "webhooks", "workspaces", "workspace_modules",
"workspace_users", "workspace_widgets", "user_preferences", "custom_field_definitions",
"backups", "share_links", "entity_links", "entity_history",
"contact_folder_permissions", "contact_folders", "guest_users", "guest_invitations",
"permission_delegations", "permission_templates", "entity_permissions", "entity_policies",
"entity_attachments", "files", "folders", "tags", "tag_assignments", "tasks", "subtasks",
"notification_preferences", "audit_log",
"automation_cron_jobs", "automation_definitions",
"automation_runs", "automation_versions", "automation_agent_definitions",
"automation_agent_runs", "automation_agent_versions",
"report_templates", "report_instances",
"consumer_inbox", "event_outbox", "outbox_deliveries",
]
def upgrade() -> None:
conn = op.get_bind()
for table in TENANT_TABLES:
# Check if table exists
exists = conn.execute(
text(f"SELECT 1 FROM information_schema.tables WHERE table_name = '{table}'")
).fetchone() is not None
if not exists:
continue
# Check if table has tenant_id column
has_tenant_id = conn.execute(
text(f"SELECT 1 FROM information_schema.columns WHERE table_name = '{table}' AND column_name = 'tenant_id'")
).fetchone() is not None
if not has_tenant_id:
continue
# Drop any existing policies
policies = conn.execute(text(
f"SELECT policyname FROM pg_policies WHERE tablename = '{table}'"
)).fetchall()
for (policyname,) in policies:
conn.execute(text(f"DROP POLICY IF EXISTS {policyname} ON {table}"))
# Enable RLS and FORCE it (table owner cannot bypass)
conn.execute(text(f"ALTER TABLE {table} ENABLE ROW LEVEL SECURITY"))
conn.execute(text(f"ALTER TABLE {table} FORCE ROW LEVEL SECURITY"))
# Fail-closed tenant isolation policy
# NULLIF converts empty string to NULL → comparison yields NULL → no rows returned
# This is fail-closed: missing tenant context = no access
conn.execute(text(f"""
CREATE POLICY {table}_tenant_isolation
ON {table}
AS PERMISSIVE
FOR ALL
TO crm_api
USING (
tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid
)
WITH CHECK (
tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid
)
"""))
def downgrade() -> None:
conn = op.get_bind()
for table in TENANT_TABLES:
exists = conn.execute(
text(f"SELECT 1 FROM information_schema.tables WHERE table_name = '{table}'")
).fetchone() is not None
if not exists:
continue
conn.execute(text(f"DROP POLICY IF EXISTS {table}_tenant_isolation ON {table}"))
conn.execute(text(f"ALTER TABLE {table} NO FORCE ROW LEVEL SECURITY"))
conn.execute(text(f"ALTER TABLE {table} DISABLE ROW LEVEL SECURITY"))