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 策略,以根据用户的 UID ZXCVFIXVIBETOKEN4ZXCV 限制访问。 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.