@mysterycode-liwen/dsh-credential-vault 1.4.3 → 1.5.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/README.md +13 -0
- package/client.js +138 -26
- package/cordis.patch.yml +3 -3
- package/icon.svg +6 -6
- package/index.js +412 -411
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,411 +1,412 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Host half of the dsh-credential-vault bundle.
|
|
3
|
-
*
|
|
4
|
-
* The browser (client.js) owns the vault data in localStorage. This half
|
|
5
|
-
* exposes it to the Harness through two things:
|
|
6
|
-
* 1. a loopback-only HTTP surface the browser POSTs its entries to, and
|
|
7
|
-
* 2. an agent tool (`dsh_credential_vault_read`) the model calls to read them.
|
|
8
|
-
*
|
|
9
|
-
* SECURITY MODEL (hardened):
|
|
10
|
-
* - The browser sends a METADATA-ONLY envelope. Secret fields (password /
|
|
11
|
-
* apiKey / content) never reach this Host process; plaintext stays in the
|
|
12
|
-
* browser's localStorage. Even a malicious full payload is sanitized here.
|
|
13
|
-
* - Nothing is persisted to disk. The snapshot lives only in memory and is
|
|
14
|
-
* re-synced by the browser on mount / change, so a restart simply starts
|
|
15
|
-
* empty until the browser reconnects.
|
|
16
|
-
* - Every tool access is recorded in a bounded, in-memory audit log that
|
|
17
|
-
* contains no plaintext (GET /audit, same-origin + loopback only).
|
|
18
|
-
*
|
|
19
|
-
* @module dsh-credential-vault
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/** Cordis plugin name. */
|
|
23
|
-
export const name = 'dsh-credential-vault'
|
|
24
|
-
|
|
25
|
-
/** The web server is the only hard dependency. */
|
|
26
|
-
export const inject = ['webServer']
|
|
27
|
-
|
|
28
|
-
/** Route prefix owned by this plugin. */
|
|
29
|
-
const PREFIX = '/dsh-credential-vault'
|
|
30
|
-
|
|
31
|
-
/** Cap the synced payload so a stray client cannot blow up the Host. Raised for large vaults. */
|
|
32
|
-
const MAX_BODY_BYTES = 16 * 1024 * 1024
|
|
33
|
-
|
|
34
|
-
/** Max entries mirrored from the browser; beyond this the tool output would be unwieldy. */
|
|
35
|
-
const MAX_ENTRIES = 20000
|
|
36
|
-
|
|
37
|
-
/** Fields that hold secrets; masked by default in tool output. */
|
|
38
|
-
const SECRET_FIELDS = ['password', 'apiKey', 'content']
|
|
39
|
-
const MASK = '••••••••'
|
|
40
|
-
|
|
41
|
-
/** Pagination defaults / limits for the read tool. */
|
|
42
|
-
const DEFAULT_LIMIT = 20
|
|
43
|
-
const MAX_LIMIT = 200
|
|
44
|
-
|
|
45
|
-
/** Fields kept in the compact "summary" projection. */
|
|
46
|
-
const SUMMARY_FIELDS = ['id', 'title', 'category', 'username', 'provider', 'expiry', 'updatedAt']
|
|
47
|
-
|
|
48
|
-
/** Current plugin version. Keep in sync with package.json when releasing. */
|
|
49
|
-
const CURRENT_VERSION = '1.
|
|
50
|
-
|
|
51
|
-
/** Where update checks read the latest published version from (GitHub Releases latest). */
|
|
52
|
-
const RELEASES_API = 'https://api.github.com/repos/mysterycode-liwen/dsh-credential-vault/releases/latest'
|
|
53
|
-
const RELEASES_URL = 'https://github.com/mysterycode-liwen/dsh-credential-vault/releases/latest'
|
|
54
|
-
const UPDATE_CHECK_TIMEOUT_MS = 8000
|
|
55
|
-
const UPDATE_CACHE_MS = 10 * 60 * 1000
|
|
56
|
-
let updateCache = { at: 0, result: null }
|
|
57
|
-
|
|
58
|
-
/** Last metadata-only snapshot received from the browser (never contains secrets). */
|
|
59
|
-
let snapshot = { entries: [], updatedAt: 0 }
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Fields mirrored from the browser. Everything else (especially SECRET_FIELDS)
|
|
63
|
-
* is dropped, so the Host can never hold a plaintext password / key / body.
|
|
64
|
-
*/
|
|
65
|
-
const META_FIELDS = ['id', 'title', 'category', 'username', 'provider', '
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
'
|
|
121
|
-
'
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (!l.prerelease) return
|
|
172
|
-
if (!
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
if (!
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
//
|
|
221
|
-
//
|
|
222
|
-
if
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
.
|
|
235
|
-
.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
//
|
|
242
|
-
//
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
//
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
.
|
|
295
|
-
.
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
//
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const
|
|
372
|
-
const
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
//
|
|
376
|
-
|
|
377
|
-
const
|
|
378
|
-
const
|
|
379
|
-
const
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the dsh-credential-vault bundle.
|
|
3
|
+
*
|
|
4
|
+
* The browser (client.js) owns the vault data in localStorage. This half
|
|
5
|
+
* exposes it to the Harness through two things:
|
|
6
|
+
* 1. a loopback-only HTTP surface the browser POSTs its entries to, and
|
|
7
|
+
* 2. an agent tool (`dsh_credential_vault_read`) the model calls to read them.
|
|
8
|
+
*
|
|
9
|
+
* SECURITY MODEL (hardened):
|
|
10
|
+
* - The browser sends a METADATA-ONLY envelope. Secret fields (password /
|
|
11
|
+
* apiKey / content) never reach this Host process; plaintext stays in the
|
|
12
|
+
* browser's localStorage. Even a malicious full payload is sanitized here.
|
|
13
|
+
* - Nothing is persisted to disk. The snapshot lives only in memory and is
|
|
14
|
+
* re-synced by the browser on mount / change, so a restart simply starts
|
|
15
|
+
* empty until the browser reconnects.
|
|
16
|
+
* - Every tool access is recorded in a bounded, in-memory audit log that
|
|
17
|
+
* contains no plaintext (GET /audit, same-origin + loopback only).
|
|
18
|
+
*
|
|
19
|
+
* @module dsh-credential-vault
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Cordis plugin name. */
|
|
23
|
+
export const name = 'dsh-credential-vault'
|
|
24
|
+
|
|
25
|
+
/** The web server is the only hard dependency. */
|
|
26
|
+
export const inject = ['webServer']
|
|
27
|
+
|
|
28
|
+
/** Route prefix owned by this plugin. */
|
|
29
|
+
const PREFIX = '/dsh-credential-vault'
|
|
30
|
+
|
|
31
|
+
/** Cap the synced payload so a stray client cannot blow up the Host. Raised for large vaults. */
|
|
32
|
+
const MAX_BODY_BYTES = 16 * 1024 * 1024
|
|
33
|
+
|
|
34
|
+
/** Max entries mirrored from the browser; beyond this the tool output would be unwieldy. */
|
|
35
|
+
const MAX_ENTRIES = 20000
|
|
36
|
+
|
|
37
|
+
/** Fields that hold secrets; masked by default in tool output. */
|
|
38
|
+
const SECRET_FIELDS = ['password', 'apiKey', 'content', 'appSecret', 'token', 'encodingAesKey', 'uploadKey', 'keyValue']
|
|
39
|
+
const MASK = '••••••••'
|
|
40
|
+
|
|
41
|
+
/** Pagination defaults / limits for the read tool. */
|
|
42
|
+
const DEFAULT_LIMIT = 20
|
|
43
|
+
const MAX_LIMIT = 200
|
|
44
|
+
|
|
45
|
+
/** Fields kept in the compact "summary" projection. */
|
|
46
|
+
const SUMMARY_FIELDS = ['id', 'title', 'category', 'username', 'provider', 'expiry', 'updatedAt']
|
|
47
|
+
|
|
48
|
+
/** Current plugin version. Keep in sync with package.json when releasing. */
|
|
49
|
+
const CURRENT_VERSION = '1.5.1'
|
|
50
|
+
|
|
51
|
+
/** Where update checks read the latest published version from (GitHub Releases latest). */
|
|
52
|
+
const RELEASES_API = 'https://api.github.com/repos/mysterycode-liwen/dsh-credential-vault/releases/latest'
|
|
53
|
+
const RELEASES_URL = 'https://github.com/mysterycode-liwen/dsh-credential-vault/releases/latest'
|
|
54
|
+
const UPDATE_CHECK_TIMEOUT_MS = 8000
|
|
55
|
+
const UPDATE_CACHE_MS = 10 * 60 * 1000
|
|
56
|
+
let updateCache = { at: 0, result: null }
|
|
57
|
+
|
|
58
|
+
/** Last metadata-only snapshot received from the browser (never contains secrets). */
|
|
59
|
+
let snapshot = { entries: [], updatedAt: 0 }
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Fields mirrored from the browser. Everything else (especially SECRET_FIELDS)
|
|
63
|
+
* is dropped, so the Host can never hold a plaintext password / key / body.
|
|
64
|
+
*/
|
|
65
|
+
const META_FIELDS = ['id', 'title', 'category', 'username', 'provider', 'keyName', 'url', 'endpoint', 'expiry',
|
|
66
|
+
'appId', 'serverUrl', 'domains', 'notes', 'createdAt', 'updatedAt']
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Force every incoming entry into a metadata-only shape. Applied defensively on
|
|
70
|
+
* POST /entries, so even a buggy or hostile client cannot smuggle a secret into
|
|
71
|
+
* the Host, the tool output, or the audit log.
|
|
72
|
+
*/
|
|
73
|
+
function sanitizeEnvelope(entry) {
|
|
74
|
+
const out = {}
|
|
75
|
+
for (const key of META_FIELDS) {
|
|
76
|
+
const value = entry[key]
|
|
77
|
+
if (typeof value === 'string' || typeof value === 'number') out[key] = value
|
|
78
|
+
}
|
|
79
|
+
// Keep the masked placeholders so the UI shape stays stable for the model.
|
|
80
|
+
for (const key of SECRET_FIELDS) {
|
|
81
|
+
if (typeof entry[key] === 'string' && entry[key] !== '') out[key] = MASK
|
|
82
|
+
}
|
|
83
|
+
return out
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// --- in-memory access audit (no plaintext, bounded) ------------------------
|
|
87
|
+
|
|
88
|
+
const MAX_AUDIT = 200
|
|
89
|
+
/** @type {{at:string, tool:string, action:string, entryId?:string, query?:string, count?:number}[]} */
|
|
90
|
+
const auditLog = []
|
|
91
|
+
function recordAudit(event) {
|
|
92
|
+
auditLog.push(Object.assign({ at: new Date().toISOString() }, event))
|
|
93
|
+
if (auditLog.length > MAX_AUDIT) auditLog.splice(0, auditLog.length - MAX_AUDIT)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --- tiny HTTP helpers (self-contained so the bundle stays 7 files) --------
|
|
97
|
+
|
|
98
|
+
function isLoopback(req) {
|
|
99
|
+
const address = req && req.socket ? req.socket.remoteAddress : undefined
|
|
100
|
+
if (address === undefined) return true
|
|
101
|
+
return address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1' || address.startsWith('127.')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isSameOrigin(req) {
|
|
105
|
+
const origin = req && req.headers ? req.headers.origin : undefined
|
|
106
|
+
if (typeof origin !== 'string' || origin === '') return true
|
|
107
|
+
const host = req && req.headers ? req.headers.host : undefined
|
|
108
|
+
if (typeof host !== 'string' || host === '') return false
|
|
109
|
+
try {
|
|
110
|
+
return new URL(origin).host === host
|
|
111
|
+
} catch {
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function sendJson(res, status, payload) {
|
|
117
|
+
if (!res || res.writableEnded || res.destroyed) return
|
|
118
|
+
const body = JSON.stringify(payload)
|
|
119
|
+
res.writeHead(status, {
|
|
120
|
+
'content-type': 'application/json; charset=utf-8',
|
|
121
|
+
'cache-control': 'no-store',
|
|
122
|
+
'content-length': Buffer.byteLength(body),
|
|
123
|
+
})
|
|
124
|
+
res.end(body)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function fail(res, status, code, message) {
|
|
128
|
+
sendJson(res, status, { ok: false, error: { code, ...(message === undefined ? {} : { message }) } })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function readJson(req, cap = MAX_BODY_BYTES) {
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
const chunks = []
|
|
134
|
+
let size = 0
|
|
135
|
+
req.on('data', (chunk) => {
|
|
136
|
+
size += chunk.length
|
|
137
|
+
if (size > cap) {
|
|
138
|
+
reject(new Error('body too large'))
|
|
139
|
+
req.destroy()
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
chunks.push(chunk)
|
|
143
|
+
})
|
|
144
|
+
req.on('end', () => {
|
|
145
|
+
try {
|
|
146
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
|
|
147
|
+
} catch {
|
|
148
|
+
reject(new Error('invalid json'))
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
req.on('error', reject)
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// --- update check helpers --------------------------------------------------
|
|
156
|
+
|
|
157
|
+
/** Parse a semver-ish tag into comparable parts. Returns null when unparseable. */
|
|
158
|
+
function parseSemver(value) {
|
|
159
|
+
if (typeof value !== 'string') return null
|
|
160
|
+
const m = value.trim().replace(/^v/i, '').match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/)
|
|
161
|
+
if (!m) return null
|
|
162
|
+
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]), prerelease: m[4] || '' }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Compare two parsed semvers: -1 (left older), 0 (equal), 1 (left newer). */
|
|
166
|
+
function compareSemver(l, r) {
|
|
167
|
+
const parts = ['major', 'minor', 'patch']
|
|
168
|
+
for (const key of parts) {
|
|
169
|
+
if (l[key] !== r[key]) return l[key] < r[key] ? -1 : 1
|
|
170
|
+
}
|
|
171
|
+
if (!l.prerelease && !r.prerelease) return 0
|
|
172
|
+
if (!l.prerelease) return 1
|
|
173
|
+
if (!r.prerelease) return -1
|
|
174
|
+
return l.prerelease < r.prerelease ? -1 : l.prerelease > r.prerelease ? 1 : 0
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Fetch the latest Release tag from GitHub, with a timeout and short cache. */
|
|
178
|
+
async function checkForUpdate() {
|
|
179
|
+
if (updateCache.result && Date.now() - updateCache.at < UPDATE_CACHE_MS) {
|
|
180
|
+
return Object.assign({}, updateCache.result, { cached: true })
|
|
181
|
+
}
|
|
182
|
+
const fetchImpl = typeof globalThis.fetch === 'function' ? globalThis.fetch.bind(globalThis) : undefined
|
|
183
|
+
if (!fetchImpl) throw new Error('fetch is not available on this Node runtime')
|
|
184
|
+
let signal
|
|
185
|
+
try { signal = AbortSignal.timeout(UPDATE_CHECK_TIMEOUT_MS) } catch { /* no timeout support */ }
|
|
186
|
+
const response = await fetchImpl(RELEASES_API, {
|
|
187
|
+
headers: { accept: 'application/vnd.github+json', 'user-agent': 'dsh-credential-vault-update-check' },
|
|
188
|
+
...(signal ? { signal } : {}),
|
|
189
|
+
})
|
|
190
|
+
if (!response.ok) throw new Error('GitHub API returned ' + response.status)
|
|
191
|
+
const body = await response.json()
|
|
192
|
+
const tag = body && typeof body.tag_name === 'string' ? body.tag_name : ''
|
|
193
|
+
const latest = parseSemver(tag)
|
|
194
|
+
if (!latest) throw new Error('Latest release tag is not a valid version: ' + tag)
|
|
195
|
+
const current = parseSemver(CURRENT_VERSION) || { major: 0, minor: 0, patch: 0, prerelease: '' }
|
|
196
|
+
const result = {
|
|
197
|
+
ok: true,
|
|
198
|
+
currentVersion: CURRENT_VERSION,
|
|
199
|
+
latestVersion: tag.replace(/^v/i, ''),
|
|
200
|
+
updateAvailable: compareSemver(current, latest) < 0,
|
|
201
|
+
releaseUrl: RELEASES_URL,
|
|
202
|
+
checkedAt: new Date().toISOString(),
|
|
203
|
+
cached: false,
|
|
204
|
+
}
|
|
205
|
+
updateCache = { at: Date.now(), result }
|
|
206
|
+
return result
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// --- HTTP surface ----------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
async function handle(req, res) {
|
|
212
|
+
try {
|
|
213
|
+
if (!isLoopback(req)) return fail(res, 403, 'NOT_LOOPBACK')
|
|
214
|
+
if (!isSameOrigin(req)) return fail(res, 403, 'BAD_ORIGIN')
|
|
215
|
+
|
|
216
|
+
let pathname = (req.url || '/').split('?')[0]
|
|
217
|
+
if (pathname.startsWith(PREFIX)) pathname = pathname.slice(PREFIX.length) || '/'
|
|
218
|
+
const method = (req.method || 'GET').toUpperCase()
|
|
219
|
+
|
|
220
|
+
// Browser -> Host: replace the snapshot with the browser's current vault.
|
|
221
|
+
// Every entry is forced through sanitizeEnvelope(), so secrets never enter
|
|
222
|
+
// this process even if the browser sends a full (legacy) payload.
|
|
223
|
+
if (pathname === '/entries' && method === 'POST') {
|
|
224
|
+
let body
|
|
225
|
+
try {
|
|
226
|
+
body = await readJson(req)
|
|
227
|
+
} catch (error) {
|
|
228
|
+
return fail(res, 400, 'BAD_JSON', String(error && error.message ? error.message : error))
|
|
229
|
+
}
|
|
230
|
+
const list = body && Array.isArray(body.entries) ? body.entries : null
|
|
231
|
+
if (!list) return fail(res, 400, 'BAD_ENTRIES', 'expected { entries: [] }')
|
|
232
|
+
// Keep only plain objects, cap the count, then strip all secret fields.
|
|
233
|
+
const entries = list
|
|
234
|
+
.filter((e) => e !== null && typeof e === 'object' && !Array.isArray(e))
|
|
235
|
+
.slice(0, MAX_ENTRIES)
|
|
236
|
+
.map(sanitizeEnvelope)
|
|
237
|
+
snapshot = { entries, updatedAt: Date.now() }
|
|
238
|
+
return sendJson(res, 200, { ok: true, count: entries.length, truncated: list.length > entries.length })
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Debug / inspection endpoint removed: it used to return a page of the raw
|
|
242
|
+
// snapshot. It is not needed by the browser and only widened the read
|
|
243
|
+
// surface. The dsh_credential_vault_read tool is the sanctioned read path.
|
|
244
|
+
if (pathname === '/entries' && method === 'GET') {
|
|
245
|
+
return fail(res, 404, 'NOT_FOUND')
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// In-memory access audit (same-origin + loopback already enforced above).
|
|
249
|
+
// Contains no plaintext — only tool names, actions, ids and counts.
|
|
250
|
+
if (pathname === '/audit' && method === 'GET') {
|
|
251
|
+
return sendJson(res, 200, { ok: true, total: auditLog.length, events: auditLog.slice() })
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Check GitHub Releases latest for a newer plugin version.
|
|
255
|
+
if (pathname === '/update-check' && method === 'GET') {
|
|
256
|
+
try {
|
|
257
|
+
return sendJson(res, 200, await checkForUpdate())
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return sendJson(res, 200, {
|
|
260
|
+
ok: false,
|
|
261
|
+
currentVersion: CURRENT_VERSION,
|
|
262
|
+
error: String(error && error.message ? error.message : error),
|
|
263
|
+
checkedAt: new Date().toISOString(),
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return fail(res, 404, 'NOT_FOUND')
|
|
269
|
+
} catch (error) {
|
|
270
|
+
return fail(res, 500, 'INTERNAL', String(error && error.message ? error.message : error))
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// --- tool projection helpers ----------------------------------------------
|
|
275
|
+
|
|
276
|
+
function projectEntry(entry, mode) {
|
|
277
|
+
const keys = mode === 'summary' ? SUMMARY_FIELDS : Object.keys(entry)
|
|
278
|
+
const out = {}
|
|
279
|
+
for (const key of keys) {
|
|
280
|
+
const value = entry[key]
|
|
281
|
+
if (value === null || value === undefined || value === '') continue
|
|
282
|
+
// Secret fields in the snapshot are already MASK placeholders (the Host
|
|
283
|
+
// never receives plaintext). Guard here too, defense-in-depth.
|
|
284
|
+
out[key] = SECRET_FIELDS.includes(key) ? MASK : value
|
|
285
|
+
}
|
|
286
|
+
return out
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function matches(entry, query, category) {
|
|
290
|
+
if (category && category !== 'all' && entry.category !== category) return false
|
|
291
|
+
if (!query) return true
|
|
292
|
+
const q = query.toLowerCase()
|
|
293
|
+
const haystack = [entry.title, entry.username, entry.provider, entry.url, entry.endpoint, entry.notes]
|
|
294
|
+
.filter((v) => typeof v === 'string')
|
|
295
|
+
.join(' ')
|
|
296
|
+
.toLowerCase()
|
|
297
|
+
return haystack.includes(q)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
302
|
+
*/
|
|
303
|
+
export function apply(ctx) {
|
|
304
|
+
ctx.effect(() => {
|
|
305
|
+
const dispose = ctx.webServer.register({
|
|
306
|
+
kind: 'prefix',
|
|
307
|
+
path: PREFIX,
|
|
308
|
+
handler: (req, res) => handle(req, res),
|
|
309
|
+
})
|
|
310
|
+
return () => {
|
|
311
|
+
if (typeof dispose === 'function') dispose()
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
// Tools / systemPrompt are optional services: read them defensively so the
|
|
316
|
+
// plugin still loads where they are unavailable.
|
|
317
|
+
const tools = typeof ctx.get === 'function' ? ctx.get('tools') : undefined
|
|
318
|
+
if (tools !== undefined && tools !== null && typeof tools.register === 'function') {
|
|
319
|
+
ctx.effect(() => {
|
|
320
|
+
const dispose = tools.register(
|
|
321
|
+
{
|
|
322
|
+
name: 'dsh_credential_vault_read',
|
|
323
|
+
description:
|
|
324
|
+
'Read metadata about credentials in the user\'s local vault (accounts, API keys, other entries stored in the sidebar vault panel). Results are paginated: use limit/offset to page through large vaults, or mode="summary" for a lightweight id/title/category list first. Secret values (passwords / API keys / text bodies) are ALWAYS masked — plaintext never leaves the browser and cannot be returned by this tool; the revealSecrets parameter is accepted for compatibility but has no effect. Use this to discover which credentials exist; to actually use a secret, ask the user to open the vault panel and copy it.',
|
|
325
|
+
parameters: {
|
|
326
|
+
type: 'object',
|
|
327
|
+
properties: {
|
|
328
|
+
query: {
|
|
329
|
+
type: 'string',
|
|
330
|
+
description: 'Optional text to filter by title, account, provider, URL or notes (case-insensitive substring).',
|
|
331
|
+
},
|
|
332
|
+
category: {
|
|
333
|
+
type: 'string',
|
|
334
|
+
enum: ['all', 'account', 'apikey', 'secretKey', 'other', 'wechatMp', 'wechatMini'],
|
|
335
|
+
description: 'Only return entries of this category. Defaults to all.',
|
|
336
|
+
},
|
|
337
|
+
mode: {
|
|
338
|
+
type: 'string',
|
|
339
|
+
enum: ['summary', 'full'],
|
|
340
|
+
description: 'summary = only id/title/category/account/provider/expiry (lightweight, recommended for browsing); full = all metadata fields (secrets always masked). Defaults to full.',
|
|
341
|
+
},
|
|
342
|
+
limit: {
|
|
343
|
+
type: 'number',
|
|
344
|
+
description: 'Page size, 1-200. Defaults to 20. Use with offset to page through large vaults.',
|
|
345
|
+
},
|
|
346
|
+
offset: {
|
|
347
|
+
type: 'number',
|
|
348
|
+
description: 'How many matching entries to skip. Defaults to 0. Increase by limit to get the next page.',
|
|
349
|
+
},
|
|
350
|
+
revealSecrets: {
|
|
351
|
+
type: 'boolean',
|
|
352
|
+
description: 'Deprecated / no-op. Secret values are always masked and are never returned by this tool. Accepted only so older tool calls keep working.',
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
additionalProperties: false,
|
|
356
|
+
},
|
|
357
|
+
output: {
|
|
358
|
+
schema: {
|
|
359
|
+
type: 'object',
|
|
360
|
+
properties: {
|
|
361
|
+
total: { type: 'number' },
|
|
362
|
+
returned: { type: 'number' },
|
|
363
|
+
hasMore: { type: 'boolean' },
|
|
364
|
+
entries: { type: 'array', items: { type: 'object' } },
|
|
365
|
+
},
|
|
366
|
+
additionalProperties: true,
|
|
367
|
+
},
|
|
368
|
+
render: () => [{ type: 'text', text: 'dsh_credential_vault_read' }],
|
|
369
|
+
},
|
|
370
|
+
async execute(args) {
|
|
371
|
+
const a = args !== null && typeof args === 'object' ? args : {}
|
|
372
|
+
const query = typeof a.query === 'string' ? a.query : ''
|
|
373
|
+
const category = typeof a.category === 'string' ? a.category : 'all'
|
|
374
|
+
const mode = a.mode === 'summary' ? 'summary' : 'full'
|
|
375
|
+
// revealSecrets is intentionally ignored: plaintext never reaches
|
|
376
|
+
// the Host, so there is nothing to reveal. Kept for compatibility.
|
|
377
|
+
const limit = Math.max(1, Math.min(MAX_LIMIT, Number.isFinite(a.limit) ? Math.floor(a.limit) : DEFAULT_LIMIT))
|
|
378
|
+
const offset = Math.max(0, Number.isFinite(a.offset) ? Math.floor(a.offset) : 0)
|
|
379
|
+
const all = Array.isArray(snapshot.entries) ? snapshot.entries : []
|
|
380
|
+
const filtered = all.filter((e) => matches(e, query, category))
|
|
381
|
+
const page = filtered.slice(offset, offset + limit)
|
|
382
|
+
recordAudit({ tool: 'dsh_credential_vault_read', action: 'read', category, query: query || undefined, count: page.length })
|
|
383
|
+
return {
|
|
384
|
+
total: filtered.length,
|
|
385
|
+
returned: page.length,
|
|
386
|
+
offset,
|
|
387
|
+
limit,
|
|
388
|
+
hasMore: offset + page.length < filtered.length,
|
|
389
|
+
updatedAt: snapshot.updatedAt,
|
|
390
|
+
entries: page.map((e) => projectEntry(e, mode)),
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
{},
|
|
395
|
+
)
|
|
396
|
+
return typeof dispose === 'function' ? dispose : () => {}
|
|
397
|
+
}, 'dsh-credential-vault-read-tool')
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const systemPrompt = typeof ctx.get === 'function' ? ctx.get('systemPrompt') : undefined
|
|
401
|
+
if (systemPrompt !== undefined && systemPrompt !== null && typeof systemPrompt.section === 'function') {
|
|
402
|
+
ctx.effect(() => {
|
|
403
|
+
systemPrompt.section({
|
|
404
|
+
name: 'tool:dsh-credential-vault',
|
|
405
|
+
order: 118,
|
|
406
|
+
text:
|
|
407
|
+
'凭据保险箱:用户把账号密码、API Key、微信公众号 / 小程序开发信息、通用密钥、恢复码等保存在侧边栏「凭据保险箱」里。需要知道有哪些凭据时用 dsh_credential_vault_read 工具(按 query / category 过滤,category 可选 account / apikey / secretKey / other / wechatMp / wechatMini;数据多时先用 mode="summary" 看标题清单,再用 limit/offset 翻页)。出于安全,该工具只返回元数据——密码 / API Key / 密钥值 / AppSecret / Token / 上传密钥等一律显示为 ••••••••,明文只留在浏览器内、不会进入模型上下文,revealSecrets 参数已失效。不要向用户索要明文密钥;需要用到具体密钥时,请让用户自行在侧边栏面板中查看或复制。',
|
|
408
|
+
})
|
|
409
|
+
return () => {}
|
|
410
|
+
}, 'dsh-credential-vault-prompt')
|
|
411
|
+
}
|
|
412
|
+
}
|