dsh-relay-phone 0.12.3 → 0.13.1
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 +1 -1
- package/src/boot-source.js +19 -1
- package/src/crypto.js +37 -1
- package/src/mobile/assets/app.js +20 -2
- package/src/pump.js +19 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-relay-phone",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"description": "Remote-control your local DeepSeek Harness from a phone through your own zero-knowledge relay: phone-first page, QR pairing, end-to-end encryption, approvals, full desktop UI fallback.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
package/src/boot-source.js
CHANGED
|
@@ -197,13 +197,31 @@ export function buildBootSource(id, gate) {
|
|
|
197
197
|
var envKey = nacl.hash(new Uint8Array([].concat(Array.prototype.slice.call(psk),
|
|
198
198
|
Array.prototype.slice.call(new TextEncoder().encode('envelope'))))).slice(0, 32)
|
|
199
199
|
|
|
200
|
+
// Replay defence on this side too: the relay could re-submit an old *response*
|
|
201
|
+
// (a stale approval result, a stale session list). Remember the nonces we have
|
|
202
|
+
// opened and refuse a repeat; bounded so a long session cannot grow forever.
|
|
203
|
+
var seenNonces = typeof Set === 'function' ? new Set() : null
|
|
204
|
+
var SEEN_LIMIT = 20000
|
|
205
|
+
|
|
200
206
|
function openEnvelope(bytes) {
|
|
201
207
|
try {
|
|
202
208
|
if (bytes.length < 24 + 16) return null
|
|
203
209
|
var nonce = bytes.slice(0, 24)
|
|
204
210
|
var box = bytes.slice(24)
|
|
205
211
|
var opened = nacl.secretbox.open(box, nonce, envKey)
|
|
206
|
-
|
|
212
|
+
if (!opened) return null
|
|
213
|
+
if (seenNonces) {
|
|
214
|
+
var id = ''
|
|
215
|
+
for (var i = 0; i < 24; i++) id += (nonce[i] < 16 ? '0' : '') + nonce[i].toString(16)
|
|
216
|
+
if (seenNonces.has(id)) return null
|
|
217
|
+
seenNonces.add(id)
|
|
218
|
+
if (seenNonces.size > SEEN_LIMIT) {
|
|
219
|
+
var excess = seenNonces.size - SEEN_LIMIT
|
|
220
|
+
var it = seenNonces.values()
|
|
221
|
+
for (var d = 0; d < excess; d++) seenNonces.delete(it.next().value)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return new Uint8Array(opened)
|
|
207
225
|
} catch (e) { return null }
|
|
208
226
|
}
|
|
209
227
|
function sealEnvelope(bytes) {
|
package/src/crypto.js
CHANGED
|
@@ -20,6 +20,10 @@ const sha256 = (...parts) => {
|
|
|
20
20
|
return new Uint8Array(hash.digest())
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
// How many opened envelopes to remember for replay detection. 50k * ~70 B
|
|
24
|
+
// is a few megabytes, and on a personal relay it covers weeks of traffic.
|
|
25
|
+
const REPLAY_MEMORY = 50_000
|
|
26
|
+
|
|
23
27
|
export class CryptoBox {
|
|
24
28
|
constructor(identity, options = {}) {
|
|
25
29
|
this.psk = Buffer.from(identity.psk, 'hex') // 32 bytes
|
|
@@ -39,9 +43,36 @@ export class CryptoBox {
|
|
|
39
43
|
// page was reloaded). Persist the table beside identity.json so a restart
|
|
40
44
|
// keeps phones paired.
|
|
41
45
|
this.sessionsPath = options.sessionsPath ?? null
|
|
46
|
+
// Replay defence. Every envelope carries a fresh random nonce, so seeing the
|
|
47
|
+
// same nonce twice means the ciphertext was captured and re-submitted by
|
|
48
|
+
// someone who cannot forge one — the relay is exactly that someone. Remember
|
|
49
|
+
// what has been opened and refuse a repeat; the set is bounded by count (an
|
|
50
|
+
// entry falls out only after REPLAY_MEMORY newer envelopes), which for a
|
|
51
|
+
// personal relay is days of traffic.
|
|
52
|
+
this.openedNonces = new Set()
|
|
42
53
|
this.#loadSessions()
|
|
43
54
|
}
|
|
44
55
|
|
|
56
|
+
/**
|
|
57
|
+
* False when this nonce has been opened before, i.e. the ciphertext is a copy.
|
|
58
|
+
* Bounded memory: oldest entries are dropped first, so a long-running process
|
|
59
|
+
* cannot grow without limit.
|
|
60
|
+
*/
|
|
61
|
+
acceptNonce(nonce) {
|
|
62
|
+
const id = nonce.toString('hex')
|
|
63
|
+
if (this.openedNonces.has(id)) return false
|
|
64
|
+
this.openedNonces.add(id)
|
|
65
|
+
if (this.openedNonces.size > REPLAY_MEMORY) {
|
|
66
|
+
const excess = this.openedNonces.size - REPLAY_MEMORY
|
|
67
|
+
let dropped = 0
|
|
68
|
+
for (const old of this.openedNonces) {
|
|
69
|
+
this.openedNonces.delete(old)
|
|
70
|
+
if (++dropped >= excess) break
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
45
76
|
#loadSessions() {
|
|
46
77
|
if (!this.sessionsPath) return
|
|
47
78
|
try {
|
|
@@ -160,7 +191,12 @@ export class CryptoBox {
|
|
|
160
191
|
const nonce = sealed.subarray(0, nacl.secretbox.nonceLength)
|
|
161
192
|
const box = sealed.subarray(nacl.secretbox.nonceLength)
|
|
162
193
|
const opened = nacl.secretbox.open(box, nonce, key)
|
|
163
|
-
|
|
194
|
+
if (!opened) return null
|
|
195
|
+
// A valid envelope that has been seen before is a replay: the relay recorded
|
|
196
|
+
// it and sent it again. Refusing it here covers every unseal path — HTTP
|
|
197
|
+
// bodies, the mobile RPC and websocket frames all come through `open`.
|
|
198
|
+
if (!this.acceptNonce(nonce)) return null
|
|
199
|
+
return Buffer.from(opened)
|
|
164
200
|
}
|
|
165
201
|
|
|
166
202
|
// -- shell signatures (Ed25519 over boot.js) --------------------------------
|
package/src/mobile/assets/app.js
CHANGED
|
@@ -115,11 +115,29 @@
|
|
|
115
115
|
return out
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
// The relay could re-submit an old response (stale approval result, stale
|
|
119
|
+
// session list). Refuse a nonce we have already opened; bounded for a phone.
|
|
120
|
+
var seenNonces = typeof Set === 'function' ? new Set() : null
|
|
121
|
+
var SEEN_LIMIT = 20000
|
|
122
|
+
|
|
118
123
|
function openEnvelope(bytes) {
|
|
119
124
|
try {
|
|
120
125
|
if (bytes.length < 24 + 16) return null
|
|
121
|
-
var
|
|
122
|
-
|
|
126
|
+
var nonce = bytes.slice(0, 24)
|
|
127
|
+
var opened = nacl.secretbox.open(bytes.slice(24), nonce, envKey)
|
|
128
|
+
if (!opened) return null
|
|
129
|
+
if (seenNonces) {
|
|
130
|
+
var id = ''
|
|
131
|
+
for (var i = 0; i < 24; i++) id += (nonce[i] < 16 ? '0' : '') + nonce[i].toString(16)
|
|
132
|
+
if (seenNonces.has(id)) return null
|
|
133
|
+
seenNonces.add(id)
|
|
134
|
+
if (seenNonces.size > SEEN_LIMIT) {
|
|
135
|
+
var excess = seenNonces.size - SEEN_LIMIT
|
|
136
|
+
var it = seenNonces.values()
|
|
137
|
+
for (var d = 0; d < excess; d++) seenNonces.delete(it.next().value)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return new Uint8Array(opened)
|
|
123
141
|
} catch (error) {
|
|
124
142
|
return null
|
|
125
143
|
}
|
package/src/pump.js
CHANGED
|
@@ -38,6 +38,8 @@ const PENDING_LIMIT = 1024 * 1024
|
|
|
38
38
|
// with a phone screenshot are easily several hundred KB; the old 256KB ceiling
|
|
39
39
|
// rejected them with a bare 400 before they ever reached DSH).
|
|
40
40
|
const SEALED_BODY_LIMIT = 32 * 1024 * 1024
|
|
41
|
+
// 24-byte nonce + 16-byte secretbox overhead: nothing smaller can be an envelope.
|
|
42
|
+
const SEALED_BODY_MIN = 40
|
|
41
43
|
const RPC_LIMIT = 32 * 1024 * 1024
|
|
42
44
|
const EMPTY = Buffer.alloc(0)
|
|
43
45
|
// Per-request tracing stays off unless explicitly requested: the pump sees
|
|
@@ -545,10 +547,15 @@ export class PumpManager {
|
|
|
545
547
|
|
|
546
548
|
// -- sealed round trips ----------------------------------------------------------
|
|
547
549
|
prepareSealedForward(sid, stream, info, headText, rest) {
|
|
550
|
+
// An envelope is never empty: 24 nonce bytes + 16 box overhead, so 40 is the
|
|
551
|
+
// smallest possible sealed body. A shorter one (in particular `0`) is a
|
|
552
|
+
// caller that skipped the envelope entirely — the hole that let a ticket-only
|
|
553
|
+
// empty POST reach the host with its cookie attached.
|
|
548
554
|
const length = Number(info.headers.get('content-length') ?? '0')
|
|
549
|
-
if (!Number.isFinite(length) || length <
|
|
555
|
+
if (!Number.isFinite(length) || length < SEALED_BODY_MIN || length > SEALED_BODY_LIMIT) {
|
|
550
556
|
if (DEBUG) console.log('[relay-dbg] 400@prepare sid=%s len=%s ct=%s', sid, length, info.headers.get('content-type'))
|
|
551
|
-
|
|
557
|
+
auditLog(`sealed request without a usable envelope (${info.path}, content-length=${length})`)
|
|
558
|
+
this.respondInline(sid, 400, 'Bad Request', 'dsh-relay-phone: sealed requests need an envelope\n')
|
|
552
559
|
this.closeStream(sid)
|
|
553
560
|
return
|
|
554
561
|
}
|
|
@@ -561,16 +568,16 @@ export class PumpManager {
|
|
|
561
568
|
maybeFinishSealedRequest(sid, stream) {
|
|
562
569
|
if (stream.buffer.length < stream.reqLength) return
|
|
563
570
|
const sealedBody = stream.buffer.subarray(0, stream.reqLength)
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
571
|
+
// Always decrypt. An empty *plaintext* is fine — it still arrives as a 40-byte
|
|
572
|
+
// envelope — but a zero-length body can never be one, so no request reaches
|
|
573
|
+
// the host without opening an envelope first.
|
|
574
|
+
const plain = this.cryptoBox.open(this.cryptoBox.envelopeKey, sealedBody)
|
|
575
|
+
if (!plain) {
|
|
576
|
+
auditLog(`sealed request body failed to open (${stream.info.path})`)
|
|
577
|
+
if (DEBUG) console.log('[relay-dbg] 400@open sid=%s path=%s len=%s bodyHead=%s', sid, stream.info.path, stream.reqLength, sealedBody.subarray(0, 16).toString('hex'))
|
|
578
|
+
this.respondInline(sid, 400, 'Bad Request')
|
|
579
|
+
this.closeStream(sid)
|
|
580
|
+
return
|
|
574
581
|
}
|
|
575
582
|
const info = stream.info
|
|
576
583
|
// A sealed call to the mobile app's own API is answered here, not proxied
|