@allbluecn/dsh-compat 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // cordis.patch.yml 解析层。
6
+ //
7
+ // 把 DSH patch 文本解析成 PatchLine[],供 applyPatches / evalDisabled /
8
+ // loadDshPlugins 消费。DSH 真实 patch 结构(实测 base/cordis.patch.yml):
9
+ //
10
+ // - insert: # 顶层唯一 key(一个 patch 文件一次 insert)
11
+ // - id: tool-plugin-manager
12
+ // name: '@deepseek-ai/dsh-plugin-manager/tools'
13
+ // disabled: true
14
+ // - id: plugin-manager
15
+ // name: '@deepseek-ai/dsh-plugin-manager'
16
+ // disabled: !!js "!ctx.get('profileContext')" # 条件启停
17
+ // - id: hmr
18
+ // name: '@deepseek-ai/dsh-hmr'
19
+ // disabled: !!js "!ctx.get('profileContext')"
20
+ // config:
21
+ // root: []
22
+ //
23
+ // 关键:`!!js` 是 YAML tag,yaml 库解析后 value 为字符串(tag 保留在 tag 属性,
24
+ // value 是表达式源码)——正好喂给 eval.ts 的白名单求值器,**不经 eval**。
25
+ // 本解析器把 disabled 统一成 boolean | string(string = !!js 表达式),
26
+ // 与 PatchLine.disabled 类型一致。
27
+
28
+ import { parse } from 'yaml'
29
+ import type { PatchLine } from './types'
30
+
31
+ /**
32
+ * 解析一个 cordis.patch.yml 文本为扁平 PatchLine[]。
33
+ *
34
+ * - 顶层期望一个 list,元素要么是 `{ insert: [...] }` 包裹,要么是裸 row;
35
+ * 两种都收(DSH 现行都是 `- insert:` 包裹,容忍裸 row 以对齐 extractInsert)。
36
+ * - `disabled`:boolean 原样;`!!js` tag → 字符串表达式(value)原样保留。
37
+ * - `config`:整块保留(applyPatches 语义是整 config 替换不合并)。
38
+ * - 未识别字段忽略;缺 id 的 row 跳过(无法定向)。
39
+ *
40
+ * @throws SyntaxError 当 YAML 非法或顶层不是 list。
41
+ */
42
+ export function parsePatchYaml(text: string): PatchLine[] {
43
+ const doc = parse(text)
44
+ if (!Array.isArray(doc)) {
45
+ throw new SyntaxError('cordis.patch.yml 顶层必须是 list(- insert: ... 或裸 row)')
46
+ }
47
+ const out: PatchLine[] = []
48
+ for (const entry of doc) {
49
+ if (!entry || typeof entry !== 'object') continue
50
+ const wrapped = entry as { insert?: unknown }
51
+ const rows = Array.isArray(wrapped.insert) ? wrapped.insert : [entry]
52
+ for (const row of rows) {
53
+ if (!row || typeof row !== 'object') continue
54
+ const r = row as Record<string, unknown>
55
+ if (typeof r.id !== 'string' || r.id === '') continue // 缺 id 无法定向,跳过
56
+ out.push({
57
+ id: r.id,
58
+ ...(typeof r.name === 'string' ? { name: r.name } : {}),
59
+ ...(r.disabled !== undefined ? { disabled: normalizeDisabled(r.disabled) } : {}),
60
+ ...(r.config !== undefined ? { config: r.config } : {}),
61
+ })
62
+ }
63
+ }
64
+ return out
65
+ }
66
+
67
+ /**
68
+ * 归一 disabled:boolean 原样;字符串(!!js 表达式源码)原样保留供求值;
69
+ * 其它类型(数字等)转 boolean。evalDisabled 再统一三态判定。
70
+ */
71
+ function normalizeDisabled(v: unknown): boolean | string {
72
+ if (typeof v === 'boolean') return v
73
+ if (typeof v === 'string') return v
74
+ return Boolean(v)
75
+ }
@@ -0,0 +1,149 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // PRE-3 build-approval 注册表单测(设计 §PRE-3)。覆盖 blockedBuildNames 提取
6
+ // (Ignored build scripts / git-hosted / 过滤通配符与凭据 git key)+ 状态机
7
+ // (recordPendingBuilds → approveBuilds → stale-approval 守卫)。
8
+
9
+ import { describe, expect, it } from 'vitest'
10
+ import {
11
+ blockedBuildNames,
12
+ recordPendingBuilds,
13
+ approveBuilds,
14
+ carriesCredential,
15
+ type BuildScriptEntry,
16
+ } from '../build-approval'
17
+
18
+ describe('blockedBuildNames', () => {
19
+ it('提取 Ignored build scripts 列表', () => {
20
+ const out = 'Ignored build scripts: esbuild, sharp, @swc/core'
21
+ expect(blockedBuildNames(out)).toEqual(['esbuild', 'sharp', '@swc/core'])
22
+ })
23
+
24
+ it('提取 git-hosted allowBuilds 键', () => {
25
+ const out = [
26
+ 'some output',
27
+ 'ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED',
28
+ 'allowBuilds:',
29
+ ' git+https://github.com/acme/x: true',
30
+ ].join('\n')
31
+ expect(blockedBuildNames(out)).toEqual(['git+https://github.com/acme/x'])
32
+ })
33
+
34
+ it('过滤通配符(* / ?)', () => {
35
+ const out = 'Ignored build scripts: *, esbuild, a?b'
36
+ expect(blockedBuildNames(out)).toEqual(['esbuild'])
37
+ })
38
+
39
+ it('过滤带凭据的 git key(密码)', () => {
40
+ const out = [
41
+ 'ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED',
42
+ 'allowBuilds:',
43
+ ' git+https://user:pass@github.com/acme/x: true',
44
+ ].join('\n')
45
+ expect(blockedBuildNames(out)).toEqual([])
46
+ })
47
+
48
+ it('过滤带凭据的 git key(HTTP(S) 用户名)', () => {
49
+ const out = [
50
+ 'ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED',
51
+ 'allowBuilds:',
52
+ ' git+https://user@github.com/acme/x: true',
53
+ ].join('\n')
54
+ expect(blockedBuildNames(out)).toEqual([])
55
+ })
56
+ })
57
+
58
+ describe('carriesCredential', () => {
59
+ it('带密码 → true', () => {
60
+ expect(carriesCredential('git+https://user:pass@github.com/acme/x')).toBe(true)
61
+ })
62
+ it('HTTP(S) 带用户名 → true', () => {
63
+ expect(carriesCredential('git+https://user@github.com/acme/x')).toBe(true)
64
+ })
65
+ it('ssh → false(非 http/https 协议)', () => {
66
+ expect(carriesCredential('git+ssh://git@github.com/acme/x')).toBe(false)
67
+ })
68
+ it('无凭据 https → false', () => {
69
+ expect(carriesCredential('git+https://github.com/acme/x')).toBe(false)
70
+ })
71
+ })
72
+
73
+ describe('recordPendingBuilds', () => {
74
+ it('空注册表 + 报告 → 全部 pending', () => {
75
+ const { registry, pending } = recordPendingBuilds([], 'Ignored build scripts: esbuild, sharp')
76
+ expect(registry).toEqual([
77
+ { name: 'esbuild', state: 'pending' },
78
+ { name: 'sharp', state: 'pending' },
79
+ ])
80
+ expect(pending).toEqual(['esbuild', 'sharp'])
81
+ })
82
+
83
+ it('并入已有条目去重(不重复加)', () => {
84
+ const existing: BuildScriptEntry[] = [{ name: 'esbuild', state: 'pending' }]
85
+ const { registry } = recordPendingBuilds(existing, 'Ignored build scripts: esbuild, sharp')
86
+ expect(registry).toEqual([
87
+ { name: 'esbuild', state: 'pending' },
88
+ { name: 'sharp', state: 'pending' },
89
+ ])
90
+ })
91
+
92
+ it('已有 allowed 的不回退为 pending', () => {
93
+ const existing: BuildScriptEntry[] = [{ name: 'esbuild', state: 'allowed' }]
94
+ const { registry } = recordPendingBuilds(existing, 'Ignored build scripts: esbuild')
95
+ expect(registry).toEqual([{ name: 'esbuild', state: 'allowed' }])
96
+ })
97
+ })
98
+
99
+ describe('approveBuilds', () => {
100
+ const base: BuildScriptEntry[] = [
101
+ { name: 'esbuild', state: 'pending' },
102
+ { name: 'sharp', state: 'pending' },
103
+ ]
104
+
105
+ it('批准 pending 名 → allowed', () => {
106
+ const next = approveBuilds(base, ['esbuild'])
107
+ expect(next).toEqual([
108
+ { name: 'esbuild', state: 'allowed' },
109
+ { name: 'sharp', state: 'pending' },
110
+ ])
111
+ })
112
+
113
+ it('批准全部 pending → 全 allowed', () => {
114
+ expect(approveBuilds(base, ['esbuild', 'sharp'])).toEqual([
115
+ { name: 'esbuild', state: 'allowed' },
116
+ { name: 'sharp', state: 'allowed' },
117
+ ])
118
+ })
119
+
120
+ it('批准不在 pending 的名字 → stale-approval', () => {
121
+ expect(() => approveBuilds(base, ['ghost'])).toThrow('stale-approval')
122
+ })
123
+
124
+ it('批准已 allowed 的名字 → stale-approval(不在 pending)', () => {
125
+ const withAllowed = approveBuilds(base, ['esbuild'])
126
+ expect(() => approveBuilds(withAllowed, ['esbuild'])).toThrow('stale-approval')
127
+ })
128
+
129
+ it('空 names → 原样返回', () => {
130
+ expect(approveBuilds(base, [])).toEqual(base)
131
+ })
132
+ })
133
+
134
+ describe('状态机端到端', () => {
135
+ it('record → approve → record(新脚本再入 pending)', () => {
136
+ let registry: BuildScriptEntry[] = []
137
+ let pending: string[] = []
138
+
139
+ ;({ registry, pending } = recordPendingBuilds(registry, 'Ignored build scripts: esbuild'))
140
+ expect(pending).toEqual(['esbuild'])
141
+
142
+ registry = approveBuilds(registry, ['esbuild'])
143
+
144
+ ;({ registry, pending } = recordPendingBuilds(registry, 'Ignored build scripts: sharp'))
145
+ expect(pending).toEqual(['sharp'])
146
+ expect(registry.find((e) => e.name === 'esbuild')?.state).toBe('allowed')
147
+ expect(registry.find((e) => e.name === 'sharp')?.state).toBe('pending')
148
+ })
149
+ })
@@ -0,0 +1,148 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // PRE-4 install-spec 校验单测(设计 §PRE-4)。覆盖四形态判定 + 安全拒绝
6
+ // (凭据 git URL / # 片段 / 私网 host / 空与 - 开头 / 已安装)。
7
+
8
+ import { describe, expect, it } from 'vitest'
9
+ import { inspectInstallSpec } from '../install-spec'
10
+
11
+ describe('inspectInstallSpec — accepted 形态', () => {
12
+ it('registry:npm 包名', () => {
13
+ expect(inspectInstallSpec('dsh-plugin-x')).toEqual({
14
+ status: 'accepted', kind: 'registry', name: 'dsh-plugin-x',
15
+ })
16
+ })
17
+
18
+ it('registry:scoped 包名', () => {
19
+ expect(inspectInstallSpec('@acme/dsh-plugin-x')).toEqual({
20
+ status: 'accepted', kind: 'registry', name: '@acme/dsh-plugin-x',
21
+ })
22
+ })
23
+
24
+ it('git:git+https URL 提取 host', () => {
25
+ expect(inspectInstallSpec('git+https://github.com/acme/dsh-plugin-x')).toEqual({
26
+ status: 'accepted', kind: 'git', host: 'https://github.com/acme/dsh-plugin-x',
27
+ })
28
+ })
29
+
30
+ it('git:# 片段带 committish(无 host)', () => {
31
+ expect(inspectInstallSpec('git+https://github.com/acme/x#v1.0.0')).toEqual({
32
+ status: 'accepted', kind: 'git',
33
+ })
34
+ })
35
+
36
+ it('tarball:https .tgz URL', () => {
37
+ expect(inspectInstallSpec('https://example.com/dsh-plugin-x-1.0.0.tgz')).toEqual({
38
+ status: 'accepted', kind: 'tarball',
39
+ })
40
+ })
41
+
42
+ it('path:本地绝对路径', () => {
43
+ expect(inspectInstallSpec('/home/me/dsh-plugin-x')).toEqual({
44
+ status: 'accepted', kind: 'path', path: '/home/me/dsh-plugin-x',
45
+ })
46
+ })
47
+ })
48
+
49
+ describe('inspectInstallSpec — 安全拒绝', () => {
50
+ it('空 spec → invalid-spec', () => {
51
+ expect(inspectInstallSpec('')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
52
+ expect(inspectInstallSpec(' ')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
53
+ })
54
+
55
+ it('- 开头(pnpm flag)→ invalid-spec', () => {
56
+ expect(inspectInstallSpec('--force')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
57
+ })
58
+
59
+ it('git URL 带密码 → invalid-spec(凭据)', () => {
60
+ expect(inspectInstallSpec('git+https://user:pass@github.com/acme/x')).toMatchObject({
61
+ status: 'refused', problem: 'invalid-spec',
62
+ })
63
+ })
64
+
65
+ it('git URL 带 HTTP(S) 用户名 → invalid-spec(凭据)', () => {
66
+ expect(inspectInstallSpec('git+https://user@github.com/acme/x')).toMatchObject({
67
+ status: 'refused', problem: 'invalid-spec',
68
+ })
69
+ })
70
+
71
+ it('git https committish 形态指向私网 → invalid-spec(SSRF,committish 不绕过)', () => {
72
+ // 标准版本钉固形态 `#v1.0.0`:hostOf 返回 undefined,此前整体跳过私网检查被 accepted
73
+ expect(inspectInstallSpec('git+https://192.168.1.1/x#v1.0.0')).toMatchObject({
74
+ status: 'refused', problem: 'invalid-spec',
75
+ })
76
+ expect(inspectInstallSpec('git+https://10.0.0.5/acme/x.git#main')).toMatchObject({
77
+ status: 'refused', problem: 'invalid-spec',
78
+ })
79
+ })
80
+
81
+ it('git https 私网 IPv6 / 整数混淆 → invalid-spec(SSRF,对齐 tarball 守卫)', () => {
82
+ // IPv6 回环 / ULA / IPv4-mapped(tarball 同款 assertFetchableUrl 才拦得住)
83
+ expect(inspectInstallSpec('git+https://[::1]/acme/x.git')).toMatchObject({
84
+ status: 'refused', problem: 'invalid-spec',
85
+ })
86
+ expect(inspectInstallSpec('git+https://[fc00::1]/acme/x.git#v1')).toMatchObject({
87
+ status: 'refused', problem: 'invalid-spec',
88
+ })
89
+ expect(inspectInstallSpec('git+https://[::ffff:127.0.0.1]/acme/x.git')).toMatchObject({
90
+ status: 'refused', problem: 'invalid-spec',
91
+ })
92
+ // 整数 / 十六进制混淆 IP(0x7f000001 = 127.0.0.1)
93
+ expect(inspectInstallSpec('git+https://2130706433/acme/x.git')).toMatchObject({
94
+ status: 'refused', problem: 'invalid-spec',
95
+ })
96
+ expect(inspectInstallSpec('git+https://0x7f000001/acme/x.git#v1')).toMatchObject({
97
+ status: 'refused', problem: 'invalid-spec',
98
+ })
99
+ // .local / .internal mDNS/内网域名
100
+ expect(inspectInstallSpec('git+https://host.internal/acme/x.git')).toMatchObject({
101
+ status: 'refused', problem: 'invalid-spec',
102
+ })
103
+ })
104
+
105
+ it('git https 公网 host 带 committish → accepted(不误拒正常钉固)', () => {
106
+ expect(inspectInstallSpec('git+https://github.com/acme/x#v1.0.0')).toEqual({
107
+ status: 'accepted', kind: 'git',
108
+ })
109
+ })
110
+
111
+ it('registry 名含 # 片段 → invalid-spec', () => {
112
+ expect(inspectInstallSpec('dsh-plugin-x#frag')).toMatchObject({
113
+ status: 'refused', problem: 'invalid-spec',
114
+ })
115
+ })
116
+
117
+ it('registry 名指向私网 host → invalid-spec(SSRF)', () => {
118
+ expect(inspectInstallSpec('localhost')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
119
+ expect(inspectInstallSpec('127.0.0.1')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
120
+ expect(inspectInstallSpec('10.0.0.5')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
121
+ expect(inspectInstallSpec('192.168.1.5')).toMatchObject({ status: 'refused', problem: 'invalid-spec' })
122
+ })
123
+
124
+ it('tarball http(s) URL 指向私网 → invalid-spec(SSRF,走 fetch-guard)', () => {
125
+ expect(inspectInstallSpec('http://169.254.169.254/x.tgz')).toMatchObject({
126
+ status: 'refused', problem: 'invalid-spec',
127
+ })
128
+ })
129
+
130
+ it('tarball git URL 带凭据 → invalid-spec', () => {
131
+ expect(inspectInstallSpec('https://user:pass@example.com/x.tgz')).toMatchObject({
132
+ status: 'refused', problem: 'invalid-spec',
133
+ })
134
+ })
135
+
136
+ it('knownNames 命中 → already-installed', () => {
137
+ const known = new Set(['dsh-plugin-x'])
138
+ expect(inspectInstallSpec('dsh-plugin-x', { knownNames: known })).toMatchObject({
139
+ status: 'refused', problem: 'already-installed',
140
+ })
141
+ })
142
+
143
+ it('tarball 本地路径 → accepted path 形态(existsSync 需 serverFn)', () => {
144
+ expect(inspectInstallSpec('./pkg/x-1.0.0.tgz')).toEqual({
145
+ status: 'accepted', kind: 'tarball', path: './pkg/x-1.0.0.tgz',
146
+ })
147
+ })
148
+ })
@@ -0,0 +1,134 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // 构建脚本审批注册表(P3 前置 PRE-3,设计见
6
+ // docs/superpowers/specs/2026-11-11-harness-dsh-plugin-safety-prereq.md §PRE-3)。
7
+ //
8
+ // 镜像 DSH `boot/plugin-manager/src/build-approval.ts` 的状态机与安全守卫,**纯逻辑
9
+ // 不含 fs/pnpm**——fs 持久化(profile dir 的 allowBuilds)与 pnpm install 执行需 npm
10
+ // install serverFn(GATED,§9.5 ①),本期只做状态机 + 安全过滤,可独立单测。
11
+ //
12
+ // 为什么单独审批:npm 插件的 install 脚本(postinstall 等)在**插件加载前**由包管理器
13
+ // 跑,无法走 harness 运行时审批(step.ask/view.approvals)。故必须是 **pre-install 门**:
14
+ // pnpm 非交互跑 → 被 block 的脚本记入 pending → 对话内用户明确批准 → 才放行。
15
+ //
16
+ // 安全不变式(移植自 DSH,不宜削弱):
17
+ // - 拒录带凭据的 git key(密码 / HTTP(S) 用户名)——凭据不进 pending/allowlist
18
+ // - 拒绝通配符(`*`/`?`)——只批精确包名,防 `*` 一刀切放行全部脚本
19
+ // - stale-approval 守卫——批准的名字必须仍在 pending,防批准已消失的脚本
20
+
21
+ /** pnpm 留给「仍需决策」的构建脚本的值。 */
22
+ const UNDECIDED_BUILD = 'set this to true or false'
23
+
24
+ /** 非交互运行报告被跳过构建脚本的位置。 */
25
+ const IGNORED_BUILDS = /\bIgnored build scripts: (.+)/
26
+
27
+ /** pnpm 拒绝准备 git-hosted 包时建议的 `allowBuilds` 键;子目录键可含空格。 */
28
+ const GIT_DEP_PREPARE = /ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED[\s\S]*?^allowBuilds:\r?\n[ \t]+(.+?): true$/m
29
+
30
+ /** 单个构建脚本的决策状态。 */
31
+ export type BuildApprovalState = 'pending' | 'allowed'
32
+
33
+ /** 注册表里一个构建脚本条目。 */
34
+ export interface BuildScriptEntry {
35
+ /** 精确包名(无通配符、无凭据)。 */
36
+ readonly name: string
37
+ readonly state: BuildApprovalState
38
+ }
39
+
40
+ /**
41
+ * 从包管理器输出提取被 block 的构建脚本名(PRE-3 纯函数)。
42
+ *
43
+ * 对齐 DSH `blockedBuildNames`:`Ignored build scripts: a,b` + git-hosted
44
+ * `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED ... allowBuilds:\n <key>: true`。
45
+ * 过滤通配符(`*`/`?`)与带凭据的 git key。
46
+ *
47
+ * @param output 包管理器运行的捕获输出
48
+ * @returns 精确包名(报告顺序;通配符与带凭据 git key 排除)
49
+ */
50
+ export function blockedBuildNames(output: string): string[] {
51
+ const ignored = IGNORED_BUILDS.exec(output)?.[1]?.split(',') ?? []
52
+ const git = GIT_DEP_PREPARE.exec(output)?.[1]
53
+ return [...ignored, ...(git === undefined ? [] : [git])]
54
+ .map((name) => name.trim())
55
+ .filter((name) => name !== '' && !/[*?]/.test(name) && !carriesCredential(name))
56
+ }
57
+
58
+ /**
59
+ * 记录一次失败运行报告的被 block 构建脚本,并入注册表(PRE-3 纯函数)。
60
+ *
61
+ * 对齐 DSH `recordPendingBuilds`:已 block 的脚本名并入 pending(去重),返回全部
62
+ * awaiting 决策的名字(含原有 pending)。**不含 fs 写**——持久化由 npm install
63
+ * serverFn 接线(GATED)。
64
+ *
65
+ * @param registry 当前注册表条目
66
+ * @param output 包管理器运行的捕获输出
67
+ * @returns 更新后的注册表 + 本轮 awaiting 决策的名字
68
+ */
69
+ export function recordPendingBuilds(
70
+ registry: readonly BuildScriptEntry[],
71
+ output: string,
72
+ ): { registry: BuildScriptEntry[]; pending: string[] } {
73
+ const known = new Set(registry.map((e) => e.name))
74
+ const added = blockedBuildNames(output).filter((name) => !known.has(name))
75
+ const next = [...registry, ...added.map((name) => ({ name, state: 'pending' as const }))]
76
+ const pending = next.filter((e) => e.state === 'pending').map((e) => e.name)
77
+ return { registry: next, pending }
78
+ }
79
+
80
+ /**
81
+ * 批准构建脚本(PRE-3 纯函数,**不执行脚本**——执行由 npm install serverFn)。
82
+ *
83
+ * 对齐 DSH `approveBuilds`:批准的名字必须仍在 pending,否则 `stale-approval`
84
+ * (防批准已消失/已处理的脚本)。批准后 state → allowed(持久,对该 profile)。
85
+ *
86
+ * @param registry 当前注册表条目
87
+ * @param names 待批准的精确包名(来自 pending 列表)
88
+ * @returns 更新后的注册表
89
+ * @throws Error `stale-approval` 若某名字不在 pending
90
+ */
91
+ export function approveBuilds(
92
+ registry: readonly BuildScriptEntry[],
93
+ names: readonly string[],
94
+ ): BuildScriptEntry[] {
95
+ const pendingNames = new Set(
96
+ registry.filter((e) => e.state === 'pending').map((e) => e.name),
97
+ )
98
+ if (names.some((name) => !pendingNames.has(name))) {
99
+ throw new Error('stale-approval')
100
+ }
101
+ if (names.length === 0) return [...registry]
102
+ const allow = new Set(names)
103
+ return registry.map((e) => (allow.has(e.name) ? { ...e, state: 'allowed' as const } : e))
104
+ }
105
+
106
+ /**
107
+ * git URL 里是否携带凭据:密码,或 HTTP(S) 上的用户名。此类 key 不录(凭据不进
108
+ * pending/allowlist)。
109
+ *
110
+ * 覆盖两种形态:
111
+ * - DSH pnpm `allowBuilds` 键:凭据在 `@git+` 前(`git+https://user:pass@host` 的
112
+ * allowBuilds 键写作 `...@git+https://host`,少见)——对齐 DSH `carriesCredential`。
113
+ * - **git clone URL(本模块主要用途)**:凭据在 `git+https://` 后(`git+https://user:pass@host`)。
114
+ * 直接解析 `git+` 后的 URL,查 password / HTTP(S) 用户名。
115
+ */
116
+ export function carriesCredential(key: string): boolean {
117
+ // 形态 A(DSH allowBuilds 键):凭据在 `@git+` 前
118
+ const at = key.indexOf('@git+')
119
+ if (at !== -1) {
120
+ const url = URL.parse(key.slice(at + '@git+'.length).replace(/#.*$/s, ''))
121
+ if (url !== null && (url.password !== '' || (url.username !== '' && /^https?:$/.test(url.protocol)))) {
122
+ return true
123
+ }
124
+ }
125
+ // 形态 B(git clone URL):凭据在 `git+` 后的 URL 里
126
+ const gitPrefix = 'git+'
127
+ if (key.startsWith(gitPrefix)) {
128
+ const url = URL.parse(key.slice(gitPrefix.length).replace(/#.*$/s, ''))
129
+ if (url !== null && (url.password !== '' || (url.username !== '' && /^https?:$/.test(url.protocol)))) {
130
+ return true
131
+ }
132
+ }
133
+ return false
134
+ }
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // DSH npm 插件安装的前置安全件(P3 前置 PRE-3/PRE-4,设计见
6
+ // docs/superpowers/specs/2026-11-11-harness-dsh-plugin-safety-prereq.md)。
7
+ //
8
+ // 这些是「开 DSH npm 安装」的强制前置安全控件(GATED,§9.5 ①)。本期只实现
9
+ // **自包含纯逻辑件**(可独立单测,不依赖 npm install serverFn 存在):
10
+ // - install-spec:安装 spec 形态/安全校验(PRE-4)
11
+ // - build-approval:构建脚本审批注册表状态机(PRE-3)
12
+ // npm install serverFn / harness 接线 / UI 面板仍 GATED(等真实需求 + 全部 PRE 建成)。
13
+ //
14
+ // 仅服务端使用(server/harness/plugin-safety/)。install-spec 复用 fetch-guard 的
15
+ // SSRF 守卫(assertFetchableUrl,含 IPv6/IPv4-mapped)。
16
+
17
+ export {
18
+ inspectInstallSpec,
19
+ type InstallSpecInspection,
20
+ type InstallSpecKind,
21
+ type InstallSpecProblem,
22
+ } from './install-spec'
23
+
24
+ export {
25
+ blockedBuildNames,
26
+ recordPendingBuilds,
27
+ approveBuilds,
28
+ carriesCredential,
29
+ type BuildApprovalState,
30
+ type BuildScriptEntry,
31
+ } from './build-approval'