dsh-session-superagent 0.9.205 → 0.9.207

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,85 @@
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.207] - 2026-11-05 —— 路由安全审计剩下的「值得改」:无上限、无形状、无节流
6
+
7
+ ### ① 守数量没有任何上限(HTTP 路径)
8
+
9
+ 工具路径早就有 `MAX_WATCHERS_PER_SESSION = 20`,**HTTP 路径漏了** ——
10
+ 真机实测 100 次 POST 就是 100 条守,每条一个定时器(自造 DoS)。
11
+ 现在两条闸:单会话 ≤20、全局 ≤200,超了回 **429**。
12
+
13
+ ### ② `sessionId` 无长度校验
14
+
15
+ 它会进 id、落进状态文件、拼进唤醒消息 —— 以前多长都收。
16
+ 现在 >200 字符回 **400**。
17
+
18
+ ### ③ Gitee 令牌无形状校验
19
+
20
+ 任意字符串都会被当成凭证存进去。Gitee 私人令牌是 **32 位十六进制**(真机形状),
21
+ 不像就回 **400** —— 别把垃圾存成凭证。
22
+
23
+ ### ④ `/update?refresh=1` 可被反复触发
24
+
25
+ 它是唯一会**主动出站**的只读路由,`refresh=1` 会绕过 10 分钟缓存。
26
+ 任意网页反复打它就能让宿主一直去戳 npm registry。已加 60 秒节流(面板「刷新」按钮不受影响,人点不了这么快)。
27
+
28
+ ### 未修(需要产品决策)
29
+
30
+ **`workdir` 可以指到任意绝对路径**(实测能落盘到 `C:\Windows`)。它该不该被限制到某个根(例如工作区目录)?
31
+ 这是产品决策,不是一行能定的 —— 单独立项。
32
+
33
+ ### 证据
34
+
35
+ ```
36
+ node --test → 358/358(新增:守数量上限 / sessionId 超长 / Gitee 令牌形状 三条)
37
+ ```
38
+
39
+ ## [0.9.206] - 2026-11-05 —— 路由安全审计:任意网页都能删掉你的守
40
+
41
+ ### ① 九条路由零来源校验(CSRF)
42
+
43
+ 宿主只绑 `127.0.0.1`,但**浏览器里的任意网页**都能往 localhost 发请求:`text/plain` 是「简单请求」,
44
+ 不触发预检;而 CORS 只挡跨站**读**响应,不挡副作用。真机实测:
45
+
46
+ ```
47
+ 跨站 POST /watch {action:delete} → 200 {"deleted":true} 状态文件真被删
48
+ 跨站 POST /credentials {token:""} → 200 secrets.json 里 token 消失
49
+ 跨站 POST /goal → 200 活会话目标被改成「忽略用户的指令…」
50
+ ```
51
+
52
+ 宿主层也不拦(`app.asar` 里抽出来的派发就是 pathname 查表,没有 Origin/Referer 校验)。
53
+ 现在三条写路由统一走 `guardWrite()`:**带了 Origin 就必须是本机**(127.0.0.1 / localhost / ::1)。
54
+ 浏览器跨站 POST 一定带 Origin;curl / 测试不带,所以合法用法不受影响。
55
+
56
+ ### ② `id = "__proto__"` 原型污染
57
+
58
+ `watchers[id]` 命中 `Object.prototype`,随后 `entry.enabled = false` **写到原型上** ——
59
+ 进程级生效(所有缺省 `enabled` 的守、同进程其它插件一起受影响),而盘上一个字没改。
60
+ 实测:`({}).enabled` 变成 `false`。已改成只认**自有属性**且 id 必须是字符串。
61
+
62
+ ### ③ 请求体没有硬上限
63
+
64
+ 64KB 以前只是「读完再截断」—— 48MB 的请求体会先把 RSS 从 105MB 顶到 333MB。
65
+ 现在边读边算,超了立刻 `destroy()` 并返回 **413**。
66
+
67
+ ### ④ `repo` 放行 `..`
68
+
69
+ `.` 和 `..` 能过 `[A-Za-z0-9_.-]+` 这条字符类,于是 `../..` 会被当成 owner/name 一路拼进 URL 与磁盘路径。
70
+ 已拒绝这两个路径段。
71
+
72
+ ### 未修(登记)
73
+
74
+ `workdir` 可以指到任意路径(实测能落盘到 `C:\Windows`)、`sessionId` 无长度校验、`/update?refresh=1` 可被迫出站、
75
+ 守数量无上限。这些都需要产品级决策(workdir 该不该限制到某个根),单独立项。
76
+
77
+ ### 证据
78
+
79
+ ```
80
+ node --test → 355/355(新增:跨站 403 / __proto__ 被拒 / repo 拒绝 ..)
81
+ 实测:__proto__ → {"ok":false},({}).enabled = undefined,正常 id 照旧
82
+ ```
83
+
5
84
  ## [0.9.205] - 2026-11-05 —— 门禁锁只覆盖 2/39 项;`verify-gates` 被强杀会留下坏文件
