fix: admin role could not read usage tables behind row-level security (BUG-1935)
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m32s
Independent Staging Quality Gate / validate-python (push) Successful in 6m54s
Independent Staging Quality Gate / publish (push) Successful in 3m8s

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
This commit is contained in:
2026-11-16 16:18:09 +08:00
co-authored by Claude Fable 5.2
parent 052e8ab6ad
commit a814661ddb
3 changed files with 74 additions and 0 deletions
+12
View File
@@ -26035,3 +26035,15 @@
- 验证与防复发:公开测试盘父母题本地样本(DeepSeek):改前 3 份每份 2 处「这条线」,改后 4 份合计 1 处;「底色」和补充套话改后 0 处。相关断言随新字句更新(原值 / 新值 / 原因写在测试注释里),问答测试 808 通过。未解决:「判断依据」里仍会出现 Narayana 的内部状态用语(该句受占星师裁定约束,未动)。Gemini 中转当天在本地只返回几百字的短答案(未改动的代码同样如此),因此没有 Gemini 的改后样本。
- 关联记录:BUG-1929、BUG-1928、BUG-1920。
- 修复版本:`claude/echo-words-1930`,待 staging 实测。
## BUG-1935 | 后台「用量和成本」、上线配置的实测成本和毛利、订阅列表一直是空的
- 状态:fixed-pending-verify(待 staging 实测)。
- 首次发现 / 最近更新:2026-11-16 / 2026-11-16。
- 触发与现象:产品负责人在 staging 后台填好模型输入、输出单价后,上线配置页四项功能的实测成本都显示「还没有数据」、毛利「算不出」;用量和成本页也没有记录。当天他在同一环境完成过多次聊天。用量聚合接口返回八项功能 runs 全为 0。
- 触发条件:self-hosted 环境(后台用 `admin_runtime` 角色读库)下,后台读取 `usage_ledger`、`usage_reservations`、`usage_events`、`user_subscriptions`、`user_product_redemptions`、`notification_templates`、`pricing_experiment_events`。
- 根因:这些表启用了行级安全,迁移只给 `admin_runtime` 授了 SELECT,没有建它的 SELECT 策略;读取不报错,只是被静默过滤成 0 行。与 BUG-147 同一根因;当时只补了出问题的那几张表,没有检查其余授权表,之后 `feature_pricing`、`feature_flags` 又各补过一次,仍没有全量核对。
- 修复:新迁移给上述七张表各建 `<表名>_admin_read` 策略(`for select to admin_runtime using (true)`)。
- 验证与防复发:新增 `frontend/tests/admin-runtime-read-policies-20261116.test.ts`,扫描全部迁移:凡启用行级安全且授予 `admin_runtime` SELECT 的表都必须有它的策略,否则失败。线上效果待部署后由产品负责人在后台确认(本会话没有后台和数据库权限)。
- 关联记录:BUG-147、BUG-135。
- 修复版本:`claude/admin-usage-rls-1935`,待 staging 实测。
@@ -0,0 +1,26 @@
-- BUG-1935: the admin role had SELECT grants on these tables but no row-level
-- security policy, so every admin read returned zero rows without an error
-- (用量和成本、上线配置里的实测成本、订阅列表都是空的). Same cause as BUG-147.
begin;
do $$
declare
t text;
begin
if exists (select 1 from pg_roles where rolname = 'admin_runtime') then
foreach t in array array[
'usage_ledger', 'usage_reservations', 'usage_events',
'user_subscriptions', 'user_product_redemptions',
'notification_templates', 'pricing_experiment_events'
] loop
if to_regclass('public.' || t) is not null then
execute format('grant select on table public.%I to admin_runtime', t);
execute format('drop policy if exists %I on public.%I', t || '_admin_read', t);
execute format('create policy %I on public.%I for select to admin_runtime using (true)', t || '_admin_read', t);
end if;
end loop;
end if;
end;
$$;
commit;
@@ -0,0 +1,36 @@
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
const MIGRATIONS = fileURLToPath(new URL("../supabase/migrations/", import.meta.url));
const NEW_MIGRATION = "20261116040000_admin_runtime_usage_read_policies.sql";
// BUG-1935: a table with row-level security and a SELECT grant to admin_runtime
// but no admin_runtime policy reads as empty, with no error. Every such table
// must have a policy, either written out or listed in the BUG-1935 migration.
test("every RLS table the admin role may read has an admin read policy (BUG-1935)", () => {
const granted = new Set<string>();
const withPolicy = new Set<string>();
const rls = new Set<string>();
for (const file of readdirSync(MIGRATIONS).filter((name) => name.endsWith(".sql")).sort()) {
const sql = readFileSync(join(MIGRATIONS, file), "utf8");
for (const match of sql.matchAll(/grant\s+([a-z, ]+?)\s+on\s+(?:table\s+)?([a-z_.,\s]+?)\s+to\s+([a-z_, ]+);/gi)) {
if (!match[3]!.includes("admin_runtime") || !/select/i.test(match[1]!)) continue;
for (const table of match[2]!.split(",").map((name) => name.trim())) if (table.startsWith("public.")) granted.add(table);
}
for (const match of sql.matchAll(/create policy\s+\S+\s+on\s+(public\.[a-z_]+)([\s\S]*?);/gi)) {
if (match[2]!.includes("admin_runtime")) withPolicy.add(match[1]!);
}
for (const match of sql.matchAll(/alter table\s+(?:if exists\s+)?(public\.[a-z_]+)\s+enable row level security/gi)) rls.add(match[1]!);
}
const listed = readFileSync(join(MIGRATIONS, NEW_MIGRATION), "utf8");
for (const match of listed.matchAll(/'([a-z_]+)'/g)) withPolicy.add(`public.${match[1]}`);
assert.match(listed, /create policy %I on public\.%I for select to admin_runtime using \(true\)/);
const missing = [...granted].filter((table) => rls.has(table) && !withPolicy.has(table)).sort();
assert.deepEqual(missing, []);
for (const table of ["usage_ledger", "usage_reservations", "usage_events", "user_subscriptions"]) {
assert.ok(listed.includes(`'${table}'`), table);
}
});