dsh-solution-ledger 0.2.4 → 0.2.5
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 +22 -0
- package/lib/guard.js +59 -0
- package/lib/index.js +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -132,6 +132,28 @@ git clone https://github.com/Catwb/dsh-solution-ledger.git
|
|
|
132
132
|
|
|
133
133
|
Host 半为此注册了六条路由:`GET /solution-ledger/list`、`POST /solution-ledger/update`、`POST /solution-ledger/delete`、`GET /solution-ledger/update-check`、`POST /solution-ledger/backup`、`GET /solution-ledger/backups`。
|
|
134
134
|
|
|
135
|
+
### 路由访问控制
|
|
136
|
+
|
|
137
|
+
这六条路由监听在 loopback 上,用户浏览器里**任何一个网页**都能向 `127.0.0.1` 发请求。0.2.4 及之前的版本没有做访问校验,实测后果是:
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
无凭据 GET /list → 200,返回完整账本
|
|
141
|
+
Origin: evil.example + cross-site 的 GET → 200,照样返回完整账本
|
|
142
|
+
text/plain 的跨站 POST /delete → 请求被处理(handler 自己回了 400)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
浏览器会拦住跨域**读取**响应,但拦不住**写请求**——CSRF 简单请求不需要预检,任意网页都能清空账本。
|
|
146
|
+
|
|
147
|
+
从 0.2.5 起,六条路由共用一个护栏(`lib/guard.js`),进入任何 handler 之前先过 `ctx.connection.requestRejection()`:
|
|
148
|
+
|
|
149
|
+
- Host/Origin 围栏:挡 DNS rebinding 与跨站调用,`Sec-Fetch-Site: cross-site` 直接 403
|
|
150
|
+
- 登录 cookie:过了围栏但没有认证的调用回 401
|
|
151
|
+
- **拿不到 `connection` 服务时一律 403**(fail closed),检查自身抛错也按 403 处理
|
|
152
|
+
|
|
153
|
+
被拒绝的响应只有 `unauthorized` / `forbidden` 两种纯文本,不带任何账本内容。
|
|
154
|
+
|
|
155
|
+
## 更新检查
|
|
156
|
+
|
|
135
157
|
## 更新检查
|
|
136
158
|
|
|
137
159
|
面板打开时会查一次自己有没有新版本。查到就在顶部显示一条提示,点**去更新**跳到 **Plugins** 页里这个插件的详情页,用那里的 **Update** 完成安装。插件自己**不装**——`updateBundle` 会先卸载旧版再装新版,而承载这个请求的正是被卸载的那个插件,中途断线是必然的。跳转过去用 DSH 原生的确认框、进度和失败回滚,比自己实现一遍可靠。
|
package/lib/guard.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trust fence for this plugin's HTTP routes.
|
|
3
|
+
*
|
|
4
|
+
* The routes register on the composition's `webServer`, which listens on
|
|
5
|
+
* loopback. Every page open in the user's browser can reach that address, so
|
|
6
|
+
* without a fence any of them could read the ledger and fire the write routes
|
|
7
|
+
* with a CORS-simple POST. That was measured, not assumed: a `text/plain`
|
|
8
|
+
* POST carrying `Origin: https://evil.example` reached the delete handler.
|
|
9
|
+
*
|
|
10
|
+
* The check itself belongs to DSH. `connection.requestRejection` applies the
|
|
11
|
+
* Host/Origin fence — which defeats DNS rebinding and cross-site calls — and
|
|
12
|
+
* then requires the login-token cookie. This module only decides what to do
|
|
13
|
+
* with its answer, and keeps that decision testable outside a live server.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Build the fence for one composition.
|
|
18
|
+
* @param connection - `ctx.get('connection')`, or undefined when the carrier is mounted without it.
|
|
19
|
+
* @returns a function answering a refused request and reporting whether it did.
|
|
20
|
+
*/
|
|
21
|
+
export function createGuard(connection) {
|
|
22
|
+
/**
|
|
23
|
+
* @param req - the HTTP request.
|
|
24
|
+
* @param res - the response to own when the request is refused.
|
|
25
|
+
* @returns true when the request must not continue.
|
|
26
|
+
*/
|
|
27
|
+
return function rejected(req, res) {
|
|
28
|
+
if (connection === undefined) {
|
|
29
|
+
// A Web carrier without Connection offers no trust check at all. Serving
|
|
30
|
+
// nothing costs a broken panel; serving the ledger costs the user's data.
|
|
31
|
+
refuse(res, 403, 'forbidden')
|
|
32
|
+
return true
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let rejection
|
|
36
|
+
try {
|
|
37
|
+
// The service accepts the request object directly: it reads `headers`.
|
|
38
|
+
rejection = connection.requestRejection(req)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// A throwing check must not become an open door.
|
|
41
|
+
console.log('solution-ledger: trust check threw, refusing:', String(error))
|
|
42
|
+
rejection = 403
|
|
43
|
+
}
|
|
44
|
+
if (rejection === undefined) return false
|
|
45
|
+
refuse(res, rejection, rejection === 401 ? 'unauthorized' : 'forbidden')
|
|
46
|
+
return true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Write one refusal without leaking any ledger state.
|
|
52
|
+
* @param res - the response to write.
|
|
53
|
+
* @param status - 401 or 403.
|
|
54
|
+
* @param body - plain-text reason.
|
|
55
|
+
*/
|
|
56
|
+
function refuse(res, status, body) {
|
|
57
|
+
res.writeHead(status, { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' })
|
|
58
|
+
res.end(body)
|
|
59
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
resolveBackupsRoot,
|
|
29
29
|
resolvePackageDir,
|
|
30
30
|
} from './backup.js'
|
|
31
|
+
import { createGuard } from './guard.js'
|
|
31
32
|
|
|
32
33
|
export const name = 'solution-ledger'
|
|
33
34
|
|
|
@@ -570,6 +571,15 @@ export async function apply(ctx, config) {
|
|
|
570
571
|
// headless composition simply skips them and keeps the tools working.
|
|
571
572
|
const webServer = ctx.get('webServer')
|
|
572
573
|
if (webServer !== undefined) {
|
|
574
|
+
// These routes listen on loopback, which every page open in the user's
|
|
575
|
+
// browser can reach. Without a fence, any of them could read the ledger and
|
|
576
|
+
// fire the write routes with a CORS-simple POST — measured here, a
|
|
577
|
+
// cross-site `text/plain` request reached the delete handler.
|
|
578
|
+
// `requestRejection` is DSH's own check: its Host/Origin fence defeats DNS
|
|
579
|
+
// rebinding and cross-site calls, and its login-token cookie gates callers
|
|
580
|
+
// that pass the fence.
|
|
581
|
+
const rejected = createGuard(ctx.get('connection'))
|
|
582
|
+
|
|
573
583
|
/**
|
|
574
584
|
* Read a request body as JSON.
|
|
575
585
|
* @param req - the HTTP request.
|
|
@@ -636,6 +646,9 @@ export async function apply(ctx, config) {
|
|
|
636
646
|
path,
|
|
637
647
|
handler: async (req, res) => {
|
|
638
648
|
try {
|
|
649
|
+
// One gate for every route: the fence runs before any handler reads
|
|
650
|
+
// a body, so an untrusted call never reaches ledger data.
|
|
651
|
+
if (rejected(req, res)) return
|
|
639
652
|
await handler(req, res)
|
|
640
653
|
} catch (error) {
|
|
641
654
|
console.log(`solution-ledger: ${path} failed:`, String(error))
|