Repository navigation
feat(worker): eNotify 导出/导入后端实现 (ONE-359) - #146
Merged
Merged
Conversation
按父单 ONE-358 v1.0 冻结规格实现应用级导出/导入后端:
- 六个接口:/api/me/{export,import/preview,import} 与
/api/admin/{export,import/preview,import},只认 cookie 会话,
带 enk_ API Key 直接 403(备份文件等同主钥匙包)。
- 格式:7 张表经 {prefix} 解析,行=列名对象;JSON 列导出为解析值,
解析失败的字段保留原串并写入该行 _raw,导入按原串写回;
未知列忽略、缺失列交给现行 schema 默认值;format_version 高于
支持的 1 时明确拒绝。
- 加密:WebCrypto AES-GCM-256 + PBKDF2(SHA-256, 210k, 16B salt)、
12B IV,信封 {format,kdf,cipher,payload};错口令零写入。
- 恢复模式:仅 instance 文件、目标 users 必须为空(非空 409);
全部写入为单次 db.batch(D1 单事务),写前全量校验 + 写后分表
计数断言,失败整体回滚。
- 合并模式:仅个人数据,ID 全部重生成、user_id 改写、
payment_history.item_id 按旧→新映射改写、config 整行覆盖,
users/user_2fa/system_settings/notification_history 忽略并明示。
- 审计:导出/导入各一条 [audit] backup 结构化日志(操作者、scope/
mode、是否加密、各表行数),口令不落盘不进日志。
- 上传:JSON 请求体(file 可为对象或原始文本),上限 10MB → 413;
每账号 20 次/分钟限流。
测试:新增 5 个测试文件(60 例),用 node:sqlite 跑真实 schema 的
D1 适配器;全量 187 例通过。前端契约见 worker/src/lib/backup/CONTRACT.md。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
父单 ONE-358 的后端实现子单(ONE-359)。规格 v1.0 冻结,本 PR 只做后端;前端是另一子单(Stage 2),按
worker/src/lib/backup/CONTRACT.md对接。一、最终路由命名与请求/响应形状
POST /api/me/exportscope: "user")POST /api/me/import/previewPOST /api/me/importPOST /api/admin/exportscope: "instance")POST /api/admin/import/previewmode缺省 restore)POST /api/admin/importmode缺省 restore,可传 merge)Authorization: Bearer enk_...一律403 {code:"API_KEY_NOT_ALLOWED"}(即使同时带有效会话);未登录 401,非管理员访问 admin 路由 403。{ encrypted?: boolean (默认 true), passphrase?: string, include_history?: boolean };响应体就是文件内容(加密信封或明文导出文件)+Content-Disposition: attachment+Cache-Control: no-store。{ file: object | "<json text>", passphrase?: string, mode?: "restore"|"merge" }。{ ok, mode, scope, encrypted, format_version, app_version, exported_at, counts, will_write, users[], can_execute, blockers[], conflicts{...}, ignored_tables[], warnings[] }。{ ok, mode, scope, written, total_written, notices[], warnings[] }(restore 的 notices 含「API Key 需重新签发、所有用户重新登录」)。{ error, code, details? };完整码表见worker/src/lib/backup/CONTRACT.md§6。二、上传方式与上限
JSON 请求体(对齐现行
web/src/lib/api.ts全 JSON 的惯例),file既可以是解析后的对象,也可以是文件原文。整个请求体上限 10 MB,超出413 PAYLOAD_TOO_LARGE(details.max_bytes)。另外每账号 20 次/分钟限流(429)。三、原子性方案及证明
db.batch([...])。D1 的 batch 是单条 SQL 事务:任一条失败整批回滚。STALE_PREVIEW),批后按「写入前 + 应写入」逐表断言实际行数(RESTORE_COUNT_MISMATCH)。worker/test/backup-restore.test.ts里注入payment_historyINSERT 失败(该表在批的中后段,之前已写 users/user_2fa/items/config),断言batchCalls === 1且七张表行数全为 0;merge 侧同款用例断言 items/config 也未落库。四、审计落点
现行 schema 没有审计表,沿用仓库已有的
[audit]console 约定(worker/src/middleware/auth.ts的 impersonation 日志)。每次导出/导入写一条结构化日志:口令不落盘、不进日志(测试断言审计行里不含口令串)。
五、实现要点
worker/src/lib/backup/format.ts— v1 格式契约:7 表顺序、JSON 列清单(items.channels/notification_hours、user_2fa.passkey_credentials、notification_configs.enabled_channels+各*_config+notification_hours)、_raw兜底、assertPlainExport校验(含format_version上限拒绝)。default_channel按标量原样。worker/src/lib/backup/crypto.ts— 只用 WebCrypto:PBKDF2-SHA256 210,000 迭代 + 16B 随机 salt → AES-GCM-256,12B 随机 IV,信封{format,format_version,kdf,cipher,payload};读取侧对 kdf/cipher 参数做白名单校验(迭代次数下限 210k、上限 10M 防 DoS)。worker/src/lib/backup/export.ts— 按{prefix}选表;scope: "user"只取本人行、system_settings恒空;notification_history默认不带。worker/src/lib/backup/import.ts— 规划(dry-run)与执行共用同一套校验;restore 用PRAGMA table_info拿现行列与默认值,未知列丢弃、缺失列交给 DB 默认值;merge 重生成 ID、改写user_id/item_id、config 整行覆盖(INSERT OR REPLACE)、跳过找不到对应通知的缴费记录并计入 warnings。worker/src/routes/backup.ts— 六个端点 + 会话-only 守卫 + 10MB 上限 + 审计;挂载于worker/src/index.ts,且注册在主 me/admin 路由之前,保证 API Key 请求先被显式拒绝。worker/src/lib/backup/CONTRACT.md— 前端对接契约(路由、形状、码表、UI 必写提示)。未改 schema、未改迁移机制、未碰会话/JWT、未对
user_2fa做任何转换。六、测试
新增
worker/test/backup-{format,crypto,restore,merge,auth}.test.ts(60 例)。测试用node:sqlite起真实内存 SQLite,加载仓库自己的worker/src/db/schema.sql(按前缀替换),因此默认值、约束、外键、batch 事务语义都是真的,不是手搓 mock。覆盖:导出→导入往返全等(JSON 列、
_raw兜底行、passkey_credentials、农历字段、default_channel、ID/时间戳/金额)、未知字段忽略 + 缺失字段现行默认值、高版本 format_version 被拒、加密往返 + 信封字段 + 错口令零写入 + 篡改拒绝、非空实例被拒、空实例恢复逐表行数与关键行(channels config、一条 items)与源一致、失败注入无半套数据、{prefix}前缀库恢复、合并模式 ID 全重生成 + 缴费引用指向新 ID + 他人数据不带入 + config 覆盖与预检一致 + 孤儿缴费跳过、未登录/非管理员 401/403 + API Key 被拒 + 10MB 超限 + 非法 JSON。CI 结果属外部系统,不在本 PR 等待范围。