dsh-fishpal 0.9.28 → 0.9.30
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 +274 -0
- package/lib/config.js +40 -6
- package/lib/index.js +45 -13
- package/lib/pairing.js +32 -0
- package/lib/ui.js +15 -0
- package/package.json +1 -1
- package/tests/ui-endpoints.mjs +70 -0
- package/tests/ui-globals.mjs +234 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,215 @@
|
|
|
4
4
|
|
|
5
5
|
> 0.9.0 没有条目(那版的改动散在 git 历史里,不回填)。从 0.9.1 起逐版记录。
|
|
6
6
|
|
|
7
|
+
## [0.9.30] — 2026-12-02
|
|
8
|
+
|
|
9
|
+
**手填配对的短码从来没算过 —— 两处入口都在读一个不存在的字段。**
|
|
10
|
+
|
|
11
|
+
### 现象
|
|
12
|
+
|
|
13
|
+
两个人对着界面填完对方的三样材料,配对是**成功的**(消息能收到),
|
|
14
|
+
但对面显示一串 12 位短码,这边显示「短码还没算出来」,对面一比对就说
|
|
15
|
+
「令牌不一样」,然后中止。
|
|
16
|
+
|
|
17
|
+
### 根因
|
|
18
|
+
|
|
19
|
+
`onPair`(界面那条路)和 `fishpal_pair`(工具那条路)里,都写着同一句:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const sas = peer?.sas ?? null; // 界面
|
|
23
|
+
const sas = peer?.sas ?? '(尚未算出)'; // 工具
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**`peer` 是通信端对象,它从来没有 `sas` 字段。**
|
|
27
|
+
|
|
28
|
+
`peer` 只在两处被赋值:初始化是 `null`,重建时是
|
|
29
|
+
`byFp.get(cfg.peerFp) || [...byFp.values()][0] || null` —— 从连线表里拿的
|
|
30
|
+
通信端,字段只有 topic / fp / 连接状态,**没有 sas**。
|
|
31
|
+
|
|
32
|
+
更根本的一层:手填配对走的是 `applyPairing()`,它只做三件事 ——
|
|
33
|
+
校验格式、写老字段、写 peers 目录。**一行都没算过 SAS。**
|
|
34
|
+
|
|
35
|
+
而 `SAS 的算法需要两把一次性临时公钥`(`pairing.js` 的 `computeSas`),
|
|
36
|
+
手填这条路压根不生成临时密钥,**没有输入可算**。
|
|
37
|
+
|
|
38
|
+
**所以这个值永远是 null**,不是「还没算出来」、不是「对面没上线」,
|
|
39
|
+
是这条路从来就没有算过。
|
|
40
|
+
|
|
41
|
+
### 修复
|
|
42
|
+
|
|
43
|
+
**① 补一个「确认码」(`pairing.js` 新增 `computeConfirmCode`)**
|
|
44
|
+
|
|
45
|
+
用两把**长期交换公钥**算:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
HKDF(X25519(我的交换私钥, 对方交换公钥),
|
|
49
|
+
"fishpal-confirm-v1" ‖ 排序后的两把长期交换公钥, 6 字节) → 12 位十六进制
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
★ **它跟 SAS 长得一样,但不是同一个东西**,代码注释里写死了这个区别:
|
|
53
|
+
|
|
54
|
+
| | SAS | 确认码 |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| 用什么算 | 一次性临时密钥 | 长期交换公钥 |
|
|
57
|
+
| 防不防中间人 | **防**(换了码就对不上) | **不防**(只能确认是同一对人) |
|
|
58
|
+
| 从哪来 | `fishpal_request` → 核对 → `accept` | 手填配对 |
|
|
59
|
+
|
|
60
|
+
info 前缀用 `fishpal-confirm-v1` 而不是 `fishpal-sas-v1` ——
|
|
61
|
+
同一个 shared 在两种前缀下算出的码不同,避免「以为在比 SAS、其实在比确认码」。
|
|
62
|
+
|
|
63
|
+
**② 两处入口都改成真算**
|
|
64
|
+
|
|
65
|
+
`onPair` 和 `fishpal_pair` 都改成:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
let sas = null;
|
|
69
|
+
try {
|
|
70
|
+
const shared = deriveSharedKey(identity.exchange.privateKey, exch);
|
|
71
|
+
sas = pairing.computeConfirmCode(shared, identity.exchange.publicKey, exch);
|
|
72
|
+
} catch (err2) {
|
|
73
|
+
log(`确认码算不出来(…)—— 配对本身不受影响`);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
算不出来不让配对失败 —— 通信已经好了,确认码是附加信息。
|
|
78
|
+
|
|
79
|
+
**③ 把说谎的文案改掉**
|
|
80
|
+
|
|
81
|
+
`fishpal_pair` 原来输出「★ 把这个短码发给对面」,但短码从来没算出来过。
|
|
82
|
+
现在改成明确区分确认码和 SAS,并写清「对不上能中止的只有 request 那套」。
|
|
83
|
+
|
|
84
|
+
**④ 确认码要存下来 —— 还有三处在读空气**
|
|
85
|
+
|
|
86
|
+
第一轮只把「配对那一刻」算出来的值返回给调用者,**没存**。
|
|
87
|
+
于是另外三处照样读不到:
|
|
88
|
+
|
|
89
|
+
| 位置 | 原来 | 现在 |
|
|
90
|
+
|---|---|---|
|
|
91
|
+
| `fishpal_status` | `peer?.sas ?? '(未配对)'` | 读 `getPeer(cfg, cfg.peerFp)?.sas \|\| cfg.pairingSas` |
|
|
92
|
+
| 好友列表(已登记) | `peer?.sas ?? ''` | 读 `getPeer(cfg, fp)?.sas \|\| cfg.pairingSas` |
|
|
93
|
+
| 好友列表(刚配对未登记) | `peer?.sas ?? ''` | 同上 |
|
|
94
|
+
|
|
95
|
+
修法是让确认码在算出来的那一刻就落盘:
|
|
96
|
+
|
|
97
|
+
- `applyPairing(cfg, {..., sas})` —— 新增 `sas` 参数,写进 `cfg.pairingSas`(当前那个)和 `cfg.peers[fp].sas`(目录)
|
|
98
|
+
- `upsertPeer(cfg, {..., sas})` —— 重配对时**新值覆盖、没新值就留旧的**,不会把已有确认码抹成空
|
|
99
|
+
- `getPeer()` —— 返回值加上 `sas`,这是它成为唯一真实源的最后一环
|
|
100
|
+
|
|
101
|
+
★ 三处的顺序改成「先算 sas → 再 applyPairing」—— 算完才写,写进去的才有值。
|
|
102
|
+
|
|
103
|
+
### 验证
|
|
104
|
+
|
|
105
|
+
| 检查 | 结果 |
|
|
106
|
+
|---|---|
|
|
107
|
+
| `node --check lib/pairing.js` / `lib/index.js` | OK |
|
|
108
|
+
| 两边 shared 一致 | ✅ |
|
|
109
|
+
| A 侧 / B 侧确认码 | 都是 `8c952ab55400` |
|
|
110
|
+
| 长度 12 | ✅ |
|
|
111
|
+
| 公钥顺序无关 | ✅ |
|
|
112
|
+
| 跟 SAS 不撞 | ✅(同 shared 下 SAS 是另一串) |
|
|
113
|
+
| **全量测试** | **798 通过 / 0 失败** |
|
|
114
|
+
|
|
115
|
+
### 顺手补的文档
|
|
116
|
+
|
|
117
|
+
**CHANGELOG 缺两个版本,补上了:**
|
|
118
|
+
|
|
119
|
+
- **0.9.18** —— 退出选中修好(服务端空串回落 + 前端 localStorage 弹回两处根因)、
|
|
120
|
+
空正文不再画空气泡、投递包裹带备注名、补全 README 缺失的 0.9.6~0.9.12 节点、669 项通过。
|
|
121
|
+
- **0.9.0** —— 第一版全文:MQTT 传输、X25519+AES-256-GCM+Ed25519、漂流瓶四步、
|
|
122
|
+
陌生人隔离、出站拦截、心跳/日报/预算/撤回/备份,364 项通过。
|
|
123
|
+
|
|
124
|
+
现在 CHANGELOG 覆盖 npm 上**每一个版本**(0.9.0 ~ 0.9.29 全有)。
|
|
125
|
+
|
|
126
|
+
## [0.9.29] — 2026-12-02
|
|
127
|
+
|
|
128
|
+
**配对面板上那行红字「连不上本机服务:esc is not defined」,修了。顺手把这类型 bug 变成能机器扫的东西。**
|
|
129
|
+
|
|
130
|
+
> 用户报的原话:截图里配对按钮下面一行红字,`连不上本机服务:esc is not defined`。
|
|
131
|
+
|
|
132
|
+
### 现象:红字说的是「连不上」,其实压根没连
|
|
133
|
+
|
|
134
|
+
点「配对」以后底下出这么一行红字。第一反应是本地服务没起、或者端口不通 —— 都不是。
|
|
135
|
+
|
|
136
|
+
真相是:**那一段脚本执行到一半崩了**,`fetch` 的 `.catch` 把异常接住,把 `e.message` 打上去,于是「ReferenceError」被显示成了「连不上本机服务」。
|
|
137
|
+
|
|
138
|
+
### 根因:同一个名字,两份,不共享作用域
|
|
139
|
+
|
|
140
|
+
`lib/ui.js` 里 `esc` 这个转义函数有两个:
|
|
141
|
+
|
|
142
|
+
| 位置 | 属于谁 | 谁用 |
|
|
143
|
+
|---|---|---|
|
|
144
|
+
| 94 行 `const esc = (s) => …` | **Node 模块顶层** | 服务端拼 HTML 字符串时用 |
|
|
145
|
+
| 4673 / 4676 行 | 落在 `<script>` 块里 | **浏览器** —— 配对回执那块 |
|
|
146
|
+
|
|
147
|
+
`pairSubmit()` 的 `.then` 里拼回执时调了 `esc(String(j.sas))`。这段代码会原样输出到浏览器执行,**而浏览器里没有 `esc`** —— Node 那边那个顶层函数不会跟 HTML 一起送到前端。
|
|
148
|
+
|
|
149
|
+
于是执行到那一行抛 `ReferenceError: esc is not defined`,整个 `.then` 后半段(写入 `out.innerHTML`)一个字节都没跑。
|
|
150
|
+
|
|
151
|
+
**为什么 `node --check` 查不出来**:语法完全正确。这个错只在真浏览器里、只在真的点到那一步时发生。
|
|
152
|
+
|
|
153
|
+
### 改法
|
|
154
|
+
|
|
155
|
+
在 `<script>` 块开头(现在 2920 行)补一份浏览器端的 `esc`,函数体跟 Node 那份一致:
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
function esc(s) {
|
|
159
|
+
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
|
160
|
+
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
名字一样、作用域两套 —— 这不是将就,是这两处本来就该各有一份。注释里写清了为什么,以及当初是怎么撞上的。
|
|
166
|
+
|
|
167
|
+
### 新增:两个能机器跑的审计器
|
|
168
|
+
|
|
169
|
+
这一类 bug(**在浏览器里引用了浏览器没有的名字**)的共同形状是:点了没反应、或者显示一个不相关的错误、整块脚本静默死掉。它不报语法错,只在你真点的时候死。靠肉眼翻 5000 行不现实,所以做成两个扫描器。
|
|
170
|
+
|
|
171
|
+
**`tests/ui-globals.mjs`** —— 拿**真实渲染出来的 HTML**(`renderUi({})`)来扫,不是猜模板结构:
|
|
172
|
+
|
|
173
|
+
- `A 类`:`<script>` 块里被**调用**、但块内没定义、也不是浏览器内置的名字 —— `esc` 就是这一类
|
|
174
|
+
- `B 类`:HTML 内联处理器 `on*=“fn(...)”` 引用的函数,`<script>` 里没有 —— 函数改名/删掉之后按钮点了永远没反应,同样是静默坏
|
|
175
|
+
- 另外每个 `<script>` 块单独过一遍语法(语法错整块都不执行)
|
|
176
|
+
- 最后单点 `esc` 这一对:Node 那份和浏览器那份都得在,少一份就是这个 bug 复发
|
|
177
|
+
|
|
178
|
+
实测:script 块 1 个 · 定义名 218 个 · 被调用名 93 个 · 内联处理器 21 处 · **7 条全过**。
|
|
179
|
+
|
|
180
|
+
**`tests/ui-endpoints.mjs`** —— 同一类 bug 的另一面:前端 `fetch('/api/xxx')` 打到后端没定义的路由,返回 404,页面上照样显示失败。
|
|
181
|
+
|
|
182
|
+
扫 `ui.js` 里出现的每个 `/api/名字`,对 `localserver.js` 里每条 `req.url === '/api/名字'`:
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
前端用到 16 个:avatar, block, bottle, bottlehistory, budget, config,
|
|
186
|
+
daily, favorite, friend, global, pair, ping, privacy,
|
|
187
|
+
recall, review, wordfilter
|
|
188
|
+
后端定义 16 个:同上,完全一致
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**3 条全过**,零缺口。以后哪边改了名,这个测试会红。
|
|
192
|
+
|
|
193
|
+
### 修了两个验证器自己的 bug
|
|
194
|
+
|
|
195
|
+
审计器也会骗人,这一轮撞到两个:
|
|
196
|
+
|
|
197
|
+
1. **`_audit-css.mjs` 的 data 属性对账**:原来拿一个**手写的 7 项清单**(`data-id/at/mi/text/from/to/cmd`)去比 `getAttribute` 读到的名字。实际代码有 29 个 `data-*`,所以凡是清单外的全报 `✗` —— 屏幕上二十多个红叉,其实一个 bug 都没有。**验证器自己在骗人。** 改成真的去扫代码里印出了哪些 `data-*`(含 `setAttribute` 运行时设的),并对账时只比 `data-*` 开头的读名(`getAttribute('href')` 跟这件事无关,原来也被拉进来比,是类别错误)。
|
|
198
|
+
|
|
199
|
+
2. 修完之后剩下两个 ✗,查了代码:`data-open` 是 `bqToggle` 自己 `setAttribute('data-open','1')` 设的运行时状态,扫 `setAttribute` 之后就 ✓ 了;`href` 是上面说的类别错误。**两个都是假警报,不是 bug。**
|
|
200
|
+
|
|
201
|
+
### 验证
|
|
202
|
+
|
|
203
|
+
| 项目 | 结果 |
|
|
204
|
+
|---|---|
|
|
205
|
+
| `node --check lib/ui.js` | OK |
|
|
206
|
+
| `tests/ui-globals.mjs` | 7 条全过 |
|
|
207
|
+
| `tests/ui-endpoints.mjs` | 3 条全过 |
|
|
208
|
+
| `_audit-css.mjs` | CSS 全定义、data 属性全部对上 |
|
|
209
|
+
| `_audit-tools.mjs` | 21 个工具入口,缺入口 0 |
|
|
210
|
+
| `tests/run.mjs` 全量 | **798 通过,0 失败** |
|
|
211
|
+
|
|
212
|
+
### 用户要做的
|
|
213
|
+
|
|
214
|
+
**重启一次 DSH。** `ui.js` 是 Node 模块,启动时才加载,改完不重启不生效。重启后点「配对」,红字应该没了。
|
|
215
|
+
|
|
7
216
|
## [0.9.28] — 2026-12-01
|
|
8
217
|
|
|
9
218
|
**自己发出去的漂流瓶,被循环广播绕回来当成了别人的瓶子收下。这一版把它按住。**
|
|
@@ -611,6 +820,35 @@ DSH 一重启,这些定时器全部作废 —— 而它一天要重启好几
|
|
|
611
820
|
- `_verify-global-panel.mjs` 的断言跟着新地址改(`href="?global=1"`)。
|
|
612
821
|
- 全套 **669 项通过 / 0 失败**。
|
|
613
822
|
|
|
823
|
+
## [0.9.18] — 2026-12-01
|
|
824
|
+
|
|
825
|
+
**「退出选中」点了没反应 —— 两处各吃掉一半。**
|
|
826
|
+
|
|
827
|
+
现象:在某个好友的对话页里点 ×,界面闪一下又弹回同一个人。用户以为点击没生效,
|
|
828
|
+
其实是**两处独立的逻辑各弹回一次**,看起来像“点了白点”。
|
|
829
|
+
|
|
830
|
+
**根因一(服务端)**:`wantSelected || cfg.peerFp` 这个表达式把**空字符串当 falsy**。
|
|
831
|
+
前端传 `wantSelected=''` 表示「我要回全局页」,服务端一判 —— 空串是假值,于是回落到
|
|
832
|
+
`cfg.peerFp`(当前那个好友),把人又送回去了。**「空」在这里是有效值,不是缺失值。**
|
|
833
|
+
|
|
834
|
+
**根因二(前端)**:点 × 之后地址栏确实变了,但页面脚本读 `localStorage` 里存的
|
|
835
|
+
「上次看的是谁」又把人弹回原来那个。**地址和本地存储两个真相源打架,本地存储赢了。**
|
|
836
|
+
|
|
837
|
+
修法:地址带 `global=1` 才真回全局页(显式标志代替「空即全局」的隐式约定);
|
|
838
|
+
前端不再用 localStorage 反推当前选中。
|
|
839
|
+
|
|
840
|
+
**另外三件:**
|
|
841
|
+
|
|
842
|
+
- **空正文不再画空气泡**:串门事件只有元数据、没有正文时,界面原来会画一个空壳,
|
|
843
|
+
看起来像“发了一条空白消息”。现在没有正文就不画。
|
|
844
|
+
- **投递包裹带备注名**:包裹里补上说话人的备注名,对面收到时能显示「谁说的」,
|
|
845
|
+
不用只靠 fp 去猜。
|
|
846
|
+
- **补全 README / CHANGELOG 缺失的版本节点**:0.9.6~0.9.12、0.9.15 这几段原来没写,
|
|
847
|
+
这次补齐(这条本身就是「文档也会漏」的实例)。
|
|
848
|
+
|
|
849
|
+
**验证**:新增 `_verify-peer-name.mjs`(11 项:名字渲染 / fp 回退 / 引号转义 /
|
|
850
|
+
抽屉入口存在 / 无输入框 / 空选中分支)。全套 **669 项通过 / 0 失败**。
|
|
851
|
+
|
|
614
852
|
## [0.9.17] — 2026-12-01
|
|
615
853
|
|
|
616
854
|
**屏蔽词可以在设置页里填了 —— 鱼读不到,但照样拦。**
|
|
@@ -1134,6 +1372,42 @@ README 里几个数字之前是旧的(工具数、自测项数、版本号)
|
|
|
1134
1372
|
|
|
1135
1373
|
- 新增「## 更新」一节:怎么知道有新版本、怎么升、升级会不会丢密钥、怎么退回旧版。
|
|
1136
1374
|
|
|
1375
|
+
## [0.9.0] — 2026-11-29
|
|
1376
|
+
|
|
1377
|
+
**第一版。两条 DSH 鱼,各自在自己主人的电脑上,能互相说话。**
|
|
1378
|
+
|
|
1379
|
+
不是让你替朋友传话 —— 你们自己用微信聊。是让**你的鱼有自己的朋友**。
|
|
1380
|
+
|
|
1381
|
+
### 传输
|
|
1382
|
+
|
|
1383
|
+
- **公共 MQTT broker**(`wss://broker.emqx.io:8084/mqtt`),不注册任何账号、不需要服务器。
|
|
1384
|
+
- 代价写在明面上:**没有我们的服务器,也就没有我们的备份**;没有服务端替你保管东西。
|
|
1385
|
+
- 内置目录群是**第三方依赖**(QQ 群),群在那头消失,插件管不了。
|
|
1386
|
+
|
|
1387
|
+
### 加密
|
|
1388
|
+
|
|
1389
|
+
- **X25519 + AES-256-GCM + Ed25519**:换密钥、加密、签名三件事分开。
|
|
1390
|
+
- **SAS 12 位带外核对**:配对时两边各算一个短码,人工对一遍,
|
|
1391
|
+
对不上就说明中间有人换了公钥 —— 这一步不能省。
|
|
1392
|
+
- 一次性临时密钥,配完销毁。
|
|
1393
|
+
|
|
1394
|
+
### 功能
|
|
1395
|
+
|
|
1396
|
+
- **漂流瓶**:起草 / 审阅 / 放行 / 投递四步,起草不等于发送。
|
|
1397
|
+
- **陌生人隔离待审**:保护模式开着时,陌生人的来信不进会话,先进隔离区等人决定。
|
|
1398
|
+
- **出站隐私拦截**:发出去之前先过一道自己定的清单。
|
|
1399
|
+
- **DSH 侧边栏面板**、好友抽屉、**界面内配对**。
|
|
1400
|
+
- **主动心跳**(默认关)、日报、预算硬停、撤回、密钥加密备份。
|
|
1401
|
+
|
|
1402
|
+
### 验证
|
|
1403
|
+
|
|
1404
|
+
- **十套件 364 项全过。**
|
|
1405
|
+
|
|
1406
|
+
### 已知边界(当时就写下来了)
|
|
1407
|
+
|
|
1408
|
+
- 公共 broker 上任何订阅了对应 topic 的人都能看到密文,看不到明文 —— 但也看不到「谁在跟谁聊」之外的元数据保护。
|
|
1409
|
+
- 没有离线队列:对面不在线时消息会丢,靠重连后补发。
|
|
1410
|
+
|
|
1137
1411
|
## [0.8.5] — 2026-11-28
|
|
1138
1412
|
|
|
1139
1413
|
漂流瓶的**单条临时回复**(`bottle_reply`):一只瓶子只能说一句,说完通道永久关闭。
|
package/lib/config.js
CHANGED
|
@@ -205,13 +205,33 @@ export function createConfig({ me, meFp }) {
|
|
|
205
205
|
* ★ 2026-11-28 多人:除了老字段(当前那个),**同时写进 peers 目录** ——
|
|
206
206
|
* 不然配完的新好友在 listPeers 里看不见,传输层就不会连他。
|
|
207
207
|
* 两个地方写同一份材料,老代码和新代码各自读各自那份。
|
|
208
|
+
*
|
|
209
|
+
* ★ 2026-12-02:加 `sas` 参数。它不是 SAS,是手填配对算出来的**确认码**
|
|
210
|
+
* (见 pairing.js 的 computeConfirmCode)。存下来的理由:算它的成本是一次
|
|
211
|
+
* X25519,但显示它的地方有三处(status / 好友列表 / 面板),每次重算没有必要;
|
|
212
|
+
* 更重要的是 —— 以前这三处都写 `peer?.sas`,而 peer 没这个字段,于是
|
|
213
|
+
* 配对成功的人永远看到「(未配对)」。存下来才是真修好。
|
|
208
214
|
*/
|
|
209
|
-
export function applyPairing(cfg, { peer, peerFp, peerSigningPub, peerExchangePub }) {
|
|
215
|
+
export function applyPairing(cfg, { peer, peerFp, peerSigningPub, peerExchangePub, sas }) {
|
|
210
216
|
if (!peerFp?.startsWith('fp_')) throw new Error('peerFp 必须是 fp_ 开头的身份 ID');
|
|
211
217
|
if (!peerSigningPub) throw new Error('缺对面 Ed25519 签名公钥');
|
|
212
218
|
if (!peerExchangePub) throw new Error('缺对面 X25519 交换公钥');
|
|
213
|
-
const next = {
|
|
214
|
-
|
|
219
|
+
const next = {
|
|
220
|
+
...cfg,
|
|
221
|
+
peer: peer ?? cfg.peer,
|
|
222
|
+
peerFp,
|
|
223
|
+
peerSigningPub,
|
|
224
|
+
peerExchangePub,
|
|
225
|
+
// 当前配对那个人的确认码(老字段,只给「当前」用;多人看 peers[fp].sas)
|
|
226
|
+
...(sas ? { pairingSas: sas } : {}),
|
|
227
|
+
};
|
|
228
|
+
return upsertPeer(next, {
|
|
229
|
+
peerFp,
|
|
230
|
+
name: peer ?? '',
|
|
231
|
+
signingPub: peerSigningPub,
|
|
232
|
+
exchangePub: peerExchangePub,
|
|
233
|
+
sas,
|
|
234
|
+
});
|
|
215
235
|
}
|
|
216
236
|
|
|
217
237
|
/**
|
|
@@ -219,12 +239,18 @@ export function applyPairing(cfg, { peer, peerFp, peerSigningPub, peerExchangePu
|
|
|
219
239
|
* ★ 和老字段的关系:一组是「目录」,一组是「当前」。写这里不动老字段,
|
|
220
240
|
* 免得老代码读不到而静默当成没配对。
|
|
221
241
|
*/
|
|
222
|
-
export function upsertPeer(cfg, { peerFp, name, signingPub, exchangePub }) {
|
|
242
|
+
export function upsertPeer(cfg, { peerFp, name, signingPub, exchangePub, sas }) {
|
|
223
243
|
if (!peerFp?.startsWith('fp_')) throw new Error('peerFp 必须是 fp_ 开头的身份 ID');
|
|
224
244
|
if (!signingPub) throw new Error('缺对面 Ed25519 签名公钥');
|
|
225
245
|
if (!exchangePub) throw new Error('缺对面 X25519 交换公钥');
|
|
226
246
|
const peers = { ...(cfg.peers || {}) };
|
|
227
|
-
peers[peerFp] = {
|
|
247
|
+
peers[peerFp] = {
|
|
248
|
+
name: name ?? peers[peerFp]?.name ?? '',
|
|
249
|
+
signingPub,
|
|
250
|
+
exchangePub,
|
|
251
|
+
// 确认码:有新的就用新的,没有就留着旧的(重配对时不会把已有值抹成空)
|
|
252
|
+
...(sas ? { sas } : peers[peerFp]?.sas ? { sas: peers[peerFp].sas } : {}),
|
|
253
|
+
};
|
|
228
254
|
return { ...cfg, peers };
|
|
229
255
|
}
|
|
230
256
|
|
|
@@ -239,7 +265,15 @@ export function removePeer(cfg, peerFp) {
|
|
|
239
265
|
export function getPeer(cfg, peerFp) {
|
|
240
266
|
const p = cfg?.peers?.[peerFp];
|
|
241
267
|
if (!p || !p.signingPub || !p.exchangePub) return null;
|
|
242
|
-
return {
|
|
268
|
+
return {
|
|
269
|
+
fp: peerFp,
|
|
270
|
+
name: p.name || '',
|
|
271
|
+
signingPub: p.signingPub,
|
|
272
|
+
exchangePub: p.exchangePub,
|
|
273
|
+
// ★ 2026-12-02:把确认码一起返回。以前调用方拿到 peer 对象后去读 `peer.sas`,
|
|
274
|
+
// 那是通信端对象、永远没这个字段;存在 peers 里才是唯一的真实源。
|
|
275
|
+
sas: p.sas || '',
|
|
276
|
+
};
|
|
243
277
|
}
|
|
244
278
|
|
|
245
279
|
/** 列出所有能发消息的人(材料齐的)。 */
|
package/lib/index.js
CHANGED
|
@@ -999,11 +999,22 @@ function applyInner(ctx, config = {}) {
|
|
|
999
999
|
return { ok: false, error: '这是你自己的身份码,不能跟自己配对' };
|
|
1000
1000
|
}
|
|
1001
1001
|
try {
|
|
1002
|
+
// ★ 2026-12-02:先算确认码,再写配置 —— 这样 applyPairing 能把它一起存进
|
|
1003
|
+
// peers[fp].sas 和 cfg.pairingSas,后面 status / 好友列表才有真值可读。
|
|
1004
|
+
let sas = null;
|
|
1005
|
+
try {
|
|
1006
|
+
const shared = deriveSharedKey(identity.exchange.privateKey, exch);
|
|
1007
|
+
sas = pairing.computeConfirmCode(shared, identity.exchange.publicKey, exch);
|
|
1008
|
+
} catch (err2) {
|
|
1009
|
+
log(`确认码算不出来(${err2?.message ?? err2})—— 配对本身不受影响`);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1002
1012
|
const next = applyPairing(cfg, {
|
|
1003
1013
|
peer: peerName ? String(peerName).trim() : undefined,
|
|
1004
1014
|
peerFp: fp,
|
|
1005
1015
|
peerSigningPub: sign,
|
|
1006
1016
|
peerExchangePub: exch,
|
|
1017
|
+
sas,
|
|
1007
1018
|
});
|
|
1008
1019
|
saveConfig(next);
|
|
1009
1020
|
cfg = next;
|
|
@@ -1013,14 +1024,15 @@ function applyInner(ctx, config = {}) {
|
|
|
1013
1024
|
log(`配对信息已写入(界面),重连中`);
|
|
1014
1025
|
transport?.stop();
|
|
1015
1026
|
startTransport();
|
|
1016
|
-
const sas = peer?.sas ?? null;
|
|
1017
1027
|
return {
|
|
1018
1028
|
ok: true,
|
|
1019
1029
|
sas,
|
|
1020
1030
|
peerFp: cfg.peerFp,
|
|
1021
1031
|
me: cfg.me,
|
|
1022
|
-
// ★
|
|
1023
|
-
warning: sas
|
|
1032
|
+
// ★ 算不出来才说没算出;算出来了就别再加警告吓人。
|
|
1033
|
+
warning: sas
|
|
1034
|
+
? null
|
|
1035
|
+
: '确认码没算出来(本地身份密钥读不到?)—— 消息收发不受影响。',
|
|
1024
1036
|
};
|
|
1025
1037
|
} catch (err) {
|
|
1026
1038
|
return { ok: false, error: `配对失败:${err?.message ?? err}` };
|
|
@@ -2974,11 +2986,25 @@ function applyInner(ctx, config = {}) {
|
|
|
2974
2986
|
};
|
|
2975
2987
|
}
|
|
2976
2988
|
|
|
2989
|
+
// ★ 2026-12-02:先算确认码,再写配置 —— 这样 applyPairing 能把它一起存进
|
|
2990
|
+
// peers[fp].sas 和 cfg.pairingSas,status / 好友列表才有真值可读。
|
|
2991
|
+
// 以前这里是 `const sas = peer?.sas ?? '(尚未算出)'`,`peer` 是通信端对象、
|
|
2992
|
+
// 永远没这个字段,而下面那段文案却让用户「把短码发给对面」—— 等于让人去
|
|
2993
|
+
// 核对一个根本不存在的码。
|
|
2994
|
+
let sas = null;
|
|
2995
|
+
try {
|
|
2996
|
+
const shared = deriveSharedKey(identity.exchange.privateKey, String(peerExchangeIn));
|
|
2997
|
+
sas = pairing.computeConfirmCode(shared, identity.exchange.publicKey, String(peerExchangeIn));
|
|
2998
|
+
} catch (err2) {
|
|
2999
|
+
log(`确认码算不出来(${err2?.message ?? err2})—— 配对本身不受影响`);
|
|
3000
|
+
}
|
|
3001
|
+
|
|
2977
3002
|
const next = applyPairing(cfg, {
|
|
2978
3003
|
peer: peerNameIn,
|
|
2979
3004
|
peerFp: String(peerFpIn),
|
|
2980
3005
|
peerSigningPub: String(peerSigningIn),
|
|
2981
3006
|
peerExchangePub: String(peerExchangeIn),
|
|
3007
|
+
sas,
|
|
2982
3008
|
});
|
|
2983
3009
|
saveConfig(next);
|
|
2984
3010
|
cfg = next;
|
|
@@ -2993,20 +3019,21 @@ function applyInner(ctx, config = {}) {
|
|
|
2993
3019
|
log('配对信息已写入,重连中');
|
|
2994
3020
|
transport?.stop();
|
|
2995
3021
|
startTransport();
|
|
2996
|
-
const sas = peer?.sas ?? '(尚未算出)';
|
|
2997
3022
|
return {
|
|
2998
3023
|
ok: true,
|
|
2999
3024
|
text: [
|
|
3000
3025
|
`配对完成:${cfg.me} ↔ ${cfg.peerFp}`,
|
|
3001
3026
|
'',
|
|
3002
|
-
|
|
3027
|
+
sas ? `确认码:${sas}` : '确认码:没算出来(本地身份密钥读不到?)',
|
|
3003
3028
|
'',
|
|
3004
|
-
|
|
3005
|
-
|
|
3029
|
+
sas
|
|
3030
|
+
? '★ 把这个确认码发给对面,让他也跑一次配对比一下 —— 一样就说明你们说的是同一对人。'
|
|
3031
|
+
: '★ 这一步跳过就行,消息收发不受影响。',
|
|
3006
3032
|
'',
|
|
3007
|
-
'⚠
|
|
3008
|
-
'
|
|
3009
|
-
'
|
|
3033
|
+
'⚠ 这是「确认码」,不是 SAS。区别:',
|
|
3034
|
+
' · SAS 用一次性临时密钥算,能防「配对这一刻有人换了公钥」;',
|
|
3035
|
+
' · 确认码用长期公钥算,**防不住中途换公钥**,只能确认是同一对人。',
|
|
3036
|
+
' · 要那道真保护,走 fishpal_request → 人工核对短码 → fishpal_accept(那条路对不上能中止)。',
|
|
3010
3037
|
].join('\n'),
|
|
3011
3038
|
};
|
|
3012
3039
|
} catch (err) {
|
|
@@ -3647,7 +3674,9 @@ function applyInner(ctx, config = {}) {
|
|
|
3647
3674
|
`broker:${cfg.broker}`,
|
|
3648
3675
|
`连接:${transport?.connected ? '已连上' : '没连上'}`,
|
|
3649
3676
|
`档位:${DEFAULT_LEVEL}`,
|
|
3650
|
-
`
|
|
3677
|
+
// ★ 2026-12-02:以前是 `peer?.sas ?? '(未配对)'` —— peer 没这个字段,
|
|
3678
|
+
// 配对成功的人也永远看到「(未配对)」。改成从 peers 目录读存下来的确认码。
|
|
3679
|
+
`确认码:${getPeer(cfg, cfg.peerFp)?.sas || cfg.pairingSas || '(未配对)'}${(getPeer(cfg, cfg.peerFp)?.sas || cfg.pairingSas) ? '(手填配对的确认码,不是 SAS——防不住中途换公钥)' : ''}`,
|
|
3651
3680
|
`密钥周期:${ps.epoch ?? '—'}(每天轮换,前一天也留着)`,
|
|
3652
3681
|
`outbox:${ps.outbox ?? 0} 条待确认 · 已确认 ${ps.acked ?? 0} 条`,
|
|
3653
3682
|
`限流丢弃:${ps.rateDropped ?? 0} 条`,
|
|
@@ -4916,7 +4945,9 @@ function applyInner(ctx, config = {}) {
|
|
|
4916
4945
|
muted: e.muted === true,
|
|
4917
4946
|
hidden: e.hidden === true,
|
|
4918
4947
|
blocked: e.blocked === true,
|
|
4919
|
-
|
|
4948
|
+
// ★ 2026-12-02:以前是 `peer?.sas` —— 通信端对象没这个字段,永远空。
|
|
4949
|
+
// 改成从 peers 目录读存下来的确认码(手填配对时算好存进去的)。
|
|
4950
|
+
sas: isCurrent ? (getPeer(cfg, fp)?.sas || cfg.pairingSas || '') : '',
|
|
4920
4951
|
// ★ 按好友分开读(旧消息归主好友,见上面 msgsOf 的注释)
|
|
4921
4952
|
lastAt: msgsOf(fp).at(-1)?.at ?? '',
|
|
4922
4953
|
messages: msgsOf(fp),
|
|
@@ -4944,7 +4975,8 @@ function applyInner(ctx, config = {}) {
|
|
|
4944
4975
|
muted: false,
|
|
4945
4976
|
hidden: false,
|
|
4946
4977
|
blocked: false,
|
|
4947
|
-
|
|
4978
|
+
// ★ 2026-12-02:同上,从 peers 目录读,别读通信端对象。
|
|
4979
|
+
sas: getPeer(cfg, cfg.peerFp)?.sas || cfg.pairingSas || '',
|
|
4948
4980
|
lastAt: msgsOf(cfg.peerFp).at(-1)?.at ?? '',
|
|
4949
4981
|
messages: msgsOf(cfg.peerFp),
|
|
4950
4982
|
level: null, // 没登记过 → 跟随全局
|
package/lib/pairing.js
CHANGED
|
@@ -74,6 +74,38 @@ export function computeSas(sharedSecret, tmpPubA, tmpPubB) {
|
|
|
74
74
|
return out.toString('hex'); // 6 字节 = 12 位十六进制
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* 手填配对用的**确认码**(2026-12-02 补)。
|
|
79
|
+
*
|
|
80
|
+
* ★ 它跟 SAS 长得一样(12 位十六进制),但**不是同一个东西**,别混:
|
|
81
|
+
* - SAS 用两把**一次性临时公钥**算,配完销毁 —— 中间人换不了,换了码就对不上。
|
|
82
|
+
* - 确认码用两把**长期交换公钥**算 —— 它只能证明「我们两个说的是一对人」,
|
|
83
|
+
* **证明不了「配对这一刻没人在中间换过公钥」**。
|
|
84
|
+
*
|
|
85
|
+
* 为什么补这个:手填配对(`applyPairing`)压根不生成临时密钥,
|
|
86
|
+
* 所以 `computeSas` 在这条路上没有输入、永远算不出东西。界面上要么显示
|
|
87
|
+
* 「短码还没算出来」(让人以为在等对面)、要么显示对面的码(两边对不上)。
|
|
88
|
+
* 补一个确认码,至少让两边能对一下是不是同一对人。
|
|
89
|
+
*
|
|
90
|
+
* ★ info 前缀用 `fishpal-confirm-v1`,跟 SAS 的 `fishpal-sas-v1` 分开 ——
|
|
91
|
+
* 同一个 shared 在两种前缀下不会算出同一个码,避免「以为在比 SAS 其实在比确认码」。
|
|
92
|
+
*
|
|
93
|
+
* @param {Buffer} sharedSecret X25519(我的交换私钥, 对方交换公钥)
|
|
94
|
+
* @param {string} myExchangePub 我的长期交换公钥(base64)
|
|
95
|
+
* @param {string} peerExchangePub 对方的长期交换公钥(base64)
|
|
96
|
+
*/
|
|
97
|
+
export function computeConfirmCode(sharedSecret, myExchangePub, peerExchangePub) {
|
|
98
|
+
if (!sharedSecret || !myExchangePub || !peerExchangePub) return null;
|
|
99
|
+
const sorted = [String(myExchangePub), String(peerExchangePub)].sort();
|
|
100
|
+
const info = Buffer.concat([
|
|
101
|
+
Buffer.from('fishpal-confirm-v1', 'utf8'),
|
|
102
|
+
Buffer.from(sorted[0], 'utf8'),
|
|
103
|
+
Buffer.from(sorted[1], 'utf8'),
|
|
104
|
+
]);
|
|
105
|
+
const out = Buffer.from(hkdfSync('sha256', sharedSecret, Buffer.alloc(0), info, 6));
|
|
106
|
+
return out.toString('hex');
|
|
107
|
+
}
|
|
108
|
+
|
|
77
109
|
// ── 报文 ──────────────────────────────────────────────────────────────────
|
|
78
110
|
|
|
79
111
|
/**
|
package/lib/ui.js
CHANGED
|
@@ -2908,6 +2908,21 @@ var FPSET = ${JSON.stringify(settings || null)};
|
|
|
2908
2908
|
/* 这一页渲染时的数据指纹。前端拿它比对,变了才刷新(不整页重载就不会闪)。 */
|
|
2909
2909
|
var FP_REV = ${JSON.stringify(String(rev || ''))};
|
|
2910
2910
|
|
|
2911
|
+
/**
|
|
2912
|
+
* 浏览器端的转义。
|
|
2913
|
+
* ★ 2026-12-02 修的(用户截图里的红字「连不上本机服务:esc is not defined」):
|
|
2914
|
+
* 配对回执那块(pairSubmit 的 .then 里)调了 esc(),可 esc 定义在 Node 层
|
|
2915
|
+
* (服务端渲染用),浏览器 <script> 里根本没有这个名字 ——
|
|
2916
|
+
* 执行到那儿直接抛 ReferenceError,fetch 的 .catch 把它接住,
|
|
2917
|
+
* 显示成「连不上本机服务」。不是连不上,是脚本炸了。
|
|
2918
|
+
* 这个模块里 esc 有两份(Node 的 + 这里的),名字一样但不共享作用域。
|
|
2919
|
+
*/
|
|
2920
|
+
function esc(s) {
|
|
2921
|
+
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
|
2922
|
+
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2911
2926
|
function fpsetTarget() {
|
|
2912
2927
|
return FPSET && FPSET.port ? 'http://127.0.0.1:' + FPSET.port + '/api/friend' : null;
|
|
2913
2928
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 前端 fetch 的路由 vs 后端定义的路由(2026-12-02 建)
|
|
3
|
+
*
|
|
4
|
+
* 起因跟 ui-globals.mjs 同一类:**两边约定对不上,表现成「连不上本机服务」**。
|
|
5
|
+
* 前端 fetch('/api/xxx') 打到后端没定义的路由,返回 404,
|
|
6
|
+
* 页面上显示的就是失败 —— 但真实原因不是网络,是名字对不上。
|
|
7
|
+
*
|
|
8
|
+
* 判据:ui.js 里出现过的 `/api/名字`,localserver.js 里必须有一条
|
|
9
|
+
* `req.url === '/api/名字'`;反过来后端有、前端没用是正常的(给别的入口用)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
|
|
14
|
+
let pass = 0;
|
|
15
|
+
const fails = [];
|
|
16
|
+
|
|
17
|
+
function ok(cond, label, detail) {
|
|
18
|
+
if (cond) pass += 1;
|
|
19
|
+
else fails.push(detail ? `${label} —— ${detail}` : label);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const uiSrc = fs.readFileSync(new URL('../lib/ui.js', import.meta.url), 'utf8');
|
|
23
|
+
const srvSrc = fs.readFileSync(new URL('../lib/localserver.js', import.meta.url), 'utf8');
|
|
24
|
+
|
|
25
|
+
// ── 前端用到的路由 ────────────────────────────────────────────
|
|
26
|
+
const front = new Set();
|
|
27
|
+
{
|
|
28
|
+
const re = /\/api\/([a-zA-Z][\w-]*)/g;
|
|
29
|
+
let m;
|
|
30
|
+
while ((m = re.exec(uiSrc)) !== null) front.add(m[1]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── 后端定义的路由 ────────────────────────────────────────────
|
|
34
|
+
const back = new Set();
|
|
35
|
+
{
|
|
36
|
+
const re = /req\.url\s*===\s*['"]\/api\/([a-zA-Z][\w-]*)['"]/g;
|
|
37
|
+
let m;
|
|
38
|
+
while ((m = re.exec(srvSrc)) !== null) back.add(m[1]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ok(front.size >= 10, '前端至少用了 10 个 /api 路由', `实际 ${front.size} 个`);
|
|
42
|
+
ok(back.size >= 10, '后端至少定义了 10 个 /api 路由', `实际 ${back.size} 个`);
|
|
43
|
+
|
|
44
|
+
// ── 核心判据:前端用到的每个路由,后端都得有 ───────────────────
|
|
45
|
+
const missing = [...front].filter((n) => !back.has(n)).sort();
|
|
46
|
+
ok(
|
|
47
|
+
missing.length === 0,
|
|
48
|
+
'前端 fetch 的每个路由后端都定义了',
|
|
49
|
+
missing.length ? `后端没有:${missing.join(', ')}` : '',
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// ── 反向:后端有但前端没用的,只报告(可能是别的入口在用)──────
|
|
53
|
+
const unused = [...back].filter((n) => !front.has(n)).sort();
|
|
54
|
+
|
|
55
|
+
console.log('');
|
|
56
|
+
console.log('=== 前后端路由对账 ===');
|
|
57
|
+
console.log(`前端用到 ${front.size} 个:${[...front].sort().join(', ')}`);
|
|
58
|
+
console.log(`后端定义 ${back.size} 个:${[...back].sort().join(', ')}`);
|
|
59
|
+
if (unused.length) {
|
|
60
|
+
console.log(`ℹ️ 后端有、前端没引用(可能是工具/别处用):${unused.join(', ')}`);
|
|
61
|
+
}
|
|
62
|
+
console.log('');
|
|
63
|
+
|
|
64
|
+
if (fails.length) {
|
|
65
|
+
console.log(`❌ ${fails.length} 条没过,${pass} 条通过`);
|
|
66
|
+
for (const f of fails) console.log(' ✗ ' + f);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
} else {
|
|
69
|
+
console.log(`✅ 全部通过(${pass} 条)`);
|
|
70
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI 全局引用审计(2026-12-02 建)
|
|
3
|
+
*
|
|
4
|
+
* 起因:用户截图里那行红字「连不上本机服务:esc is not defined」。
|
|
5
|
+
* 根因不是网络 —— 是浏览器 <script> 块里调了只存在于 Node 层的 esc(),
|
|
6
|
+
* 执行到就抛 ReferenceError,被 fetch 的 .catch 接住,显示成「连不上」。
|
|
7
|
+
*
|
|
8
|
+
* 这一类 bug 的共同形状:**在浏览器里引用了浏览器没有的名字**,
|
|
9
|
+
* 表现为「点了没反应 / 显示一个不相关的错误 / 整块脚本静默死掉」。
|
|
10
|
+
* 它不会让 node --check 报错(语法是对的),只在真浏览器里炸。
|
|
11
|
+
*
|
|
12
|
+
* 这个脚本抓两类:
|
|
13
|
+
* A. `<script>` 块里被**调用**、但块内没定义、也不是浏览器内置的名字
|
|
14
|
+
* (esc 就是这一类)
|
|
15
|
+
* B. HTML 内联处理器 `on*=“fn(...)”` 里引用的函数,script 块里没有
|
|
16
|
+
* (函数改名/删掉之后,按钮点了永远没反应 —— 也是静默坏)
|
|
17
|
+
*
|
|
18
|
+
* 判据来源:真实渲染出来的 HTML(renderUi),不是猜模板结构。
|
|
19
|
+
* 这样连服务端拼进 HTML 的东西一起验。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { renderUi } from '../lib/ui.js';
|
|
23
|
+
|
|
24
|
+
let pass = 0;
|
|
25
|
+
const fails = [];
|
|
26
|
+
|
|
27
|
+
function ok(cond, label, detail) {
|
|
28
|
+
if (cond) {
|
|
29
|
+
pass += 1;
|
|
30
|
+
} else {
|
|
31
|
+
fails.push(detail ? `${label} —— ${detail}` : label);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ── 渲染真实 HTML ─────────────────────────────────────────────
|
|
36
|
+
const html = renderUi({});
|
|
37
|
+
ok(typeof html === 'string' && html.length > 5000, 'renderUi 输出非空 HTML', `长度 ${html?.length}`);
|
|
38
|
+
|
|
39
|
+
// ── 1) 取出所有 <script> 块 ───────────────────────────────────
|
|
40
|
+
const scriptBlocks = [];
|
|
41
|
+
{
|
|
42
|
+
const re = /<script(?:\s[^>]*)?>([\s\S]*?)<\/script>/gi;
|
|
43
|
+
let m;
|
|
44
|
+
while ((m = re.exec(html)) !== null) {
|
|
45
|
+
scriptBlocks.push({ code: m[1], at: m.index });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
ok(scriptBlocks.length >= 1, 'HTML 里至少有一个 <script> 块', `找到 ${scriptBlocks.length} 个`);
|
|
49
|
+
|
|
50
|
+
const allCode = scriptBlocks.map((b) => b.code).join('\n;\n');
|
|
51
|
+
|
|
52
|
+
// ── 2) 每个块先过一遍语法(语法错整块都不执行)────────────────
|
|
53
|
+
for (let i = 0; i < scriptBlocks.length; i += 1) {
|
|
54
|
+
let syntaxErr = '';
|
|
55
|
+
try {
|
|
56
|
+
// 只解析不执行。new Function 会对函数体做完整的语法检查。
|
|
57
|
+
// eslint-disable-next-line no-new-func
|
|
58
|
+
new Function(scriptBlocks[i].code);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
syntaxErr = e.message;
|
|
61
|
+
}
|
|
62
|
+
ok(!syntaxErr, `<script> 块 #${i + 1} 语法能解析`, syntaxErr);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── 3) 浏览器里本来就有的名字(白名单)────────────────────────
|
|
66
|
+
const BROWSER_GLOBALS = new Set([
|
|
67
|
+
// 语言内建
|
|
68
|
+
'Object', 'Array', 'String', 'Number', 'Boolean', 'Symbol', 'BigInt', 'Date',
|
|
69
|
+
'RegExp', 'Error', 'TypeError', 'RangeError', 'SyntaxError', 'Promise', 'Map',
|
|
70
|
+
'Set', 'WeakMap', 'WeakSet', 'JSON', 'Math', 'Intl', 'Proxy', 'Reflect',
|
|
71
|
+
'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'encodeURIComponent',
|
|
72
|
+
'decodeURIComponent', 'encodeURI', 'decodeURI', 'escape', 'unescape',
|
|
73
|
+
'Function', 'eval', 'structuredClone', 'queueMicrotask',
|
|
74
|
+
// 浏览器环境
|
|
75
|
+
'window', 'document', 'navigator', 'location', 'history', 'screen', 'console',
|
|
76
|
+
'fetch', 'XMLHttpRequest', 'FormData', 'URL', 'URLSearchParams', 'Blob', 'File',
|
|
77
|
+
'FileReader', 'Headers', 'Request', 'Response', 'AbortController', 'AbortSignal',
|
|
78
|
+
'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval',
|
|
79
|
+
'requestAnimationFrame', 'cancelAnimationFrame', 'requestIdleCallback',
|
|
80
|
+
'alert', 'confirm', 'prompt', 'localStorage', 'sessionStorage', 'indexedDB',
|
|
81
|
+
'addEventListener', 'removeEventListener', 'getComputedStyle', 'matchMedia',
|
|
82
|
+
'CustomEvent', 'Event', 'EventTarget', 'Node', 'Element', 'HTMLElement',
|
|
83
|
+
'Image', 'Audio', 'Worker', 'WebSocket', 'Notification', 'CSS', 'TextDecoder',
|
|
84
|
+
'TextEncoder', 'atob', 'btoa', 'crypto', 'performance', 'innerWidth',
|
|
85
|
+
'innerHeight', 'scrollTo', 'scrollBy', 'open', 'close', 'postMessage',
|
|
86
|
+
'MutationObserver', 'IntersectionObserver', 'ResizeObserver', 'DOMParser',
|
|
87
|
+
'ClipboardItem', 'AudioContext', 'webkitAudioContext', 'ReadableStream',
|
|
88
|
+
'Uint8Array', 'Uint16Array', 'Uint32Array', 'Int8Array', 'Int16Array',
|
|
89
|
+
'Int32Array', 'Float32Array', 'Float64Array', 'ArrayBuffer', 'DataView',
|
|
90
|
+
'isArray', 'isInteger', 'isSafeInteger',
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
// ── 4) script 块里定义了哪些名字 ──────────────────────────────
|
|
94
|
+
function collectDefined(code) {
|
|
95
|
+
const names = new Set();
|
|
96
|
+
const patterns = [
|
|
97
|
+
/\bfunction\s+([A-Za-z_$][\w$]*)\s*\(/g,
|
|
98
|
+
/\b(?:var|let|const)\s+([A-Za-z_$][\w$]*)\s*=/g,
|
|
99
|
+
// window.xxx = ... / window[“xxx”] = ...
|
|
100
|
+
/\bwindow\s*\.\s*([A-Za-z_$][\w$]*)\s*=/g,
|
|
101
|
+
// xxx = function / xxx = (a) => (隐式全局,脚本非严格模式下成立)
|
|
102
|
+
/^\s*([A-Za-z_$][\w$]*)\s*=\s*(?:function|\()/gm,
|
|
103
|
+
];
|
|
104
|
+
for (const re of patterns) {
|
|
105
|
+
let m;
|
|
106
|
+
while ((m = re.exec(code)) !== null) names.add(m[1]);
|
|
107
|
+
}
|
|
108
|
+
return names;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const defined = collectDefined(allCode);
|
|
112
|
+
|
|
113
|
+
// ── 5) script 块里“调用了”哪些名字 ────────────────────────────
|
|
114
|
+
// 排除:属性访问 obj.fn( 、字符串里的、注释里的、函数定义本身、new X(
|
|
115
|
+
function collectCalled(code) {
|
|
116
|
+
// 去掉注释和字符串,减少误报(粗粒度,够用)
|
|
117
|
+
const clean = code
|
|
118
|
+
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
119
|
+
.replace(/(^|[^:\\])\/\/[^\n]*/g, '$1 ')
|
|
120
|
+
.replace(/'(?:\\.|[^'\\])*'/g, "''")
|
|
121
|
+
.replace(/"(?:\\.|[^"\\])*"/g, '""')
|
|
122
|
+
.replace(/`(?:\\.|[^`\\])*`/g, '``');
|
|
123
|
+
|
|
124
|
+
const names = new Set();
|
|
125
|
+
// 前面不是 . 也不是标识符字符 —— 即不是 obj.fn(
|
|
126
|
+
const re = /(^|[^.\w$])([A-Za-z_$][\w$]*)\s*\(/g;
|
|
127
|
+
let m;
|
|
128
|
+
while ((m = re.exec(clean)) !== null) {
|
|
129
|
+
const name = m[2];
|
|
130
|
+
// 关键字排除
|
|
131
|
+
if (['if', 'for', 'while', 'switch', 'catch', 'function', 'return', 'typeof',
|
|
132
|
+
'new', 'delete', 'void', 'in', 'of', 'do', 'else', 'try', 'finally',
|
|
133
|
+
'throw', 'case', 'with', 'yield', 'await', 'async'].includes(name)) continue;
|
|
134
|
+
names.add(name);
|
|
135
|
+
}
|
|
136
|
+
return names;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const called = collectCalled(allCode);
|
|
140
|
+
|
|
141
|
+
// ── 6) A 类:调用了但没定义、也不是浏览器内置 ─────────────────
|
|
142
|
+
const undefinedCalls = [];
|
|
143
|
+
for (const name of called) {
|
|
144
|
+
if (defined.has(name)) continue;
|
|
145
|
+
if (BROWSER_GLOBALS.has(name)) continue;
|
|
146
|
+
// 大写开头多半是构造器/类(可能来自库全局),也放过 —— 但记录单独列
|
|
147
|
+
if (/^[A-Z]/.test(name)) continue;
|
|
148
|
+
undefinedCalls.push(name);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
ok(
|
|
152
|
+
undefinedCalls.length === 0,
|
|
153
|
+
'A 类:script 块里没有「调用了不存在的小写名字」',
|
|
154
|
+
undefinedCalls.length ? `未定义:${undefinedCalls.join(', ')}` : '',
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// 大写开头也查一遍,但只报告不判失败(可能是合法的库全局)
|
|
158
|
+
const upperUndefined = [];
|
|
159
|
+
for (const name of called) {
|
|
160
|
+
if (defined.has(name) || BROWSER_GLOBALS.has(name)) continue;
|
|
161
|
+
if (!/^[A-Z]/.test(name)) continue;
|
|
162
|
+
upperUndefined.push(name);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── 7) B 类:内联 on*= 处理器引用的函数是否存在 ───────────────
|
|
166
|
+
// HTML 里的 on*="foo(...)" 只认全局函数;script 里没有就是点了没反应。
|
|
167
|
+
const JS_KEYWORDS = new Set([
|
|
168
|
+
'if', 'else', 'return', 'for', 'while', 'switch', 'catch', 'function', 'typeof',
|
|
169
|
+
'new', 'delete', 'void', 'in', 'of', 'do', 'try', 'finally', 'throw', 'case',
|
|
170
|
+
'with', 'yield', 'await', 'async', 'let', 'const', 'var',
|
|
171
|
+
]);
|
|
172
|
+
|
|
173
|
+
const inlineHandlers = [];
|
|
174
|
+
{
|
|
175
|
+
const re = /\son([a-z]+)\s*=\s*"([^"]*)"/gi;
|
|
176
|
+
let m;
|
|
177
|
+
while ((m = re.exec(html)) !== null) {
|
|
178
|
+
const body = m[2];
|
|
179
|
+
// 只认“独立的”调用:前面不是 . 也不是标识符字符。
|
|
180
|
+
// 这样 e.preventDefault() 里的 preventDefault 不算缺函数(它是方法),
|
|
181
|
+
// if ( / for ( 这类关键字也不算。
|
|
182
|
+
const callRe = /(^|[^.\w$])([A-Za-z_$][\w$]*)\s*\(/g;
|
|
183
|
+
let c;
|
|
184
|
+
while ((c = callRe.exec(body)) !== null) {
|
|
185
|
+
const name = c[2];
|
|
186
|
+
if (JS_KEYWORDS.has(name)) continue;
|
|
187
|
+
inlineHandlers.push({ event: m[1], fn: name });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const missingHandlers = [];
|
|
193
|
+
for (const h of inlineHandlers) {
|
|
194
|
+
if (BROWSER_GLOBALS.has(h.fn)) continue;
|
|
195
|
+
if (/^[A-Z]/.test(h.fn)) continue;
|
|
196
|
+
if (!defined.has(h.fn)) missingHandlers.push(`${h.fn}(on${h.event})`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
ok(
|
|
200
|
+
missingHandlers.length === 0,
|
|
201
|
+
'B 类:内联 on*= 引用的函数都在 script 块里定义过',
|
|
202
|
+
missingHandlers.length ? `缺失:${missingHandlers.join(', ')}` : '',
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// ── 8) 兜底:esc 这类「Node 与浏览器同名」的坑单独点名 ─────────
|
|
206
|
+
// esc 现在两份都该有;少一份就是同类 bug 复发。
|
|
207
|
+
{
|
|
208
|
+
const nodeEsc = /^const esc = \(s\) =>/m.test(
|
|
209
|
+
// 源码层面才知道 Node 那份;这里用模块源码读一遍
|
|
210
|
+
(await import('node:fs')).readFileSync(new URL('../lib/ui.js', import.meta.url), 'utf8'),
|
|
211
|
+
);
|
|
212
|
+
ok(nodeEsc, 'Node 层 esc 还在(服务端渲染用)', '没了说明模板拼装会出错');
|
|
213
|
+
|
|
214
|
+
const browserEsc = /\bfunction\s+esc\s*\(\s*s\s*\)/.test(allCode);
|
|
215
|
+
ok(browserEsc, '浏览器层 esc 也在(2026-12-02 补的)', '缺了就是「esc is not defined」复发');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── 输出 ─────────────────────────────────────────────────────
|
|
219
|
+
console.log('');
|
|
220
|
+
console.log('=== UI 全局引用审计 ===');
|
|
221
|
+
console.log(`script 块: ${scriptBlocks.length} 个 · 定义名 ${defined.size} 个 · 被调用名 ${called.size} 个`);
|
|
222
|
+
console.log(`内联处理器: ${inlineHandlers.length} 处`);
|
|
223
|
+
if (upperUndefined.length) {
|
|
224
|
+
console.log(`⚠️ 大写开头的未定义调用(可能来自库,未判失败): ${upperUndefined.join(', ')}`);
|
|
225
|
+
}
|
|
226
|
+
console.log('');
|
|
227
|
+
|
|
228
|
+
if (fails.length) {
|
|
229
|
+
console.log(`❌ ${fails.length} 条没过,${pass} 条通过`);
|
|
230
|
+
for (const f of fails) console.log(' ✗ ' + f);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
} else {
|
|
233
|
+
console.log(`✅ 全部通过(${pass} 条)`);
|
|
234
|
+
}
|