fix(security): P0 安全修复 — Access Token 吊销 + OpenAPI 保护 + RLS 补齐 + CI 加固 + 测试修复
P0-5: Access Token 吊销机制 - 新增内存 DashMap 黑名单(token_hash → exp),支持单 token 吊销 - 密码修改/登出时自动清除用户权限缓存,强制重新认证 - 惰性清理过期条目,防止内存无限增长 P0-6: OpenAPI 端点安全 - 生产构建返回 404,仅 cfg(debug_assertions) 模式可用 - 防止 385+ API 端点 schema 对外暴露 P0-4: RLS 策略补充迁移 (m000169) - 幂等遍历所有含 tenant_id 的表,补齐缺失的 RLS 策略 - 覆盖 m000088 之后创建的约 20 张新表 P0-3: CI 安全加固 - 移除 CI 中硬编码密码 123123,改用 postgres - 保持 cargo audit / npm-audit 严格门禁 P0-7: AI prompt 集成测试修复 - get_active_prompt 改按 analysis_type 查找而非 name - list_prompts 过滤参数从 category 改为 analysis_type - 167 集成测试全部通过(原 164 passed / 3 failed)
This commit is contained in:
@@ -175,6 +175,7 @@ mod m20260526_000165_ai_prompt_fix_analysis_type;
|
||||
mod m20260526_000166_create_ai_knowledge_bases;
|
||||
mod m20260526_000167_create_ai_knowledge_documents;
|
||||
mod m20260527_000168_ai_knowledge_v2_menu;
|
||||
mod m20260529_000169_supplement_rls_for_new_tables;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -357,6 +358,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260526_000166_create_ai_knowledge_bases::Migration),
|
||||
Box::new(m20260526_000167_create_ai_knowledge_documents::Migration),
|
||||
Box::new(m20260527_000168_ai_knowledge_v2_menu::Migration),
|
||||
Box::new(m20260529_000169_supplement_rls_for_new_tables::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
let conn = manager.get_connection();
|
||||
|
||||
// 为 m000088 之后创建的新表补充 RLS 策略。
|
||||
// 幂等操作:仅影响尚未启用 RLS 或缺少策略的表。
|
||||
conn.execute_unprepared(
|
||||
r#"
|
||||
DO $$
|
||||
DECLARE
|
||||
tbl TEXT;
|
||||
policy_exists BOOLEAN;
|
||||
BEGIN
|
||||
FOR tbl IN
|
||||
SELECT c.table_name FROM information_schema.columns c
|
||||
JOIN information_schema.tables t
|
||||
ON c.table_name = t.table_name AND c.table_schema = t.table_schema
|
||||
WHERE c.column_name = 'tenant_id'
|
||||
AND c.table_schema = 'public'
|
||||
AND t.table_type = 'BASE TABLE'
|
||||
ORDER BY c.table_name
|
||||
LOOP
|
||||
-- 启用 RLS(幂等)
|
||||
EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', tbl);
|
||||
|
||||
-- 检查是否已有 tenant_isolation 策略
|
||||
SELECT EXISTS(
|
||||
SELECT 1 FROM pg_policies
|
||||
WHERE tablename = tbl
|
||||
AND policyname = 'tenant_isolation'
|
||||
) INTO policy_exists;
|
||||
|
||||
IF NOT policy_exists THEN
|
||||
EXECUTE format(
|
||||
'CREATE POLICY tenant_isolation ON %I USING (
|
||||
current_setting(''app.current_tenant_id'', true) != ''''
|
||||
AND tenant_id = current_setting(''app.current_tenant_id'', true)::uuid
|
||||
)',
|
||||
tbl
|
||||
);
|
||||
RAISE NOTICE 'Created RLS policy for table: %', tbl;
|
||||
END IF;
|
||||
END LOOP;
|
||||
END;
|
||||
$$;
|
||||
"#,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
// 回滚不需要移除 RLS,保持 m000088 的策略不变
|
||||
// 此迁移补充的 RLS 策略在 down() 中保留,因为 m000088 已处理回滚
|
||||
let _ = manager;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user