dsh-sendcomfy 0.5.2 → 0.5.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.
- package/README.md +13 -1
- package/client.js +2 -2
- package/index.js +43 -2
- package/lib/http.mjs +65 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -113,10 +113,22 @@ AI 会自己 `comfy_choose_workflow` → `comfy_workflow` → `comfy_run`,并
|
|
|
113
113
|
|
|
114
114
|
---
|
|
115
115
|
|
|
116
|
+
## 访问控制
|
|
117
|
+
|
|
118
|
+
`/api/sendcomfy/*` 能改配置、触发跑图、上传图片,所以每条请求先过准入检查再分发:
|
|
119
|
+
|
|
120
|
+
- 有 `connection` 服务时走 `connection.requestRejection({ headers })`:Host/Origin 栅栏(挡 DNS rebinding 与跨站调用)+ 浏览器登录态,与官方插件同一口径;
|
|
121
|
+
- 取不到 `connection` 时退回本机回环栅栏:`Host` 必须回环、`Origin` 必须同源、`Origin: null` 拒绝;
|
|
122
|
+
- 拒绝返回 401/403,请求体不解析、不触达业务层。
|
|
123
|
+
|
|
124
|
+
`engines.dsh` 声明为 `>=0.2.2-rc.5`(`requestRejection` 从该版本可用)。
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
116
128
|
## 开发
|
|
117
129
|
|
|
118
130
|
```bash
|
|
119
|
-
npm test # node:test,
|
|
131
|
+
npm test # node:test,166 个用例
|
|
120
132
|
```
|
|
121
133
|
|
|
122
134
|
纯 ESM、零运行时依赖,只用 Node 内建 + 全局 `fetch`/`FormData`/`Blob`。
|
package/client.js
CHANGED
|
@@ -74,7 +74,7 @@ window.__ModuleLoader__.load({
|
|
|
74
74
|
'wf.importFileTooBig': '文件过大(上限 4MB)',
|
|
75
75
|
'wf.importing': '导入中…',
|
|
76
76
|
'wf.importedAs': '已导入:{name}',
|
|
77
|
-
'panel.version': 'v0.5.
|
|
77
|
+
'panel.version': 'v0.5.3',
|
|
78
78
|
'tab.workflows': '工作流',
|
|
79
79
|
'conn.configureCta': '去配置连接',
|
|
80
80
|
'tab.models': '模型',
|
|
@@ -228,7 +228,7 @@ window.__ModuleLoader__.load({
|
|
|
228
228
|
'wf.importFileTooBig': 'File is too large (4MB limit)',
|
|
229
229
|
'wf.importing': 'Importing…',
|
|
230
230
|
'wf.importedAs': 'Imported: {name}',
|
|
231
|
-
'panel.version': 'v0.5.
|
|
231
|
+
'panel.version': 'v0.5.3',
|
|
232
232
|
'tab.workflows': 'Workflows',
|
|
233
233
|
'conn.configureCta': 'Configure connection',
|
|
234
234
|
'tab.models': 'Models',
|
package/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import { readFile, stat } from 'node:fs/promises'
|
|
|
12
12
|
import { isAbsolute, join, resolve } from 'node:path'
|
|
13
13
|
|
|
14
14
|
import { createActions, humanizeError } from './lib/actions.mjs'
|
|
15
|
-
import { createApiHandler } from './lib/http.mjs'
|
|
15
|
+
import { createApiHandler, loopbackAdmission } from './lib/http.mjs'
|
|
16
16
|
import { createStateStore } from './lib/state.mjs'
|
|
17
17
|
|
|
18
18
|
export const name = 'dsh-sendcomfy'
|
|
@@ -66,7 +66,48 @@ export function apply(ctx) {
|
|
|
66
66
|
const actions = createActions({ store, logger })
|
|
67
67
|
|
|
68
68
|
/* ── 1. 浏览器面板 API ─────────────────────────────────────── */
|
|
69
|
-
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 取 DSH 的 `connection` 服务(浏览器登录态 + Host/Origin 栅栏的持有者)。
|
|
72
|
+
* 懒取:它可能在本插件激活之后才装好;拿不到就退回本机回环栅栏。
|
|
73
|
+
*/
|
|
74
|
+
function connectionService() {
|
|
75
|
+
const usable = (value) => (value && typeof value.requestRejection === 'function' ? value : undefined)
|
|
76
|
+
try {
|
|
77
|
+
const direct = usable(ctx.connection)
|
|
78
|
+
if (direct) return direct
|
|
79
|
+
} catch {
|
|
80
|
+
/* 服务未定义 */
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
if (typeof ctx.get === 'function') return usable(ctx.get('connection'))
|
|
84
|
+
} catch {
|
|
85
|
+
/* 服务未注册 */
|
|
86
|
+
}
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 准入检查:每条 /api/sendcomfy/* 请求在处理前先过这一关。
|
|
92
|
+
* 有 `connection` 时用它的 `requestRejection`(Host/Origin 栅栏 + 浏览器登录态,
|
|
93
|
+
* 与官方插件同一套口径);没有时退回 `loopbackAdmission`(只放行回环 Host、
|
|
94
|
+
* 同源 Origin)。宁可误拒也不放行未鉴权调用 —— 这些路由能改配置、触发跑图。
|
|
95
|
+
*/
|
|
96
|
+
function admit(req) {
|
|
97
|
+
const connection = connectionService()
|
|
98
|
+
if (connection) {
|
|
99
|
+
try {
|
|
100
|
+
const rejection = connection.requestRejection({ headers: (req && req.headers) || {} })
|
|
101
|
+
return rejection === 401 || rejection === 403 ? rejection : undefined
|
|
102
|
+
} catch (error) {
|
|
103
|
+
logger('准入检查抛错,按拒绝处理:', error?.message || error)
|
|
104
|
+
return 403
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return loopbackAdmission(req)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const handler = createApiHandler(actions, { prefix: ROUTE_PREFIX, admit })
|
|
70
111
|
ctx.effect(
|
|
71
112
|
() => ctx.webServer.register({
|
|
72
113
|
kind: 'prefix',
|
package/lib/http.mjs
CHANGED
|
@@ -114,12 +114,58 @@ function truthyQuery(value) {
|
|
|
114
114
|
return ['1', 'true', 'yes', 'on'].includes(String(value || '').toLowerCase())
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/** 回环主机名(Host 头去掉端口后的小写形式)。 */
|
|
118
|
+
const LOOPBACK_HOSTNAMES = new Set(['127.0.0.1', 'localhost', '[::1]', '::1', '[::ffff:127.0.0.1]'])
|
|
119
|
+
|
|
120
|
+
function hostnameOf(authority) {
|
|
121
|
+
const text = String(authority || '').trim().toLowerCase()
|
|
122
|
+
if (text.startsWith('[')) return text.slice(0, text.indexOf(']') + 1)
|
|
123
|
+
return text.replace(/:\d+$/, '')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isLoopbackHostname(hostname) {
|
|
127
|
+
return LOOPBACK_HOSTNAMES.has(hostname) || /^127\.\d+\.\d+\.\d+$/.test(hostname)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 本机回环栅栏 —— 在没有 DSH `connection` 服务可用时的兜底准入检查。
|
|
132
|
+
*
|
|
133
|
+
* 规则(比 connection 的弱,但挡住主要的两种攻击):
|
|
134
|
+
* 1. `Host` 必须是回环地址,挡住 DNS rebinding(把 evil.com 解析到 127.0.0.1);
|
|
135
|
+
* 2. 若带 `Origin`,必须与 `Host` 同源,挡住别的网页对我们发起跨站请求;
|
|
136
|
+
* `Origin: null`(sandbox iframe / file://)一律拒绝。
|
|
137
|
+
*
|
|
138
|
+
* 拿到 `connection` 服务时不会走到这里 —— 那条路径额外带浏览器登录态校验。
|
|
139
|
+
*
|
|
140
|
+
* @param {import('node:http').IncomingMessage} req
|
|
141
|
+
* @returns {403 | undefined} 拒绝状态码,或 undefined 表示放行
|
|
142
|
+
*/
|
|
143
|
+
export function loopbackAdmission(req) {
|
|
144
|
+
const headers = (req && req.headers) || {}
|
|
145
|
+
const host = String(headers.host || '')
|
|
146
|
+
if (!isLoopbackHostname(hostnameOf(host))) return 403
|
|
147
|
+
const origin = headers.origin
|
|
148
|
+
if (origin === undefined || origin === '') return undefined
|
|
149
|
+
if (String(origin).toLowerCase() === 'null') return 403
|
|
150
|
+
try {
|
|
151
|
+
const originAuthority = new URL(String(origin)).host
|
|
152
|
+
return originAuthority.toLowerCase() === host.toLowerCase() ? undefined : 403
|
|
153
|
+
} catch {
|
|
154
|
+
return 403
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
117
158
|
/**
|
|
118
159
|
* @param {ReturnType<import('./actions.mjs').createActions>} actions
|
|
119
|
-
* @param {{
|
|
160
|
+
* @param {{
|
|
161
|
+
* prefix?: string,
|
|
162
|
+
* admit?: (req: import('node:http').IncomingMessage) => (401 | 403 | undefined),
|
|
163
|
+
* }} [options]
|
|
164
|
+
* `admit` 是准入检查:返回 401/403 表示拒绝,返回 undefined 表示放行。
|
|
165
|
+
* 不给时不做检查(仅供测试与无宿主场景),Host 插件总会传一个。
|
|
120
166
|
* @returns {(req: import('node:http').IncomingMessage, res: import('node:http').ServerResponse) => Promise<void>}
|
|
121
167
|
*/
|
|
122
|
-
export function createApiHandler(actions, { prefix = DEFAULT_PREFIX } = {}) {
|
|
168
|
+
export function createApiHandler(actions, { prefix = DEFAULT_PREFIX, admit } = {}) {
|
|
123
169
|
return async function handle(req, res) {
|
|
124
170
|
if (!req || !res) return
|
|
125
171
|
const startedAt = Date.now()
|
|
@@ -150,6 +196,23 @@ export function createApiHandler(actions, { prefix = DEFAULT_PREFIX } = {}) {
|
|
|
150
196
|
} catch {
|
|
151
197
|
/* 诊断失败不影响请求 */
|
|
152
198
|
}
|
|
199
|
+
// 准入检查必须在任何路由分发之前:所有 /api/sendcomfy/* 都能读到配置、
|
|
200
|
+
// 触发 ComfyUI 生成、上传图片,未鉴权的请求一条也不该被处理。
|
|
201
|
+
if (typeof admit === 'function') {
|
|
202
|
+
let rejection
|
|
203
|
+
try {
|
|
204
|
+
rejection = admit(req)
|
|
205
|
+
} catch {
|
|
206
|
+
rejection = 403
|
|
207
|
+
}
|
|
208
|
+
if (rejection === 401 || rejection === 403) {
|
|
209
|
+
sendJson(res, rejection, {
|
|
210
|
+
ok: false,
|
|
211
|
+
error: '请求未通过 DSH 准入检查(Host/Origin 栅栏或浏览器登录态)',
|
|
212
|
+
})
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
}
|
|
153
216
|
let url
|
|
154
217
|
try {
|
|
155
218
|
url = new URL(req.url || '/', 'http://127.0.0.1')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sendcomfy",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "ComfyUI bridge plugin for DeepSeek Harness (DSH): browse ComfyUI server workflows and models from the sidebar, and let the session model pick, parametrize, and run workflows on demand — no import step.",
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"LICENSE"
|
|
28
28
|
],
|
|
29
29
|
"engines": {
|
|
30
|
-
"node": ">=22"
|
|
30
|
+
"node": ">=22",
|
|
31
|
+
"dsh": ">=0.2.2-rc.5"
|
|
31
32
|
},
|
|
32
33
|
"dsh": {
|
|
33
34
|
"bundle": {
|