@tokensapi/dsh-plugin-check 0.3.1 → 0.3.3

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.
@@ -1,144 +1,139 @@
1
- /* ============================================================
2
- * 阶段①:清单体检(纯静态,秒级,离线)
3
- * ============================================================
4
- * 每一条规则都对应一种已经真实发生过的"把宿主搞崩/搞坏"路径,
5
- * 规则说明写在 README;error 挡上架,warning 提示即将收紧或
6
- * 潜在错配。判定为纯函数,便于单测与市场门禁复用。
7
- * ============================================================ */
8
- import { existsSync, readFileSync, statSync } from 'node:fs'
9
- import { join, resolve } from 'node:path'
10
- import semver from 'semver'
11
- import { extractPatchRows } from './patch-rows.mjs'
12
-
13
- /** 安装即执行任意代码的脚本;市场受控安装的既有红线。 */
14
- const FORBIDDEN_LIFECYCLE = ['preinstall', 'install', 'postinstall', 'prepare']
15
-
16
- /**
17
- * 携带身份语义的核心包:cordis 上下文与 dsh 服务在宿主/插件各持一份时
18
- * Symbol 不等,注册对不上。schemastery、cosmokit 等纯工具库不在此列,
19
- * 插件可以正常依赖。
20
- */
21
- const CORE_SCOPE = '@deepseek-ai/'
22
- const isIdentityCore = (name) => name === '@deepseek-ai/cordis'
23
- || name.startsWith('@deepseek-ai/cordis-')
24
- || name.startsWith('@deepseek-ai/dsh')
25
-
26
- const SUSPICIOUS_FILE = /(\.env(\.|$)|id_rsa|\.pem$|\.p12$|\.key$)/u
27
-
28
- /**
29
- * 对一个插件目录执行全部清单规则。
30
- * @param {string} pluginDir - 插件根目录(含 package.json)。
31
- * @param {{ runtime?: string }} [options] - runtime 为目标 DSH 运行时
32
- * 版本(如 0.1.3-alpha.1);给出时才执行 engine/peer 与其的交集判定。
33
- * @returns {{ findings: Array<{level: 'error'|'warning', rule: string, message: string}>, manifest?: object, rows?: object[] }}
34
- */
35
- export function checkManifest(pluginDir, options = {}) {
36
- const findings = []
37
- const error = (rule, message) => findings.push({ level: 'error', rule, message })
38
- const warning = (rule, message) => findings.push({ level: 'warning', rule, message })
39
-
40
- const manifestPath = resolve(pluginDir, 'package.json')
41
- if (!existsSync(manifestPath)) {
42
- error('M1-manifest', 'package.json 不存在')
43
- return { findings }
44
- }
45
- let manifest
46
- try {
47
- manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
48
- } catch (cause) {
49
- error('M1-manifest', `package.json 不是合法 JSON: ${cause instanceof Error ? cause.message : String(cause)}`)
50
- return { findings }
51
- }
52
-
53
- if (typeof manifest.name !== 'string' || manifest.name === '') {
54
- error('M1-manifest', 'name 缺失')
55
- }
56
- if (typeof manifest.version !== 'string' || semver.valid(manifest.version) === null) {
57
- error('M1-manifest', 'version 不是合法 SemVer')
58
- } else if (semver.prerelease(manifest.version) !== null) {
59
- warning('M1-manifest', `version ${manifest.version} 是预发布版;市场上架要求精确稳定版`)
60
- }
61
-
62
- const scripts = manifest.scripts ?? {}
63
- for (const script of FORBIDDEN_LIFECYCLE) {
64
- if (typeof scripts[script] === 'string') {
65
- error('M2-lifecycle', `禁止 ${script} 生命周期脚本(安装即执行任意代码,市场受控安装会拒绝);构建请改用 prepack`)
66
- }
67
- }
68
-
69
- const dependencies = manifest.dependencies ?? {}
70
- for (const name of Object.keys(dependencies)) {
71
- if (isIdentityCore(name)) {
72
- error('M3-core-peer', `${name} 出现在 dependencies;核心运行时必须声明为 peerDependencies,否则宿主与插件各持一份实例,服务注册无法对上,典型症状是整个 Cowork 启动失败`)
73
- }
74
- }
75
-
76
- const dsh = manifest.dsh ?? {}
77
- const bundle = dsh.bundle ?? {}
78
- let rows
79
- if (typeof bundle.patch !== 'string' || bundle.patch === '') {
80
- error('M4-bundle', 'dsh.bundle.patch 缺失;宿主靠它把插件挂进加载树')
81
- } else {
82
- const patchPath = resolve(pluginDir, bundle.patch)
83
- if (!existsSync(patchPath)) {
84
- error('M4-bundle', `dsh.bundle.patch 指向的文件不存在: ${bundle.patch}`)
85
- } else {
86
- const extracted = extractPatchRows(readFileSync(patchPath, 'utf8'))
87
- for (const problem of extracted.problems) error('M4-bundle', `补丁行不合法: ${problem}`)
88
- rows = extracted.rows
89
- if (rows.length === 0 && extracted.problems.length === 0) {
90
- error('M4-bundle', '补丁未声明任何插件行')
91
- }
92
- const packageName = typeof manifest.name === 'string' ? manifest.name : ''
93
- for (const row of rows ?? []) {
94
- if (packageName !== '' && row.name !== packageName && !row.name.startsWith(`${packageName}/`)) {
95
- error('M5-row-scope', `补丁行 "${row.id ?? row.name}" 指向 ${row.name},超出本包命名空间(${packageName});插件只能挂载自己的入口`)
96
- }
97
- }
98
- }
99
- }
100
-
101
- const engine = typeof dsh.engine === 'string' ? dsh.engine : undefined
102
- if (engine === undefined) {
103
- warning('M6-engine', 'dsh.engine 未声明(目标 DSH 运行时 SemVer 范围,如 ">=0.1.3-alpha.1 <0.2");该字段将成为上架必填')
104
- } else if (semver.validRange(engine, { includePrerelease: true }) === null) {
105
- error('M6-engine', `dsh.engine 不是合法 SemVer 范围: ${engine}`)
106
- } else if (options.runtime !== undefined
107
- && !semver.satisfies(options.runtime, engine, { includePrerelease: true })) {
108
- error('M6-engine', `dsh.engine "${engine}" 不覆盖目标运行时 ${options.runtime}`)
109
- }
110
-
111
- const peers = manifest.peerDependencies ?? {}
112
- if (options.runtime !== undefined) {
113
- for (const [name, range] of Object.entries(peers)) {
114
- if (!name.startsWith(`${CORE_SCOPE}dsh`)) continue
115
- if (semver.validRange(range, { includePrerelease: true }) === null) {
116
- error('M7-peer-range', `peer ${name} 的范围不合法: ${range}`)
117
- } else if (!semver.satisfies(options.runtime, range, { includePrerelease: true })) {
118
- warning('M7-peer-range', `peer ${name}@"${range}" 不含目标运行时 ${options.runtime};宿主升级后此插件可能在运行时断裂(接口错配类故障)`)
119
- }
120
- }
121
- }
122
-
123
- if (manifest.engines?.node === undefined) {
124
- warning('M8-node-engines', 'engines.node 未声明;宿主运行在 Node 22+')
125
- }
126
- if (manifest.license === undefined) {
127
- warning('M9-license', 'license 未声明')
128
- }
129
-
130
- const filesList = Array.isArray(manifest.files) ? manifest.files : []
131
- for (const entry of filesList) {
132
- if (typeof entry === 'string' && SUSPICIOUS_FILE.test(entry)) {
133
- error('M10-secrets', `files 白名单包含疑似凭据文件: ${entry}`)
134
- }
135
- }
136
- for (const candidate of ['.env', 'id_rsa']) {
137
- const candidatePath = join(pluginDir, candidate)
138
- if (existsSync(candidatePath) && statSync(candidatePath).isFile() && filesList.length === 0) {
139
- warning('M10-secrets', `目录含 ${candidate} 且未用 files 白名单限定发布内容,npm pack 可能把它带上`)
140
- }
141
- }
142
-
143
- return { findings, manifest, rows }
144
- }
1
+ /* ============================================================
2
+ * 阶段①:清单体检(纯静态,秒级,离线)
3
+ * ============================================================
4
+ * 每一条规则都对应一种已经真实发生过的"把宿主搞崩/搞坏"路径,
5
+ * 规则说明写在 README;error 挡上架,warning 提示即将收紧或
6
+ * 潜在错配。判定为纯函数,便于单测与市场门禁复用。
7
+ * ============================================================ */
8
+ import { existsSync, readFileSync, statSync } from 'node:fs'
9
+ import { join, resolve } from 'node:path'
10
+ import semver from 'semver'
11
+ import { checkPatchContent } from './patch-rows.mjs'
12
+
13
+ /** 安装即执行任意代码的脚本;市场受控安装的既有红线。 */
14
+ const FORBIDDEN_LIFECYCLE = ['preinstall', 'install', 'postinstall', 'prepare']
15
+
16
+ /**
17
+ * 携带身份语义的核心包:cordis 上下文与 dsh 服务在宿主/插件各持一份时
18
+ * Symbol 不等,注册对不上。schemastery、cosmokit 等纯工具库不在此列,
19
+ * 插件可以正常依赖。
20
+ */
21
+ const CORE_SCOPE = '@deepseek-ai/'
22
+ const isIdentityCore = (name) => name === '@deepseek-ai/cordis'
23
+ || name.startsWith('@deepseek-ai/cordis-')
24
+ || name.startsWith('@deepseek-ai/dsh')
25
+
26
+ const SUSPICIOUS_FILE = /(\.env(\.|$)|id_rsa|\.pem$|\.p12$|\.key$)/u
27
+
28
+ /**
29
+ * 对一个插件目录执行全部清单规则。
30
+ * @param {string} pluginDir - 插件根目录(含 package.json)。
31
+ * @param {{ runtime?: string }} [options] - runtime 为目标 DSH 运行时
32
+ * 版本(如 0.1.3-alpha.1);给出时才执行 engine/peer 与其的交集判定。
33
+ * @returns {{ findings: Array<{level: 'error'|'warning', rule: string, message: string}>, manifest?: object, rows?: object[] }}
34
+ */
35
+ export function checkManifest(pluginDir, options = {}) {
36
+ const findings = []
37
+ const error = (rule, message) => findings.push({ level: 'error', rule, message })
38
+ const warning = (rule, message) => findings.push({ level: 'warning', rule, message })
39
+
40
+ const manifestPath = resolve(pluginDir, 'package.json')
41
+ if (!existsSync(manifestPath)) {
42
+ error('M1-manifest', 'package.json 不存在')
43
+ return { findings }
44
+ }
45
+ let manifest
46
+ try {
47
+ manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
48
+ } catch (cause) {
49
+ error('M1-manifest', `package.json 不是合法 JSON: ${cause instanceof Error ? cause.message : String(cause)}`)
50
+ return { findings }
51
+ }
52
+
53
+ if (typeof manifest.name !== 'string' || manifest.name === '') {
54
+ error('M1-manifest', 'name 缺失')
55
+ }
56
+ if (typeof manifest.version !== 'string' || semver.valid(manifest.version) === null) {
57
+ error('M1-manifest', 'version 不是合法 SemVer')
58
+ } else if (semver.prerelease(manifest.version) !== null) {
59
+ warning('M1-manifest', `version ${manifest.version} 是预发布版;市场上架要求精确稳定版`)
60
+ }
61
+
62
+ const scripts = manifest.scripts ?? {}
63
+ for (const script of FORBIDDEN_LIFECYCLE) {
64
+ if (typeof scripts[script] === 'string') {
65
+ error('M2-lifecycle', `禁止 ${script} 生命周期脚本(安装即执行任意代码,市场受控安装会拒绝);构建请改用 prepack`)
66
+ }
67
+ }
68
+
69
+ const dependencies = manifest.dependencies ?? {}
70
+ for (const name of Object.keys(dependencies)) {
71
+ if (isIdentityCore(name)) {
72
+ error('M3-core-peer', `${name} 出现在 dependencies;核心运行时必须声明为 peerDependencies,否则宿主与插件各持一份实例,服务注册无法对上,典型症状是整个 Cowork 启动失败`)
73
+ }
74
+ }
75
+
76
+ const dsh = manifest.dsh ?? {}
77
+ const bundle = dsh.bundle ?? {}
78
+ let rows
79
+ if (typeof bundle.patch !== 'string' || bundle.patch === '') {
80
+ error('M4-bundle', 'dsh.bundle.patch 缺失;宿主靠它把插件挂进加载树')
81
+ } else {
82
+ const patchPath = resolve(pluginDir, bundle.patch)
83
+ if (!existsSync(patchPath)) {
84
+ error('M4-bundle', `dsh.bundle.patch 指向的文件不存在: ${bundle.patch}`)
85
+ } else {
86
+ // M4/M5 与会话内体检共用同一份实现,避免两条路径判定漂移。
87
+ const checked = checkPatchContent(
88
+ typeof manifest.name === 'string' ? manifest.name : '',
89
+ readFileSync(patchPath, 'utf8'),
90
+ )
91
+ findings.push(...checked.findings)
92
+ rows = checked.rows
93
+ }
94
+ }
95
+
96
+ const engine = typeof dsh.engine === 'string' ? dsh.engine : undefined
97
+ if (engine === undefined) {
98
+ warning('M6-engine', 'dsh.engine 未声明(目标 DSH 运行时 SemVer 范围,如 ">=0.1.3-alpha.1 <0.2");该字段将成为上架必填')
99
+ } else if (semver.validRange(engine, { includePrerelease: true }) === null) {
100
+ error('M6-engine', `dsh.engine 不是合法 SemVer 范围: ${engine}`)
101
+ } else if (options.runtime !== undefined
102
+ && !semver.satisfies(options.runtime, engine, { includePrerelease: true })) {
103
+ error('M6-engine', `dsh.engine "${engine}" 不覆盖目标运行时 ${options.runtime}`)
104
+ }
105
+
106
+ const peers = manifest.peerDependencies ?? {}
107
+ if (options.runtime !== undefined) {
108
+ for (const [name, range] of Object.entries(peers)) {
109
+ if (!name.startsWith(`${CORE_SCOPE}dsh`)) continue
110
+ if (semver.validRange(range, { includePrerelease: true }) === null) {
111
+ error('M7-peer-range', `peer ${name} 的范围不合法: ${range}`)
112
+ } else if (!semver.satisfies(options.runtime, range, { includePrerelease: true })) {
113
+ warning('M7-peer-range', `peer ${name}@"${range}" 不含目标运行时 ${options.runtime};宿主升级后此插件可能在运行时断裂(接口错配类故障)`)
114
+ }
115
+ }
116
+ }
117
+
118
+ if (manifest.engines?.node === undefined) {
119
+ warning('M8-node-engines', 'engines.node 未声明;宿主运行在 Node 22+')
120
+ }
121
+ if (manifest.license === undefined) {
122
+ warning('M9-license', 'license 未声明')
123
+ }
124
+
125
+ const filesList = Array.isArray(manifest.files) ? manifest.files : []
126
+ for (const entry of filesList) {
127
+ if (typeof entry === 'string' && SUSPICIOUS_FILE.test(entry)) {
128
+ error('M10-secrets', `files 白名单包含疑似凭据文件: ${entry}`)
129
+ }
130
+ }
131
+ for (const candidate of ['.env', 'id_rsa']) {
132
+ const candidatePath = join(pluginDir, candidate)
133
+ if (existsSync(candidatePath) && statSync(candidatePath).isFile() && filesList.length === 0) {
134
+ warning('M10-secrets', `目录含 ${candidate} 且未用 files 白名单限定发布内容,npm pack 可能把它带上`)
135
+ }
136
+ }
137
+
138
+ return { findings, manifest, rows }
139
+ }
@@ -84,3 +84,25 @@ export function extractPatchRows(content) {
84
84
  candidates.forEach((entry, index) => visit(entry, `第 ${index + 1} 行`))
85
85
  return { rows, problems }
86
86
  }
