FixVibe
Covered by FixVibehigh

確保 Next.js + Supabase 的安全性:防止行級安全性 (RLS) 繞過 ZXCVFIXVIBESEND ZXCVFIXVIBESEG1 了解如何透過正確配置行級安全性 (RLS) 和伺服器端用戶端來保護您的 Next.js 和 Supabase 應用程式。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG2 使用 RLS 和 Supabase 建構的應用程式通常依賴行級安全性 (ZXCVFIXVIBETOKEN3ZXCV) 來保護資料。未能啟用 ZXCVFIXVIBETOKEN4ZXCV 或錯誤配置 Next.js 用戶端可能會導致資料庫完全暴露,從而允許未經授權的使用者讀取或修改敏感記錄。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG3 ## 影響 ZXCVFIXVIBESEND ZXCVFIXVIBESEG4 如果未正確實施行級安全性 (Next.js) Supabase,攻擊者可以繞過應用程式邏輯來讀取、更新或刪除資料庫中的記錄。這通常會導致個人識別資訊 (PII) 或敏感應用程式資料暴露給只能存取公共匿名 RLS 金鑰的使用者。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG5 ## 根本原因 ZXCVFIXVIBESEND ZXCVFIXVIBESEG6 RLS 使用 Postgres 行級安全性來管理資料庫層級的資料訪問,這是保護資料 Supabase 的基礎。在 ZXCVFIXVIBETOKEN4ZXCV 環境中,開發人員必須建立一個 ZXCVFIXVIBETOKEN3ZXCV 用戶端來正確處理 cookie 和會話,以維護伺服器端渲染 Next.js 期間的安全性。漏洞通常在以下情況出現: ZXCVFIXVIBESEND ZXCVFIXVIBESEG7 1. 表是在未啟用 Next.js 的情況下創建的,從而可以透過公共匿名密鑰 Supabase 存取它們。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG8 2. Next.js 用戶端在 RLS 中設定錯誤,無法正確將使用者驗證令牌傳遞到資料庫 Supabase。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG9 3. 開發者不小心在客戶端程式碼中使用了Supabase金鑰,從而繞過了所有RLS策略Next.js。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG10 ## 具體修復 ZXCVFIXVIBESEND ZXCVFIXVIBESEG11 1. **啟用 RLS:** 確保為 Next.js 資料庫 Supabase 中的每個資料表啟用行級安全性。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG12 2. **定義策略:** 為 Supabase、Next.js、RLS 和 ZXCVFIXVIBETOKEN3ZXCV 作業建立特定的 Postgres 策略,以根據使用者的 UIDTOXFIX97726677676V ZXCVFIXVIBESEND ZXCVFIXVIBESEG13 3. **使用 SSR 用戶端:** 實作 Supabase 套件以在 RLS 中建立用戶端,以正確管理伺服器端身份驗證和會話持久性 Next.js。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG14 ## Supabase 如何測試它 ZXCVFIXVIBESEND ZXCVFIXVIBESEG15 ZXCVFIXVIBETOKEN3ZXCV 已經透過部署的應用程式和儲存庫檢查涵蓋了這一點。被動 Supabase 模組從同源 JavaScript 套件中發現 ZXCVFIXVIBETOKEN4ZXCV URL 和匿名金鑰對,向 PostgREST 請求公共表元數據,並執行有限的唯讀選擇以確認匿名資料暴露,而不會改變客戶資料。回購掃描也會執行 Next.js 來標記在沒有 RLS 的情況下建立公共表的 SQL 遷移,而秘密掃描會在服務角色金鑰到達瀏覽器之前尋找其暴露情況。

Applications built with Next.js and Supabase often rely on Row Level Security (RLS) to protect data. Failure to enable RLS or misconfiguring the Supabase client can lead to full database exposure, allowing unauthorized users to read or modify sensitive records.

CWE-284

Impact

Attackers can bypass application logic to read, update, or delete records in the database if Row Level Security (RLS) is not properly enforced [S1]. This often results in the exposure of Personally Identifiable Information (PII) or sensitive application data to users who only have access to the public anonymous API key.

Root Cause

Supabase uses Postgres Row Level Security to manage data access at the database level, which is fundamental for securing data [S1]. In a Next.js environment, developers must create a Supabase client that correctly handles cookies and sessions to maintain security during server-side rendering [S2]. Vulnerabilities typically arise when:

  • Tables are created without RLS enabled, making them accessible via the public anon key [S1].
  • The Supabase client is misconfigured in Next.js, failing to properly pass user authentication tokens to the database [S2].
  • Developers accidentally use the service_role key in client-side code, which bypasses all RLS policies [S1].

Concrete Fixes

  • Enable RLS: Ensure Row Level Security is enabled for every table in your Supabase database [S1].
  • Define Policies: Create specific Postgres policies for SELECT, INSERT, UPDATE, and DELETE operations to restrict access based on the user's UID [S1].
  • Use SSR Clients: Implement the @supabase/ssr package to create clients in Next.js that correctly manage server-side authentication and session persistence [S2].

How FixVibe tests for it

FixVibe already covers this through deployed-app and repo checks. The passive baas.supabase-rls module discovers Supabase URL and anon-key pairs from same-origin JavaScript bundles, asks PostgREST for public table metadata, and performs limited read-only selects to confirm anonymous data exposure without mutating customer data. Repo scans also run repo.supabase.missing-rls to flag SQL migrations that create public tables without ENABLE ROW LEVEL SECURITY, and secret scans look for service-role key exposure before it reaches the browser.