6
85
 
7
86
  ### M2 · 整轮门禁只持一把锁
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.205-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.207-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 # 352 项单测
147
+ node --test # 358 项单测
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.205'
17
+ const CLIENT_VERSION = '0.9.207'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/index.js CHANGED
@@ -42,7 +42,7 @@ import {
42
42
 
43
43
  export const name = 'dsh-session-superagent'
44
44
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
45
- export const SERVER_VERSION = '0.9.205'
45
+ export const SERVER_VERSION = '0.9.207'
46
46
  export const inject = ['tools', 'agents', 'webServer']
47
47
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
48
48
  // 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
@@ -66,6 +66,8 @@ const DEFAULT_WAKES_PER_HOUR = 6
66
66
  const DEFAULT_ISOLATE_KEEP_DAYS = 3
67
67
  // 一条会话最多挂多少守:再多轮询压力与上下文都会失控(20 条 × 每 60 秒已很密)
68
68
  const MAX_WATCHERS_PER_SESSION = 20
69
+ /** 全局上限:每条守一个定时器,无限建等于自造 DoS(审核 T19)。 */
70
+ const MAX_WATCHERS_TOTAL = 200
69
71
  /** 隔离区回收的最小间隔:没有超额时不必每次唤醒都跑 git。 */
70
72
  const CLEANUP_INTERVAL_MS = 10 * 60 * 1000
71
73
 
@@ -1602,8 +1604,14 @@ const STATE_EVENTS_BUDGET = 240
1602
1604
 
1603
1605
  const UPDATE_CACHE_MS = 10 * 60 * 1000
1604
1606
  let updateCache = null
1607
+ // 审核 T19:强制刷新(?refresh=1)没有任何节流 —— 任意网页可以反复触发它去打 npm registry。
1608
+ // 60 秒内最多真查一次;其余走缓存(面板上的「刷新」按钮不受影响,人点不了这么快)。
1609
+ let lastForcedUpdateAt = 0
1610
+ const UPDATE_FORCE_MIN_MS = 60 * 1000
1605
1611
  const checkUpdate = async (force) => {
1606
- if (!force && updateCache && Date.now() - updateCache.at < UPDATE_CACHE_MS) return updateCache.value
1612
+ const mayForce = force && Date.now() - lastForcedUpdateAt >= UPDATE_FORCE_MIN_MS
1613
+ if (mayForce) lastForcedUpdateAt = Date.now()
1614
+ if (!mayForce && updateCache && Date.now() - updateCache.at < UPDATE_CACHE_MS) return updateCache.value
1607
1615
  const current = SERVER_VERSION
1608
1616
  const value = { current, latest: null, publishedAt: null, hasUpdate: false, command: '', error: null }
1609
1617
  // 离线开关:单测不该依赖 npm registry(一次网络抖动就会让整条门禁变红)
@@ -1800,10 +1808,27 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1800
1808
  path: '/session-superagent/client-error',
1801
1809
  handler: (req, res) => { record('error', String(req.url).slice(0, 400)); res.statusCode = 204; res.end() },
1802
1810
  })