87
+
88
+ /**
89
+ * 对补丁内容执行 M4/M5 判定。
90
+ * 目录体检与会话内体检共用这一份实现:两边曾经各写各的,结果
91
+ * 会话内看不到 M5,把装上即崩的插件判成合格。
92
+ * @param {string} packageName - 本包 npm 包名。
93
+ * @param {string} content - cordis.patch.yml 全文。
94
+ * @returns {{ findings: Array<{level: 'error', rule: string, message: string}>, rows: object[] }}
95
+ */
96
+ export function checkPatchContent(packageName, content) {
97
+ const findings = []
98
+ const error = (rule, message) => findings.push({ level: 'error', rule, message })
99
+ const { rows, problems } = extractPatchRows(content)
100
+ for (const problem of problems) error('M4-bundle', `补丁行不合法: ${problem}`)
101
+ if (rows.length === 0 && problems.length === 0) error('M4-bundle', '补丁未声明任何插件行')
102
+ for (const row of rows) {
103
+ if (packageName !== '' && row.name !== packageName && !row.name.startsWith(`${packageName}/`)) {
104
+ error('M5-row-scope', `补丁行 "${row.id ?? row.name}" 指向 ${row.name},超出本包命名空间(${packageName});插件只能挂载自己的入口`)
105
+ }
106
+ }
107
+ return { findings, rows }
108
+ }