dsh-chrome-bridge 2.0.1 → 2.0.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/index.js +3 -1
  2. package/package.json +1 -1
  3. package/panel.js +49 -12
package/index.js CHANGED
@@ -59,10 +59,12 @@ export function apply(ctx, config) {
59
59
  if (typeof ctx.inject === 'function') {
60
60
  ctx.inject(['webServer'], (sub) => {
61
61
  if (!sub || typeof sub.effect !== 'function' || !sub.webServer) return
62
+ const getConnection = () => (typeof ctx.get === 'function' ? ctx.get('connection') : typeof sub.get === 'function' ? sub.get('connection') : null)
62
63
  sub.effect(() => sub.webServer.register({
63
64
  kind: 'exact',
64
65
  path: PANEL_ROUTE,
65
- handler: panelHandler({ call, sock: hostUp, origin: () => sub.webServer.getOrigin(), trace }),
66
+ auth: 'required',
67
+ handler: panelHandler({ call, sock: hostUp, origin: () => sub.webServer.getOrigin(), connection: getConnection, trace }),
66
68
  }), 'chrome-bridge: settings panel route')
67
69
  trace(`设置页路由已挂上 ${PANEL_ROUTE}`)
68
70
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-chrome-bridge",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "DSH plugin: native browser_* tools that drive the Chrome you are already using, over native messaging + CDP. No WebSocket, no port, no Connect button. The native host and the MV3 extension ship inside this package and are installed automatically on first load; the one step left is adding the DSH Bridge extension to Chrome, from the Chrome Web Store or as the bundled unpacked copy. Until then browser_* tools fail loudly with those exact steps.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/panel.js CHANGED
@@ -5,8 +5,9 @@
5
5
  * POST /dsh-chrome-bridge/panel {action} → pause(只能暂停)/ cursor / window / releaseAll / install,回新状态
6
6
  *
7
7
  * 页面(client.js)跑在 DSH 的网页端,只能走 HTTP;这里再转成 socket 上的 `panel` 操作。
8
- * webServer 本身不做鉴权,而用户的 Chrome 里随便哪个网页都摸得到 127.0.0.1,所以自己把门:
9
- * 只收本机来的连接、跨站的 Origin 一律拒、POST 必须是 JSON(跨站发不出来,会先被预检挡住)。
8
+ * 路由同时接 DSH 宿主双代鉴权:DSH 0.2.3+ 走 webServer auth='required' 与第三参数 peer(仅放行 owner),
9
+ * DSH 0.2.2 走 connection.requestRejection(req) 验登录态,无 connection 的老宿主退回本机围栏;
10
+ * 同时对所有 POST / 跨站请求强制校 Sec-Fetch-Site、Origin 和 application/json。
10
11
  * 「恢复控制」故意不开放 —— 这条路模型也摸得到,那一下只能用户在 Chrome 的扩展面板里点。
11
12
  */
12
13
 
@@ -62,16 +63,19 @@ function handoffOf(ho) {
62
63
  return { id: ho.id ?? null, tabId: ho.tabId ?? null, reason: str(ho.reason), title: str(ho.title), url: str(ho.url), at: Number(ho.at) || 0 }
63
64
  }
64
65
 
65
- /** 跨站请求一律拒:没带 Origin(同源 GET、curl)放行;带了就必须是 DSH 自己。 */
66
- function allowed(req, origin) {
67
- if (!LOOPBACK.has(req.socket.remoteAddress)) return false
68
- if (!LOOPBACK_HOST.test(req.headers.host || '')) return false
66
+ /** 校验来源头:允许省略 Origin(同源 GET、原生客户端),带了就必须是 DSH 自己、同 Host 或桌面壳 dsh-app://app(限本机)。 */
67
+ function originAllowed(req, origin) {
68
+ const site = String(req.headers['sec-fetch-site'] || '').toLowerCase()
69
+ if (site === 'cross-site' || site === 'same-site') return false
69
70
  const from = req.headers.origin
70
71
  if (!from) return true
71
72
  if (from === 'null') return false
73
+ if (from === 'dsh-app://app') return LOOPBACK.has(req.socket.remoteAddress)
74
+ const host = String(req.headers.host || '')
75
+ if (host && (from === `http://${host}` || from === `https://${host}`)) return true
72
76
  let own
73
77
  try { own = origin() } catch { own = '' }
74
- if (from === own) return true
78
+ if (own && from === own) return true
75
79
  try {
76
80
  const a = new URL(from), b = new URL(own)
77
81
  return LOOPBACK_HOST.test(a.host) && a.port === b.port && a.protocol === b.protocol
@@ -80,6 +84,37 @@ function allowed(req, origin) {
80
84
  }
81
85
  }
82
86
 
87
+ /** 无 DSH connection 服务时的兜底本机围栏:仅本机连接、本机 Host、同源 Origin。 */
88
+ function allowed(req, origin) {
89
+ if (!LOOPBACK.has(req.socket.remoteAddress)) return false
90
+ if (!LOOPBACK_HOST.test(req.headers.host || '')) return false
91
+ return originAllowed(req, origin)
92
+ }
93
+
94
+ /**
95
+ * 双代 DSH 宿主准入检查:
96
+ * - DSH 0.2.3+:webServer 传第 3 参数 peer,仅放行 owner 主体;
97
+ * - DSH 0.2.2:调用 connection.requestRejection(req) 校验登录态与跨站围栏;
98
+ * - 若 connection 服务存在但缺失 requestRejection(版本形态不符):默认拒绝(fail-closed);
99
+ * - 无 connection 的老宿主 / 本地单测:退回本机围栏 allowed()。
100
+ * 返回 0 表示放行,否则返回 HTTP 错误码(401 / 403)。
101
+ */
102
+ function admissionStatus(req, peer, connection, origin) {
103
+ if (peer !== undefined) {
104
+ if (peer?.principal?.kind !== 'owner') return 403
105
+ return originAllowed(req, origin) ? 0 : 403
106
+ }
107
+ let conn = null
108
+ try { conn = typeof connection === 'function' ? connection() : connection } catch { conn = null }
109
+ if (conn && typeof conn.requestRejection === 'function') {
110
+ const rej = conn.requestRejection(req)
111
+ if (rej) return rej
112
+ return originAllowed(req, origin) ? 0 : 403
113
+ }
114
+ if (conn) return 403
115
+ return allowed(req, origin) ? 0 : 403
116
+ }
117
+
83
118
  function readJson(req, limit = 4096) {
84
119
  return new Promise((resolve, reject) => {
85
120
  let size = 0
@@ -99,17 +134,19 @@ function readJson(req, limit = 4096) {
99
134
  * @param deps.call - socket 调用
100
135
  * @param deps.sock - 宿主在不在的判断函数(或 socket 路径)
101
136
  * @param deps.origin - DSH 网页端自己的 origin(webServer.getOrigin)
137
+ * @param deps.connection - 可选:返回 DSH connection 服务(ctx.get('connection'))
102
138
  * @param deps.trace - 日志
103
139
  */
104
- export function panelHandler({ call, sock, origin, trace = () => {} }) {
105
- return async (req, res) => {
140
+ export function panelHandler({ call, sock, origin, connection = null, trace = () => {} }) {
141
+ return async (req, res, peer) => {
106
142
  const send = (code, body) => {
107
143
  res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
108
144
  res.end(JSON.stringify(body))
109
145
  }
110
- if (!allowed(req, origin)) {
111
- trace(`设置页路由拒绝了一个请求:${req.socket.remoteAddress} host=${req.headers.host} origin=${req.headers.origin}`)
112
- return send(403, { error: 'forbidden' })
146
+ const denied = admissionStatus(req, peer, connection, origin)
147
+ if (denied) {
148
+ trace(`设置页路由拒绝了一个请求(${denied}):${req.socket.remoteAddress} host=${req.headers.host} origin=${req.headers.origin}`)
149
+ return send(denied, { error: denied === 401 ? 'unauthorized' : 'forbidden' })
113
150
  }
114
151
  if (req.method === 'GET') return send(200, await panelState(call, sock))
115
152
  if (req.method !== 'POST') return send(405, { error: 'method not allowed' })