FOUNDATIONS PHASE 2 · 22
Security / RLS:不要相信 Client 說「我是誰」
安全設計先找 trust boundary:哪些 input 可被使用者控制?哪些 identity 已被驗證?哪些規則必須在 server/database 再 enforce?RLS 是把 row authorization 下沉到資料層的一種工具。
Learning outcomes
- 能標出 client/server/database trust boundary。
- 能區分 validation、authentication、authorization。
- 能解釋 least privilege。
- 能描述 RLS 為何比只靠 frontend filter 更可靠。
1. Client Input 永遠可被修改
{
"owner_id": "admin",
"score": 999
}DevTools、curl、script 都能跳過 UI。Server 必須重新驗證資料與 identity。
2. 三個不同問題
Validation:
score 合法嗎?
Authentication:
你是誰?
Authorization:
你能改這筆 course 嗎?3. Least Privilege
Credential/user/process 只拿完成工作所需最小權限。能用普通 user session 完成的操作,不應拿 privileged admin secret 在 Browser 執行。
4. RLS
courses.owner_id
=
authenticated_user_idRow Level Security 可讓 database 依已驗證 identity 決定哪些 rows 能 SELECT/UPDATE/DELETE。
5. RLS 不是資料 Validation
RLS
誰能操作哪列。
Constraint
資料本身是否合法。
Project checkpoint:Course Workspace Trust Map
Browser
untrusted input
↓ HTTPS
Server/Auth
verified identity
↓
Database
RLS authorization
constraints integrityDebug evidence:User A 看到了 User B 資料
- request authenticated identity 是誰?
- query 是否用了 client supplied owner_id?
- RLS 是否 enabled?
- policy 是否真的限制 owner?
- privileged key 是否誤下放?
Knowledge check
- Client validation 為什麼不是 security boundary?
- AuthN/AuthZ 差在哪?
- Least privilege 是什麼?
- score range 應用 RLS 還是 constraint?