dsh-session-superagent 0.9.206 → 0.9.208

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/CHANGELOG.md CHANGED
@@ -2,6 +2,80 @@
2
2
 
3
3
  格式参照 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## [0.9.208] - 2026-11-05 —— 覆盖审计顺手挖出一个真 bug:`deferReason` 被拼成 `quota09:30Z`
6
+
7
+ ### ① 真 bug:额度文案拼接错位
8
+
9
+ ```js
10
+ entry.deferReason = 'quota'
11
+ + new Date(Date.now() + throttle).toISOString().slice(11, 16) + 'Z' // → 'quota09:30Z'
12
+ ```
13
+
14
+ `deferReason` 是**机器可读的稳定原因**(面板按它查 `DEFER_LABELS`、打标签、筛选),掺进时间就永远查不到,
15
+ **面板「按暂停原因筛选」永远是空列表**。
16
+
17
+ 改法不是就地改字符串,而是把文案抽成**纯函数** `quotaDeferText()` 并加单测钉住:
18
+ `reason` 必须**恰好**是已知的稳定值、不许含 `:`;时间只能出现在人读的那句话里。
19
+ T20 是把 bug 跑出来的(`actual: 'quota00:01Z'`),现在这条用例进套件了。
20
+
21
+ ### ② 清掉一处空三元
22
+
23
+ `bumpFailure(entry, diag && diag.kind === 'auth' ? baseMs : baseMs)` —— 两个分支同一个值。
24
+ 写它的人本来想区分什么,但没写出来;删掉,行为不变。
25
+
26
+ ### ③ 补两条覆盖(T20 报的「必须补」里最便宜的两条)
27
+
28
+ - **凭证写入校验**:`sshKeyPath` / `ghConfigDir` 必须绝对路径 —— 以前只有**一个 grep 源码**的测试在守
29
+ (源码里有那句字符串就绿),现在真打 HTTP 看 400。
30
+ - **凭证清空仍然合法**(三个字段都传空串)。
31
+
32
+ ### ④ 覆盖审计本身的方法论(记录)
33
+
34
+ 官方 `--experimental-test-coverage` 对 `lib/index.js` **不可信**:5 个测试文件、33 处用
35
+ `import('../lib/index.js?xxx=…')` 重复加载同一模块,报告器只保留其中一个实例 ——
36
+ 同一条命令连跑两次给 24.14% 和 11.03%,只跑单个文件反而报 28.03%(统计上不可能)。
37
+ 真实值是 **59.17% 行 / 67.9% 函数**(V8 原始区间合并)。洞集中在「必须真起进程 / 真等定时器 / 真写盘」那一层。
38
+
39
+ ### 证据
40
+
41
+ ```
42
+ node --test → 360/360(新增:额度文案回归、凭证校验两条)
43
+ ```
44
+
45
+ ## [0.9.207] - 2026-11-05 —— 路由安全审计剩下的「值得改」:无上限、无形状、无节流
46
+
47
+ ### ① 守数量没有任何上限(HTTP 路径)
48
+
49
+ 工具路径早就有 `MAX_WATCHERS_PER_SESSION = 20`,**HTTP 路径漏了** ——
50
+ 真机实测 100 次 POST 就是 100 条守,每条一个定时器(自造 DoS)。
51
+ 现在两条闸:单会话 ≤20、全局 ≤200,超了回 **429**。
52
+
53
+ ### ② `sessionId` 无长度校验
54
+
55
+ 它会进 id、落进状态文件、拼进唤醒消息 —— 以前多长都收。
56
+ 现在 >200 字符回 **400**。
57
+
58
+ ### ③ Gitee 令牌无形状校验
59
+
60
+ 任意字符串都会被当成凭证存进去。Gitee 私人令牌是 **32 位十六进制**(真机形状),
61
+ 不像就回 **400** —— 别把垃圾存成凭证。
62
+
63
+ ### ④ `/update?refresh=1` 可被反复触发
64
+
65
+ 它是唯一会**主动出站**的只读路由,`refresh=1` 会绕过 10 分钟缓存。
66
+ 任意网页反复打它就能让宿主一直去戳 npm registry。已加 60 秒节流(面板「刷新」按钮不受影响,人点不了这么快)。
67
+
68
+ ### 未修(需要产品决策)
69
+
70
+ **`workdir` 可以指到任意绝对路径**(实测能落盘到 `C:\Windows`)。它该不该被限制到某个根(例如工作区目录)?
71
+ 这是产品决策,不是一行能定的 —— 单独立项。
72
+
73
+ ### 证据
74
+
75
+ ```
76
+ node --test → 358/358(新增:守数量上限 / sessionId 超长 / Gitee 令牌形状 三条)
77
+ ```
78
+
5
79
  ## [0.9.206] - 2026-11-05 —— 路由安全审计:任意网页都能删掉你的守
