7c20b518d4
Repositorio estava ha ~5 sessoes sem commit. Consolida tudo desde d088a89.
Ver commit.md na raiz para descricao completa por sessao.
# Numeros
- A# auditoria abertos: 0/30
- V# verificacoes abertos: 5/52 (todos adiados com plano)
- T# testes escritos: 10/10
- Vitest: 192/192
- SQL integration: 33/33
- E2E (Playwright, novo): 5/5
- Migrations: 17 (10 novas Sessao 6)
- Areas auditadas: 7 (+documentos com 10 V#)
# Highlights Sessao 6 (hoje)
- V#34/V#41 Opcao B2: tenant_features com plano + override (RPC SECURITY DEFINER, tela /saas/tenant-features)
- A#20 rev2 self-hosted: defesa em 5 camadas (honeypot + rate limit + math captcha condicional + paranoid mode + dashboard /saas/security)
- Documentos hardening (V#43-V#49): tenant scoping em storage policies (vazamento entre clinicas eliminado), RPC validate_share_token, signatures policy granular
- SaaS Twilio Config (/saas/twilio-config): UI editavel para SID/webhook/cotacao; AUTH_TOKEN permanece em env var
- T#9 + T#10: useAgendaEvents.spec.js + Playwright E2E (descobriu bug no front que foi corrigido)
# Sessoes anteriores (1-5) consolidadas
- Sessao 1: auth/router/session, normalizeRole extraido
- Sessao 2: agenda - composables/services consolidados
- Sessao 3: pacientes - tenant_id em todas queries
- Sessao 4: security review pagina publica - 14/15 vulnerabilidades corrigidas
- Sessao 5: SaaS - P0 (A#30: 7 tabelas com RLS off corrigidas)
# .gitignore ajustado
- supabase/* + !supabase/functions/ (mantem 10 edge functions, ignora .temp/migrations gerados pelo CLI)
- database-novo/backups/ (regeneravel via db.cjs backup)
- test-results/ + playwright-report/
- .claude/settings.local.json (config local com senha de dev removida do tracking)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
168 lines
9.3 KiB
SQL
168 lines
9.3 KiB
SQL
-- =============================================================================
|
|
-- Migration: 20260418000005_saas_rls_emergency_fix
|
|
-- Corrige A#30 (P0) — 7 tabelas SaaS estavam com RLS desabilitado + grants
|
|
-- totais pra anon/authenticated/service_role. Qualquer usuário anônimo
|
|
-- podia alterar/deletar dados críticos (tenant_features, plan_prices,
|
|
-- subscription_intents_personal/tenant, plan_public, ...).
|
|
--
|
|
-- Estratégia:
|
|
-- 1. Habilitar RLS em todas as 7 tabelas
|
|
-- 2. REVOKE ALL de anon (nunca deveria ter tido)
|
|
-- 3. REVOKE ALL de authenticated (controle passa a ser via policy)
|
|
-- 4. Policies explícitas por caso de uso
|
|
-- =============================================================================
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 1. REVOKE grants inseguros
|
|
-- -----------------------------------------------------------------------------
|
|
REVOKE ALL ON public.tenant_features FROM anon, authenticated;
|
|
REVOKE ALL ON public.plan_prices FROM anon, authenticated;
|
|
REVOKE ALL ON public.plan_public FROM anon, authenticated;
|
|
REVOKE ALL ON public.plan_public_bullets FROM anon, authenticated;
|
|
REVOKE ALL ON public.subscription_intents_personal FROM anon, authenticated;
|
|
REVOKE ALL ON public.subscription_intents_tenant FROM anon, authenticated;
|
|
REVOKE ALL ON public.tenant_feature_exceptions_log FROM anon, authenticated;
|
|
|
|
-- Concede o mínimo necessário (controlado por RLS abaixo)
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON public.tenant_features TO authenticated;
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON public.plan_prices TO authenticated;
|
|
GRANT SELECT ON public.plan_public TO anon, authenticated;
|
|
GRANT INSERT, UPDATE, DELETE ON public.plan_public TO authenticated;
|
|
GRANT SELECT ON public.plan_public_bullets TO anon, authenticated;
|
|
GRANT INSERT, UPDATE, DELETE ON public.plan_public_bullets TO authenticated;
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON public.subscription_intents_personal TO authenticated;
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON public.subscription_intents_tenant TO authenticated;
|
|
GRANT SELECT ON public.tenant_feature_exceptions_log TO authenticated;
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 2. HABILITAR RLS em todas
|
|
-- -----------------------------------------------------------------------------
|
|
ALTER TABLE public.tenant_features ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.plan_prices ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.plan_public ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.plan_public_bullets ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.subscription_intents_personal ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.subscription_intents_tenant ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE public.tenant_feature_exceptions_log ENABLE ROW LEVEL SECURITY;
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 3. POLICIES — tenant_features
|
|
-- -----------------------------------------------------------------------------
|
|
-- SELECT: membros do tenant leem as features do próprio tenant. Saas admin lê tudo.
|
|
DROP POLICY IF EXISTS tenant_features_select ON public.tenant_features;
|
|
CREATE POLICY tenant_features_select ON public.tenant_features
|
|
FOR SELECT TO authenticated
|
|
USING (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (SELECT tm.tenant_id FROM public.tenant_members tm WHERE tm.user_id = auth.uid() AND tm.status = 'active')
|
|
);
|
|
|
|
-- WRITE: apenas tenant_admin do próprio tenant OU saas_admin.
|
|
DROP POLICY IF EXISTS tenant_features_write ON public.tenant_features;
|
|
CREATE POLICY tenant_features_write ON public.tenant_features
|
|
FOR ALL TO authenticated
|
|
USING (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (
|
|
SELECT tm.tenant_id FROM public.tenant_members tm
|
|
WHERE tm.user_id = auth.uid()
|
|
AND tm.status = 'active'
|
|
AND tm.role IN ('tenant_admin','admin')
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (
|
|
SELECT tm.tenant_id FROM public.tenant_members tm
|
|
WHERE tm.user_id = auth.uid()
|
|
AND tm.status = 'active'
|
|
AND tm.role IN ('tenant_admin','admin')
|
|
)
|
|
);
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 4. POLICIES — plan_prices (SaaS admin only pra escrita; authenticated lê)
|
|
-- -----------------------------------------------------------------------------
|
|
DROP POLICY IF EXISTS plan_prices_read ON public.plan_prices;
|
|
CREATE POLICY plan_prices_read ON public.plan_prices
|
|
FOR SELECT TO authenticated
|
|
USING (true); -- preços são públicos pra usuários logados
|
|
|
|
DROP POLICY IF EXISTS plan_prices_write ON public.plan_prices;
|
|
CREATE POLICY plan_prices_write ON public.plan_prices
|
|
FOR ALL TO authenticated
|
|
USING (public.is_saas_admin())
|
|
WITH CHECK (public.is_saas_admin());
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 5. POLICIES — plan_public + plan_public_bullets (anon pode ler — landing page)
|
|
-- -----------------------------------------------------------------------------
|
|
DROP POLICY IF EXISTS plan_public_read_anon ON public.plan_public;
|
|
CREATE POLICY plan_public_read_anon ON public.plan_public
|
|
FOR SELECT TO anon, authenticated
|
|
USING (true);
|
|
|
|
DROP POLICY IF EXISTS plan_public_write ON public.plan_public;
|
|
CREATE POLICY plan_public_write ON public.plan_public
|
|
FOR ALL TO authenticated
|
|
USING (public.is_saas_admin())
|
|
WITH CHECK (public.is_saas_admin());
|
|
|
|
DROP POLICY IF EXISTS plan_public_bullets_read_anon ON public.plan_public_bullets;
|
|
CREATE POLICY plan_public_bullets_read_anon ON public.plan_public_bullets
|
|
FOR SELECT TO anon, authenticated
|
|
USING (true);
|
|
|
|
DROP POLICY IF EXISTS plan_public_bullets_write ON public.plan_public_bullets;
|
|
CREATE POLICY plan_public_bullets_write ON public.plan_public_bullets
|
|
FOR ALL TO authenticated
|
|
USING (public.is_saas_admin())
|
|
WITH CHECK (public.is_saas_admin());
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 6. POLICIES — subscription_intents_personal + _tenant
|
|
-- -----------------------------------------------------------------------------
|
|
-- Dono vê o próprio intent; saas admin vê tudo; owner cria/atualiza seus próprios.
|
|
DROP POLICY IF EXISTS subscription_intents_personal_owner ON public.subscription_intents_personal;
|
|
CREATE POLICY subscription_intents_personal_owner ON public.subscription_intents_personal
|
|
FOR ALL TO authenticated
|
|
USING (user_id = auth.uid() OR public.is_saas_admin())
|
|
WITH CHECK (user_id = auth.uid() OR public.is_saas_admin());
|
|
|
|
DROP POLICY IF EXISTS subscription_intents_tenant_member ON public.subscription_intents_tenant;
|
|
CREATE POLICY subscription_intents_tenant_member ON public.subscription_intents_tenant
|
|
FOR ALL TO authenticated
|
|
USING (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (
|
|
SELECT tm.tenant_id FROM public.tenant_members tm
|
|
WHERE tm.user_id = auth.uid()
|
|
AND tm.status = 'active'
|
|
AND tm.role IN ('tenant_admin','admin')
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (
|
|
SELECT tm.tenant_id FROM public.tenant_members tm
|
|
WHERE tm.user_id = auth.uid()
|
|
AND tm.status = 'active'
|
|
AND tm.role IN ('tenant_admin','admin')
|
|
)
|
|
);
|
|
|
|
-- ─────────────────────────────────────────────────────────────────────────
|
|
-- 7. POLICY — tenant_feature_exceptions_log (somente leitura)
|
|
-- -----------------------------------------------------------------------------
|
|
-- Log de auditoria. Inserts vêm de triggers/funções server-side (SECURITY DEFINER).
|
|
DROP POLICY IF EXISTS tenant_feature_exceptions_log_read ON public.tenant_feature_exceptions_log;
|
|
CREATE POLICY tenant_feature_exceptions_log_read ON public.tenant_feature_exceptions_log
|
|
FOR SELECT TO authenticated
|
|
USING (
|
|
public.is_saas_admin()
|
|
OR tenant_id IN (SELECT tm.tenant_id FROM public.tenant_members tm WHERE tm.user_id = auth.uid() AND tm.status = 'active')
|
|
);
|
|
|
|
COMMENT ON TABLE public.tenant_features IS
|
|
'Controle de features por tenant. RLS: member do tenant lê; tenant_admin ou saas_admin escreve. Antes da migration 20260418000005 estava com RLS off + GRANT ALL pra anon (A#30).';
|