surfcad-mcp 0.2.0 → 0.2.2
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 +65 -8
- package/data/commands.json +1996 -301
- package/package.json +1 -1
- package/src/bridge/bridgePage.mjs +132 -0
- package/src/bridge/bridgePopup.mjs +261 -0
- package/src/bridge/localAddress.mjs +1 -1
- package/src/bridge/relayHub.mjs +282 -34
- package/src/bridge/relayServer.mjs +81 -13
- package/src/catalog.mjs +10 -0
- package/src/errors.mjs +24 -1
- package/src/nodeClient.mjs +27 -6
- package/src/prompt/promptSession.mjs +450 -0
- package/src/prompt/promptTools.mjs +152 -0
- package/src/prompt/webhookPost.mjs +111 -0
- package/src/prompt/webhookUrl.mjs +206 -0
- package/src/relay.mjs +319 -59
- package/src/relayMessages.mjs +8 -0
- package/src/screenshotFile.mjs +232 -0
- package/src/server.mjs +95 -19
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* POST a prompt to a registered webhook.
|
|
3
|
+
* Times out after 10s and retries once. Does not follow redirects.
|
|
4
|
+
* `insecureTls` is honored only when the caller already decided the test
|
|
5
|
+
* switch applies (loopback target, and not NODE_ENV=production).
|
|
6
|
+
*/
|
|
7
|
+
import https from 'node:https';
|
|
8
|
+
import { isIP } from 'node:net';
|
|
9
|
+
|
|
10
|
+
export const WEBHOOK_TIMEOUT_MS = 10_000;
|
|
11
|
+
export const WEBHOOK_ATTEMPTS = 2;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Pin the connection to the address we already allowed, so DNS cannot
|
|
15
|
+
* rebind to a private address between the check and the POST.
|
|
16
|
+
* @param {string} address
|
|
17
|
+
*/
|
|
18
|
+
export function pinLookup(address) {
|
|
19
|
+
const family = isIP(address) === 6 ? 6 : 4;
|
|
20
|
+
return (_hostname, options, callback) => {
|
|
21
|
+
const cb = typeof options === 'function' ? options : callback;
|
|
22
|
+
const all = typeof options === 'object' && options?.all === true;
|
|
23
|
+
if (all) cb(null, [{ address, family }]);
|
|
24
|
+
else cb(null, address, family);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} url
|
|
30
|
+
* @param {Buffer} payload
|
|
31
|
+
* @param {{ timeoutMs: number, insecureTls: boolean, pinAddress?: string }} opts
|
|
32
|
+
*/
|
|
33
|
+
function defaultHttpsPost(url, payload, opts) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
let parsed;
|
|
36
|
+
try {
|
|
37
|
+
parsed = new URL(url);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
reject(err);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (parsed.protocol !== 'https:') {
|
|
43
|
+
reject(new Error('https required'));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const requestOptions = {
|
|
47
|
+
protocol: 'https:',
|
|
48
|
+
hostname: parsed.hostname,
|
|
49
|
+
port: parsed.port || 443,
|
|
50
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: {
|
|
53
|
+
'content-type': 'application/json',
|
|
54
|
+
'content-length': String(payload.length),
|
|
55
|
+
accept: 'application/json',
|
|
56
|
+
},
|
|
57
|
+
rejectUnauthorized: opts.insecureTls !== true,
|
|
58
|
+
servername: parsed.hostname,
|
|
59
|
+
timeout: opts.timeoutMs,
|
|
60
|
+
};
|
|
61
|
+
if (opts.pinAddress) requestOptions.lookup = pinLookup(opts.pinAddress);
|
|
62
|
+
const req = https.request(requestOptions, (res) => {
|
|
63
|
+
res.resume();
|
|
64
|
+
const code = res.statusCode || 0;
|
|
65
|
+
if (code >= 200 && code < 300) resolve({ ok: true });
|
|
66
|
+
else resolve({ ok: false, error: 'webhook_failed' });
|
|
67
|
+
});
|
|
68
|
+
req.on('timeout', () => {
|
|
69
|
+
req.destroy();
|
|
70
|
+
const err = new Error('webhook timeout');
|
|
71
|
+
err.name = 'TimeoutError';
|
|
72
|
+
reject(err);
|
|
73
|
+
});
|
|
74
|
+
req.on('error', reject);
|
|
75
|
+
req.end(payload);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {string} url
|
|
81
|
+
* @param {unknown} body
|
|
82
|
+
* @param {{
|
|
83
|
+
* timeoutMs?: number,
|
|
84
|
+
* attempts?: number,
|
|
85
|
+
* insecureTls?: boolean,
|
|
86
|
+
* pinAddress?: string,
|
|
87
|
+
* request?: typeof defaultHttpsPost,
|
|
88
|
+
* }} [options]
|
|
89
|
+
*/
|
|
90
|
+
export async function postPromptWebhook(url, body, options = {}) {
|
|
91
|
+
const timeoutMs = options.timeoutMs ?? WEBHOOK_TIMEOUT_MS;
|
|
92
|
+
const attempts = options.attempts ?? WEBHOOK_ATTEMPTS;
|
|
93
|
+
const request = options.request || defaultHttpsPost;
|
|
94
|
+
const payload = Buffer.from(JSON.stringify(body));
|
|
95
|
+
const opts = {
|
|
96
|
+
timeoutMs,
|
|
97
|
+
insecureTls: options.insecureTls === true,
|
|
98
|
+
pinAddress: options.pinAddress,
|
|
99
|
+
};
|
|
100
|
+
let lastError = 'webhook_failed';
|
|
101
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
102
|
+
try {
|
|
103
|
+
const out = await request(url, payload, opts);
|
|
104
|
+
if (out?.ok === true) return { ok: true };
|
|
105
|
+
lastError = 'webhook_failed';
|
|
106
|
+
} catch (err) {
|
|
107
|
+
lastError = err?.name === 'TimeoutError' ? 'webhook_timeout' : 'webhook_failed';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { ok: false, error: lastError };
|
|
111
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webhook URL checks for in-viewer prompts.
|
|
3
|
+
* The URL stays in process memory. Callers must not log it and must not write it to disk.
|
|
4
|
+
* `SURFCAD_WEBHOOK_TLS_INSECURE=1` is a test switch: it allows a loopback target and,
|
|
5
|
+
* for that loopback target only, a self-signed certificate. `NODE_ENV=production`
|
|
6
|
+
* ignores the switch, so production keeps rejecting localhost and private addresses.
|
|
7
|
+
*/
|
|
8
|
+
import { lookup } from 'node:dns/promises';
|
|
9
|
+
import { BlockList, isIP } from 'node:net';
|
|
10
|
+
|
|
11
|
+
export const WEBHOOK_TLS_INSECURE_ENV = 'SURFCAD_WEBHOOK_TLS_INSECURE';
|
|
12
|
+
|
|
13
|
+
const block = new BlockList();
|
|
14
|
+
for (const [addr, prefix] of [
|
|
15
|
+
['0.0.0.0', 8],
|
|
16
|
+
['10.0.0.0', 8],
|
|
17
|
+
['127.0.0.0', 8],
|
|
18
|
+
['169.254.0.0', 16],
|
|
19
|
+
['172.16.0.0', 12],
|
|
20
|
+
['192.0.0.0', 24],
|
|
21
|
+
['192.0.2.0', 24],
|
|
22
|
+
['192.168.0.0', 16],
|
|
23
|
+
['198.18.0.0', 15],
|
|
24
|
+
['198.51.100.0', 24],
|
|
25
|
+
['203.0.113.0', 24],
|
|
26
|
+
['100.64.0.0', 10],
|
|
27
|
+
['224.0.0.0', 4],
|
|
28
|
+
['240.0.0.0', 4],
|
|
29
|
+
]) {
|
|
30
|
+
block.addSubnet(addr, prefix, 'ipv4');
|
|
31
|
+
}
|
|
32
|
+
block.addAddress('::', 'ipv6');
|
|
33
|
+
block.addAddress('::1', 'ipv6');
|
|
34
|
+
block.addSubnet('fc00::', 7, 'ipv6');
|
|
35
|
+
block.addSubnet('fe80::', 10, 'ipv6');
|
|
36
|
+
block.addSubnet('ff00::', 8, 'ipv6');
|
|
37
|
+
block.addSubnet('2001:db8::', 32, 'ipv6');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {NodeJS.ProcessEnv} [env]
|
|
41
|
+
*/
|
|
42
|
+
export function webhookInsecureTlsAllowed(env = process.env) {
|
|
43
|
+
if (!env || env.NODE_ENV === 'production') return false;
|
|
44
|
+
return env[WEBHOOK_TLS_INSECURE_ENV] === '1';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} hostname
|
|
49
|
+
*/
|
|
50
|
+
export function isLoopbackName(hostname) {
|
|
51
|
+
const host = String(hostname || '').toLowerCase().replace(/\.$/, '');
|
|
52
|
+
return host === 'localhost' || host === 'localhost.localdomain' || host.endsWith('.localhost');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {string} ip
|
|
57
|
+
*/
|
|
58
|
+
export function isLoopbackIp(ip) {
|
|
59
|
+
const lower = String(ip || '').toLowerCase();
|
|
60
|
+
if (lower === '::1') return true;
|
|
61
|
+
if (lower.startsWith('::ffff:')) {
|
|
62
|
+
const v4 = lower.slice('::ffff:'.length);
|
|
63
|
+
return isIP(v4) === 4 && v4.startsWith('127.');
|
|
64
|
+
}
|
|
65
|
+
return isIP(lower) === 4 && lower.startsWith('127.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {string} hostname
|
|
70
|
+
*/
|
|
71
|
+
export function isLoopbackHost(hostname) {
|
|
72
|
+
const host = String(hostname || '').replace(/^\[|\]$/g, '');
|
|
73
|
+
return isLoopbackName(host) || isLoopbackIp(host);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {string} hostname
|
|
78
|
+
*/
|
|
79
|
+
function isPrivateName(hostname) {
|
|
80
|
+
const host = String(hostname || '').toLowerCase().replace(/\.$/, '');
|
|
81
|
+
return isLoopbackName(host) || host.endsWith('.local');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {string} hostname
|
|
86
|
+
*/
|
|
87
|
+
function isObfuscatedIp(hostname) {
|
|
88
|
+
if (/^0x[0-9a-f]+$/i.test(hostname)) return true;
|
|
89
|
+
if (/^\d+$/.test(hostname)) return true;
|
|
90
|
+
const parts = hostname.split('.');
|
|
91
|
+
if (parts.length >= 2 && parts.length <= 4 && parts.every((part) => /^(0x[0-9a-f]+|\d+)$/i.test(part))) {
|
|
92
|
+
if (isIP(hostname) !== 4) return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {string} ip
|
|
99
|
+
*/
|
|
100
|
+
function expandedIps(ip) {
|
|
101
|
+
const lower = String(ip || '').toLowerCase();
|
|
102
|
+
const out = [lower];
|
|
103
|
+
if (lower.startsWith('::ffff:')) {
|
|
104
|
+
const v4 = lower.slice('::ffff:'.length);
|
|
105
|
+
if (isIP(v4) === 4) out.push(v4);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {string} ip
|
|
112
|
+
*/
|
|
113
|
+
export function isBlockedIp(ip) {
|
|
114
|
+
for (const candidate of expandedIps(ip)) {
|
|
115
|
+
const kind = isIP(candidate);
|
|
116
|
+
if (kind === 0) return true;
|
|
117
|
+
if (block.check(candidate, kind === 4 ? 'ipv4' : 'ipv6')) return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param {string} message
|
|
124
|
+
*/
|
|
125
|
+
function fail(message) {
|
|
126
|
+
return { ok: false, code: 'bad_webhook_url', message };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {string} hostname
|
|
131
|
+
*/
|
|
132
|
+
export async function defaultResolveHost(hostname) {
|
|
133
|
+
const pending = lookup(hostname, { all: true, verbatim: true });
|
|
134
|
+
let timer;
|
|
135
|
+
try {
|
|
136
|
+
return await Promise.race([
|
|
137
|
+
pending.then((rows) => rows.map((row) => row.address)),
|
|
138
|
+
new Promise((_, reject) => {
|
|
139
|
+
timer = setTimeout(() => reject(new Error('dns_timeout')), 5000);
|
|
140
|
+
}),
|
|
141
|
+
]);
|
|
142
|
+
} finally {
|
|
143
|
+
if (timer) clearTimeout(timer);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {unknown} raw
|
|
149
|
+
* @param {{ allowLoopback?: boolean, resolveHost?: (hostname: string) => Promise<string[]> }} [options]
|
|
150
|
+
* @returns {Promise<{ ok: true, host: string, addresses: string[] } | { ok: false, code: string, message: string }>}
|
|
151
|
+
*/
|
|
152
|
+
export async function validateWebhookUrl(raw, options = {}) {
|
|
153
|
+
if (typeof raw !== 'string') return fail('Webhook URL is required');
|
|
154
|
+
const text = raw.trim();
|
|
155
|
+
if (!text || text.length > 2048 || /\s/.test(text) || text.includes('\0')) {
|
|
156
|
+
return fail(text ? 'Webhook URL is not a valid URL' : 'Webhook URL is required');
|
|
157
|
+
}
|
|
158
|
+
let url;
|
|
159
|
+
try {
|
|
160
|
+
url = new URL(text);
|
|
161
|
+
} catch {
|
|
162
|
+
return fail('Webhook URL is not a valid URL');
|
|
163
|
+
}
|
|
164
|
+
if (url.protocol !== 'https:') return fail('Webhook URL must use https');
|
|
165
|
+
const authority = text.slice(text.indexOf('://') + 3).split('/')[0];
|
|
166
|
+
if (url.username || url.password || authority.includes('@')) {
|
|
167
|
+
return fail('Webhook URL must not include credentials');
|
|
168
|
+
}
|
|
169
|
+
if (url.port === '0') return fail('Webhook URL port is not allowed');
|
|
170
|
+
const hostname = url.hostname.replace(/\.$/, '').toLowerCase();
|
|
171
|
+
if (!hostname || isObfuscatedIp(hostname)) {
|
|
172
|
+
return fail(hostname ? 'Webhook URL must not target a private address' : 'Webhook URL is not a valid URL');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const allowLoopback = options.allowLoopback === true;
|
|
176
|
+
if (isPrivateName(hostname)) {
|
|
177
|
+
if (!(allowLoopback && isLoopbackName(hostname))) {
|
|
178
|
+
return fail('Webhook URL must not target localhost or a private address');
|
|
179
|
+
}
|
|
180
|
+
return { ok: true, host: hostname, addresses: ['127.0.0.1'] };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let addresses;
|
|
184
|
+
if (isIP(hostname) !== 0) {
|
|
185
|
+
addresses = [hostname];
|
|
186
|
+
} else {
|
|
187
|
+
const resolveHost = options.resolveHost || defaultResolveHost;
|
|
188
|
+
try {
|
|
189
|
+
addresses = await resolveHost(hostname);
|
|
190
|
+
} catch {
|
|
191
|
+
return fail('Webhook host could not be resolved');
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!Array.isArray(addresses) || addresses.length === 0) {
|
|
195
|
+
return fail('Webhook host could not be resolved');
|
|
196
|
+
}
|
|
197
|
+
for (const address of addresses) {
|
|
198
|
+
if (typeof address !== 'string' || isIP(address) === 0) {
|
|
199
|
+
return fail('Webhook host could not be resolved');
|
|
200
|
+
}
|
|
201
|
+
if (!isBlockedIp(address)) continue;
|
|
202
|
+
if (allowLoopback && isLoopbackIp(address)) continue;
|
|
203
|
+
return fail('Webhook URL must not target localhost or a private address');
|
|
204
|
+
}
|
|
205
|
+
return { ok: true, host: hostname, addresses };
|
|
206
|
+
}
|