dsh-workspace-migrate 0.5.1 → 0.5.3

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 +3 -1
  2. package/index.js +39 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # dsh-workspace-migrate
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/dsh-workspace-migrate)](https://www.npmjs.com/package/dsh-workspace-migrate)
4
+ [![DSH Plugin Store](https://img.shields.io/badge/DSH%20Plugin%20Store-listed-4C8DFF)](https://www.dshplugin.store/plugin/lij0000-arch/dsh-workspace-migrate)
4
5
  [![release](https://img.shields.io/github/v/release/lij0000-arch/dsh-workspace-migrate?label=release)](https://github.com/lij0000-arch/dsh-workspace-migrate/releases)
5
6
  [![stars](https://img.shields.io/github/stars/lij0000-arch/dsh-workspace-migrate)](https://github.com/lij0000-arch/dsh-workspace-migrate)
6
7
  [![license](https://img.shields.io/github/license/lij0000-arch/dsh-workspace-migrate)](LICENSE)
@@ -40,7 +41,8 @@ dsh plugin --profile desktop add dsh-workspace-migrate
40
41
  - **读**:`~/.dsh/sessions`(会话事件流)、`~/.dsh/storages/workspace.json`(项目登记、置顶/归档集合)、`~/.dsh/attachments`、以及所选项目目录
41
42
  - **写**:经公开服务新建会话与项目登记(`sessionPersistence` / `workspaceRegistry`)、按路径映射复制项目文件与附件
42
43
  - **删**:只有「撤销这次导入」会删,而且只删**本次进程内由本插件创建**的项目登记与会话目录,其它 id 一律拒绝
43
- - **网络**:不向外发起请求;仅在本机 `127.0.0.1` 注册 `/api/dsh-migrate` 供当前页面调用,并校验请求来源
44
+ - **网络**:不向外发起请求;仅在本机 `127.0.0.1` 注册 `/api/dsh-migrate` 供当前页面调用
45
+ - **鉴权**:每个请求都经宿主 `ctx.connection.requestRejection()` 校验浏览器签名凭据,被拒返回 401/403;宿主没有该服务时**直接返回 503**,不回退到 Origin/Host 比较(请求头可以伪造,不能当身份凭据)
44
46
  - **凭据**:不读取、不导出 `~/.dsh/.credentials.yaml`
45
47
  - **隐私**:迁移包含完整对话与项目文件,导出目录不要放在公开位置
46
48
 
package/index.js CHANGED
@@ -1157,16 +1157,6 @@ export function apply(ctx, config = {}) {
1157
1157
  res.end(body)
1158
1158
  }
1159
1159
 
1160
- function sameOrigin(req) {
1161
- const origin = req.headers.origin
1162
- if (!origin) return true
1163
- try {
1164
- return new URL(origin).host === req.headers.host
1165
- } catch {
1166
- return false
1167
- }
1168
- }
1169
-
1170
1160
  async function readJson(req) {
1171
1161
  const chunks = []
1172
1162
  let size = 0
@@ -1179,9 +1169,46 @@ export function apply(ctx, config = {}) {
1179
1169
  return JSON.parse(Buffer.concat(chunks).toString('utf8'))
1180
1170
  }
1181
1171
 
1172
+ /**
1173
+ * 身份认证。宿主提供 `connection` 服务时必须用它:
1174
+ * ctx.connection.admit({ headers }) 会校验浏览器签名凭据(或 API token),
1175
+ * 返回 `{}` 或 `{ rejection: 401 | 403 }`。
1176
+ * 没有该服务(老宿主)时退回 Origin 检查,并额外要求 Origin 存在——
1177
+ * 缺 Origin 的请求只可能来自非浏览器客户端。
1178
+ */
1179
+ /**
1180
+ * 身份认证。只认宿主的 `connection` 服务:
1181
+ * ctx.connection.requestRejection({ headers }) —— 宿主自带的 Host/Origin 围栏
1182
+ * + 浏览器签名凭据(login-token cookie)校验,被拒时返回 401/403。
1183
+ * 没有这个服务就**直接拒绝**:Origin/Host 只是请求头,客户端可以随便写,
1184
+ * 不能当身份凭据用。迁移、回滚、清理这类会写盘的操作宁可不可用,也不可无认证。
1185
+ */
1186
+ function authenticate(req) {
1187
+ const connection = ctx.get('connection')
1188
+ if (!connection) return { status: 503, reason: 'unavailable' }
1189
+ try {
1190
+ if (typeof connection.requestRejection === 'function') {
1191
+ const rejection = connection.requestRejection({ headers: req.headers })
1192
+ return rejection ? { status: rejection } : {}
1193
+ }
1194
+ const admission = connection.admit({ headers: req.headers })
1195
+ if (admission && admission.rejection) return { status: admission.rejection }
1196
+ return {}
1197
+ } catch {
1198
+ return { status: 401 }
1199
+ }
1200
+ }
1201
+
1182
1202
  async function handler(req, res) {
1183
- if (!sameOrigin(req)) {
1184
- sendJson(res, 403, { error: '拒绝跨站请求' })
1203
+ const auth = authenticate(req)
1204
+ if (auth.status) {
1205
+ const message =
1206
+ auth.reason === 'unavailable'
1207
+ ? '宿主未提供 connection 认证服务,已拒绝处理(不做无身份校验的迁移)'
1208
+ : auth.status === 401
1209
+ ? '需要先在 Harness 中登录(缺少或无效的浏览器凭据)'
1210
+ : '拒绝跨站请求'
1211
+ sendJson(res, auth.status, { error: message })
1185
1212
  return
1186
1213
  }
1187
1214
  const url = new URL(req.url ?? '/', 'http://localhost')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workspace-migrate",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "把 DeepSeek Harness 的工作区、项目成果和对话内容整体迁到另一台电脑",