@luosuqwq/dsh-diagram 0.1.5 → 0.1.6
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/index.js +32 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
// image_annotate existing image -> PNG with boxes/arrows/labels
|
|
8
8
|
// and a `diagram_guide` tool that hands over the style guide on demand.
|
|
9
9
|
|
|
10
|
-
import { existsSync, mkdirSync, readFileSync, statSync } from 'node:fs'
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
12
|
+
import { homedir } from 'node:os'
|
|
11
13
|
import { readFile } from 'node:fs/promises'
|
|
12
14
|
import path from 'node:path'
|
|
13
15
|
import { fileURLToPath } from 'node:url'
|
|
@@ -20,8 +22,32 @@ export const name = 'dsh-diagram'
|
|
|
20
22
|
export const inject = ['tools', 'webServer', 'systemPrompt']
|
|
21
23
|
|
|
22
24
|
const ROUTE = '/plugins/dsh-diagram/asset'
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
+
// URLs carry an HMAC over the absolute path, so only files this plugin handed
|
|
26
|
+
// out can be fetched, while previously returned links keep working after a
|
|
27
|
+
// restart. The key is created once under the DSH home.
|
|
28
|
+
let assetKeyCache = null
|
|
29
|
+
function assetKey() {
|
|
30
|
+
if (assetKeyCache !== null) return assetKeyCache
|
|
31
|
+
const dir = path.join(homedir(), '.dsh', 'plugins', 'dsh-diagram')
|
|
32
|
+
const file = path.join(dir, 'asset.key')
|
|
33
|
+
try {
|
|
34
|
+
const value = readFileSync(file, 'utf8').trim()
|
|
35
|
+
if (value.length >= 32) return (assetKeyCache = value)
|
|
36
|
+
} catch {}
|
|
37
|
+
mkdirSync(dir, { recursive: true })
|
|
38
|
+
assetKeyCache = randomBytes(32).toString('hex')
|
|
39
|
+
writeFileSync(file, assetKeyCache, { mode: 0o600 })
|
|
40
|
+
return assetKeyCache
|
|
41
|
+
}
|
|
42
|
+
function signAssetPath(file) {
|
|
43
|
+
return createHmac('sha256', assetKey()).update(path.resolve(file).toLowerCase()).digest('hex').slice(0, 32)
|
|
44
|
+
}
|
|
45
|
+
function assetSignatureValid(file, provided) {
|
|
46
|
+
if (typeof provided !== 'string' || provided.length !== 32) return false
|
|
47
|
+
const expected = Buffer.from(signAssetPath(file))
|
|
48
|
+
const actual = Buffer.from(provided)
|
|
49
|
+
return expected.length === actual.length && timingSafeEqual(expected, actual)
|
|
50
|
+
}
|
|
25
51
|
const OUTPUT_DIR = ['.dsh', 'diagram']
|
|
26
52
|
const MIME = new Map([
|
|
27
53
|
['.png', 'image/png'],
|
|
@@ -93,11 +119,10 @@ function origin(ctx) {
|
|
|
93
119
|
function assetMarkdown(ctx, file) {
|
|
94
120
|
let version = '0'
|
|
95
121
|
try { version = String(Math.round(statSync(file).mtimeMs)) } catch {}
|
|
96
|
-
return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
|
|
122
|
+
return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&s=${signAssetPath(file)}&v=${version}`
|
|
97
123
|
}
|
|
98
124
|
|
|
99
125
|
function imageResult(ctx, info, alt) {
|
|
100
|
-
SERVED_ASSETS.add(path.resolve(info.path))
|
|
101
126
|
const url = assetMarkdown(ctx, info.path)
|
|
102
127
|
return stripUndefined({
|
|
103
128
|
path: info.path,
|
|
@@ -130,8 +155,9 @@ function serveAsset(req, res) {
|
|
|
130
155
|
try {
|
|
131
156
|
const requestUrl = new URL(req.url ?? ROUTE, 'http://127.0.0.1')
|
|
132
157
|
const file = requestUrl.searchParams.get('file')
|
|
158
|
+
const signature = requestUrl.searchParams.get('s')
|
|
133
159
|
const contentType = MIME.get(path.extname(file ?? '').toLowerCase())
|
|
134
|
-
if (!file || !
|
|
160
|
+
if (!file || !assetSignatureValid(file, signature) || !contentType || !existsSync(file) || !statSync(file).isFile()) {
|
|
135
161
|
res.writeHead(404)
|
|
136
162
|
res.end()
|
|
137
163
|
return
|
package/package.json
CHANGED