Skip to content

RLS & Bảo mật

TÓM TẮT

Supabase hỗ trợ Row Level Security (RLS) để kiểm soát truy cập data ở database level. Hiện tại chủ yếu dùng Anon Key cho frontend (read-only) và Service Role Key cho backend (read-write).

Mô hình truy cập hiện tại

ClientKey typeQuyền
Frontend (React)Anon KeyRead-only (transactions, forecasts, configs)
Backend (Python)Service Role KeyFull CRUD (ML results, training jobs)
GitHub ActionsService Role KeyWrite ML results

RLS Policies (Cần cấu hình)

HIỆN TRẠNG

RLS policies hiện chưa được enable đầy đủ. SQL schema có comment hướng dẫn nhưng chưa áp dụng.

sql
-- Ví dụ: Enable RLS
ALTER TABLE transactions ENABLE ROW LEVEL SECURITY;
ALTER TABLE forecast_results ENABLE ROW LEVEL SECURITY;

-- Ví dụ: Policy cho authenticated users
CREATE POLICY "Users can view own transactions"
  ON transactions FOR SELECT
  USING (client_id = auth.jwt() ->> 'client_id');

Khuyến nghị bảo mật

  1. Enable RLS cho tất cả bảng chứa data nhạy cảm
  2. Không expose Service Role Key trong frontend code
  3. Sử dụng environment variables cho tất cả API keys
  4. Configure CORS origins chỉ cho domain production
  5. Audit access logs định kỳ

Tài liệu liên quan

BoxMe Forecast — Tài liệu kỹ thuật nội bộ