@240xu/dsh-message-ops 0.47.0 → 0.47.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.
package/README.md CHANGED
@@ -2043,7 +2043,8 @@ applyScriptLive`),replace 语义由 `applySurfaceReplace` 唯一拥有。旧
2043
2043
  | --- | --- |
2044
2044
  | 0.46.0 | 消息详情区分「取消」与「读取失败」:写操作作废了在途 `/text` 请求时不再误报失败(保持读取态并自动重取一次),超时用专用文案 |
2045
2045
  | 0.46.1 | **P0**:宿主 0.2.3 服务隔离使 `connection.admit` 抛错,旧代码把异常当拒绝 → 全站 401。现降级到静态围栏(回环 + same-origin + BrowserAuth):真机 `GET /messages` 200、`POST /revert` 200、`POST /restore` 200 全部复验 |
2046
- | 兼容说明 | 0.2.3 + 本插件(`inject=[]`)时 `--trusted-host` 判定不可用:非回环站点仍 403;需宿主允许 fiber 级 inject 或提供独立来源判定 API |
2046
+ | 0.46.1 | 围栏在 `admit` 抛隔离错误时降级到静态围栏(不再全站 401) |
2047
+ | 0.47.1 | 新增 `config.trustedHosts`:0.2.3 + `inject=[]` 时部署方可显式声明可信 Host(同源 + BrowserAuth cookie 条件不放宽),非回环站点重新可用 |
2047
2048
 
2048
2049
  当前规模:**744 tests / 740 pass / 4 skip / 0 todo / 0 fail**(`npm test`)。
2049
2050
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@240xu/dsh-message-ops",
3
- "version": "0.47.0",
3
+ "version": "0.47.1",
4
4
  "description": "DSH web 插件:消息级回滚 / 删除 / 恢复 / 分叉 / 编辑重发(补 dsh 官方没有的部分)。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -169,6 +169,41 @@ async function readJsonBody(req) {
169
169
  }
170
170
  }
171
171
 
172
+ /**
173
+ * 判定请求是否来自**显式配置**的可信 Host(0.47.1,srv-audit E19)。
174
+ *
175
+ * 宿主 0.2.3 下 `connection.admit` 不可调用时,非回环站点只能靠部署方在插件
176
+ * `config.trustedHosts` 里显式声明。判定与宿主同条件:
177
+ * - Host(去端口)必须精确命中列表;
178
+ * - `sec-fetch-site` 存在时必须是 same-origin(拒 cross-site);
179
+ * - `Origin` 存在时必须与本请求 Host 同源。
180
+ * 之后仍要过 `webAuthGate`(BrowserAuth cookie)—— 配置只放宽「来源」,不放宽认证。
181
+ *
182
+ * @param {object} req Node HTTP 请求(需 headers)
183
+ * @returns {boolean} 是否命中配置的可信 Host
184
+ */
185
+ function isConfiguredTrustedHost(req) {
186
+ if (__trustedHosts.length === 0) return false
187
+ const headers = (req && req.headers) || {}
188
+ const hostHeader = String(headers.host || '').toLowerCase()
189
+ if (!hostHeader) return false
190
+ const hostnameOnly = hostHeader.split(':')[0]
191
+ if (!__trustedHosts.includes(hostHeader) && !__trustedHosts.includes(hostnameOnly)) return false
192
+ const site = String(headers['sec-fetch-site'] || '').toLowerCase()
193
+ if (site && site !== 'same-origin' && site !== 'none') return false
194
+ const origin = String(headers.origin || '').toLowerCase()
195
+ if (origin) {
196
+ let originHost = ''
197
+ try {
198
+ originHost = new URL(origin).host
199
+ } catch {
200
+ return false
201
+ }
202
+ if (originHost !== hostHeader && originHost !== hostnameOnly) return false
203
+ }
204
+ return true
205
+ }
206
+
172
207
  /**
173
208
  * 统一入口围栏:全部 /api/message-ops/* 路由先过这一层。
174
209
  *
@@ -228,8 +263,13 @@ async function fence(targetCtx, req, res) {
228
263
  }
229
264
  return true
230
265
  }
231
- if (!isTrustedApiRequest(req)) {
232
- sendJson(res, 403, { ok: false, error: 'untrusted request origin (loopback Host + same-origin only)' })
266
+ if (!isTrustedApiRequest(req) && !isConfiguredTrustedHost(req)) {
267
+ sendJson(res, 403, {
268
+ ok: false,
269
+ error: __trustedHosts.length > 0
270
+ ? 'untrusted request origin (host not in config.trustedHosts + same-origin only)'
271
+ : 'untrusted request origin (loopback Host + same-origin only)',
272
+ })
233
273
  return false
234
274
  }
235
275
  // 0.10.8:宿主同条件 GET 被读全量(审计缺口)。宿主 /api admit = 三层围栏
@@ -1490,6 +1530,11 @@ async function runDiskWrite({ logPath, diag, plannedOf, scriptOf, resultOf }) {
1490
1530
  /** 测试钩子(非公开 API):直接驱动 list / search / text 与文件派生缓存。 */
1491
1531
  export const _internals = {
1492
1532
  errorStatusOf,
1533
+ validateConfig,
1534
+ isConfiguredTrustedHost,
1535
+ setTrustedHostsForTests: hosts => {
1536
+ __trustedHosts = hosts
1537
+ },
1493
1538
  publicErrorOf,
1494
1539
  runningSourceOf: () => __runningSource,
1495
1540
  resetCapturedServicesForTests,
@@ -1977,7 +2022,39 @@ function registerToolTolerantly(ctx, loadDefineTool) {
1977
2022
  // 插件入口:HTTP 路由 + 容错工具注册
1978
2023
  // ---------------------------------------------------------------------------
1979
2024
 
1980
- export function apply(ctx) {
2025
+ /**
2026
+ * 校验插件配置(0.47.1,srv-audit E19)。
2027
+ *
2028
+ * 宿主 0.2.3 + `inject=[]` 时 `connection.admit` 不可调用(服务隔离),插件拿不到
2029
+ * trustedHosts 判定。此处允许部署方显式配置可信 Host(cordis.yml 的插件 `config`),
2030
+ * 让经 `--trusted-host` 暴露的站点仍可用 —— 与宿主判定同条件(同源 + BrowserAuth cookie)。
2031
+ *
2032
+ * @param {unknown} config cordis 传入的插件配置
2033
+ * @returns {{trustedHosts: string[]}} 规范化后的配置
2034
+ * @throws {Error} 配置形状非法时 fail loud(不静默忽略)
2035
+ */
2036
+ export function validateConfig(config) {
2037
+ if (config === undefined || config === null) return { trustedHosts: [] }
2038
+ if (typeof config !== 'object' || Array.isArray(config)) throw new Error('dsh-message-ops: config must be an object')
2039
+ const unknown = Object.keys(config).filter(key => key !== 'trustedHosts')
2040
+ if (unknown.length > 0) throw new Error(`dsh-message-ops: unknown config key "${unknown[0]}"`)
2041
+ const raw = config.trustedHosts
2042
+ if (raw === undefined) return { trustedHosts: [] }
2043
+ if (!Array.isArray(raw)) throw new Error('dsh-message-ops: trustedHosts must be an array of host names')
2044
+ const hosts = []
2045
+ for (const item of raw) {
2046
+ if (typeof item !== 'string' || item.trim() === '') throw new Error('dsh-message-ops: trustedHosts entries must be non-empty strings')
2047
+ if (/^[^:/]*:/.test(item) || item.includes('/')) throw new Error(`dsh-message-ops: trustedHosts entry "${item}" must be a bare host name (no port, no scheme)`)
2048
+ hosts.push(item.trim().toLowerCase())
2049
+ }
2050
+ return { trustedHosts: hosts }
2051
+ }
2052
+
2053
+ /** 本插件进程内生效的可信 Host 列表(apply 时写入;测试可重置)。 */
2054
+ let __trustedHosts = []
2055
+
2056
+ export function apply(ctx, config) {
2057
+ __trustedHosts = validateConfig(config).trustedHosts
1981
2058
  // 诊断(0.5.4 调试):会话状态服务解析情况
1982
2059
  try {
1983
2060
  const names = ['sessions', 'agents', 'sessionController', 'sessionList', 'session-manager']