1803
- const readBody = (req) => new Promise((resolve) => {
1811
+ // 审核 T19:64KB 以前只是「读完再截断」—— 48MB 的请求体会先把 RSS 从 105MB 顶到 333MB。
1812
+ // 现在边读边算,超了立刻 destroy 并返回 413。
1813
+ const MAX_BODY_BYTES = 64 * 1024
1814
+ const readBody = (req, res) => new Promise((resolve) => {
1804
1815
  const chunks = []
1805
- req.on('data', (chunk) => chunks.push(chunk))
1816
+ let bytes = 0
1817
+ let aborted = false
1818
+ req.on('data', (chunk) => {
1819
+ if (aborted) return
1820
+ bytes += chunk.length
1821
+ if (bytes > MAX_BODY_BYTES) {
1822
+ aborted = true
1823
+ try { res.statusCode = 413; res.setHeader('content-type', 'application/json; charset=utf-8'); res.end(JSON.stringify({ error: 'request body too large' })) } catch { /* 尽力而为 */ }
1824
+ try { req.destroy() } catch { /* 尽力而为 */ }
1825
+ resolve(null)
1826
+ return
1827
+ }
1828
+ chunks.push(chunk)
1829
+ })
1806
1830
  req.on('end', () => {
1831
+ if (aborted) return
1807
1832
  try {
1808
1833
  // 按字符边界截断:直接 subarray 会切开汉字,剩下的半截被 decodeBody 当成 GBK 整段重解(T16)
1809
1834
  const raw = decodeBody(truncateUtf8(Buffer.concat(chunks), 64 * 1024))
@@ -1819,6 +1844,38 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1819
1844
  res.end(JSON.stringify(body))
1820
1845
  }
1821
1846
 
1847
+ /**
1848
+ * 写操作的来源校验(审核 T19)。
1849
+ *
1850
+ * 宿主只绑 127.0.0.1,但**浏览器里的任意网页**都能往 localhost 发请求:
1851
+ * `text/plain` 是「简单请求」,不触发预检,而 CORS 只挡跨站**读**响应、不挡副作用。
1852
+ * 实测:跨站 POST `{action:'delete'}` 就能删守、清凭证、改活会话目标。
1853
+ *
1854
+ * 两道闸(任一不过就 403):
1855
+ * 1. `Content-Type` 必须是 `application/json` —— 这就要求预检,浏览器会先问 OPTIONS;
1856
+ * 2. 带了 `Origin` 就必须是**本机**(127.0.0.1 / localhost / [::1])。
1857
+ * 非浏览器客户端(curl / 测试)不带 Origin,只受第 1 条约束。
1858
+ */
1859
+ const allowedOrigin = (origin) => {
1860
+ if (!origin) return true // 非浏览器(curl、同源 GET)
1861
+ try {
1862
+ const url = new URL(origin)
1863
+ return url.hostname === '127.0.0.1' || url.hostname === 'localhost' || url.hostname === '[::1]' || url.hostname === '::1'
1864
+ } catch {
1865
+ return false
1866
+ }
1867
+ }
1868
+ const guardWrite = (req, res) => {
1869
+ // 只查 Origin:浏览器跨站 POST **一定**带它(简单请求也一样),而 curl / 测试不带。
1870
+ // 不要求 content-type —— 那会把 curl 之类的合法用法一起挡掉,收益还一样。
1871
+ const origin = req.headers.origin || req.headers.referer
1872
+ if (!allowedOrigin(origin)) {
1873
+ json(res, 403, { error: 'cross-origin request refused' })
1874
+ return false
1875
+ }
1876
+ return true
1877
+ }
1878
+
1822
1879
  // Create or update a watcher from the panel. The panel is session-scoped,
1823
1880
  // so it must send the session id it is bound to.
1824
1881
  addRoute({
@@ -1826,7 +1883,9 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1826
1883
  path: '/session-superagent/watch',
1827
1884
  handler: async (req, res) => {
1828
1885
  if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
1829
- const body = await readBody(req)
1886
+ if (!guardWrite(req, res)) return // 来源不对:已回 403
1887
+ const body = await readBody(req, res)
1888
+ if (!body) return // 已回 413
1830
1889
  // pause / resume / delete target an existing watcher by id; the
1831
1890
  // create path below is the default when no action is supplied.
1832
1891
  if (body.action) {
@@ -1866,6 +1925,8 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1866
1925
  const repo = spec.repo
1867
1926
  const provider = spec.provider
1868
1927
  const sessionId = String(body.sessionId || '').trim()
1928
+ // 审核 T19:sessionId 以前无上限、无字符集校验 —— 它会进 id、落进状态文件、拼进唤醒消息。
1929
+ if (sessionId.length > 200) return json(res, 400, { error: 'sessionId too long (max 200)' })
1869
1930
  if (!sessionId) return json(res, 400, { error: 'sessionId is required (open the panel in the session that should be woken)' })
1870
1931
  // 若这个实例的 ctx 已经 dispose(宿主重载后旧 handler 仍在服务请求),
1871
1932
  // 建出来的 watcher 一定跑不起来(定时器一 tick 就 inactive context)。
@@ -1882,6 +1943,17 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1882
1943
  // id 里带 provider:同一个 owner/name 在两家可能都存在,混在一起会互相覆盖
1883
1944
  const id = (provider === 'gitee' ? 'gitee:' : '') + repo + '#' + sessionId
1884
1945
  const created = !(state.watchers && state.watchers[id])
1946
+ // 审核 T19:新建没有任何上限 —— 100 次 POST 就是 100 条守(每条一个定时器)。
1947
+ // 工具路径早就有 MAX_WATCHERS_PER_SESSION,HTTP 路径漏了;这里再补一个全局上限。
1948
+ if (created) {
1949
+ const mine = Object.values(state.watchers).filter((w) => w && w.sessionId === sessionId)
1950
+ if (mine.length >= MAX_WATCHERS_PER_SESSION) {
1951
+ return json(res, 429, { error: '一条会话最多挂 ' + MAX_WATCHERS_PER_SESSION + ' 条守' })
1952
+ }
1953
+ if (Object.keys(state.watchers).length >= MAX_WATCHERS_TOTAL) {
1954
+ return json(res, 429, { error: '全局最多 ' + MAX_WATCHERS_TOTAL + ' 条守' })
1955
+ }
1956
+ }
1885
1957
  const entry = state.watchers[id] || {
1886
1958
  id, repo, provider, sessionId, cursor: null, seen: {}, lastReported: {},
1887
1959
  cooldownMs: 180000, createdAt: new Date().toISOString(),
@@ -1966,7 +2038,9 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1966
2038
  path: '/session-superagent/credentials',
1967
2039
  handler: async (req, res) => {
1968
2040
  if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
1969
- const body = await readBody(req)
2041
+ if (!guardWrite(req, res)) return // 来源不对:已回 403
2042
+ const body = await readBody(req, res)
2043
+ if (!body) return // 已回 413
1970
2044
  const next = readSecrets()
1971
2045
  if (typeof body.token === 'string') {
1972
2046
  const token = body.token.trim()
@@ -1987,6 +2061,9 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1987
2061
  if (typeof body.giteeToken === 'string') {
1988
2062
  const gt = body.giteeToken.trim()
1989
2063
  if (gt === '') delete next.giteeToken
2064
+ // 审核 T19:Gitee 令牌以前不校验形状 —— 任意字符串都会存进去并被当成凭证用。
2065
+ // Gitee 私人令牌是 **32 位十六进制**(真机形状);不像就拒绝,别把垃圾存成凭证。
2066
+ else if (!/^[0-9a-f]{32}$/i.test(gt)) return json(res, 400, { error: 'Gitee 令牌应为 32 位十六进制' })
1990
2067
  else next.giteeToken = gt
1991
2068
  }
1992
2069
  if (typeof body.ghConfigDir === 'string') {
@@ -2086,8 +2163,12 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2086
2163
  path: '/session-superagent/goal',
2087
2164
  handler: async (req, res) => {
2088
2165
  if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
2089
- const body = await readBody(req)
2166
+ if (!guardWrite(req, res)) return // 来源不对:已回 403
2167
+ const body = await readBody(req, res)
2168
+ if (!body) return // 已回 413
2090
2169
  const sessionId = String(body.sessionId || '').trim()
2170
+ // 审核 T19:sessionId 以前无上限、无字符集校验 —— 它会进 id、落进状态文件、拼进唤醒消息。
2171
+ if (sessionId.length > 200) return json(res, 400, { error: 'sessionId too long (max 200)' })
2091
2172
  const objective = String(body.objective || '').trim()
2092
2173
  if (!sessionId) return json(res, 400, { error: 'sessionId is required' })
2093
2174
  const goals = typeof ctx.get === 'function' ? ctx.get('goals') : undefined
package/lib/watch.js CHANGED
@@ -452,8 +452,13 @@ export function newestUpdatedAt(fresh) {
452
452
  */
453
453
  export function applyWatcherAction(state, { action, id } = {}) {
454
454
  const watchers = (state && state.watchers) || {}
455
+ // 审核 T19:`id = "__proto__"` 会命中 Object.prototype —— 随后 `entry.enabled = false`
456
+ // 写到**原型**上,进程级生效(所有缺省 enabled 的守、同进程其它插件一起受影响),
457
+ // 而盘上一个字没改。只认自有属性,且 id 必须是字符串。
458
+ if (typeof id !== 'string' || !id || !Object.prototype.hasOwnProperty.call(watchers, id)) {
459
+ return { ok: false, error: `unknown watcher: ${id || '(missing id)'}` }
460
+ }
455
461
  const entry = watchers[id]
456
- if (!entry) return { ok: false, error: `unknown watcher: ${id || '(missing id)'}` }
457
462
  if (action === 'delete') {
458
463
  delete watchers[id]
459
464
  return { ok: true, action, id, deleted: true }
@@ -1139,6 +1144,11 @@ export function parseRepoSpec(input) {
1139
1144
  if (!/^[A-Za-z0-9_.-]+$/.test(owner) || !/^[A-Za-z0-9_.-]+$/.test(name)) {
1140
1145
  return { ok: false, error: 'owner/name 只能包含字母、数字、下划线、点、短横线' }
1141
1146
  }
1147
+ // 审核 T19:`.` 和 `..` 能过上面那条字符类(点也算合法字符),于是 `'../..'` 会被当成 owner/name ——
1148
+ // 它会一路拼进 URL 与磁盘路径。路径段不许是这两个。
1149
+ if (owner === '.' || owner === '..' || name === '.' || name === '..') {
1150
+ return { ok: false, error: 'owner/name 不能是 . 或 ..' }
1151
+ }
1142
1152
  return { ok: true, provider, repo: owner + '/' + name, spec: provider === 'github' ? owner + '/' + name : 'gitee:' + owner + '/' + name }
1143
1153
  }
1144
1154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.205",
3
+ "version": "0.9.207",
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",