6
80
 
7
81
  ### ① 九条路由零来源校验(CSRF)
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  <img src="assets/icon-180.png" width="88" height="88" alt="">
4
4
 
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
- [![Version](https://img.shields.io/badge/version-0.9.206-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.208-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
7
7
  [![npm](https://img.shields.io/npm/v/dsh-session-superagent.svg)](https://www.npmjs.com/package/dsh-session-superagent)
8
8
  [![dsh](https://img.shields.io/badge/DeepSeek%20Harness-plugin-orange.svg)](https://github.com/topics/dsh-plugin)
9
9
 
@@ -144,7 +144,7 @@ Gitee 私有仓库要在「设置 → 凭证」填私人令牌;公开仓库不
144
144
  ## 开发
145
145
 
146
146
  ```sh
147
- node --test # 355 项单测
147
+ node --test # 360 项单测
148
148
  ```
149
149
 
150
150
  ### 工具输出:文本给人和 agent,JSON 给程序
package/client.js CHANGED
@@ -14,7 +14,7 @@ window.__ModuleLoader__.load({
14
14
  const { useCallback, useEffect, useRef, useState } = React
15
15
 
16
16
  // 客户端版本探针:每次模块加载写一次,用来确认浏览器里跑的是哪一版
17
- const CLIENT_VERSION = '0.9.206'
17
+ const CLIENT_VERSION = '0.9.208'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/gh-format.js CHANGED
@@ -577,3 +577,22 @@ export function linkedPrNote(linked) {
577
577
  return '已有 PR #' + first.number + (stateText || draftText || blocked ? '(' + stateText + draftText + blocked + ')' : '')
578
578
  + (list.length > 1 ? ' 等 ' + list.length + ' 个' : '')
579
579
  }
580
+
581
+ /**
582
+ * 额度推迟的文案(审核 T20 的回归点)。
583
+ *
584
+ * 以前这段是内联拼的,拼错过一次:
585
+ * `deferReason = 'quota' + new Date(...).toISOString().slice(11, 16) + 'Z'` → `quota09:30Z`
586
+ * `deferReason` 是**机器可读的稳定原因**(面板按它筛选、打标签),掺进时间就永远查不到。
587
+ * 抽成纯函数 + 单测钉住:reason 必须**恰好**是下面这些之一,时间只能出现在人读的那句话里。
588
+ */
589
+ export const DEFER_REASONS = ['ratelimit', 'quota', 'truncated', 'busy', 'window', 'observe', 'backoff']
590
+
591
+ export function quotaDeferText(remaining, resumeInMs, now = Date.now()) {
592
+ const at = new Date(now + Math.max(0, Number(resumeInMs) || 0))
593
+ const hhmm = at.toISOString().slice(11, 16)
594
+ return {
595
+ reason: 'quota',
596
+ text: '额度紧张(剩余 ' + String(remaining) + '),已推迟到 ' + hhmm + ' UTC',
597
+ }
598
+ }
package/lib/index.js CHANGED
@@ -23,6 +23,7 @@ import { zstdDecompressSync } from 'node:zlib'
23
23
 
24
24
  import { redact } from './redact.js'
25
25
  import { decodeBody, truncateUtf8 } from './body.js'
26
+ import { quotaDeferText } from './gh-format.js'
26
27
  import { listGiteeItems, fetchGiteeLastComment, addGiteeLabel, ensureGiteeLabel, giteeWhoami, giteeRepo, mapGiteeRepoAccess, DEFAULT_ACK_LABEL_GITEE, giteeLabels, giteeHasLabel, isValidGiteeLabel, suggestGiteeLabel } from './gitee.js'
27
28
  import {
28
29
  advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
@@ -42,7 +43,7 @@ import {
42
43
 
43
44
  export const name = 'dsh-session-superagent'
44
45
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
45
- export const SERVER_VERSION = '0.9.206'
46
+ export const SERVER_VERSION = '0.9.208'
46
47
  export const inject = ['tools', 'agents', 'webServer']
47
48
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
48
49
  // 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
@@ -66,6 +67,8 @@ const DEFAULT_WAKES_PER_HOUR = 6
66
67
  const DEFAULT_ISOLATE_KEEP_DAYS = 3
67
68
  // 一条会话最多挂多少守:再多轮询压力与上下文都会失控(20 条 × 每 60 秒已很密)
68
69
  const MAX_WATCHERS_PER_SESSION = 20
70
+ /** 全局上限:每条守一个定时器,无限建等于自造 DoS(审核 T19)。 */
71
+ const MAX_WATCHERS_TOTAL = 200
69
72
  /** 隔离区回收的最小间隔:没有超额时不必每次唤醒都跑 git。 */
70
73
  const CLEANUP_INTERVAL_MS = 10 * 60 * 1000
71
74
 
@@ -1179,9 +1182,11 @@ async function pollOnce(ctx, entry, baseMs) {
1179
1182
  const throttle = throttleDelayMs({ remaining: parsed.remaining, resetAt: parsed.resetAt })
1180
1183
  if (throttle > 0) {
1181
1184
  entry.nextAt = new Date(Date.now() + throttle).toISOString()
1182
- entry.lastDeferred = '额度紧张(剩余 ' + parsed.remaining + '),已推迟到 '
1183
- entry.deferReason = 'quota'
1184
- + new Date(Date.now() + throttle).toISOString().slice(11, 16) + 'Z'
1185
+ // 审核 T20:以前 deferReason 被拼成 'quota09:30Z' —— 面板按 reason 筛选永远空。
1186
+ // 文案抽成纯函数(quotaDeferText)并有单测钉住:reason 只有稳定原因,时间只在人读的那句里。
1187
+ const quotaText = quotaDeferText(parsed.remaining, throttle)
1188
+ entry.lastDeferred = quotaText.text
1189
+ entry.deferReason = quotaText.reason
1185
1190
  entry.lastError = null
1186
1191
  entry.needsReauth = false
1187
1192
  return
@@ -1236,7 +1241,7 @@ async function pollWithItems(ctx, entry, baseMs, parsed) {
1236
1241
  // 兜底不再默认「可能是代理干扰」:那会把客户往网络上游带。
1237
1242
  // 现在只有确诊是网关页才说代理;其余情况指向 watch_doctor 与本仓库 issue。
1238
1243
  setEntryError(entry, (diag ? diag.hint : '(返回内容不是合法 JSON:先跑 watch_doctor;确认不是凭证问题就带上这段提 issue)') + ' —— 已按退避自动重试')
1239
- bumpFailure(entry, diag && diag.kind === 'auth' ? baseMs : baseMs)
1244
+ bumpFailure(entry, baseMs)
1240
1245
  if (diag && diag.kind === 'auth') {
1241
1246
  const wait = permanentBackoffMs('auth', baseMs)
1242
1247
  if (wait > 0) entry.nextAt = new Date(Date.now() + wait).toISOString()
@@ -1602,8 +1607,14 @@ const STATE_EVENTS_BUDGET = 240
1602
1607
 
1603
1608
  const UPDATE_CACHE_MS = 10 * 60 * 1000
1604
1609
  let updateCache = null
1610
+ // 审核 T19:强制刷新(?refresh=1)没有任何节流 —— 任意网页可以反复触发它去打 npm registry。
1611
+ // 60 秒内最多真查一次;其余走缓存(面板上的「刷新」按钮不受影响,人点不了这么快)。
1612
+ let lastForcedUpdateAt = 0
1613
+ const UPDATE_FORCE_MIN_MS = 60 * 1000
1605
1614
  const checkUpdate = async (force) => {
1606
- if (!force && updateCache && Date.now() - updateCache.at < UPDATE_CACHE_MS) return updateCache.value
1615
+ const mayForce = force && Date.now() - lastForcedUpdateAt >= UPDATE_FORCE_MIN_MS
1616
+ if (mayForce) lastForcedUpdateAt = Date.now()
1617
+ if (!mayForce && updateCache && Date.now() - updateCache.at < UPDATE_CACHE_MS) return updateCache.value
1607
1618
  const current = SERVER_VERSION
1608
1619
  const value = { current, latest: null, publishedAt: null, hasUpdate: false, command: '', error: null }
1609
1620
  // 离线开关:单测不该依赖 npm registry(一次网络抖动就会让整条门禁变红)
@@ -1917,6 +1928,8 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1917
1928
  const repo = spec.repo
1918
1929
  const provider = spec.provider
1919
1930
  const sessionId = String(body.sessionId || '').trim()
1931
+ // 审核 T19:sessionId 以前无上限、无字符集校验 —— 它会进 id、落进状态文件、拼进唤醒消息。
1932
+ if (sessionId.length > 200) return json(res, 400, { error: 'sessionId too long (max 200)' })
1920
1933
  if (!sessionId) return json(res, 400, { error: 'sessionId is required (open the panel in the session that should be woken)' })
1921
1934
  // 若这个实例的 ctx 已经 dispose(宿主重载后旧 handler 仍在服务请求),
1922
1935
  // 建出来的 watcher 一定跑不起来(定时器一 tick 就 inactive context)。
@@ -1933,6 +1946,17 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1933
1946
  // id 里带 provider:同一个 owner/name 在两家可能都存在,混在一起会互相覆盖
1934
1947
  const id = (provider === 'gitee' ? 'gitee:' : '') + repo + '#' + sessionId
1935
1948
  const created = !(state.watchers && state.watchers[id])
1949
+ // 审核 T19:新建没有任何上限 —— 100 次 POST 就是 100 条守(每条一个定时器)。
1950
+ // 工具路径早就有 MAX_WATCHERS_PER_SESSION,HTTP 路径漏了;这里再补一个全局上限。
1951
+ if (created) {
1952
+ const mine = Object.values(state.watchers).filter((w) => w && w.sessionId === sessionId)
1953
+ if (mine.length >= MAX_WATCHERS_PER_SESSION) {
1954
+ return json(res, 429, { error: '一条会话最多挂 ' + MAX_WATCHERS_PER_SESSION + ' 条守' })
1955
+ }
1956
+ if (Object.keys(state.watchers).length >= MAX_WATCHERS_TOTAL) {
1957
+ return json(res, 429, { error: '全局最多 ' + MAX_WATCHERS_TOTAL + ' 条守' })
1958
+ }
1959
+ }
1936
1960
  const entry = state.watchers[id] || {
1937
1961
  id, repo, provider, sessionId, cursor: null, seen: {}, lastReported: {},
1938
1962
  cooldownMs: 180000, createdAt: new Date().toISOString(),
@@ -2040,6 +2064,9 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2040
2064
  if (typeof body.giteeToken === 'string') {
2041
2065
  const gt = body.giteeToken.trim()
2042
2066
  if (gt === '') delete next.giteeToken
2067
+ // 审核 T19:Gitee 令牌以前不校验形状 —— 任意字符串都会存进去并被当成凭证用。
2068
+ // Gitee 私人令牌是 **32 位十六进制**(真机形状);不像就拒绝,别把垃圾存成凭证。
2069
+ else if (!/^[0-9a-f]{32}$/i.test(gt)) return json(res, 400, { error: 'Gitee 令牌应为 32 位十六进制' })
2043
2070
  else next.giteeToken = gt
2044
2071
  }
2045
2072
  if (typeof body.ghConfigDir === 'string') {
@@ -2143,6 +2170,8 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2143
2170
  const body = await readBody(req, res)
2144
2171
  if (!body) return // 已回 413
2145
2172
  const sessionId = String(body.sessionId || '').trim()
2173
+ // 审核 T19:sessionId 以前无上限、无字符集校验 —— 它会进 id、落进状态文件、拼进唤醒消息。
2174
+ if (sessionId.length > 200) return json(res, 400, { error: 'sessionId too long (max 200)' })
2146
2175
  const objective = String(body.objective || '').trim()
2147
2176
  if (!sessionId) return json(res, 400, { error: 'sessionId is required' })
2148
2177
  const goals = typeof ctx.get === 'function' ? ctx.get('goals') : undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.206",
3
+ "version": "0.9.208",
4
4
  "description": "Session-level superagent for DeepSeek Harness: watch a GitHub repository and wake a session when something needs doing. Credentials never reach the model.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",