@sparkelf/dsh-mobile-bridge-server 0.2.7 → 0.2.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sparkelf/dsh-mobile-bridge-server",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "description": "Multi-user bridge server connecting mobile browsers to local DeepSeek Harness instances through outbound tunnels.",
6
6
  "license": "MIT",
@@ -29,4 +29,4 @@
29
29
  "ws": "^8.18.0",
30
30
  "zxing-wasm": "3.1.3"
31
31
  }
32
- }
32
+ }
package/src/index.ts CHANGED
File without changes
package/src/server.ts CHANGED
@@ -174,6 +174,36 @@ function requestIp(req: IncomingMessage): string {
174
174
  return value?.trim() || req.socket.remoteAddress || 'unknown'
175
175
  }
176
176
 
177
+ /**
178
+ * Whether one request reached this server over TLS.
179
+ *
180
+ * The session cookie carries `Secure` only when it does, because a browser refuses to store or send
181
+ * a `Secure` cookie over plain HTTP. The phone service worker authenticates its relay socket from
182
+ * that cookie alone -- it opens `/ws/client` without a token -- so a deployment served over HTTP
183
+ * without this check lost the cookie, and the phone could not authenticate its socket at all.
184
+ * @param req - Request whose scheme to classify.
185
+ * @returns True when the client used HTTPS.
186
+ */
187
+ function requestIsSecure(req: IncomingMessage): boolean {
188
+ const forwarded = req.headers['x-forwarded-proto']
189
+ const value = Array.isArray(forwarded) ? forwarded[0] : forwarded?.split(',')[0]
190
+ if (value !== undefined && value.trim() !== '') return value.trim() === 'https'
191
+ return (req.socket as { encrypted?: boolean }).encrypted === true
192
+ }
193
+
194
+ /**
195
+ * Session cookie attributes, with `Secure` only where a browser can return the cookie.
196
+ * @param req - Request the cookie answers.
197
+ * @param token - Session token to store.
198
+ * @param days - Lifetime in days.
199
+ * @returns The `Set-Cookie` value.
200
+ */
201
+ function sessionCookie(req: IncomingMessage, token: string, days: number): string {
202
+ return COOKIE + '=' + encodeURIComponent(token)
203
+ + '; HttpOnly; SameSite=Lax; Path=/; Max-Age=' + String(days * 86400)
204
+ + (requestIsSecure(req) ? '; Secure' : '')
205
+ }
206
+
177
207
  function requestDeviceName(req: IncomingMessage): string {
178
208
  const userAgent = req.headers['user-agent'] ?? ''
179
209
  const platform = /iPad/u.test(userAgent)
@@ -319,10 +349,10 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
319
349
  return { token, device }
320
350
  }
321
351
 
322
- const respondLogin = (res: ServerResponse, token: string, days: number): void => {
352
+ const respondLogin = (req: IncomingMessage, res: ServerResponse, token: string, days: number): void => {
323
353
  res.writeHead(200, {
324
354
  'content-type': 'application/json',
325
- 'set-cookie': [COOKIE + '=' + encodeURIComponent(token) + '; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=' + String(days * 86400)],
355
+ 'set-cookie': [sessionCookie(req, token, days)],
326
356
  })
327
357
  res.end(JSON.stringify({ token }))
328
358
  }
@@ -409,7 +439,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
409
439
  if (priorDevice === undefined) throw new Error('scan a desktop pairing QR first')
410
440
  store.bindDevice(token, priorDevice.bridgeId, { name: requestDeviceName(req), ip: requestIp(req) }, priorDevice.id)
411
441
  }
412
- respondLogin(res, token, days)
442
+ respondLogin(req, res, token, days)
413
443
  } catch (error) { fail(res, 401, 'email login failed', error) }
414
444
  return
415
445
  }
@@ -419,7 +449,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
419
449
  const verify = externalAuth[provider]
420
450
  if (verify === undefined) throw new Error('unknown provider')
421
451
  const externalId = await verify(payload)
422
- respondLogin(res, store.loginExternal(provider, externalId), DEFAULT_SESSION_DAYS)
452
+ respondLogin(req, res, store.loginExternal(provider, externalId), DEFAULT_SESSION_DAYS)
423
453
  } catch (error) { fail(res, 401, 'external login failed', error) }
424
454
  return
425
455
  }
@@ -434,7 +464,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
434
464
  return
435
465
  }
436
466
  const { token } = pairDevice(req, ticket, 'bridge', randomBytes(12).toString('hex'))
437
- respondLogin(res, token, ticket.sessionDays)
467
+ respondLogin(req, res, token, ticket.sessionDays)
438
468
  } catch (error) { fail(res, 401, 'pairing login failed', error) }
439
469
  return
440
470
  }
@@ -445,7 +475,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
445
475
  if (ticket.email2fa === undefined) throw new Error('no pending challenge')
446
476
  store.consumeEmailCode(ticket.email2fa, emailCode)
447
477
  const { token } = pairDevice(req, ticket, 'bridge', randomBytes(12).toString('hex'))
448
- respondLogin(res, token, ticket.sessionDays)
478
+ respondLogin(req, res, token, ticket.sessionDays)
449
479
  } catch (error) { fail(res, 401, 'pairing verification failed', error) }
450
480
  return
451
481
  }
@@ -497,7 +527,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
497
527
  if (socket?.readyState === WebSocket.OPEN) socket.send(JSON.stringify({ control: 'paired', devices: deviceViews(ticket.bridgeId) }))
498
528
  }
499
529
  res.writeHead(302, {
500
- 'set-cookie': [COOKIE + '=' + encodeURIComponent(token) + '; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=' + String(days * 86400)],
530
+ 'set-cookie': [sessionCookie(req, token, days)],
501
531
  location: '/',
502
532
  })
503
533
  res.end()
@@ -621,7 +651,11 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
621
651
  const device = store.deviceFor(token)
622
652
  const bridgeId = device?.bridgeId
623
653
  if (device === undefined || bridgeId === undefined) {
624
- wss.handleUpgrade(req, socket, head, client => { client.close(4003, 'device revoked') })
654
+ // 4003 means the desktop revoked this device, and the phone treats it as terminal: it clears
655
+ // the pairing and stops reconnecting. A request that simply arrived without a usable session
656
+ // is not that, and reporting it as a revocation left a phone whose cookie was missing with a
657
+ // dead pairing it never got told to renew. 4004 keeps the two apart.
658
+ wss.handleUpgrade(req, socket, head, client => { client.close(4004, 'pairing required') })
625
659
  return
626
660
  }
627
661
  if (bridges.get(bridgeId)?.readyState !== WebSocket.OPEN) {