@withone/mcp 2.3.2 → 2.3.3
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 +1 -1
- package/build/client.js +55 -46
- package/build/client.test.js +175 -1
- package/build/helpers.js +53 -3
- package/build/index.js +1 -6
- package/build/path-variables.test.js +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -126,7 +126,7 @@ ONE_IDENTITY_TYPE=user
|
|
|
126
126
|
| `ONE_IDENTITY` | The identifier for the entity (e.g., user ID, team ID) | Any string |
|
|
127
127
|
| `ONE_IDENTITY_TYPE` | The type of identity | `user`, `team`, `organization`, `project` |
|
|
128
128
|
|
|
129
|
-
When set, the MCP server will only return connections associated with the specified identity. This is useful for multi-tenant applications where you want to scope integrations to specific users or entities.
|
|
129
|
+
When set, the MCP server will only return connections associated with the specified identity, and `execute_one_action` will only run on those connections. This is useful for multi-tenant applications where you want to scope integrations to specific users or entities.
|
|
130
130
|
|
|
131
131
|
### Access Control
|
|
132
132
|
|
package/build/client.js
CHANGED
|
@@ -9,10 +9,18 @@
|
|
|
9
9
|
import axios from 'axios';
|
|
10
10
|
import FormData from 'form-data';
|
|
11
11
|
import { callerHeaders } from './caller.js';
|
|
12
|
-
import { fetchPaginatedData, replacePathVariables } from './helpers.js';
|
|
12
|
+
import { describeError, fetchPaginatedData, replacePathVariables, summarizeErrorData } from './helpers.js';
|
|
13
13
|
import { ToolError, oneApiError, oneRefusal } from './tool-error.js';
|
|
14
14
|
/** Header names that only the server may set on an outgoing request. */
|
|
15
15
|
const RESERVED_HEADER_PREFIX = 'x-one-';
|
|
16
|
+
/**
|
|
17
|
+
* Headers that control the connection to One rather than describe the request.
|
|
18
|
+
* Node also takes the TLS server name from `Host`, so a caller must not set it
|
|
19
|
+
* on a request carrying the secret.
|
|
20
|
+
*/
|
|
21
|
+
const TRANSPORT_HEADERS = new Set(['host', 'connection', 'content-length', 'transfer-encoding', 'keep-alive', 'proxy-connection', 'te', 'trailer', 'upgrade']);
|
|
22
|
+
/** Headers an upstream may obey as the method in place of the request line. */
|
|
23
|
+
const METHOD_OVERRIDE_HEADERS = new Set(['x-http-method-override', 'x-http-method', 'x-method-override']);
|
|
16
24
|
/** The prefix One renames every upstream response header to, so its presence marks a response the platform gave. */
|
|
17
25
|
const PLATFORM_HEADER_PREFIX = 'x-passthrough-';
|
|
18
26
|
/**
|
|
@@ -24,58 +32,20 @@ export function answeredByPlatform(headers) {
|
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
26
34
|
* Drops any caller-supplied header that One itself controls (`x-one-secret`,
|
|
27
|
-
* `x-one-connection-key`, `x-one-action-id`, ...)
|
|
28
|
-
*
|
|
35
|
+
* `x-one-connection-key`, `x-one-action-id`, ...) or that controls the
|
|
36
|
+
* connection (`Host`, `Content-Length`, ...). Comparison is case-insensitive
|
|
37
|
+
* because HTTP header names are.
|
|
29
38
|
*/
|
|
30
39
|
export function stripReservedHeaders(headers) {
|
|
31
40
|
if (!headers)
|
|
32
41
|
return {};
|
|
33
42
|
return Object.fromEntries(Object.entries(headers)
|
|
34
|
-
.filter(([name]) =>
|
|
43
|
+
.filter(([name]) => {
|
|
44
|
+
const normalized = name.trim().toLowerCase();
|
|
45
|
+
return !normalized.startsWith(RESERVED_HEADER_PREFIX) && !TRANSPORT_HEADERS.has(normalized);
|
|
46
|
+
})
|
|
35
47
|
.map(([name, value]) => [name, String(value)]));
|
|
36
48
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Bounds an upstream error body before it is logged: small payloads pass
|
|
39
|
-
* through, larger ones are truncated to a string, and an unserializable value
|
|
40
|
-
* is replaced with a placeholder. Keeps a huge or odd error response from
|
|
41
|
-
* bloating a single log line.
|
|
42
|
-
*/
|
|
43
|
-
function summarizeErrorData(data) {
|
|
44
|
-
if (data === undefined || data === null)
|
|
45
|
-
return undefined;
|
|
46
|
-
let serialized;
|
|
47
|
-
try {
|
|
48
|
-
serialized = typeof data === 'string' ? data : JSON.stringify(data);
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
return '[unserializable response body]';
|
|
52
|
-
}
|
|
53
|
-
const MAX = 1000;
|
|
54
|
-
return serialized.length <= MAX ? data : `${serialized.slice(0, MAX)}… (${serialized.length} chars, truncated)`;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* A log-safe summary of a failed request. Axios errors carry the full request
|
|
58
|
-
* config, including the `x-one-secret` header, so they must never be logged
|
|
59
|
-
* whole. Only the status, the upstream message, the route (query stripped), and
|
|
60
|
-
* a bounded copy of the response body are kept.
|
|
61
|
-
*/
|
|
62
|
-
export function describeError(error) {
|
|
63
|
-
if (axios.isAxiosError(error)) {
|
|
64
|
-
const url = error.config?.url;
|
|
65
|
-
return {
|
|
66
|
-
status: error.response?.status,
|
|
67
|
-
statusText: error.response?.statusText,
|
|
68
|
-
method: error.config?.method?.toUpperCase(),
|
|
69
|
-
url: typeof url === 'string' ? url.split('?')[0] : undefined,
|
|
70
|
-
message: error.message,
|
|
71
|
-
data: summarizeErrorData(error.response?.data),
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
if (error instanceof Error) {
|
|
75
|
-
return { name: error.name, message: error.message };
|
|
76
|
-
}
|
|
77
|
-
return { error: String(error) };
|
|
78
|
-
}
|
|
79
49
|
/**
|
|
80
50
|
* Client for interacting with the One API
|
|
81
51
|
*/
|
|
@@ -308,6 +278,13 @@ export class OneClient {
|
|
|
308
278
|
const action = preloadedAction ?? await this.getActionDetails(actionId);
|
|
309
279
|
const method = action.method;
|
|
310
280
|
const contentType = isFormData ? 'multipart/form-data' : isFormUrlEncoded ? 'application/x-www-form-urlencoded' : 'application/json';
|
|
281
|
+
// The local filters judge the action's declared method, so a request must
|
|
282
|
+
// not ask the platform to perform a different one.
|
|
283
|
+
for (const [name, value] of Object.entries(stripReservedHeaders(headers))) {
|
|
284
|
+
if (METHOD_OVERRIDE_HEADERS.has(name.trim().toLowerCase()) && value.trim().toUpperCase() !== method.toUpperCase()) {
|
|
285
|
+
throw new ToolError(`Header ${name.trim()} asks for ${value.trim()}, but this action is a ${method.toUpperCase()} request. Remove the header, or use an action with that method.`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
311
288
|
// Caller-supplied headers are merged first and may adjust things like
|
|
312
289
|
// Content-Type, but they can never override One's own routing and auth
|
|
313
290
|
// headers. Those are applied last, after the connection and action
|
|
@@ -329,6 +306,11 @@ export class OneClient {
|
|
|
329
306
|
const finalActionPath = replacePathVariables(action.path, pathVariables ?? {});
|
|
330
307
|
const normalizedPath = finalActionPath.startsWith('/') ? finalActionPath : `/${finalActionPath}`;
|
|
331
308
|
const url = `${this.baseUrl}/v1/passthrough${normalizedPath}`;
|
|
309
|
+
// This request carries the secret, and the local filters only describe
|
|
310
|
+
// passthrough, so it must not resolve to any other route of the One API.
|
|
311
|
+
if (!new URL(url).pathname.startsWith(new URL(`${this.baseUrl}/v1/passthrough/`).pathname)) {
|
|
312
|
+
throw new ToolError(`Action path ${action.path} does not stay on One's passthrough route, so it was not sent.`);
|
|
313
|
+
}
|
|
332
314
|
let requestData = data;
|
|
333
315
|
if (typeof requestData === 'string') {
|
|
334
316
|
try {
|
|
@@ -425,6 +407,33 @@ export class OneClient {
|
|
|
425
407
|
getUserConnections() {
|
|
426
408
|
return [...this.connections];
|
|
427
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Refuses a connection key outside this server's scope, set by
|
|
412
|
+
* `ONE_CONNECTION_KEYS` or `ONE_IDENTITY`. The list is loaded once per
|
|
413
|
+
* process, so a key it lacks is checked against a fresh copy before being
|
|
414
|
+
* refused: it may be a connection made since, or the first load may have failed.
|
|
415
|
+
* @param connectionKey - The key the caller asked to run on
|
|
416
|
+
* @throws {ToolError} If the key is out of scope, or the list cannot be read
|
|
417
|
+
*/
|
|
418
|
+
async ensureConnectionInScope(connectionKey) {
|
|
419
|
+
const keysRestricted = this.connectionKeys !== undefined && !this.connectionKeys.includes('*');
|
|
420
|
+
if (!keysRestricted && !this.identity)
|
|
421
|
+
return;
|
|
422
|
+
const isLoaded = () => this.connections.some((c) => c.key === connectionKey);
|
|
423
|
+
if (isLoaded())
|
|
424
|
+
return;
|
|
425
|
+
const loaded = this.connections;
|
|
426
|
+
try {
|
|
427
|
+
await this.fetchConnections();
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
this.connections = loaded;
|
|
431
|
+
throw oneApiError('Could not confirm this connection is available to this server', error);
|
|
432
|
+
}
|
|
433
|
+
if (!isLoaded()) {
|
|
434
|
+
throw new ToolError(`Connection key "${connectionKey}" is not one this server may use. Use a connection key from list_one_integrations.`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
428
437
|
/**
|
|
429
438
|
* Gets available connectors/platforms
|
|
430
439
|
* @returns Array of available connection definitions
|
package/build/client.test.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { describe, it, before, after, beforeEach } from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import http from 'node:http';
|
|
4
|
-
import {
|
|
4
|
+
import { inspect } from 'node:util';
|
|
5
|
+
import { OneClient, stripReservedHeaders, answeredByPlatform } from './client.js';
|
|
6
|
+
import { describeError, fetchPaginatedData } from './helpers.js';
|
|
5
7
|
import { ToolError } from './tool-error.js';
|
|
6
8
|
// A passthrough call must never let the caller override One's own routing/auth
|
|
7
9
|
// headers (the scoping bypass), and a failed call must never log the secret.
|
|
@@ -56,6 +58,31 @@ describe("executePassthroughRequest keeps One's headers authoritative", () => {
|
|
|
56
58
|
assert.equal(h['x-custom-trace'], 't-1', 'a non-reserved custom header still passes through');
|
|
57
59
|
assert.equal(h['content-type'], 'application/json', 'the default content type is kept');
|
|
58
60
|
});
|
|
61
|
+
it('a caller cannot set the headers that control the connection', async () => {
|
|
62
|
+
const client = new OneClient(SECRET, base);
|
|
63
|
+
await client.executePassthroughRequest(exec({
|
|
64
|
+
data: { a: 1 },
|
|
65
|
+
headers: { Host: 'attacker.example', 'Content-Length': '1', Connection: 'close', 'x-custom-trace': 't-1' },
|
|
66
|
+
}), { ...ACTION, method: 'POST' });
|
|
67
|
+
const h = received[0].headers;
|
|
68
|
+
assert.equal(h.host, new URL(base).host);
|
|
69
|
+
assert.equal(h['content-length'], '7', 'the length comes from the body that is sent');
|
|
70
|
+
assert.notEqual(h.connection, 'close');
|
|
71
|
+
assert.equal(h['x-custom-trace'], 't-1', 'a custom header still passes through');
|
|
72
|
+
});
|
|
73
|
+
it('sends nothing when a method override differs from the action method', async () => {
|
|
74
|
+
const client = new OneClient(SECRET, base);
|
|
75
|
+
const post = { ...ACTION, method: 'POST' };
|
|
76
|
+
for (const name of ['X-HTTP-Method-Override', 'x-http-method', 'X-Method-Override', ' x-http-method-override ']) {
|
|
77
|
+
await assert.rejects(client.executePassthroughRequest(exec({ headers: { [name]: 'DELETE' } }), post), (e) => e instanceof ToolError && /DELETE/.test(e.message) && /POST/.test(e.message), name);
|
|
78
|
+
}
|
|
79
|
+
assert.equal(received.length, 0, 'nothing is sent upstream');
|
|
80
|
+
});
|
|
81
|
+
it('allows a method override that matches the action method, in any casing', async () => {
|
|
82
|
+
const client = new OneClient(SECRET, base);
|
|
83
|
+
await client.executePassthroughRequest(exec({ headers: { 'X-HTTP-Method-Override': ' post ' } }), { ...ACTION, method: 'POST' });
|
|
84
|
+
assert.equal(received.length, 1);
|
|
85
|
+
});
|
|
59
86
|
it('names the expected path variables instead of sending an unfilled template', async () => {
|
|
60
87
|
const client = new OneClient(SECRET, base);
|
|
61
88
|
const templated = { ...ACTION, path: '/v1/things/{{thingId}}' };
|
|
@@ -64,6 +91,23 @@ describe("executePassthroughRequest keeps One's headers authoritative", () => {
|
|
|
64
91
|
await client.executePassthroughRequest(exec({ pathVariables: { thing_id: 7 } }), templated);
|
|
65
92
|
assert.equal(received.length, 1);
|
|
66
93
|
});
|
|
94
|
+
it('sends nothing when a path variable would leave the action route', async () => {
|
|
95
|
+
const client = new OneClient(SECRET, base);
|
|
96
|
+
const templated = { ...ACTION, path: '/v0/{{a}}/{{b}}/{{c}}' };
|
|
97
|
+
await assert.rejects(client.executePassthroughRequest(exec({ pathVariables: { a: '..', b: '..', c: 'connections' } }), templated), ToolError);
|
|
98
|
+
assert.equal(received.length, 0, 'nothing is sent upstream');
|
|
99
|
+
});
|
|
100
|
+
it('sends nothing when an action path resolves outside passthrough', async () => {
|
|
101
|
+
const client = new OneClient(SECRET, base);
|
|
102
|
+
const encoded = { ...ACTION, path: '/%2e%2e/%2E%2e/connections' };
|
|
103
|
+
await assert.rejects(client.executePassthroughRequest(exec({}), encoded), ToolError);
|
|
104
|
+
assert.equal(received.length, 0, 'nothing is sent upstream');
|
|
105
|
+
});
|
|
106
|
+
it('keeps ordinary paths working under a base URL with a path prefix', async () => {
|
|
107
|
+
const client = new OneClient(SECRET, `${base}/prefix`);
|
|
108
|
+
await client.executePassthroughRequest(exec({}), ACTION);
|
|
109
|
+
assert.equal(received.length, 1);
|
|
110
|
+
});
|
|
67
111
|
it('a caller may still set Content-Type, in any casing', async () => {
|
|
68
112
|
const client = new OneClient(SECRET, base);
|
|
69
113
|
await client.executePassthroughRequest(exec({ headers: { 'content-type': 'text/plain' } }), ACTION);
|
|
@@ -127,6 +171,128 @@ describe("executePassthroughRequest keeps One's headers authoritative", () => {
|
|
|
127
171
|
assert.ok(message.includes('422') && message.includes('upstream said no'), "the tool error carries the status and One's reason");
|
|
128
172
|
});
|
|
129
173
|
});
|
|
174
|
+
describe('ensureConnectionInScope', () => {
|
|
175
|
+
let server;
|
|
176
|
+
let base = '';
|
|
177
|
+
let rows = [];
|
|
178
|
+
let listQueries = [];
|
|
179
|
+
let failLists = false;
|
|
180
|
+
const client = (options = {}) => new OneClient({ secret: SECRET, baseUrl: base, fetchConnectors: false, ...options });
|
|
181
|
+
before(async () => {
|
|
182
|
+
server = http.createServer((req, res) => {
|
|
183
|
+
listQueries.push(new URL(req.url ?? '', 'http://x').searchParams);
|
|
184
|
+
res.writeHead(failLists ? 500 : 200, { 'Content-Type': 'application/json' });
|
|
185
|
+
res.end(JSON.stringify(failLists ? { message: 'boom' } : { rows: rows.map((key) => ({ key, platform: 'test' })), total: rows.length }));
|
|
186
|
+
});
|
|
187
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve()));
|
|
188
|
+
base = `http://127.0.0.1:${server.address().port}`;
|
|
189
|
+
});
|
|
190
|
+
after(() => {
|
|
191
|
+
server.close();
|
|
192
|
+
});
|
|
193
|
+
beforeEach(() => {
|
|
194
|
+
rows = ['key_a'];
|
|
195
|
+
listQueries = [];
|
|
196
|
+
failLists = false;
|
|
197
|
+
});
|
|
198
|
+
it('allows any key when the server has no scope, without listing', async () => {
|
|
199
|
+
const c = client();
|
|
200
|
+
await c.ensureConnectionInScope('key_b');
|
|
201
|
+
assert.equal(listQueries.length, 0);
|
|
202
|
+
});
|
|
203
|
+
it("allows the identity's own key without listing again", async () => {
|
|
204
|
+
const c = client({ identity: 'user_a' });
|
|
205
|
+
await c.initialize();
|
|
206
|
+
await c.ensureConnectionInScope('key_a');
|
|
207
|
+
assert.equal(listQueries.length, 1);
|
|
208
|
+
});
|
|
209
|
+
it("refuses another identity's key, using the identity-filtered list", async () => {
|
|
210
|
+
const c = client({ identity: 'user_a' });
|
|
211
|
+
await c.initialize();
|
|
212
|
+
await assert.rejects(c.ensureConnectionInScope('key_b'), (e) => e instanceof ToolError && /is not one this server may use/.test(e.message));
|
|
213
|
+
assert.equal(listQueries.at(-1)?.get('identity'), 'user_a', 'the check reads the same filtered list');
|
|
214
|
+
});
|
|
215
|
+
it('allows a connection made after the list was loaded', async () => {
|
|
216
|
+
const c = client({ identity: 'user_a' });
|
|
217
|
+
await c.initialize();
|
|
218
|
+
rows = ['key_a', 'key_new'];
|
|
219
|
+
await c.ensureConnectionInScope('key_new');
|
|
220
|
+
assert.deepEqual(c.getUserConnections().map((x) => x.key), ['key_a', 'key_new']);
|
|
221
|
+
});
|
|
222
|
+
const silenced = async (run) => {
|
|
223
|
+
const original = console.error;
|
|
224
|
+
console.error = () => { };
|
|
225
|
+
try {
|
|
226
|
+
return await run();
|
|
227
|
+
}
|
|
228
|
+
finally {
|
|
229
|
+
console.error = original;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
it('recovers when the first load failed', async () => {
|
|
233
|
+
const c = client({ identity: 'user_a' });
|
|
234
|
+
failLists = true;
|
|
235
|
+
await silenced(() => c.initialize());
|
|
236
|
+
failLists = false;
|
|
237
|
+
await c.ensureConnectionInScope('key_a');
|
|
238
|
+
});
|
|
239
|
+
it('refuses when the list cannot be read, and keeps the loaded list', async () => {
|
|
240
|
+
const c = client({ identity: 'user_a' });
|
|
241
|
+
await c.initialize();
|
|
242
|
+
failLists = true;
|
|
243
|
+
await silenced(() => assert.rejects(c.ensureConnectionInScope('key_b'), (e) => e instanceof ToolError && /Could not confirm/.test(e.message)));
|
|
244
|
+
assert.deepEqual(c.getUserConnections().map((x) => x.key), ['key_a']);
|
|
245
|
+
});
|
|
246
|
+
it('still applies ONE_CONNECTION_KEYS', async () => {
|
|
247
|
+
const c = client({ connectionKeys: ['key_a'] });
|
|
248
|
+
await c.initialize();
|
|
249
|
+
await assert.rejects(c.ensureConnectionInScope('key_b'), ToolError);
|
|
250
|
+
await c.ensureConnectionInScope('key_a');
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
describe('a failed paginated fetch', () => {
|
|
254
|
+
// Rendered the way console.error renders its arguments, which is what prints
|
|
255
|
+
// a whole axios error's request headers.
|
|
256
|
+
const captureLog = async (run) => {
|
|
257
|
+
const logged = [];
|
|
258
|
+
const original = console.error;
|
|
259
|
+
console.error = (...args) => {
|
|
260
|
+
logged.push(args.map((a) => (typeof a === 'string' ? a : inspect(a, { depth: null }))).join(' '));
|
|
261
|
+
};
|
|
262
|
+
try {
|
|
263
|
+
await assert.rejects(run());
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
console.error = original;
|
|
267
|
+
}
|
|
268
|
+
return logged.join('\n');
|
|
269
|
+
};
|
|
270
|
+
it('never logs the secret when One answers with an error', async () => {
|
|
271
|
+
const server = http.createServer((_req, res) => {
|
|
272
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
273
|
+
res.end(JSON.stringify({ message: 'upstream said no' }));
|
|
274
|
+
});
|
|
275
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve()));
|
|
276
|
+
const url = `http://127.0.0.1:${server.address().port}/v1/vault/connections`;
|
|
277
|
+
try {
|
|
278
|
+
const log = await captureLog(() => fetchPaginatedData(url, { 'x-one-secret': SECRET }));
|
|
279
|
+
assert.ok(!log.includes(SECRET), 'the secret never reaches the log');
|
|
280
|
+
assert.ok(log.includes('500') && log.includes('upstream said no'), 'the log still says what failed');
|
|
281
|
+
}
|
|
282
|
+
finally {
|
|
283
|
+
server.close();
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
it('never logs the secret when One cannot be reached', async () => {
|
|
287
|
+
const closed = http.createServer();
|
|
288
|
+
await new Promise((resolve) => closed.listen(0, '127.0.0.1', () => resolve()));
|
|
289
|
+
const port = closed.address().port;
|
|
290
|
+
await new Promise((resolve) => closed.close(() => resolve()));
|
|
291
|
+
const log = await captureLog(() => fetchPaginatedData(`http://127.0.0.1:${port}/v1/vault/connections`, { 'x-one-secret': SECRET }));
|
|
292
|
+
assert.ok(!log.includes(SECRET), 'the secret never reaches the log');
|
|
293
|
+
assert.ok(log.includes('ECONNREFUSED'), 'the log still says what failed');
|
|
294
|
+
});
|
|
295
|
+
});
|
|
130
296
|
describe('answeredByPlatform', () => {
|
|
131
297
|
it('reads a renamed upstream header, in any casing, as the platform answering', () => {
|
|
132
298
|
assert.equal(answeredByPlatform({ 'x-passthrough-content-type': 'application/json' }), true);
|
|
@@ -140,6 +306,14 @@ describe('stripReservedHeaders', () => {
|
|
|
140
306
|
assert.deepEqual(stripReservedHeaders({ n: 5 }), { n: '5' });
|
|
141
307
|
assert.deepEqual(stripReservedHeaders(undefined), {});
|
|
142
308
|
});
|
|
309
|
+
it('drops connection-level headers case-insensitively', () => {
|
|
310
|
+
for (const name of ['Host', 'host', 'CONNECTION', 'Content-Length', 'Transfer-Encoding', 'Keep-Alive', 'Proxy-Connection', 'TE', 'Trailer', 'Upgrade']) {
|
|
311
|
+
assert.deepEqual(stripReservedHeaders({ [name]: 'x', Accept: 'y' }), { Accept: 'y' }, name);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
it('ignores surrounding whitespace in a header name', () => {
|
|
315
|
+
assert.deepEqual(stripReservedHeaders({ 'Host ': 'a', ' host': 'b', ' x-one-secret': 'c', Accept: 'y' }), { Accept: 'y' });
|
|
316
|
+
});
|
|
143
317
|
});
|
|
144
318
|
describe('describeError', () => {
|
|
145
319
|
it('summarizes a plain error to its name and message', () => {
|
package/build/helpers.js
CHANGED
|
@@ -9,6 +9,48 @@
|
|
|
9
9
|
import axios from 'axios';
|
|
10
10
|
import { ToolError } from './tool-error.js';
|
|
11
11
|
import { parseSections, buildDigest, renderDigestBanner, renderDigestNotice, selectSections, flattenSections, parseSectionFlag, collapseSections, omittedSections, } from './knowledge-sections.js';
|
|
12
|
+
/**
|
|
13
|
+
* Bounds an upstream error body before it is logged: small payloads pass
|
|
14
|
+
* through, larger ones are truncated to a string, and an unserializable value
|
|
15
|
+
* is replaced with a placeholder. Keeps a huge or odd error response from
|
|
16
|
+
* bloating a single log line.
|
|
17
|
+
*/
|
|
18
|
+
export function summarizeErrorData(data) {
|
|
19
|
+
if (data === undefined || data === null)
|
|
20
|
+
return undefined;
|
|
21
|
+
let serialized;
|
|
22
|
+
try {
|
|
23
|
+
serialized = typeof data === 'string' ? data : JSON.stringify(data);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return '[unserializable response body]';
|
|
27
|
+
}
|
|
28
|
+
const MAX = 1000;
|
|
29
|
+
return serialized.length <= MAX ? data : `${serialized.slice(0, MAX)}… (${serialized.length} chars, truncated)`;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A log-safe summary of a failed request. Axios errors carry the full request
|
|
33
|
+
* config, including the `x-one-secret` header, so they must never be logged
|
|
34
|
+
* whole. Only the status, the upstream message, the route (query stripped), and
|
|
35
|
+
* a bounded copy of the response body are kept.
|
|
36
|
+
*/
|
|
37
|
+
export function describeError(error) {
|
|
38
|
+
if (axios.isAxiosError(error)) {
|
|
39
|
+
const url = error.config?.url;
|
|
40
|
+
return {
|
|
41
|
+
status: error.response?.status,
|
|
42
|
+
statusText: error.response?.statusText,
|
|
43
|
+
method: error.config?.method?.toUpperCase(),
|
|
44
|
+
url: typeof url === 'string' ? url.split('?')[0] : undefined,
|
|
45
|
+
message: error.message,
|
|
46
|
+
data: summarizeErrorData(error.response?.data),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (error instanceof Error) {
|
|
50
|
+
return { name: error.name, message: error.message };
|
|
51
|
+
}
|
|
52
|
+
return { error: String(error) };
|
|
53
|
+
}
|
|
12
54
|
/**
|
|
13
55
|
* Paginates through API results by repeatedly calling a fetch function until all data is retrieved.
|
|
14
56
|
* @param fetchFn - Function that fetches paginated data, takes skip and limit parameters
|
|
@@ -30,7 +72,7 @@ export async function paginateResults(fetchFn, limit = 100) {
|
|
|
30
72
|
return allResults;
|
|
31
73
|
}
|
|
32
74
|
catch (error) {
|
|
33
|
-
console.error("Error in pagination:", error);
|
|
75
|
+
console.error("Error in pagination:", describeError(error));
|
|
34
76
|
throw error;
|
|
35
77
|
}
|
|
36
78
|
}
|
|
@@ -56,7 +98,7 @@ export async function fetchPaginatedData(baseUrl, headers, additionalParams) {
|
|
|
56
98
|
return response.data;
|
|
57
99
|
}
|
|
58
100
|
catch (error) {
|
|
59
|
-
console.error(`Failed to fetch from ${baseUrl}:`, error);
|
|
101
|
+
console.error(`Failed to fetch from ${baseUrl}:`, describeError(error));
|
|
60
102
|
throw error;
|
|
61
103
|
}
|
|
62
104
|
};
|
|
@@ -209,7 +251,15 @@ export function replacePathVariables(path, variables) {
|
|
|
209
251
|
return encodeURIComponent(value.toString());
|
|
210
252
|
};
|
|
211
253
|
// Double brackets first, so {{name}} is not read as {name} wrapped in braces.
|
|
212
|
-
|
|
254
|
+
const filled = path.replace(/\{\{([^}]+)\}\}/g, (_match, variable) => lookup(variable)).replace(/\{([^}]+)\}/g, (_match, variable) => lookup(variable));
|
|
255
|
+
// encodeURIComponent leaves dots alone, and a dot segment would move the
|
|
256
|
+
// request off the action's route. Checked on the filled path because
|
|
257
|
+
// adjacent variables can assemble one.
|
|
258
|
+
const route = filled.split(/[?#]/, 1)[0];
|
|
259
|
+
if (route.split('/').some((segment) => segment === '.' || segment === '..')) {
|
|
260
|
+
throw new ToolError(`Invalid pathVariables for ${path}: a path segment cannot be "." or "..".`);
|
|
261
|
+
}
|
|
262
|
+
return filled;
|
|
213
263
|
}
|
|
214
264
|
const PERMISSION_METHODS = {
|
|
215
265
|
read: ["GET"],
|
package/build/index.js
CHANGED
|
@@ -163,12 +163,7 @@ async function handleExecuteOneAction(args) {
|
|
|
163
163
|
if (!isActionAllowed(args.actionId, ONE_ACTION_IDS)) {
|
|
164
164
|
throw new ToolError(`Action "${args.actionId}" is not one this server may run. Use an action listed in list_one_integrations or returned by find_one_actions.`);
|
|
165
165
|
}
|
|
166
|
-
|
|
167
|
-
const allowedKeys = oneClient.getUserConnections().map(c => c.key);
|
|
168
|
-
if (!allowedKeys.includes(args.connectionKey)) {
|
|
169
|
-
throw new ToolError(`Connection key "${args.connectionKey}" is not one this server may use. Use a connection key from list_one_integrations.`);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
166
|
+
await oneClient.ensureConnectionInScope(args.connectionKey);
|
|
172
167
|
const actionDetails = await oneClient.getActionDetails(args.actionId);
|
|
173
168
|
if (!isMethodAllowed(actionDetails.method, ONE_PERMISSIONS)) {
|
|
174
169
|
throw new ToolError(`Action "${args.actionId}" is a ${actionDetails.method.toUpperCase()} request, which this server's "${ONE_PERMISSIONS}" permission level does not allow, so it cannot be run here.`);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, it } from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import { replacePathVariables } from './helpers.js';
|
|
4
|
+
import { ToolError } from './tool-error.js';
|
|
4
5
|
describe('replacePathVariables', () => {
|
|
5
6
|
it('fills exact names in both bracket styles', () => {
|
|
6
7
|
assert.equal(replacePathVariables('/tickets/{{ticketId}}/comments/{id}', { ticketId: 42, id: 'c 1' }), '/tickets/42/comments/c%201');
|
|
@@ -23,4 +24,21 @@ describe('replacePathVariables', () => {
|
|
|
23
24
|
it('rejects empty values', () => {
|
|
24
25
|
assert.throws(() => replacePathVariables('/x/{{id}}', { id: '' }), /Missing value for path variable: id/);
|
|
25
26
|
});
|
|
27
|
+
it('refuses a value that would become a dot segment', () => {
|
|
28
|
+
for (const value of ['.', '..']) {
|
|
29
|
+
assert.throws(() => replacePathVariables('/a/{{id}}/b', { id: value }), ToolError, value);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
it('refuses dot segments assembled from adjacent variables', () => {
|
|
33
|
+
assert.throws(() => replacePathVariables('/a/{{x}}{{y}}/b', { x: '.', y: '.' }), ToolError);
|
|
34
|
+
});
|
|
35
|
+
it('keeps values that only contain dots', () => {
|
|
36
|
+
assert.equal(replacePathVariables('/a/{{x}}/{{y}}/{{z}}', { x: '...', y: '..hidden', z: 'file.txt' }), '/a/.../..hidden/file.txt');
|
|
37
|
+
});
|
|
38
|
+
it('keeps a value with a slash inside one encoded segment', () => {
|
|
39
|
+
assert.equal(replacePathVariables('/a/{{x}}', { x: '../..' }), '/a/..%2F..');
|
|
40
|
+
});
|
|
41
|
+
it('allows dots in a variable after the query separator', () => {
|
|
42
|
+
assert.equal(replacePathVariables('/a?q={{q}}', { q: '..' }), '/a?q=..');
|
|
43
|
+
});
|
|
26
44
|
});
|