dsh-aupdate 0.4.1 → 0.4.2

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.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/index.js +59 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -130,6 +130,11 @@ mtime 为准:那是磁盘事实。会话目录整个读不到时返回「不
130
130
  日志只经由 `GET /api/dsh-aupdate/logs?lines=N` 读,参数夹在 1 到 1000 之间,
131
131
  不提供写入口——面板能看,但改不了日志。
132
132
 
133
+ 四个接口都要先过宿主的连接准入(`connection.admit` / 旧宿主的
134
+ `requestRejection`),没通过就回 401/403,拿不到 `connection` 服务或它抛错时
135
+ 一律 503。不用「只认回环地址」这种判据:浏览器里任何网页都能向 127.0.0.1 发请求。
136
+ `/state` 和 `/logs` 也照样要过,因为它们分别会吐全量设置和读磁盘上的日志文件。
137
+
133
138
  ## 效率
134
139
 
135
140
  - 只取 dist-tags:24 个包从 2.88 MB 降到 655 B
@@ -162,6 +167,13 @@ queue is flushed before reading, so lines from the check you just triggered are
162
167
  already there. Timestamps render in local time, rotation past 1 MB is called out,
163
168
  and the total line count is shown.
164
169
 
170
+ All four routes go through the host's connection admission (`connection.admit`, or
171
+ `requestRejection` on older hosts) first: a rejection answers 401/403, and a missing
172
+ or throwing `connection` service answers 503. Checking only the loopback address is
173
+ not an admission decision — any web page in the browser can reach 127.0.0.1.
174
+ `/state` and `/logs` are gated too; they expose the whole settings object and read
175
+ the log file off disk.
176
+
165
177
  The log is read through `GET /api/dsh-aupdate/logs?lines=N` only, with the count
166
178
  clamped between 1 and 1000. There is no write path: the page can look, not edit.
167
179
 
package/index.js CHANGED
@@ -158,6 +158,16 @@ function propOf(target, key) {
158
158
  }
159
159
 
160
160
  function serviceOf(ctx, key) {
161
+ // 有 reflect 就先走 reflect.get:cordis 4 的 ctx 是 Proxy,裸读(以及 ctx.get)
162
+ // 碰未在 inject 里声明的服务会抛 "cannot get property without inject",而不是
163
+ // 返回 undefined。connection 正是那种没声明的服务,用 ctx.get 会被我们自己的
164
+ // catch 吞成 null,于是每个请求都判成 503。
165
+ try {
166
+ const reflect = propOf(ctx, 'reflect')
167
+ if (reflect !== undefined && reflect !== null && typeof reflect.get === 'function') {
168
+ return reflect.get(key) ?? null
169
+ }
170
+ } catch {}
161
171
  try {
162
172
  return (typeof ctx?.get === 'function' ? ctx.get(key) : undefined) ?? null
163
173
  } catch {
@@ -1536,6 +1546,52 @@ function sendJson(res, status, value) {
1536
1546
  } catch {}
1537
1547
  }
1538
1548
 
1549
+ /**
1550
+ * 谁能调这些路由。
1551
+ *
1552
+ * 浏览器里任何一个网页都能向 127.0.0.1 发请求,所以「只看 Host/Origin/回环地址」
1553
+ * 不算准入。宿主把这件事交给 connection 服务,这里只做转发:
1554
+ *
1555
+ * - 新宿主是 admit(req),返回 { rejection: status } 信封;旧宿主是
1556
+ * requestRejection(req),直接返回状态码。两种拼写都要认,认错一种就会把
1557
+ * 鉴权整个关掉。
1558
+ * - 返回 undefined 才是放行。
1559
+ * - 服务不在、或调用抛错,一律当拒绝(503)。这里不能「降级成放行」——
1560
+ * 那等于宿主一抖动,设置页对全世界的网页开放。
1561
+ *
1562
+ * connection 故意不写进 inject:cordis 的注入是全或无的,多声明一个依赖会让
1563
+ * 整个作用域等一个可能永远不来的服务。改成每次请求惰性读一次。
1564
+ */
1565
+ function admitRequest(scope, req, res) {
1566
+ const connection = serviceOf(scope, 'connection')
1567
+ // 必须回 true:调用点写的是 if (admitRequest(...)) return,回 undefined 的话
1568
+ // 请求照旧往下走,鉴权就形同虚设(写的时候真踩过一次)。
1569
+ const write = (status) => {
1570
+ try {
1571
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
1572
+ res.end(JSON.stringify({ error: 'unauthorized' }))
1573
+ } catch {}
1574
+ return true
1575
+ }
1576
+ if (connection === null) return write(503)
1577
+ let status
1578
+ try {
1579
+ const admit = propOf(connection, 'admit')
1580
+ if (typeof admit === 'function') {
1581
+ const verdict = admit.call(connection, req)
1582
+ status = verdict && typeof verdict === 'object' ? verdict.rejection : undefined
1583
+ } else {
1584
+ const reject = propOf(connection, 'requestRejection')
1585
+ if (typeof reject !== 'function') return write(503)
1586
+ status = reject.call(connection, req)
1587
+ }
1588
+ } catch {
1589
+ return write(503)
1590
+ }
1591
+ if (status === undefined || status === null) return false
1592
+ return write(typeof status === 'number' ? status : 403)
1593
+ }
1594
+
1539
1595
  function readBody(req) {
1540
1596
  return new Promise((settle) => {
1541
1597
  let size = 0
@@ -1635,6 +1691,9 @@ function registerRoutes(scope, ctxRef) {
1635
1691
  if (webServer === null || typeof webServer.register !== 'function') return false
1636
1692
  const handler = async (req, res) => {
1637
1693
  try {
1694
+ // 先过宿主准入,再碰任何设置或文件。读接口也过:/state 里有全量设置,
1695
+ // /logs 会读磁盘上的日志文件,都不该由任意网页拿到。
1696
+ if (admitRequest(scope, req, res)) return
1638
1697
  const url = new URL(req.url ?? '/', 'http://localhost')
1639
1698
  const suffix = url.pathname.slice(ROUTE_PREFIX.length)
1640
1699
  const method = req.method ?? 'GET'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-aupdate",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "description": "定时检查 DSH 插件更新,串行安装后就地生效,不重启;支持多种调度节奏、失败抑制、按包回滚,并在动手前留快照、忙时自动让开;设置页里可直接查看运行日志。",
6
6
  "license": "MIT",