qiyu-cli 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +464 -2
- package/dist/cli.js +248 -0
- package/dist/commands/api.js +770 -0
- package/dist/commands/auth.js +349 -0
- package/dist/commands/credentials-source.js +70 -0
- package/dist/commands/db.js +503 -0
- package/dist/commands/local-fallback-wiring.js +67 -0
- package/dist/commands/local-fallback.js +34 -0
- package/dist/commands/query.js +564 -0
- package/dist/commands/skill-specs.js +64 -0
- package/dist/commands/skill.js +863 -0
- package/dist/commands/spec-types.js +145 -0
- package/dist/commands/stats.js +299 -0
- package/dist/commands/store-flags.js +56 -0
- package/dist/commands/store.js +258 -0
- package/dist/commands/sync-run.js +479 -0
- package/dist/commands/sync.js +757 -0
- package/dist/commands/tree.js +27 -0
- package/dist/core/config.js +259 -0
- package/dist/core/credentials.js +71 -0
- package/dist/core/edition.js +18 -0
- package/dist/core/envelope.js +201 -0
- package/dist/core/errors.js +356 -0
- package/dist/core/http.js +348 -0
- package/dist/core/ratelimit.js +126 -0
- package/dist/core/registry/index.js +212 -0
- package/dist/core/registry/params/ai.knowledge.addKnowledge.json +88 -0
- package/dist/core/registry/params/ai.knowledge.deleteKnowledges.json +46 -0
- package/dist/core/registry/params/ai.knowledge.editChannelAnswer.json +60 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledge.json +95 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledgeNoSimilars.json +102 -0
- package/dist/core/registry/params/ai.knowledge.getCategoryTree.json +39 -0
- package/dist/core/registry/params/ai.knowledge.getDetail.json +46 -0
- package/dist/core/registry/params/ai.knowledge.search.json +67 -0
- package/dist/core/registry/params/ai.statistic.transferHuman.json +60 -0
- package/dist/core/registry/params/category.list.json +39 -0
- package/dist/core/registry/params/chat.queue.group.query.json +39 -0
- package/dist/core/registry/params/crm.modifyOrder.json +48 -0
- package/dist/core/registry/params/crm.modifyUser.json +48 -0
- package/dist/core/registry/params/crm.syncCrmInfo.json +46 -0
- package/dist/core/registry/params/crm.tagChange.json +74 -0
- package/dist/core/registry/params/data.overview.ipcc.json +32 -0
- package/dist/core/registry/params/data.overview.session.json +53 -0
- package/dist/core/registry/params/evaluation.setting.get.json +39 -0
- package/dist/core/registry/params/event.applyStaff.json +116 -0
- package/dist/core/registry/params/event.evaluate.json +74 -0
- package/dist/core/registry/params/event.inviteEvaluate.json +81 -0
- package/dist/core/registry/params/event.qcResult.json +166 -0
- package/dist/core/registry/params/event.queryQueueStatus.json +39 -0
- package/dist/core/registry/params/event.queueEnter.json +40 -0
- package/dist/core/registry/params/event.queueLeave.json +46 -0
- package/dist/core/registry/params/event.quitQueue.json +39 -0
- package/dist/core/registry/params/event.sessionChange.json +95 -0
- package/dist/core/registry/params/event.sessionData.json +452 -0
- package/dist/core/registry/params/event.sessionEnd.json +117 -0
- package/dist/core/registry/params/event.sessionStart.json +109 -0
- package/dist/core/registry/params/event.updateUInfo.json +46 -0
- package/dist/core/registry/params/export.session.check.json +7 -0
- package/dist/core/registry/params/export.session.json +7 -0
- package/dist/core/registry/params/export.session.one.json +7 -0
- package/dist/core/registry/params/export.session.one.message.json +7 -0
- package/dist/core/registry/params/get_call_order.json +50 -0
- package/dist/core/registry/params/get_call_user_info.json +36 -0
- package/dist/core/registry/params/get_order.json +50 -0
- package/dist/core/registry/params/get_token.json +22 -0
- package/dist/core/registry/params/get_user_info.json +36 -0
- package/dist/core/registry/params/icoutcall.callIntercept.list.json +32 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.fuzzySearch.json +53 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.getDialogFlowList.json +32 -0
- package/dist/core/registry/params/icoutcall.get.customer.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobdetail.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobproperties.json +39 -0
- package/dist/core/registry/params/icoutcall.history.seat.json +67 -0
- package/dist/core/registry/params/icoutcall.history.team.json +67 -0
- package/dist/core/registry/params/icoutcall.job.create.json +53 -0
- package/dist/core/registry/params/icoutcall.job.importcustomer.json +46 -0
- package/dist/core/registry/params/icoutcall.job.pause.json +39 -0
- package/dist/core/registry/params/icoutcall.job.readdcustomer.json +109 -0
- package/dist/core/registry/params/icoutcall.job.start.json +39 -0
- package/dist/core/registry/params/icoutcall.job.terminate.json +39 -0
- package/dist/core/registry/params/icoutcall.phone.getLineList.json +32 -0
- package/dist/core/registry/params/ipcc.calltask.add.auto.json +117 -0
- package/dist/core/registry/params/ipcc.calltask.add.byCustom.json +96 -0
- package/dist/core/registry/params/ipcc.calltask.add.byRelation.json +82 -0
- package/dist/core/registry/params/ipcc.calltask.add.toGroup.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.add.toStaff.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.get.json +47 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.add.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.delete.json +40 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.get.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.update.json +40 -0
- package/dist/core/registry/params/ipcc.customfield.template.get.json +47 -0
- package/dist/core/registry/params/ipcc.login.json +47 -0
- package/dist/core/registry/params/kefu.add.json +130 -0
- package/dist/core/registry/params/kefu.delete.json +39 -0
- package/dist/core/registry/params/kefu.update.json +137 -0
- package/dist/core/registry/params/kefu.v2.query.json +39 -0
- package/dist/core/registry/params/kefuGroup.add.json +39 -0
- package/dist/core/registry/params/kefuGroup.delete.json +39 -0
- package/dist/core/registry/params/kefuGroup.update.json +46 -0
- package/dist/core/registry/params/kefuGroup.updateKefus.json +46 -0
- package/dist/core/registry/params/merchant.account.login.json +53 -0
- package/dist/core/registry/params/merchant.changePassword.json +46 -0
- package/dist/core/registry/params/merchant.create.json +81 -0
- package/dist/core/registry/params/merchant.disable.json +39 -0
- package/dist/core/registry/params/merchant.enable.json +39 -0
- package/dist/core/registry/params/merchant.list.json +46 -0
- package/dist/core/registry/params/merchant.query.json +46 -0
- package/dist/core/registry/params/merchant.update.json +74 -0
- package/dist/core/registry/params/message.send.image.json +53 -0
- package/dist/core/registry/params/message.send.json +53 -0
- package/dist/core/registry/params/message.send.voice.json +53 -0
- package/dist/core/registry/params/message.sendFile.json +39 -0
- package/dist/core/registry/params/message.uploadFile.json +39 -0
- package/dist/core/registry/params/online.session.category.list.json +39 -0
- package/dist/core/registry/params/online.session.customfield.template.json +39 -0
- package/dist/core/registry/params/online.session.customfield.update.json +74 -0
- package/dist/core/registry/params/order.upload.json +40 -0
- package/dist/core/registry/params/push.msg.image.json +74 -0
- package/dist/core/registry/params/push.msg.richText.json +74 -0
- package/dist/core/registry/params/push.msg.text.json +74 -0
- package/dist/core/registry/params/receive.message.json +89 -0
- package/dist/core/registry/params/robot.data.getQuestionsByCategoryName.json +67 -0
- package/dist/core/registry/params/robot.data.knowledge.json +46 -0
- package/dist/core/registry/params/robot.data.kpoint.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardByEffectiveQuestion.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardQuestionList.json +46 -0
- package/dist/core/registry/params/robot.evaluation.comment.json +67 -0
- package/dist/core/registry/params/robot.evaluation.setting.json +46 -0
- package/dist/core/registry/params/robot.session.assist.json +74 -0
- package/dist/core/registry/params/robot.session.chat.json +74 -0
- package/dist/core/registry/params/robot.session.close.json +53 -0
- package/dist/core/registry/params/robot.session.commonQuestions.json +74 -0
- package/dist/core/registry/params/robot.session.create.json +81 -0
- package/dist/core/registry/params/robot.session.evaluation.json +67 -0
- package/dist/core/registry/params/robot.session.msgAssociate.json +46 -0
- package/dist/core/registry/params/robot.session.msgEvaluation.json +53 -0
- package/dist/core/registry/params/robot.session.msgPredict.json +74 -0
- package/dist/core/registry/params/robot.session.transfer.json +36 -0
- package/dist/core/registry/params/robot.session.uploadFile.json +32 -0
- package/dist/core/registry/params/robot.statistic.summary.json +53 -0
- package/dist/core/registry/params/robot.transfer.config.json +15 -0
- package/dist/core/registry/params/session.user.profile.json +39 -0
- package/dist/core/registry/params/settings.wxTemplates.json +46 -0
- package/dist/core/registry/params/smstask.create.json +53 -0
- package/dist/core/registry/params/smstask.status.json +39 -0
- package/dist/core/registry/params/staff.group.list.new.json +39 -0
- package/dist/core/registry/params/staff.group.members.new.json +46 -0
- package/dist/core/registry/params/staff.list.new.json +46 -0
- package/dist/core/registry/params/staff.login.json +47 -0
- package/dist/core/registry/params/statistic.overview.json +61 -0
- package/dist/core/registry/params/statistic.performance.json +82 -0
- package/dist/core/registry/params/statistic.salaryDetail.json +54 -0
- package/dist/core/registry/params/statistic.satisfaction.report.json +68 -0
- package/dist/core/registry/params/statistic.staffAttendance.json +75 -0
- package/dist/core/registry/params/statistic.staffquality.json +75 -0
- package/dist/core/registry/params/statistic.staffworklod.json +68 -0
- package/dist/core/registry/params/tag.all.json +32 -0
- package/dist/core/registry/params/tag.batchUpdate.json +32 -0
- package/dist/core/registry/params/tag.update.json +60 -0
- package/dist/core/registry/params/ticket.apply.json +46 -0
- package/dist/core/registry/params/ticket.batch.modify.json +53 -0
- package/dist/core/registry/params/ticket.create.json +151 -0
- package/dist/core/registry/params/ticket.filter.count.json +46 -0
- package/dist/core/registry/params/ticket.filter.list.json +46 -0
- package/dist/core/registry/params/ticket.finish.json +60 -0
- package/dist/core/registry/params/ticket.followers.update.json +60 -0
- package/dist/core/registry/params/ticket.list.json +74 -0
- package/dist/core/registry/params/ticket.log.json +39 -0
- package/dist/core/registry/params/ticket.modify.json +74 -0
- package/dist/core/registry/params/ticket.new.detail.json +39 -0
- package/dist/core/registry/params/ticket.reminder.json +60 -0
- package/dist/core/registry/params/ticket.reopen.json +67 -0
- package/dist/core/registry/params/ticket.reply.json +60 -0
- package/dist/core/registry/params/ticket.search.json +116 -0
- package/dist/core/registry/params/ticket.template.fields.json +39 -0
- package/dist/core/registry/params/ticket.template.list.json +39 -0
- package/dist/core/registry/params/ticket.transfer.json +74 -0
- package/dist/core/registry/params/track.event.set.json +32 -0
- package/dist/core/registry/params/wecom.robot.switch.json +43 -0
- package/dist/core/registry/params.js +84 -0
- package/dist/core/registry/specs.json +2172 -0
- package/dist/core/registry/validate.json +11 -0
- package/dist/core/registry/validation-overrides.js +98 -0
- package/dist/core/sanitize.js +24 -0
- package/dist/core/sign.js +21 -0
- package/dist/core/time.js +194 -0
- package/dist/core/validate.js +140 -0
- package/dist/output.js +263 -0
- package/dist/postinstall.js +178 -0
- package/dist/query/errors.js +395 -0
- package/dist/query/mask.js +368 -0
- package/dist/query/metric-catalog.js +85 -0
- package/dist/query/sandbox.js +544 -0
- package/dist/query/table-query.js +297 -0
- package/dist/query/time-shape.js +44 -0
- package/dist/schedule/install.js +377 -0
- package/dist/skill/check.js +429 -0
- package/dist/skill/detect.js +336 -0
- package/dist/skill/install.js +1202 -0
- package/dist/skill/paths.js +343 -0
- package/dist/skill/qiyu/SKILL.md +86 -0
- package/dist/skill/qiyu/errors.md +55 -0
- package/dist/skill/qiyu/forbidden.md +52 -0
- package/dist/skill/qiyu/metrics.md +65 -0
- package/dist/skill/qiyu/protocols.md +55 -0
- package/dist/skill/qiyu/setup.md +50 -0
- package/dist/skill/refs.js +379 -0
- package/dist/store/config.js +137 -0
- package/dist/store/duckdb.js +321 -0
- package/dist/store/index.js +18 -0
- package/dist/store/retry.js +36 -0
- package/dist/store/schema.js +419 -0
- package/dist/store/types.js +4 -0
- package/dist/sync/day.js +8 -0
- package/dist/sync/engine.js +818 -0
- package/dist/sync/rate-gate.js +95 -0
- package/dist/sync/registry.js +22 -0
- package/dist/sync/state.js +114 -0
- package/dist/sync/tasks/dim.js +186 -0
- package/dist/sync/tasks/knowledge.js +252 -0
- package/dist/sync/tasks/pace.js +66 -0
- package/dist/sync/tasks/pipeline.js +321 -0
- package/dist/sync/tasks/reports.js +448 -0
- package/dist/sync/tasks/sessions.js +188 -0
- package/dist/sync/tasks/tickets.js +219 -0
- package/dist/sync/types.js +1 -0
- package/dist/sync/windowing.js +56 -0
- package/package.json +52 -4
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// src/query/sandbox.ts
|
|
2
|
+
// E 组的三层只读沙箱(spec §5.1–§5.4)—— `query --sql` 的唯一执行入口。
|
|
3
|
+
//
|
|
4
|
+
// ⚠️ 三层缺一不可(spec §5.4 全部实测):
|
|
5
|
+
// layer1 语句白名单:extractStatements 条数闸门 + prepare().statementType ∈ {SELECT, EXPLAIN}
|
|
6
|
+
// + type=4 追加「无 ANALYZE 记号」闸门(EXPLAIN ANALYZE 会真正执行内层语句,探针 16)
|
|
7
|
+
// layer2 文件/网络封锁:enable_external_access=false + lock_configuration=true
|
|
8
|
+
// layer3 只读打开:DuckDBInstance.create(path, { access_mode: 'READ_ONLY' })
|
|
9
|
+
// 每层单独都有实测穿透:仅 layer3 时 read_csv / COPY TO 仍成功(tests/query/sandbox.test.ts
|
|
10
|
+
// 「三层对照」用例钉死);仅 layer2 时 INSERT/CREATE/DROP 不碰文件系统、照写不误;
|
|
11
|
+
// 纯 EXPLAIN 的内层写/文件访问仍会尝试执行(分别由 layer3/layer2 兜底)。三者叠加才是完整防线。
|
|
12
|
+
// ⚠️ EXPLAIN ANALYZE 是特例:内层 CALL/PRAGMA 改的是**实例级配置**(如 enable_profiling),
|
|
13
|
+
// 不碰文件、不是库写 —— layer2/3 都拦不住,且会把 profile 树打到 stdout 破坏「stdout 只出
|
|
14
|
+
// JSON」的契约。所以必须在 layer1 直接拒绝(见 hasAnalyzeToken)。
|
|
15
|
+
//
|
|
16
|
+
// ⚠️ 分层约束(spec §5.1):本文件不 import `sync/`、`commands/`、`store/duckdb.ts` 的
|
|
17
|
+
// Store 实现。依赖缺失的识别函数 `isModuleNotFound` 定义在 `store/retry.ts`(架构审查
|
|
18
|
+
// M-4 后下沉,让 query/ 的依赖面与 spec 白名单逐字一致),两条路径的 hint 逐字一致由它保证。
|
|
19
|
+
//
|
|
20
|
+
// ⚠️ 依赖加载:`@duckdb/node-api` 只经 dynamic import(与 store/duckdb.ts 同款惰性边界)。
|
|
21
|
+
import { isLockError, retryWithBackoff, isModuleNotFound } from '../store/retry.js';
|
|
22
|
+
const defaultImporter = () => import('@duckdb/node-api');
|
|
23
|
+
// —— 加固序列(⚠️ 顺序敏感,spec §5.2 探针 3/6/10)——
|
|
24
|
+
// TimeZone / memory_limit 必须在 lock_configuration 之前设:lock 之后连
|
|
25
|
+
// `SET TimeZone='UTC'` 都报 `configuration has been locked`。
|
|
26
|
+
// memory_limit='1GB' 是硬编码常量(YAGNI,不设配置项):128MB 下大排序实测 OOM,
|
|
27
|
+
// 1GB 足以覆盖本库任何表,同时防跑飞。
|
|
28
|
+
const HARDENING_SQL = [
|
|
29
|
+
"SET TimeZone='Asia/Shanghai'",
|
|
30
|
+
"SET memory_limit='1GB'",
|
|
31
|
+
'SET enable_external_access=false',
|
|
32
|
+
'SET lock_configuration=true',
|
|
33
|
+
];
|
|
34
|
+
// 加固自检(spec §5.2 第 7 步):防 DuckDB 未来改名导致 layer2 静默失效。
|
|
35
|
+
// 含 memory_limit —— 只读 ea/lc 时「删除 memory_limit 加固行」不会有任何测试/自检
|
|
36
|
+
// 变红(审查 I-5);它同样是 spec §5.2 声明的加固项,必须自证生效。
|
|
37
|
+
const SELF_CHECK_SQL = "SELECT current_setting('enable_external_access') AS ea, " +
|
|
38
|
+
"current_setting('lock_configuration') AS lc, " +
|
|
39
|
+
"current_setting('memory_limit') AS ml";
|
|
40
|
+
/**
|
|
41
|
+
* DuckDB 的 memory_limit 回显 → 字节数(实测格式 `953.6 MiB` / `51.1 GiB` / `1.0 GiB`)。
|
|
42
|
+
* **无法解析 → 返回 0**,调用方必须把 0 当成失败(fail-closed)——否则未来 DuckDB 改
|
|
43
|
+
* 回显格式时自检会静默放行一个没加固的会话(复审发现的 fail-open)。
|
|
44
|
+
* 支持 B/KiB/MiB/GiB/TiB(spaced 或紧贴写法都认)。
|
|
45
|
+
*/
|
|
46
|
+
export function memoryLimitBytes(display) {
|
|
47
|
+
if (typeof display !== 'string')
|
|
48
|
+
return 0;
|
|
49
|
+
const m = /^([\d.]+)\s*(B|KiB|MiB|GiB|TiB)$/.exec(display.trim());
|
|
50
|
+
if (m === null)
|
|
51
|
+
return 0;
|
|
52
|
+
const unit = { B: 1, KiB: 1024, MiB: 1024 ** 2, GiB: 1024 ** 3, TiB: 1024 ** 4 };
|
|
53
|
+
const n = Number(m[1]) * unit[m[2]];
|
|
54
|
+
return Number.isFinite(n) ? n : 0;
|
|
55
|
+
}
|
|
56
|
+
// statementType 数值 → 类型名(spec §5.3 映射表;实测值见任务 brief)。
|
|
57
|
+
// ⚠️ 补全 3/5/9/15(edge F6):此前 DELETE/UPDATE/DROP/ALTER 被拒时报「类型 5」这类裸数字,
|
|
58
|
+
// Agent 既查不到含义也无法据此改查询;spec §十一要求点明类型名。
|
|
59
|
+
const TYPE_NAMES = {
|
|
60
|
+
2: 'INSERT', 3: 'UPDATE', 5: 'DELETE', 7: 'CREATE', 9: 'ALTER', 11: 'COPY',
|
|
61
|
+
15: 'DROP', 16: 'EXPORT', 17: 'PRAGMA', 19: 'CALL', 20: 'SET', 21: 'INSTALL',
|
|
62
|
+
25: 'ATTACH',
|
|
63
|
+
};
|
|
64
|
+
/** 所有抛出的错误都带 `kind`(+ 可选 `code`/`hint`/`retryable`),供命令层 thrownToExit 同族映射。 */
|
|
65
|
+
function sandboxError(kind, message, extra = {}) {
|
|
66
|
+
const e = new Error(message);
|
|
67
|
+
e.kind = kind;
|
|
68
|
+
if (extra.code !== undefined)
|
|
69
|
+
e.code = extra.code;
|
|
70
|
+
if (extra.hint !== undefined)
|
|
71
|
+
e.hint = extra.hint;
|
|
72
|
+
if (extra.retryable !== undefined)
|
|
73
|
+
e.retryable = extra.retryable;
|
|
74
|
+
// 原生错误原文(裁决 R9):Task 5 的 classifyDuckDbError 从这里补算表/列候选。
|
|
75
|
+
if (extra.cause !== undefined)
|
|
76
|
+
e.cause = extra.cause;
|
|
77
|
+
return e;
|
|
78
|
+
}
|
|
79
|
+
/** 依赖缺失 → store_not_initialized(与 store/duckdb.ts 的 depError 同口径、同 hint)。 */
|
|
80
|
+
function depError(e) {
|
|
81
|
+
const mn = isModuleNotFound(e);
|
|
82
|
+
return sandboxError('store_not_initialized', `无法加载 DuckDB 依赖:${e instanceof Error ? e.message : String(e)}`, {
|
|
83
|
+
hint: mn && mn.missing.startsWith('@duckdb/node-bindings')
|
|
84
|
+
? '当前平台无 DuckDB 预编译包(无 node-gyp 回退)。请在受支持的平台上安装 @duckdb/node-api@1.5.6-r.1'
|
|
85
|
+
: '未安装 DuckDB 依赖。请在 qiyu-cli 的安装环境中运行:npm install @duckdb/node-api@1.5.6-r.1',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** ro 撞写锁退避超限 → store_locked(消息/hint 与 store/duckdb.ts 同款,spec §5.2)。 */
|
|
89
|
+
function lockedError(cfg) {
|
|
90
|
+
return sandboxError('store_locked', `数据库被写进程占用(等待 ${cfg.readOnlyRetryMs}ms 后超时):${cfg.path}`, {
|
|
91
|
+
retryable: true,
|
|
92
|
+
hint: '请稍后重试;等待上限可经配置文件 readOnlyRetryMs(默认 30000 毫秒)调整,或用 --db 指向其他数据库文件',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function typeName(t) {
|
|
96
|
+
return TYPE_NAMES[t] ?? `类型 ${t}`;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 列名去重(与 @duckdb/node-api 的 `deduplicatedColumnNames()` 规则一致,但用 Map):
|
|
100
|
+
* 重复名第 2 次起加 `:N` 后缀(`x`、`x:1`、`x:2`)。
|
|
101
|
+
*
|
|
102
|
+
* 为什么不直接调驱动的同名方法:它用普通对象当计数器,`__proto__` 这类名字会命中原型链
|
|
103
|
+
* (`columnNameCount['__proto__']` 是 `Object.prototype`,不是 undefined),导致重复列
|
|
104
|
+
* 不加后缀 —— 实测 `SELECT a AS "__proto__", a AS "__proto__"` 返回两个 `"__proto__"`,
|
|
105
|
+
* 而行对象只有 `{}`(键互相覆盖)。Map 没有这个洞。
|
|
106
|
+
*/
|
|
107
|
+
function deduplicateNames(names) {
|
|
108
|
+
const seen = new Map();
|
|
109
|
+
return names.map((name) => {
|
|
110
|
+
const count = (seen.get(name) ?? 0) + 1;
|
|
111
|
+
seen.set(name, count);
|
|
112
|
+
return count > 1 ? `${name}:${count - 1}` : name;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 剥掉 SQL 里的注释与字符串/标识符字面量,只留下「可能承载关键字」的骨架。
|
|
117
|
+
*
|
|
118
|
+
* 为什么要剥(而不是直接对原文查 ANALYZE):
|
|
119
|
+
* `EXPLAIN SELECT 'ANALYZE' AS s` / `EXPLAIN SELECT 1 AS "ANALYZE"` / 注释里的
|
|
120
|
+
* ANALYZE 都是合法且无害的 —— 直接查会把它们误拒。
|
|
121
|
+
* `keepDoubleQuoted`:保留双引号标识符原文。EXPLAIN 的选项名可以被双引号包裹
|
|
122
|
+
* (实测 `EXPLAIN ("ANALYZE")` 同样执行内层),选项列表必须按原文检查;
|
|
123
|
+
* 其余位置则把 `"…"` 当作普通字面量剥掉(列名/别名里的 ANALYZE 不误伤)。
|
|
124
|
+
* 剥不干净的怪输入(未闭合引号/注释、dollar 引号不配对)→ malformed,
|
|
125
|
+
* 由调用方拒绝(fail closed)。
|
|
126
|
+
*
|
|
127
|
+
* ⚠️ 两条实测词法事实(复审复现,别改回简化版):
|
|
128
|
+
* 1. **行注释的终止符是 `\n` 或 `\r`**:DuckDB 认 `\r`(`EXPLAIN --x\rANALYZE CALL …`
|
|
129
|
+
* 实测 type=4 且执行内层)。只找 `\n` 会让 `\r` 之后的整段被当成注释丢掉 → 漏判 ANALYZE。
|
|
130
|
+
* 2. **块注释可嵌套**:实测在块注释里再开一层块注释是合法 SQL(DuckDB 按深度闭合,
|
|
131
|
+
* 不是「遇到第一个结束标记就收」);若内层结束后紧跟一个引号,按「第一个结束标记」
|
|
132
|
+
* 剥会把剩余部分误当字符串字面量 → 漏判 ANALYZE(复审给出的绕过句正是这种形态)。
|
|
133
|
+
* 必须按深度扫描(开标记 +1、结束标记 −1,深度归零才结束)。
|
|
134
|
+
*/
|
|
135
|
+
function stripSqlNoise(sql, keepDoubleQuoted) {
|
|
136
|
+
let out = '';
|
|
137
|
+
let i = 0;
|
|
138
|
+
const n = sql.length;
|
|
139
|
+
while (i < n) {
|
|
140
|
+
const ch = sql[i];
|
|
141
|
+
const next = i + 1 < n ? sql[i + 1] : '';
|
|
142
|
+
// 行注释:-- 到 \n 或 \r(实测 DuckDB 两者都终止;\r\n 由先到的 \r 覆盖)
|
|
143
|
+
if (ch === '-' && next === '-') {
|
|
144
|
+
let j = i + 2;
|
|
145
|
+
while (j < n && sql[j] !== '\n' && sql[j] !== '\r')
|
|
146
|
+
j += 1;
|
|
147
|
+
if (j >= n)
|
|
148
|
+
break; // 注释到输入末尾:后面确实没有内容
|
|
149
|
+
out += '\n';
|
|
150
|
+
i = j + 1;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
// 块注释:/* … */,**按嵌套深度**扫描(实测 DuckDB 支持嵌套;未闭合 → malformed)
|
|
154
|
+
if (ch === '/' && next === '*') {
|
|
155
|
+
let depth = 1;
|
|
156
|
+
let j = i + 2;
|
|
157
|
+
while (j < n && depth > 0) {
|
|
158
|
+
if (sql[j] === '/' && j + 1 < n && sql[j + 1] === '*') {
|
|
159
|
+
depth += 1;
|
|
160
|
+
j += 2;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (sql[j] === '*' && j + 1 < n && sql[j + 1] === '/') {
|
|
164
|
+
depth -= 1;
|
|
165
|
+
j += 2;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
j += 1;
|
|
169
|
+
}
|
|
170
|
+
if (depth > 0)
|
|
171
|
+
return { skeleton: out, malformed: true };
|
|
172
|
+
out += ' ';
|
|
173
|
+
i = j;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
// 单引号字符串:'' 为转义
|
|
177
|
+
if (ch === "'") {
|
|
178
|
+
i += 1;
|
|
179
|
+
let closed = false;
|
|
180
|
+
while (i < n) {
|
|
181
|
+
if (sql[i] === "'") {
|
|
182
|
+
if (i + 1 < n && sql[i + 1] === "'") {
|
|
183
|
+
i += 2;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
i += 1;
|
|
187
|
+
closed = true;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
i += 1;
|
|
191
|
+
}
|
|
192
|
+
if (!closed)
|
|
193
|
+
return { skeleton: out, malformed: true };
|
|
194
|
+
out += " '' ";
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
// 双引号标识符:"" 为转义
|
|
198
|
+
if (ch === '"') {
|
|
199
|
+
const start = i;
|
|
200
|
+
i += 1;
|
|
201
|
+
let closed = false;
|
|
202
|
+
while (i < n) {
|
|
203
|
+
if (sql[i] === '"') {
|
|
204
|
+
if (i + 1 < n && sql[i + 1] === '"') {
|
|
205
|
+
i += 2;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
i += 1;
|
|
209
|
+
closed = true;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
i += 1;
|
|
213
|
+
}
|
|
214
|
+
if (!closed)
|
|
215
|
+
return { skeleton: out, malformed: true };
|
|
216
|
+
out += keepDoubleQuoted ? ` ${sql.slice(start, i)} ` : " '' ";
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
// dollar 引号:$tag$ … $tag$(tag 可为空;标签内只允许标识符字符)
|
|
220
|
+
if (ch === '$') {
|
|
221
|
+
const m = /^\$[A-Za-z_][A-Za-z0-9_]*\$|^\$\$/.exec(sql.slice(i));
|
|
222
|
+
if (m) {
|
|
223
|
+
const tag = m[0];
|
|
224
|
+
const end = sql.indexOf(tag, i + tag.length);
|
|
225
|
+
if (end === -1)
|
|
226
|
+
return { skeleton: out, malformed: true };
|
|
227
|
+
out += " '' ";
|
|
228
|
+
i = end + tag.length;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
out += ch;
|
|
233
|
+
i += 1;
|
|
234
|
+
}
|
|
235
|
+
return { skeleton: out, malformed: false };
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* type=4 的内层闸门:语句骨架里出现 ANALYZE 记号即视为「EXPLAIN ANALYZE 家族」。
|
|
239
|
+
*
|
|
240
|
+
* 实测(@duckdb/node-api 1.5.6-r.1,spec §5.3 探针 16):
|
|
241
|
+
* · `EXPLAIN ANALYZE CALL enable_profiling()` → 报告 type=4,且**真正执行**内层 CALL,
|
|
242
|
+
* 把实例配置 enable_profiling 改成 query_tree —— 之后每条查询的 profile 树都打到
|
|
243
|
+
* stdout,直接破坏「stdout 只出 JSON」的契约;
|
|
244
|
+
* · `EXPLAIN (ANALYZE)`、`EXPLAIN (ANALYZE false)`、`EXPLAIN (ANALYZE, FORMAT JSON)`、
|
|
245
|
+
* `EXPLAIN ("ANALYZE")`、`EXPLAIN ANALYSE`(同义拼写)同样执行内层;选项值不影响;
|
|
246
|
+
* · 纯 `EXPLAIN CALL …` 不执行内层(配置不变)。
|
|
247
|
+
* 所以按记号一刀切拒绝;双引号标识符只在 EXPLAIN 选项列表位置按原文检查
|
|
248
|
+
*(`EXPLAIN SELECT 1 AS "ANALYZE"` 这类列名/别名不误伤)。DuckDB 还接受英式拼写
|
|
249
|
+
* ANALYSE,一并覆盖。极端误拒(列名恰叫 analyze 且被 EXPLAIN)是 fail closed,
|
|
250
|
+
* hint 里说明去掉 ANALYZE 即可。
|
|
251
|
+
*/
|
|
252
|
+
function hasAnalyzeToken(sql) {
|
|
253
|
+
// ① 普通骨架:字符串/标识符/注释全部剥掉后查裸记号。
|
|
254
|
+
const bare = stripSqlNoise(sql, false);
|
|
255
|
+
if (bare.malformed)
|
|
256
|
+
return true; // 解析不了的怪输入:宁可拒绝
|
|
257
|
+
if (/\bANALY[ZS]E\b/i.test(bare.skeleton))
|
|
258
|
+
return true;
|
|
259
|
+
// ② EXPLAIN 选项列表:双引号保留原文,`("ANALYZE")` 也会执行内层。
|
|
260
|
+
const quoted = stripSqlNoise(sql, true);
|
|
261
|
+
if (quoted.malformed)
|
|
262
|
+
return true;
|
|
263
|
+
const opts = /^\s*EXPLAIN\s*\(([^)]*)\)/i.exec(quoted.skeleton);
|
|
264
|
+
return opts !== null && /\bANALY[ZS]E\b/i.test(opts[1]);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* prepare 期原生错误 → 粗分类(spec §十一;细化由 Task 5 的 classifyDuckDbError 负责)。
|
|
268
|
+
* layer2 的拦截就发生在 prepare 期(`Permission Error: … file system operations are
|
|
269
|
+
* disabled by configuration`),所以这里必须把「文件/网络访问」与普通 SQL 错误分开。
|
|
270
|
+
* ⚠️ 只认 layer2 的两个特征串,不能把泛化的 `Permission Error` 也算进来 ——
|
|
271
|
+
* 读库文件本身的 OS 权限错误也带这个前缀,误报成「沙箱拦截」会误导用户。
|
|
272
|
+
*/
|
|
273
|
+
function mapPrepareError(e) {
|
|
274
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
275
|
+
if (/file system operations are disabled|Cannot access file/i.test(msg)) {
|
|
276
|
+
return sandboxError('usage', '该语句尝试访问文件或网络,已被只读沙箱拦截', {
|
|
277
|
+
code: 'sandbox_blocked',
|
|
278
|
+
hint: '沙箱只允许读取数据库内的数据:read_csv / read_text / glob / COPY TO / ATTACH / EXPORT / INSTALL 均不可用',
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
// 裁决 R9:把原生错误挂在 cause 上,Task 5 从原文补算表/列候选(message 格式保持不变)。
|
|
282
|
+
return sandboxError('usage', `SQL 无法执行:${msg}`, {
|
|
283
|
+
code: 'query_error',
|
|
284
|
+
hint: '请检查表名、列名与 SQL 语法',
|
|
285
|
+
cause: e,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* 以三层防护打开只读沙箱会话。`query/` 只暴露 `run` / `close` —— 不存在
|
|
290
|
+
* 「可信 SQL 绕过白名单」的旁路(spec §5.2:旁路 API 本身就是攻击面)。
|
|
291
|
+
*/
|
|
292
|
+
export async function openSandbox(cfg, deps = {}) {
|
|
293
|
+
let mod;
|
|
294
|
+
try {
|
|
295
|
+
mod = (await (deps.importer ?? defaultImporter)());
|
|
296
|
+
}
|
|
297
|
+
catch (e) {
|
|
298
|
+
throw depError(e);
|
|
299
|
+
}
|
|
300
|
+
// layer3:真的以 READ_ONLY 打开(ro→ro 并发可用的前提)。
|
|
301
|
+
const open = async () => {
|
|
302
|
+
const inst = await mod.DuckDBInstance.create(cfg.path, { access_mode: 'READ_ONLY' });
|
|
303
|
+
try {
|
|
304
|
+
return { inst, conn: await inst.connect() };
|
|
305
|
+
}
|
|
306
|
+
catch (e) {
|
|
307
|
+
// create 成功但 connect 失败 → instance 泄漏(锁可能随进程滞留),失败路径自清理。
|
|
308
|
+
try {
|
|
309
|
+
inst.closeSync?.();
|
|
310
|
+
}
|
|
311
|
+
catch { /* 清理错误不掩盖原异常 */ }
|
|
312
|
+
throw e;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
let inst;
|
|
316
|
+
let conn;
|
|
317
|
+
try {
|
|
318
|
+
const r = await retryWithBackoff({
|
|
319
|
+
tryOpen: open,
|
|
320
|
+
isRetryable: isLockError,
|
|
321
|
+
maxMs: cfg.readOnlyRetryMs,
|
|
322
|
+
sleep: deps.sleep,
|
|
323
|
+
now: deps.now,
|
|
324
|
+
});
|
|
325
|
+
inst = r.inst;
|
|
326
|
+
conn = r.conn;
|
|
327
|
+
}
|
|
328
|
+
catch (e) {
|
|
329
|
+
if (isLockError(e))
|
|
330
|
+
throw lockedError(cfg);
|
|
331
|
+
// 其余打开失败(库不存在 / 非 DuckDB 文件 / 权限)→ store_not_initialized(spec §十一:
|
|
332
|
+
// 库不存在 → 退出码 1、hint 指 `qiyu store init`)。底层原文保留在 message 里。
|
|
333
|
+
throw sandboxError('store_not_initialized', `无法打开数据库:${e instanceof Error ? e.message : String(e)}`, { hint: '请确认数据库文件存在且由本 CLI 创建(qiyu store init),或用 --db 指向其他数据库文件' });
|
|
334
|
+
}
|
|
335
|
+
// ⚠️ 关闭顺序与 store/duckdb.ts:305 同:先 conn 再 inst —— 只关 instance 不关 conn,
|
|
336
|
+
// 锁会随进程存活、永不释放。两个 closeSync 都包 try,清理期错误不掩盖原结果。
|
|
337
|
+
const closeRaw = () => {
|
|
338
|
+
try {
|
|
339
|
+
conn.closeSync?.();
|
|
340
|
+
}
|
|
341
|
+
catch { /* 清理期错误不掩盖 */ }
|
|
342
|
+
try {
|
|
343
|
+
inst.closeSync?.();
|
|
344
|
+
}
|
|
345
|
+
catch { /* 清理期错误不掩盖 */ }
|
|
346
|
+
};
|
|
347
|
+
// layer2:加固 + 自检。任一步失败都先关连接再抛 —— 不留半加固的会话。
|
|
348
|
+
try {
|
|
349
|
+
for (const sql of HARDENING_SQL)
|
|
350
|
+
await conn.run(sql);
|
|
351
|
+
const r = await conn.runAndReadAll(SELF_CHECK_SQL);
|
|
352
|
+
const row = r.getRowObjectsJson()[0] ?? {};
|
|
353
|
+
// memory_limit 的回显是格式化字符串(实测 `SET memory_limit='1GB'` → "953.6 MiB",
|
|
354
|
+
// 未加固时是机器内存的 80%,本机 51.1 GiB),无法回读原值 —— 按**上限**断言:
|
|
355
|
+
// 加固的意义是「封顶」(1GB → 953.6 MiB),上限取 1 GiB 既容得下加固值、
|
|
356
|
+
// 又挡得住未加固的默认值(内存 ≥1.25 GiB 的机器默认值即 >1 GiB;更小的机器上
|
|
357
|
+
// 默认值本就接近加固值,漏判无害)。解析失败返回 0 必须算失败(fail-closed,
|
|
358
|
+
// 复审 I-5 残留):否则 DuckDB 改回显格式时静默放行一个没封顶的会话。
|
|
359
|
+
const mlBytes = memoryLimitBytes(row.ml);
|
|
360
|
+
if (row.ea !== false || row.lc !== true || mlBytes === 0 || mlBytes > 1024 ** 3) {
|
|
361
|
+
throw sandboxError('internal', `只读沙箱自检失败:enable_external_access=${String(row.ea)},`
|
|
362
|
+
+ `lock_configuration=${String(row.lc)},memory_limit=${String(row.ml)}`, { hint: '当前 DuckDB 版本的配置项可能已改名;请报告此问题,不要在此状态下查询' });
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch (e) {
|
|
366
|
+
closeRaw();
|
|
367
|
+
if (e instanceof Error && e.kind !== undefined)
|
|
368
|
+
throw e;
|
|
369
|
+
throw sandboxError('internal', `只读沙箱加固失败:${e instanceof Error ? e.message : String(e)}`, { hint: '数据库配置无法锁定;请报告此问题,不要在此状态下查询' });
|
|
370
|
+
}
|
|
371
|
+
let closed = false;
|
|
372
|
+
const run = async (sql, opts = {}) => {
|
|
373
|
+
if (closed)
|
|
374
|
+
throw sandboxError('usage', '数据库连接已关闭', { code: 'query_error' });
|
|
375
|
+
const params = opts.params ?? [];
|
|
376
|
+
const limit = opts.limit ?? 1000;
|
|
377
|
+
const timeoutMs = opts.timeoutMs ?? 30000;
|
|
378
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 10000) {
|
|
379
|
+
throw sandboxError('usage', `limit 必须是 1–10000 的整数(收到 ${String(opts.limit)})`, { code: 'query_error' });
|
|
380
|
+
}
|
|
381
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs < 0) {
|
|
382
|
+
throw sandboxError('usage', `timeoutMs 必须是非负数(0 = 无超时,收到 ${String(opts.timeoutMs)})`, { code: 'query_error' });
|
|
383
|
+
}
|
|
384
|
+
// layer1-①:语句数闸门。多语句实测每条都会执行(spec §5.4 第 9 行),
|
|
385
|
+
// `count === 1` 是必需闸门;'SELECT 1;;' / 尾注释 / 尾分号仍是 count=1,放行。
|
|
386
|
+
let ex;
|
|
387
|
+
try {
|
|
388
|
+
ex = await conn.extractStatements(sql);
|
|
389
|
+
}
|
|
390
|
+
catch (e) {
|
|
391
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
392
|
+
// ⚠️ 实测:空串 / 纯空白 / 纯注释 / ';' 抛 "Error in native callback";
|
|
393
|
+
// **但语法错误也在这一步抛**(`SELEC 1` → "Failed to extract statements: Parser
|
|
394
|
+
// Error: syntax error …")。不能一律报「SQL 为空」—— 那样用户打错一个关键字会得到
|
|
395
|
+
// 一条与事实不符的错误(spec §十一:语法错 → usage/query_error + 底层原文)。
|
|
396
|
+
if (/Error in native callback/i.test(msg)) {
|
|
397
|
+
throw sandboxError('usage', 'SQL 为空或无有效语句', {
|
|
398
|
+
code: 'query_error',
|
|
399
|
+
hint: '请提供一条 SELECT / DESCRIBE / SHOW / EXPLAIN 查询',
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
throw mapPrepareError(e);
|
|
403
|
+
}
|
|
404
|
+
if (ex.count === 0) {
|
|
405
|
+
throw sandboxError('usage', 'SQL 为空或无有效语句', {
|
|
406
|
+
code: 'query_error',
|
|
407
|
+
hint: '请提供一条 SELECT / DESCRIBE / SHOW / EXPLAIN 查询',
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
if (ex.count > 1) {
|
|
411
|
+
throw sandboxError('usage', `一次只接受一条语句(检测到 ${ex.count} 条)`, {
|
|
412
|
+
code: 'sandbox_blocked',
|
|
413
|
+
hint: '多语句会被逐条执行;请拆成多次查询',
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
// layer1-②:prepare(layer2 的文件/网络拦截也在这里先报)。
|
|
417
|
+
// ⚠️ 成功的 prepared statement 持有原生句柄 —— 每次 run 结束都 destroySync,
|
|
418
|
+
// 否则长会话反复查询会累积原生内存(prepare 失败不产出对象,无需释放)。
|
|
419
|
+
let p;
|
|
420
|
+
try {
|
|
421
|
+
p = await ex.prepare(0);
|
|
422
|
+
}
|
|
423
|
+
catch (e) {
|
|
424
|
+
throw mapPrepareError(e);
|
|
425
|
+
}
|
|
426
|
+
try {
|
|
427
|
+
// layer1-③:参数占位符。未绑参的 `?` 在执行期才报错,提前拦下更清楚。
|
|
428
|
+
if (p.parameterCount !== params.length) {
|
|
429
|
+
throw sandboxError('usage', `SQL 含 ${p.parameterCount} 个参数占位符,实际提供 ${params.length} 个参数(--sql 不接受参数占位符)`, { code: 'query_error', hint: '请把过滤条件写成字面量;参数化查询仅供 CLI 内部代码使用' });
|
|
430
|
+
}
|
|
431
|
+
// layer1-④:类型白名单。DESCRIBE / SHOW / PRAGMA table_info / PRAGMA database_list
|
|
432
|
+
// 都报告为 1(只读形态,放行)。`typeof` 守卫:注入的假模块若把 StatementType 的值
|
|
433
|
+
// 给成字符串,不能让它静默地把所有语句都挡掉。
|
|
434
|
+
const ST = mod.StatementType ?? {};
|
|
435
|
+
const SELECT = typeof ST.SELECT === 'number' ? ST.SELECT : 1; // 实测值:SELECT=1
|
|
436
|
+
const EXPLAIN = typeof ST.EXPLAIN === 'number' ? ST.EXPLAIN : 4; // 实测值:EXPLAIN=4
|
|
437
|
+
if (p.statementType !== SELECT && p.statementType !== EXPLAIN) {
|
|
438
|
+
throw sandboxError('usage', `只读沙箱不接受 ${typeName(p.statementType)} 语句`, {
|
|
439
|
+
code: 'sandbox_blocked',
|
|
440
|
+
hint: '只允许 SELECT / DESCRIBE / SHOW / EXPLAIN 等只读查询',
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
// layer1-⑤:EXPLAIN ANALYZE 内层执行闸门(spec §5.3 探针 16)。ANALYZE 会让 DuckDB
|
|
444
|
+
// **真正执行**内层语句;内层 CALL/PRAGMA 改的是实例级配置(不碰文件、不是库写),
|
|
445
|
+
// layer2/3 都拦不住。纯 EXPLAIN(无 ANALYZE)不执行内层,放行。
|
|
446
|
+
// 只对 type=4 扫描:普通 SELECT 里叫 analyze 的列别名是合法的。
|
|
447
|
+
if (p.statementType === EXPLAIN && hasAnalyzeToken(sql)) {
|
|
448
|
+
throw sandboxError('usage', 'EXPLAIN ANALYZE 会真正执行内层语句,沙箱不接受;去掉 ANALYZE 即可查看执行计划', {
|
|
449
|
+
code: 'sandbox_blocked',
|
|
450
|
+
hint: '请改用不带 ANALYZE 的纯 EXPLAIN 查看执行计划',
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
// 执行:runAndReadUntil 按 chunk 倍数读、**不精确限行**(请求 10 行实测返回 2048 行),
|
|
454
|
+
// 所以多读一行、用「返回行数 > limit」判定截断,而不是 SQL LIMIT 包装
|
|
455
|
+
//(包装要剥尾分号/尾注释,`SELECT 1; -- x` 会炸)。
|
|
456
|
+
if (params.length > 0)
|
|
457
|
+
p.bind(params);
|
|
458
|
+
let timer = null;
|
|
459
|
+
if (timeoutMs > 0) {
|
|
460
|
+
timer = setTimeout(() => {
|
|
461
|
+
try {
|
|
462
|
+
conn.interrupt();
|
|
463
|
+
}
|
|
464
|
+
catch { /* 中断失败不掩盖查询本身的结果 */ }
|
|
465
|
+
}, timeoutMs);
|
|
466
|
+
}
|
|
467
|
+
let reader;
|
|
468
|
+
try {
|
|
469
|
+
reader = await p.runAndReadUntil(limit + 1);
|
|
470
|
+
}
|
|
471
|
+
catch (e) {
|
|
472
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
473
|
+
if (/INTERRUPT/i.test(msg)) {
|
|
474
|
+
// 实测:interrupt 约 300ms 生效,中断后连接仍可用(探针 6/9)。
|
|
475
|
+
// ⚠️ hint 不得点名 `--timeout`(审查 I-4):只有 `query` 接受该 flag,
|
|
476
|
+
// `db tables` / `db describe` / `stats fields` 共用本出口,建议一个它们
|
|
477
|
+
// 不认的参数等于给一条走不通的路(`未知参数:--timeout`,退出码 2)。
|
|
478
|
+
throw sandboxError('query_timeout', `查询超过 ${timeoutMs}ms 未完成,已中断`, {
|
|
479
|
+
code: 'query_timeout',
|
|
480
|
+
hint: '请缩小时间范围或加过滤条件,把查询收窄到能在超时内完成',
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
// 裁决 R11:执行期原生错误也挂 cause —— errors.ts 从原文补 OOM 的可操作 hint
|
|
484
|
+
//(不挂的话「Out of Memory Error → 缩小时间范围」在生产路径永远不触发)。
|
|
485
|
+
throw sandboxError('internal', `查询执行失败:${msg}`, {
|
|
486
|
+
code: 'internal',
|
|
487
|
+
hint: '请检查查询条件;若反复出现请报告此问题',
|
|
488
|
+
cause: e,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
finally {
|
|
492
|
+
if (timer !== null)
|
|
493
|
+
clearTimeout(timer);
|
|
494
|
+
}
|
|
495
|
+
const all = reader.getRowObjectsJson();
|
|
496
|
+
const truncated = all.length > limit;
|
|
497
|
+
const rows = truncated ? all.slice(0, limit) : all;
|
|
498
|
+
// ⚠️ 列名一律取 `columnNames()`,**不要**用 `Object.keys(rows[0])`(edge F3):
|
|
499
|
+
// 驱动的 `getRowObjectsJson()` 以属性赋值构行,名为 `__proto__` 的列会变成**原型
|
|
500
|
+
// 赋值而不是自有键** —— 实测 `SELECT session_id, "__proto__", normal_extra FROM …`
|
|
501
|
+
// 时 `Object.keys` 少了它,显式请求的列连同值凭空消失、退出码 0、零警告。
|
|
502
|
+
// 取真实列名后逐行核对自有键:缺键说明该列的值经此驱动 API 无法表示,**必须
|
|
503
|
+
// fail-closed**(静默丢一列比报错更糟:Agent 会把「没有这一列」当成事实)。
|
|
504
|
+
// ⚠️ 行的键是**去重后**的列名(重名列变成 `x`、`x:1`、`x:2`),与 `columnNames()`
|
|
505
|
+
// (保留重复、未去重)不是同一个序列(安全审查 Critical,2026-10-20 实测):
|
|
506
|
+
// `SELECT userPhone, userPhone FROM sessions`
|
|
507
|
+
// → columnNames() = ["userPhone","userPhone"],行 = {"userPhone":…, "userPhone:1":…}
|
|
508
|
+
// 若把未去重的 columnNames() 交给脱敏层,`userPhone:1` 从未被遍历 → 自连接
|
|
509
|
+
// `SELECT * FROM sessions a JOIN sessions b` 时第二张表的 PII 原样输出,且
|
|
510
|
+
// meta.masked 仍为 true、零警告。
|
|
511
|
+
//
|
|
512
|
+
// ⚠️ **不能直接用驱动的 `deduplicatedColumnNames()`**:它用普通对象当计数器
|
|
513
|
+
//(`columnNameCount[name]`),名为 `__proto__` 的列会命中原型链而永不加 `:N` 后缀
|
|
514
|
+
// ——实测 `SELECT a AS "__proto__", a AS "__proto__"` 返回 ["__proto__","__proto__"]
|
|
515
|
+
// 而行是 `{}`(两个同名键互相覆盖)。这里用 Map 自算,规则与驱动一致。
|
|
516
|
+
const columns = deduplicateNames(reader.columnNames());
|
|
517
|
+
for (const row of rows) {
|
|
518
|
+
for (const name of columns) {
|
|
519
|
+
if (Object.hasOwn(row, name))
|
|
520
|
+
continue;
|
|
521
|
+
throw sandboxError('internal', `查询结果含无法安全表示的列名 ${name}`
|
|
522
|
+
+ '(驱动把该名字映射成了对象原型属性,取不到值)', {
|
|
523
|
+
code: 'internal',
|
|
524
|
+
hint: `请给该列起别名后重试,例如 SELECT "${name}" AS proto_col …`,
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return { columns, rows, truncated };
|
|
529
|
+
}
|
|
530
|
+
finally {
|
|
531
|
+
try {
|
|
532
|
+
p.destroySync?.();
|
|
533
|
+
}
|
|
534
|
+
catch { /* 释放期错误不掩盖查询结果/原异常 */ }
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
const close = async () => {
|
|
538
|
+
if (closed)
|
|
539
|
+
return;
|
|
540
|
+
closed = true;
|
|
541
|
+
closeRaw();
|
|
542
|
+
};
|
|
543
|
+
return { run, close };
|
|
544
|
+
}
|