@goodandready/dsh-messenger-gateway 0.4.17 → 0.4.18
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/lib/client.js +1 -1
- package/lib/http.js +55 -5
- package/lib/index.js +3 -2
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -612,7 +612,7 @@ window.__ModuleLoader__.load({
|
|
|
612
612
|
|
|
613
613
|
const loadStatus = React.useCallback(async () => {
|
|
614
614
|
try {
|
|
615
|
-
const res = await fetch(`${ROUTE_PREFIX}/status`, { cache: 'no-store', signal: timeoutSignal(10000) })
|
|
615
|
+
const res = await fetch(`${ROUTE_PREFIX}/status`, { credentials: 'same-origin', cache: 'no-store', signal: timeoutSignal(10000) })
|
|
616
616
|
const data = await res.json().catch(() => ({}))
|
|
617
617
|
if (res.ok && data.ok) {
|
|
618
618
|
setStatus(data)
|
package/lib/http.js
CHANGED
|
@@ -36,6 +36,44 @@ export function isLoopback(value) {
|
|
|
36
36
|
|| address.startsWith('::ffff:127.')
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
function isPrivateIPv4(address) {
|
|
40
|
+
const parts = address.split('.')
|
|
41
|
+
if (parts.length !== 4) return false
|
|
42
|
+
for (const p of parts) {
|
|
43
|
+
if (!/^\d{1,3}$/.test(p)) return false
|
|
44
|
+
const n = Number(p)
|
|
45
|
+
if (n < 0 || n > 255) return false
|
|
46
|
+
}
|
|
47
|
+
const o0 = Number(parts[0])
|
|
48
|
+
const o1 = Number(parts[1])
|
|
49
|
+
if (o0 === 10) return true
|
|
50
|
+
if (o0 === 192 && o1 === 168) return true
|
|
51
|
+
if (o0 === 172 && o1 >= 16 && o1 <= 31) return true
|
|
52
|
+
if (o0 === 169 && o1 === 254) return true
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isPrivateAddress(value) {
|
|
57
|
+
let address = String(value || '').toLowerCase().replace(/^\[|\]$/g, '')
|
|
58
|
+
if (address.startsWith('::ffff:')) {
|
|
59
|
+
address = address.slice('::ffff:'.length)
|
|
60
|
+
}
|
|
61
|
+
if (isLoopback(address)) return true
|
|
62
|
+
if (isPrivateIPv4(address)) return true
|
|
63
|
+
|
|
64
|
+
if (address.includes(':')) {
|
|
65
|
+
const firstHextet = address.split(':')[0]
|
|
66
|
+
const num = Number.parseInt(firstHextet, 16)
|
|
67
|
+
if (!Number.isNaN(num)) {
|
|
68
|
+
if ((num & 0xfe00) === 0xfc00) return true // fc00::/7 ULA (RFC 4193)
|
|
69
|
+
if ((num & 0xffc0) === 0xfe80) return true // fe80::/10 Link-Local (RFC 4291)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return false
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const isLoopbackOrLan = isPrivateAddress
|
|
76
|
+
|
|
39
77
|
export function isTrustedSettingsRequest(req, expectedSecret) {
|
|
40
78
|
const remote = req?.socket?.remoteAddress
|
|
41
79
|
const host = String(req?.headers?.host || '')
|
|
@@ -55,7 +93,14 @@ export function isTrustedSettingsRequest(req, expectedSecret) {
|
|
|
55
93
|
}
|
|
56
94
|
}
|
|
57
95
|
|
|
58
|
-
|
|
96
|
+
// Reject non-private / WAN requests without secret
|
|
97
|
+
if (remote !== undefined && !isPrivateAddress(remote)) {
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// If host is loopback (localhost/127.0.0.1/::1), remote address must also be loopback
|
|
102
|
+
const hostName = host.split(':')[0].toLowerCase().replace(/^\[|\]$/g, '')
|
|
103
|
+
if (isLoopback(hostName) && remote !== undefined && !isLoopback(remote)) {
|
|
59
104
|
return false
|
|
60
105
|
}
|
|
61
106
|
|
|
@@ -82,7 +127,9 @@ export function isTrustedSettingsRequest(req, expectedSecret) {
|
|
|
82
127
|
}
|
|
83
128
|
|
|
84
129
|
if (site === 'same-origin') {
|
|
85
|
-
|
|
130
|
+
if (remote === undefined || isLoopback(remote)) {
|
|
131
|
+
return true
|
|
132
|
+
}
|
|
86
133
|
}
|
|
87
134
|
|
|
88
135
|
return isLoopback(remote)
|
|
@@ -93,8 +140,6 @@ export function isAuthorizedMessengerRequest(req, webhooksConfig) {
|
|
|
93
140
|
if (site === 'cross-site') return false
|
|
94
141
|
|
|
95
142
|
const secret = String(webhooksConfig?.secret || '').trim()
|
|
96
|
-
if (isTrustedSettingsRequest(req, secret)) return true
|
|
97
|
-
if (isLoopback(req?.socket?.remoteAddress)) return true
|
|
98
143
|
if (secret) {
|
|
99
144
|
const authHeader = req?.headers?.authorization || ''
|
|
100
145
|
const bearer = authHeader.startsWith('Bearer ') ? authHeader.slice(7).trim() : ''
|
|
@@ -105,7 +150,12 @@ export function isAuthorizedMessengerRequest(req, webhooksConfig) {
|
|
|
105
150
|
}
|
|
106
151
|
}
|
|
107
152
|
|
|
108
|
-
|
|
153
|
+
const remote = req?.socket?.remoteAddress
|
|
154
|
+
if (remote !== undefined && !isLoopback(remote)) {
|
|
155
|
+
return false
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return isTrustedSettingsRequest(req, secret) || isLoopback(remote)
|
|
109
159
|
}
|
|
110
160
|
|
|
111
161
|
export function timingSafeCompare(a, b) {
|
package/lib/index.js
CHANGED
|
@@ -458,6 +458,7 @@ export function apply(ctx, config) {
|
|
|
458
458
|
kind: 'exact', path: '/dsh-messenger-gateway/status',
|
|
459
459
|
handler: async (req, res) => {
|
|
460
460
|
if (req.method !== 'GET') return writeJson(res, 405, { ok: false, error: 'GET only' })
|
|
461
|
+
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
461
462
|
const gw = getGw()
|
|
462
463
|
const botInfo = gw?.getBotInfo?.() || {}
|
|
463
464
|
const uptimeSec = gw?.stats?.startedAt ? Math.max(0, Math.round((Date.now() - gw.stats.startedAt) / 1000)) : 0
|
|
@@ -571,9 +572,9 @@ export function apply(ctx, config) {
|
|
|
571
572
|
ctx.effect(() => getWebServer().register({
|
|
572
573
|
kind: 'exact', path: '/dsh-messenger-gateway/config',
|
|
573
574
|
handler: async (req, res) => {
|
|
574
|
-
if (req.method
|
|
575
|
-
if (req.method !== 'PUT') return writeJson(res, 405, { ok: false, error: 'GET or PUT' })
|
|
575
|
+
if (req.method !== 'GET' && req.method !== 'PUT') return writeJson(res, 405, { ok: false, error: 'GET or PUT' })
|
|
576
576
|
if (!isTrustedSettingsRequest(req, source().webhooks?.secret)) return writeJson(res, 403, { ok: false, error: 'forbidden' })
|
|
577
|
+
if (req.method === 'GET') return writeJson(res, 200, { ok: true, config: publicConfig(source(), getGw(), ctx) })
|
|
577
578
|
if (!settingsApi?.isReady?.()) return writeJson(res, 503, { ok: false, error: 'settings not ready' })
|
|
578
579
|
const parsedConfig = await readJsonSafe(req, res, 1_048_576)
|
|
579
580
|
if (!parsedConfig.ok) return
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-messenger-gateway",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.18",
|
|
4
4
|
"description": "Telegram messenger bridge for DeepSeek Harness: sessions, steer, homes, inline asks, notify bridge, and optional TTS voice notes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|