fix: ledger reads conversations as the signed-in user; database checks match the real roles (BUG-2057 follow-up)
The first staging gate for the batch failed on two database-backed tests that cannot run locally: the full table list lacked profile_avatar_images, and the ledger test asked a table privilege as a login role that has no access to the public schema. Following that test further showed the route read chat_sessions with the service role, which is revoked on that table, so question rows lost their link and synastry follow-ups were mislabelled. No migration change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
This commit is contained in:
@@ -27620,6 +27620,7 @@
|
||||
- 头像上传的服务端(界面在 BUG-2059):浏览器把图裁成不超过 256 × 256 的正方形小图再上传,原图不离开设备。`PUT / GET / DELETE /api/account/avatar/image`:只收 JPEG 和 WebP,类型和尺寸从文件自己的开头字节读,不看文件名和浏览器报的类型;上限 200 KB,边读边数;每小时最多 10 次。图片存在数据库新表 `profile_avatar_images`(迁移 `20261120100000_profile_avatar_images.sql`,可重复执行),只有本人能读写删,后台角色没有权限;数据库备份自动包含;注销账号时由现有清理流程和级联删除一并删掉。账户数据只多一个 `avatarImage`(版本号),不带图片字节。
|
||||
- 验证与防复发:`frontend/tests/credits-groundwork-2057.test.ts`(13 条:每个流水类型和功能键都有名称;未知的归「其他」并记日志;分页在同一毫秒的多条记录上不重不漏;接口只返回本人的记录和链接;开关关与开时套餐接口和下单接口的行为;改开关需要权限并留审计)。`frontend/tests/database-credit-ledger-2057.test.ts` 用真实表结构验证行级权限,需要 Docker,本机没有跑,要在流水线上确认。`frontend/tests/avatar-image-2057.test.ts`(6 条:类型、尺寸、超限各自的错误码;未登录 401;别人的头像读不到也删不掉;移除;限频;迁移版本唯一且是最新一个)。`database-account-deletion.test.ts` 增加「注销后本人的头像没有了、别人的还在」,同样需要 Docker。BUG-1022 的两条测试和 BUG-1526 的一条测试按原值、新值、原因更新。
|
||||
- 关联记录:BUG-1022、BUG-1526、BUG-1937、BUG-1939、BUG-1980、BUG-2040。
|
||||
- 补充(2026-11-20,推到测试环境后的第一次门禁):门禁失败,没有部署。两处都是这次新写、本机没有 Docker 没跑过的数据库测试:`database-local-business.test.ts` 的全表名单没有加 `profile_avatar_images`;`database-credit-ledger-2057.test.ts` 用还没有切换角色的登录角色去问后台角色的表权限,这个登录角色连 public 都进不去(`permission denied for schema public`)。改成名单加上新表、权限那一问用测试库的管理员身份。顺着这条测试往下核对时发现接口里一处真问题:点数记录用服务端角色去读对话表,而服务端角色对这张表没有任何权限(2026-07-15 的迁移明确收回),读取静默失败,结果是提问那几行没有跳转箭头、合盘追问会被写成普通提问。改成用登录用户自己的身份读对话表(行级权限只给本人的),测试里钉住这一点。迁移没有改动。教训:新增表要同时改全表名单;用服务端角色读一张表之前先查它有没有权限;只在门禁里能跑的测试,第一次推送后要看完门禁再算数。
|
||||
- 修复版本:`claude/credits-groundwork-2057`。
|
||||
|
||||
## BUG-2056 | 有几处操作的成功或失败还写在页面里,占着布局
|
||||
|
||||
@@ -132,7 +132,8 @@ export async function GET(request: Request) {
|
||||
if (reservations === null) return NextResponse.json({ error: "暂时无法读取点数记录" }, { status: 500 });
|
||||
|
||||
const sessionIds = [...new Set((consultationRequests ?? []).map((row) => row.session_id).filter(isUuid))];
|
||||
const sessions = sessionIds.length === 0 ? [] : await rowsOf<SessionRow>(admin
|
||||
// Read as the signed-in user: the service role has no access to this table.
|
||||
const sessions = sessionIds.length === 0 ? [] : await rowsOf<SessionRow>(supabase
|
||||
.from("chat_sessions")
|
||||
.select("id,session_type")
|
||||
.eq("user_id", userId)
|
||||
|
||||
@@ -165,6 +165,10 @@ test("BUG-2057: the ledger route reads as the user, filters by the session's use
|
||||
assert.match(route, new RegExp(`\\.from\\("${table}"\\)[\\s\\S]{0,140}\\.eq\\("user_id", userId\\)`), table);
|
||||
}
|
||||
assert.match(route, /\.from\("redemption_codes"\)[\s\S]{0,140}\.eq\("redeemed_by", userId\)/);
|
||||
// The service role is revoked on chat_sessions, so that one lookup goes through the user's own client.
|
||||
assert.match(route, /rowsOf<SessionRow>\(supabase\s+\.from\("chat_sessions"\)/);
|
||||
assert.match(migrations("20260715030000_user_profiles_chat_sessions.sql"),
|
||||
/revoke all on table public\.chat_sessions from anon, authenticated, service_role;[\s\S]*grant select on table public\.chat_sessions to authenticated;/);
|
||||
// Nothing internal is selected from the ledger.
|
||||
assert.match(route, /\.select\("id,transaction_type,amount,request_id,redemption_code_id,created_at"\)/);
|
||||
assert.doesNotMatch(route, /input_tokens|output_tokens|balance_after|"model"|code_hash|order_no/);
|
||||
|
||||
@@ -60,6 +60,10 @@ test("a signed-in user reads only their own credit ledger rows and avatar; the s
|
||||
await assert.rejects(user.query("select 1 from public.usage_reservations limit 1"), /permission denied/);
|
||||
await assert.rejects(user.query("select 1 from public.redemption_codes limit 1"), /permission denied/);
|
||||
|
||||
// Conversations are the other way round: the signed-in user reads them, the service role cannot.
|
||||
assert.equal((await user.query("select 1 from public.chat_sessions")).rowCount, 0);
|
||||
assert.equal(fixture.psql("select has_table_privilege('service_role', 'public.chat_sessions', 'select')"), "f");
|
||||
|
||||
// The uploaded avatar: a user reads, replaces and removes only their own row.
|
||||
const avatarRow = (id: string, version: string) =>
|
||||
`('${id}','image/jpeg',decode('ffd8ff','hex'),3,256,256,'${version}')`;
|
||||
@@ -88,11 +92,11 @@ test("a signed-in user reads only their own credit ledger rows and avatar; the s
|
||||
await assert.rejects(user.query(`insert into public.profile_avatar_images${columns} values ${bad}`), /check constraint/);
|
||||
}
|
||||
// The admin console's database role has no access at all.
|
||||
const adminRole = await service.query("select has_table_privilege('admin_runtime','public.profile_avatar_images','select') as can");
|
||||
assert.equal(adminRole.rows[0]?.can, false);
|
||||
// Asked as the fixture's superuser: a login role without a role set cannot even name the schema.
|
||||
assert.equal(fixture.psql("select has_table_privilege('admin_runtime', 'public.profile_avatar_images', 'select')"), "f");
|
||||
|
||||
await service.query("set role service_role");
|
||||
for (const table of ["usage_reservations", "consultation_requests", "personal_reports", "chat_sessions", "redemption_codes"]) {
|
||||
for (const table of ["usage_reservations", "consultation_requests", "personal_reports", "redemption_codes"]) {
|
||||
const result = await service.query(`select count(*)::int as n from public.${table}`);
|
||||
assert.equal(typeof result.rows[0]?.n, "number", table);
|
||||
}
|
||||
|
||||
@@ -370,6 +370,10 @@ test("local PostgreSQL applies the reviewed business schema and serves authentic
|
||||
"personal_reports",
|
||||
"pricing_experiment_events",
|
||||
"product_entitlements",
|
||||
// 原值: product_entitlements 之后直接是 profiles
|
||||
// 新值: 加上 profile_avatar_images
|
||||
// 原因: BUG-2057 新增用户上传头像的表
|
||||
"profile_avatar_images",
|
||||
"profiles",
|
||||
// 原值: profiles 之后直接是 redemption_attempts
|
||||
// 新值: 中间加入 rectification_telemetry 与 rectification_telemetry_reported_cases
|
||||
|
||||
Reference in New Issue
Block a user