@lobu/gateway 2.8.0 → 3.0.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/package.json +2 -2
- package/src/__tests__/agent-config-routes.test.ts +254 -0
- package/src/__tests__/agent-history-routes.test.ts +72 -0
- package/src/__tests__/agent-routes.test.ts +68 -0
- package/src/__tests__/agent-schedules-routes.test.ts +59 -0
- package/src/__tests__/agent-settings-store.test.ts +323 -0
- package/src/__tests__/chat-instance-manager-slack.test.ts +204 -0
- package/src/__tests__/chat-response-bridge.test.ts +131 -0
- package/src/__tests__/config-memory-plugins.test.ts +92 -0
- package/src/__tests__/config-request-store.test.ts +127 -0
- package/src/__tests__/connection-routes.test.ts +144 -0
- package/src/__tests__/core-services-store-selection.test.ts +92 -0
- package/src/__tests__/docker-deployment.test.ts +1211 -0
- package/src/__tests__/embedded-deployment.test.ts +342 -0
- package/src/__tests__/grant-store.test.ts +148 -0
- package/src/__tests__/http-proxy.test.ts +281 -0
- package/src/__tests__/instruction-service.test.ts +37 -0
- package/src/__tests__/link-buttons.test.ts +112 -0
- package/src/__tests__/lobu.test.ts +32 -0
- package/src/__tests__/mcp-config-service.test.ts +347 -0
- package/src/__tests__/mcp-proxy.test.ts +696 -0
- package/src/__tests__/message-handler-bridge.test.ts +17 -0
- package/src/__tests__/model-selection.test.ts +172 -0
- package/src/__tests__/oauth-templates.test.ts +39 -0
- package/src/__tests__/platform-adapter-slack-send.test.ts +114 -0
- package/src/__tests__/platform-helpers-model-resolution.test.ts +253 -0
- package/src/__tests__/provider-inheritance.test.ts +212 -0
- package/src/__tests__/routes/cli-auth.test.ts +337 -0
- package/src/__tests__/routes/interactions.test.ts +121 -0
- package/src/__tests__/secret-proxy.test.ts +85 -0
- package/src/__tests__/session-manager.test.ts +572 -0
- package/src/__tests__/setup.ts +133 -0
- package/src/__tests__/skill-and-mcp-registry.test.ts +203 -0
- package/src/__tests__/slack-routes.test.ts +161 -0
- package/src/__tests__/system-config-resolver.test.ts +75 -0
- package/src/__tests__/system-message-limiter.test.ts +89 -0
- package/src/__tests__/system-skills-service.test.ts +362 -0
- package/src/__tests__/transcription-service.test.ts +222 -0
- package/src/__tests__/utils/rate-limiter.test.ts +102 -0
- package/src/__tests__/worker-connection-manager.test.ts +497 -0
- package/src/__tests__/worker-job-router.test.ts +722 -0
- package/src/api/index.ts +1 -0
- package/src/api/platform.ts +292 -0
- package/src/api/response-renderer.ts +157 -0
- package/src/auth/agent-metadata-store.ts +168 -0
- package/src/auth/api-auth-middleware.ts +69 -0
- package/src/auth/api-key-provider-module.ts +213 -0
- package/src/auth/base-provider-module.ts +201 -0
- package/src/auth/chatgpt/chatgpt-oauth-module.ts +185 -0
- package/src/auth/chatgpt/device-code-client.ts +218 -0
- package/src/auth/chatgpt/index.ts +1 -0
- package/src/auth/claude/oauth-module.ts +280 -0
- package/src/auth/cli/token-service.ts +249 -0
- package/src/auth/external/client.ts +560 -0
- package/src/auth/external/device-code-client.ts +225 -0
- package/src/auth/mcp/config-service.ts +392 -0
- package/src/auth/mcp/proxy.ts +1088 -0
- package/src/auth/mcp/string-substitution.ts +17 -0
- package/src/auth/mcp/tool-cache.ts +90 -0
- package/src/auth/oauth/base-client.ts +267 -0
- package/src/auth/oauth/client.ts +153 -0
- package/src/auth/oauth/credentials.ts +7 -0
- package/src/auth/oauth/providers.ts +69 -0
- package/src/auth/oauth/state-store.ts +150 -0
- package/src/auth/oauth-templates.ts +179 -0
- package/src/auth/provider-catalog.ts +220 -0
- package/src/auth/provider-model-options.ts +41 -0
- package/src/auth/settings/agent-settings-store.ts +565 -0
- package/src/auth/settings/auth-profiles-manager.ts +216 -0
- package/src/auth/settings/index.ts +12 -0
- package/src/auth/settings/model-preference-store.ts +52 -0
- package/src/auth/settings/model-selection.ts +135 -0
- package/src/auth/settings/resolved-settings-view.ts +298 -0
- package/src/auth/settings/template-utils.ts +44 -0
- package/src/auth/settings/token-service.ts +88 -0
- package/src/auth/system-env-store.ts +98 -0
- package/src/auth/user-agents-store.ts +68 -0
- package/src/channels/binding-service.ts +214 -0
- package/src/channels/index.ts +4 -0
- package/src/cli/gateway.ts +1304 -0
- package/src/cli/index.ts +74 -0
- package/src/commands/built-in-commands.ts +80 -0
- package/src/commands/command-dispatcher.ts +94 -0
- package/src/commands/command-reply-adapters.ts +27 -0
- package/src/config/file-loader.ts +618 -0
- package/src/config/index.ts +588 -0
- package/src/config/network-allowlist.ts +71 -0
- package/src/connections/chat-instance-manager.ts +1284 -0
- package/src/connections/chat-response-bridge.ts +618 -0
- package/src/connections/index.ts +7 -0
- package/src/connections/interaction-bridge.ts +831 -0
- package/src/connections/message-handler-bridge.ts +415 -0
- package/src/connections/platform-auth-methods.ts +15 -0
- package/src/connections/types.ts +84 -0
- package/src/gateway/connection-manager.ts +291 -0
- package/src/gateway/index.ts +700 -0
- package/src/gateway/job-router.ts +201 -0
- package/src/gateway-main.ts +200 -0
- package/src/index.ts +41 -0
- package/src/infrastructure/queue/index.ts +12 -0
- package/src/infrastructure/queue/queue-producer.ts +148 -0
- package/src/infrastructure/queue/redis-queue.ts +361 -0
- package/src/infrastructure/queue/types.ts +133 -0
- package/src/infrastructure/redis/system-message-limiter.ts +94 -0
- package/src/interactions/config-request-store.ts +198 -0
- package/src/interactions.ts +363 -0
- package/src/lobu.ts +311 -0
- package/src/metrics/prometheus.ts +159 -0
- package/src/modules/module-system.ts +179 -0
- package/src/orchestration/base-deployment-manager.ts +900 -0
- package/src/orchestration/deployment-utils.ts +98 -0
- package/src/orchestration/impl/docker-deployment.ts +620 -0
- package/src/orchestration/impl/embedded-deployment.ts +268 -0
- package/src/orchestration/impl/index.ts +8 -0
- package/src/orchestration/impl/k8s/deployment.ts +1061 -0
- package/src/orchestration/impl/k8s/helpers.ts +610 -0
- package/src/orchestration/impl/k8s/index.ts +1 -0
- package/src/orchestration/index.ts +333 -0
- package/src/orchestration/message-consumer.ts +584 -0
- package/src/orchestration/scheduled-wakeup.ts +704 -0
- package/src/permissions/approval-policy.ts +36 -0
- package/src/permissions/grant-store.ts +219 -0
- package/src/platform/file-handler.ts +66 -0
- package/src/platform/link-buttons.ts +57 -0
- package/src/platform/renderer-utils.ts +44 -0
- package/src/platform/response-renderer.ts +84 -0
- package/src/platform/unified-thread-consumer.ts +187 -0
- package/src/platform.ts +318 -0
- package/src/proxy/http-proxy.ts +752 -0
- package/src/proxy/proxy-manager.ts +81 -0
- package/src/proxy/secret-proxy.ts +402 -0
- package/src/proxy/token-refresh-job.ts +143 -0
- package/src/routes/internal/audio.ts +141 -0
- package/src/routes/internal/device-auth.ts +566 -0
- package/src/routes/internal/files.ts +226 -0
- package/src/routes/internal/history.ts +69 -0
- package/src/routes/internal/images.ts +127 -0
- package/src/routes/internal/interactions.ts +84 -0
- package/src/routes/internal/middleware.ts +23 -0
- package/src/routes/internal/schedule.ts +226 -0
- package/src/routes/internal/types.ts +22 -0
- package/src/routes/openapi-auto.ts +239 -0
- package/src/routes/public/agent-access.ts +23 -0
- package/src/routes/public/agent-config.ts +675 -0
- package/src/routes/public/agent-history.ts +422 -0
- package/src/routes/public/agent-schedules.ts +296 -0
- package/src/routes/public/agent.ts +1086 -0
- package/src/routes/public/agents.ts +373 -0
- package/src/routes/public/channels.ts +191 -0
- package/src/routes/public/cli-auth.ts +883 -0
- package/src/routes/public/connections.ts +574 -0
- package/src/routes/public/landing.ts +16 -0
- package/src/routes/public/oauth.ts +147 -0
- package/src/routes/public/settings-auth.ts +104 -0
- package/src/routes/public/slack.ts +173 -0
- package/src/routes/shared/agent-ownership.ts +101 -0
- package/src/routes/shared/token-verifier.ts +34 -0
- package/src/services/core-services.ts +1053 -0
- package/src/services/image-generation-service.ts +257 -0
- package/src/services/instruction-service.ts +318 -0
- package/src/services/mcp-registry.ts +94 -0
- package/src/services/platform-helpers.ts +287 -0
- package/src/services/session-manager.ts +262 -0
- package/src/services/settings-resolver.ts +74 -0
- package/src/services/system-config-resolver.ts +90 -0
- package/src/services/system-skills-service.ts +229 -0
- package/src/services/transcription-service.ts +684 -0
- package/src/session.ts +110 -0
- package/src/spaces/index.ts +1 -0
- package/src/spaces/space-resolver.ts +17 -0
- package/src/stores/in-memory-agent-store.ts +403 -0
- package/src/stores/redis-agent-store.ts +279 -0
- package/src/utils/public-url.ts +44 -0
- package/src/utils/rate-limiter.ts +94 -0
- package/tsconfig.json +33 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import * as crypto from "node:crypto";
|
|
3
|
+
import * as http from "node:http";
|
|
4
|
+
import * as net from "node:net";
|
|
5
|
+
import { generateWorkerToken } from "@lobu/core";
|
|
6
|
+
import { __testOnly, startHttpProxy, stopHttpProxy } from "../proxy/http-proxy";
|
|
7
|
+
|
|
8
|
+
// Generate a stable 32-byte encryption key for tests
|
|
9
|
+
const TEST_ENCRYPTION_KEY = crypto.randomBytes(32).toString("base64");
|
|
10
|
+
|
|
11
|
+
// Single proxy server shared across all test suites
|
|
12
|
+
let proxyPort: number;
|
|
13
|
+
let proxyServer: http.Server;
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
process.env.ENCRYPTION_KEY = TEST_ENCRYPTION_KEY;
|
|
17
|
+
// Default to unrestricted for auth tests; domain tests use per-deployment config
|
|
18
|
+
process.env.WORKER_ALLOWED_DOMAINS = "*";
|
|
19
|
+
|
|
20
|
+
proxyPort = 10000 + Math.floor(Math.random() * 50000);
|
|
21
|
+
proxyServer = await startHttpProxy(proxyPort, "127.0.0.1");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(async () => {
|
|
25
|
+
await stopHttpProxy(proxyServer);
|
|
26
|
+
delete process.env.ENCRYPTION_KEY;
|
|
27
|
+
delete process.env.WORKER_ALLOWED_DOMAINS;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
function makeBasicAuth(username: string, password: string): string {
|
|
31
|
+
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Send a raw HTTP proxy request via TCP socket to avoid Bun's HTTP client
|
|
36
|
+
* retrying on 407 responses.
|
|
37
|
+
*/
|
|
38
|
+
function rawProxyRequest(
|
|
39
|
+
targetUrl: string,
|
|
40
|
+
options: { proxyAuth?: string } = {}
|
|
41
|
+
): Promise<{ statusCode: number; headers: string; body: string }> {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const socket = new net.Socket();
|
|
44
|
+
socket.connect(proxyPort, "127.0.0.1", () => {
|
|
45
|
+
let req = `GET ${targetUrl} HTTP/1.1\r\nHost: ${new URL(targetUrl).host}\r\n`;
|
|
46
|
+
if (options.proxyAuth) {
|
|
47
|
+
req += `Proxy-Authorization: ${options.proxyAuth}\r\n`;
|
|
48
|
+
}
|
|
49
|
+
req += "Connection: close\r\n\r\n";
|
|
50
|
+
socket.write(req);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
let data = "";
|
|
54
|
+
socket.on("data", (chunk: Buffer) => {
|
|
55
|
+
data += chunk.toString();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
socket.on("end", () => {
|
|
59
|
+
// Parse status code from first line: "HTTP/1.1 407 ..."
|
|
60
|
+
const firstLineEnd = data.indexOf("\r\n");
|
|
61
|
+
const statusLine = data.substring(0, firstLineEnd);
|
|
62
|
+
const statusMatch = statusLine.match(/HTTP\/\d\.\d (\d+)/);
|
|
63
|
+
const statusCode = statusMatch ? parseInt(statusMatch[1]!, 10) : 0;
|
|
64
|
+
|
|
65
|
+
const headerEnd = data.indexOf("\r\n\r\n");
|
|
66
|
+
const headers = data.substring(0, headerEnd);
|
|
67
|
+
const body = headerEnd !== -1 ? data.substring(headerEnd + 4) : "";
|
|
68
|
+
|
|
69
|
+
resolve({ statusCode, headers, body });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
socket.on("error", reject);
|
|
73
|
+
socket.setTimeout(5000, () => {
|
|
74
|
+
socket.destroy();
|
|
75
|
+
reject(new Error("Request timed out"));
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Send a CONNECT request through the proxy and return the raw response line.
|
|
82
|
+
*/
|
|
83
|
+
function connectRequest(
|
|
84
|
+
host: string,
|
|
85
|
+
port: number,
|
|
86
|
+
options: { proxyAuth?: string } = {}
|
|
87
|
+
): Promise<{ statusLine: string }> {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
const socket = new net.Socket();
|
|
90
|
+
socket.connect(proxyPort, "127.0.0.1", () => {
|
|
91
|
+
let req = `CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\n`;
|
|
92
|
+
if (options.proxyAuth) {
|
|
93
|
+
req += `Proxy-Authorization: ${options.proxyAuth}\r\n`;
|
|
94
|
+
}
|
|
95
|
+
req += "\r\n";
|
|
96
|
+
socket.write(req);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
let data = "";
|
|
100
|
+
socket.on("data", (chunk: Buffer) => {
|
|
101
|
+
data += chunk.toString();
|
|
102
|
+
const lineEnd = data.indexOf("\r\n");
|
|
103
|
+
if (lineEnd !== -1) {
|
|
104
|
+
socket.destroy();
|
|
105
|
+
resolve({ statusLine: data.substring(0, lineEnd) });
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
socket.on("error", reject);
|
|
110
|
+
socket.setTimeout(5000, () => {
|
|
111
|
+
socket.destroy();
|
|
112
|
+
reject(new Error("CONNECT request timed out"));
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createValidToken(deploymentName: string): string {
|
|
118
|
+
return generateWorkerToken("test-user", "test-conv", deploymentName, {
|
|
119
|
+
channelId: "test-channel",
|
|
120
|
+
platform: "test",
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ─── Auth tests ──────────────────────────────────────────────────────────────
|
|
125
|
+
|
|
126
|
+
describe("HTTP Proxy Authentication", () => {
|
|
127
|
+
describe("HTTP requests", () => {
|
|
128
|
+
test("rejects request with no auth (407)", async () => {
|
|
129
|
+
const res = await rawProxyRequest("http://example.com/test");
|
|
130
|
+
expect(res.statusCode).toBe(407);
|
|
131
|
+
expect(res.headers.toLowerCase()).toContain("proxy-authenticate");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("rejects request with invalid token (407)", async () => {
|
|
135
|
+
const res = await rawProxyRequest("http://example.com/test", {
|
|
136
|
+
proxyAuth: makeBasicAuth("my-deployment", "not-a-valid-token"),
|
|
137
|
+
});
|
|
138
|
+
expect(res.statusCode).toBe(407);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("rejects request with deployment name mismatch (407)", async () => {
|
|
142
|
+
const token = createValidToken("real-deployment");
|
|
143
|
+
const res = await rawProxyRequest("http://example.com/test", {
|
|
144
|
+
proxyAuth: makeBasicAuth("fake-deployment", token),
|
|
145
|
+
});
|
|
146
|
+
expect(res.statusCode).toBe(407);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("rejects request with empty password (407)", async () => {
|
|
150
|
+
const res = await rawProxyRequest("http://example.com/test", {
|
|
151
|
+
proxyAuth: makeBasicAuth("my-deployment", ""),
|
|
152
|
+
});
|
|
153
|
+
expect(res.statusCode).toBe(407);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("accepts request with valid token", async () => {
|
|
157
|
+
const deploymentName = "test-worker-http";
|
|
158
|
+
const token = createValidToken(deploymentName);
|
|
159
|
+
const res = await rawProxyRequest("http://example.com/", {
|
|
160
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
161
|
+
});
|
|
162
|
+
// Should pass auth — either upstream response or 502 (network error)
|
|
163
|
+
expect(res.statusCode).not.toBe(407);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("CONNECT requests", () => {
|
|
168
|
+
test("rejects CONNECT with no auth (407)", async () => {
|
|
169
|
+
const res = await connectRequest("example.com", 443);
|
|
170
|
+
expect(res.statusLine).toContain("407");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("rejects CONNECT with invalid token (407)", async () => {
|
|
174
|
+
const res = await connectRequest("example.com", 443, {
|
|
175
|
+
proxyAuth: makeBasicAuth("my-deployment", "garbage-token"),
|
|
176
|
+
});
|
|
177
|
+
expect(res.statusLine).toContain("407");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("rejects CONNECT with deployment mismatch (407)", async () => {
|
|
181
|
+
const token = createValidToken("actual-deployment");
|
|
182
|
+
const res = await connectRequest("example.com", 443, {
|
|
183
|
+
proxyAuth: makeBasicAuth("wrong-deployment", token),
|
|
184
|
+
});
|
|
185
|
+
expect(res.statusLine).toContain("407");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("accepts CONNECT with valid token (200)", async () => {
|
|
189
|
+
const deploymentName = "test-worker-connect";
|
|
190
|
+
const token = createValidToken(deploymentName);
|
|
191
|
+
const res = await connectRequest("example.com", 443, {
|
|
192
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
193
|
+
});
|
|
194
|
+
expect(res.statusLine).toContain("200");
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ─── Startup tests ───────────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
describe("HTTP Proxy Startup", () => {
|
|
202
|
+
test("rejects on port conflict (EADDRINUSE)", async () => {
|
|
203
|
+
const blockingPort = 10000 + Math.floor(Math.random() * 50000);
|
|
204
|
+
const blocker = http.createServer();
|
|
205
|
+
await new Promise<void>((resolve) =>
|
|
206
|
+
blocker.listen(blockingPort, "127.0.0.1", resolve)
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
await expect(
|
|
211
|
+
startHttpProxy(blockingPort, "127.0.0.1")
|
|
212
|
+
).rejects.toMatchObject({ code: "EADDRINUSE" });
|
|
213
|
+
} finally {
|
|
214
|
+
await new Promise<void>((resolve, reject) =>
|
|
215
|
+
blocker.close((err) => (err ? reject(err) : resolve()))
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("binds to specified host and port", async () => {
|
|
221
|
+
const port = 10000 + Math.floor(Math.random() * 50000);
|
|
222
|
+
const server = await startHttpProxy(port, "127.0.0.1");
|
|
223
|
+
try {
|
|
224
|
+
const addr = server.address();
|
|
225
|
+
expect(addr).not.toBeNull();
|
|
226
|
+
if (typeof addr === "object" && addr) {
|
|
227
|
+
expect(addr.port).toBe(port);
|
|
228
|
+
expect(addr.address).toBe("127.0.0.1");
|
|
229
|
+
}
|
|
230
|
+
} finally {
|
|
231
|
+
await stopHttpProxy(server);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// ─── Domain filtering tests ──────────────────────────────────────────────────
|
|
237
|
+
// Global config is WORKER_ALLOWED_DOMAINS=* (unrestricted), so all domains pass.
|
|
238
|
+
// Domain restriction via per-agent grants requires Redis and is tested separately.
|
|
239
|
+
|
|
240
|
+
describe("HTTP Proxy Domain Filtering (unrestricted mode)", () => {
|
|
241
|
+
const deploymentName = "domain-test-worker";
|
|
242
|
+
|
|
243
|
+
test("rejects request to loopback IP literal", async () => {
|
|
244
|
+
const token = createValidToken(deploymentName);
|
|
245
|
+
const res = await rawProxyRequest("http://127.0.0.1/", {
|
|
246
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
247
|
+
});
|
|
248
|
+
expect(res.statusCode).toBe(403);
|
|
249
|
+
expect(res.body).toContain("Target IP not allowed");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("rejects request to IPv4-mapped IPv6 loopback (hex form)", async () => {
|
|
253
|
+
expect(__testOnly.isBlockedIpAddress("::ffff:7f00:1")).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("rejects CONNECT when hostname resolves to loopback", async () => {
|
|
257
|
+
const token = createValidToken(deploymentName);
|
|
258
|
+
const res = await connectRequest("localhost", 443, {
|
|
259
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
260
|
+
});
|
|
261
|
+
expect(res.statusLine).toContain("403");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("allows request to any domain in unrestricted mode", async () => {
|
|
265
|
+
const token = createValidToken(deploymentName);
|
|
266
|
+
const res = await rawProxyRequest("http://example.com/", {
|
|
267
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
268
|
+
});
|
|
269
|
+
// Passes auth + domain check — either upstream response or 502
|
|
270
|
+
expect(res.statusCode).not.toBe(403);
|
|
271
|
+
expect(res.statusCode).not.toBe(407);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("allows CONNECT to any domain in unrestricted mode", async () => {
|
|
275
|
+
const token = createValidToken(deploymentName);
|
|
276
|
+
const res = await connectRequest("example.com", 443, {
|
|
277
|
+
proxyAuth: makeBasicAuth(deploymentName, token),
|
|
278
|
+
});
|
|
279
|
+
expect(res.statusLine).toContain("200");
|
|
280
|
+
});
|
|
281
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { MockRedisClient } from "@lobu/core/testing";
|
|
3
|
+
import { AgentSettingsStore } from "../auth/settings/agent-settings-store";
|
|
4
|
+
import { InstructionService } from "../services/instruction-service";
|
|
5
|
+
|
|
6
|
+
describe("InstructionService", () => {
|
|
7
|
+
let store: AgentSettingsStore;
|
|
8
|
+
let service: InstructionService;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
store = new AgentSettingsStore(new MockRedisClient() as any);
|
|
12
|
+
service = new InstructionService(undefined, store);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("returns stronger fallback guidance when agent instructions are unconfigured", async () => {
|
|
16
|
+
const sessionContext = await service.getSessionContext(
|
|
17
|
+
"telegram",
|
|
18
|
+
{
|
|
19
|
+
agentId: "agent-1",
|
|
20
|
+
userId: "user-1",
|
|
21
|
+
workingDirectory: "/workspace/thread-1",
|
|
22
|
+
} as any,
|
|
23
|
+
{ settingsUrl: "http://localhost:8080/api/v1/agents/agent-1/config" }
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(sessionContext.agentInstructions).toContain(
|
|
27
|
+
"## Agent Configuration Notice"
|
|
28
|
+
);
|
|
29
|
+
expect(sessionContext.agentInstructions).toContain(
|
|
30
|
+
"IDENTITY.md, SOUL.md, USER.md"
|
|
31
|
+
);
|
|
32
|
+
expect(sessionContext.agentInstructions).not.toContain("ScheduleReminder");
|
|
33
|
+
expect(sessionContext.agentInstructions).not.toContain(
|
|
34
|
+
"Do not invent product capabilities"
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { extractSettingsLinkButtons } from "../platform/link-buttons";
|
|
3
|
+
|
|
4
|
+
describe("extractSettingsLinkButtons", () => {
|
|
5
|
+
test("extracts settings link and replaces with label", () => {
|
|
6
|
+
const content =
|
|
7
|
+
"Click [Open Settings](https://example.com/connect/claim?claim=abc123) to continue";
|
|
8
|
+
const { processedContent, linkButtons } =
|
|
9
|
+
extractSettingsLinkButtons(content);
|
|
10
|
+
|
|
11
|
+
expect(linkButtons).toHaveLength(1);
|
|
12
|
+
expect(linkButtons[0]!.text).toBe("Open Settings");
|
|
13
|
+
expect(linkButtons[0]!.url).toBe(
|
|
14
|
+
"https://example.com/connect/claim?claim=abc123"
|
|
15
|
+
);
|
|
16
|
+
expect(processedContent).toBe("Click Open Settings to continue");
|
|
17
|
+
expect(processedContent).not.toContain("https://");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("extracts settings link with agent param", () => {
|
|
21
|
+
const content =
|
|
22
|
+
"[Settings](https://example.com/connect/claim?claim=abc&agent=agent-1)";
|
|
23
|
+
const { linkButtons } = extractSettingsLinkButtons(content);
|
|
24
|
+
|
|
25
|
+
expect(linkButtons).toHaveLength(1);
|
|
26
|
+
expect(linkButtons[0]!.url).toBe(
|
|
27
|
+
"https://example.com/connect/claim?claim=abc&agent=agent-1"
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("extracts multiple settings links", () => {
|
|
32
|
+
const content =
|
|
33
|
+
"[First](https://a.com/connect/claim?claim=1) and [Second](https://b.com/connect/claim?claim=2)";
|
|
34
|
+
const { processedContent, linkButtons } =
|
|
35
|
+
extractSettingsLinkButtons(content);
|
|
36
|
+
|
|
37
|
+
expect(linkButtons).toHaveLength(2);
|
|
38
|
+
expect(processedContent).toBe("First and Second");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("filters out localhost URLs", () => {
|
|
42
|
+
const content =
|
|
43
|
+
"[Settings](http://localhost:3000/connect/claim?claim=token)";
|
|
44
|
+
const { processedContent, linkButtons } =
|
|
45
|
+
extractSettingsLinkButtons(content);
|
|
46
|
+
|
|
47
|
+
expect(linkButtons).toHaveLength(0);
|
|
48
|
+
// Label still replaces the link
|
|
49
|
+
expect(processedContent).toBe("Settings");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("filters out 127.0.0.1 URLs", () => {
|
|
53
|
+
const content = "[Settings](http://127.0.0.1/connect/claim?claim=token)";
|
|
54
|
+
const { linkButtons } = extractSettingsLinkButtons(content);
|
|
55
|
+
expect(linkButtons).toHaveLength(0);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("does not match non-settings links", () => {
|
|
59
|
+
const content = "[Home](https://example.com/home)";
|
|
60
|
+
const { processedContent, linkButtons } =
|
|
61
|
+
extractSettingsLinkButtons(content);
|
|
62
|
+
|
|
63
|
+
expect(linkButtons).toHaveLength(0);
|
|
64
|
+
expect(processedContent).toBe(content); // unchanged
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("does not match links without claim= parameter", () => {
|
|
68
|
+
const content = "[Settings](https://example.com/agent)";
|
|
69
|
+
const { processedContent, linkButtons } =
|
|
70
|
+
extractSettingsLinkButtons(content);
|
|
71
|
+
|
|
72
|
+
expect(linkButtons).toHaveLength(0);
|
|
73
|
+
expect(processedContent).toBe(content);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("returns empty buttons for content without links", () => {
|
|
77
|
+
const content = "No links here, just plain text";
|
|
78
|
+
const { processedContent, linkButtons } =
|
|
79
|
+
extractSettingsLinkButtons(content);
|
|
80
|
+
|
|
81
|
+
expect(linkButtons).toHaveLength(0);
|
|
82
|
+
expect(processedContent).toBe(content);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("handles HTTP and HTTPS", () => {
|
|
86
|
+
const httpContent = "[A](http://example.com/connect/claim?claim=x)";
|
|
87
|
+
const httpsContent = "[B](https://example.com/connect/claim?claim=y)";
|
|
88
|
+
|
|
89
|
+
const httpResult = extractSettingsLinkButtons(httpContent);
|
|
90
|
+
const httpsResult = extractSettingsLinkButtons(httpsContent);
|
|
91
|
+
|
|
92
|
+
expect(httpResult.linkButtons).toHaveLength(1);
|
|
93
|
+
expect(httpsResult.linkButtons).toHaveLength(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("mixed localhost and remote links only keeps remote", () => {
|
|
97
|
+
const content =
|
|
98
|
+
"[Local](http://localhost/connect/claim?claim=a) and [Remote](https://app.com/connect/claim?claim=b)";
|
|
99
|
+
const { linkButtons } = extractSettingsLinkButtons(content);
|
|
100
|
+
|
|
101
|
+
expect(linkButtons).toHaveLength(1);
|
|
102
|
+
expect(linkButtons[0]!.url).toContain("app.com");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("keeps backward compatibility with legacy /agent claim links", () => {
|
|
106
|
+
const content = "[Legacy](https://example.com/agent?claim=legacy)";
|
|
107
|
+
const { linkButtons } = extractSettingsLinkButtons(content);
|
|
108
|
+
|
|
109
|
+
expect(linkButtons).toHaveLength(1);
|
|
110
|
+
expect(linkButtons[0]!.url).toBe("https://example.com/agent?claim=legacy");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { Lobu } from "../lobu";
|
|
3
|
+
|
|
4
|
+
const originalMemoryUrl = process.env.MEMORY_URL;
|
|
5
|
+
const originalAdminPassword = process.env.ADMIN_PASSWORD;
|
|
6
|
+
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
if (originalMemoryUrl === undefined) {
|
|
9
|
+
delete process.env.MEMORY_URL;
|
|
10
|
+
} else {
|
|
11
|
+
process.env.MEMORY_URL = originalMemoryUrl;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (originalAdminPassword === undefined) {
|
|
15
|
+
delete process.env.ADMIN_PASSWORD;
|
|
16
|
+
} else {
|
|
17
|
+
process.env.ADMIN_PASSWORD = originalAdminPassword;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("Lobu", () => {
|
|
22
|
+
test("applies config.memory to the gateway environment", () => {
|
|
23
|
+
delete process.env.MEMORY_URL;
|
|
24
|
+
|
|
25
|
+
new Lobu({
|
|
26
|
+
redis: "redis://localhost:6379",
|
|
27
|
+
memory: "https://memory.example.com",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
expect(process.env.MEMORY_URL).toBe("https://memory.example.com");
|
|
31
|
+
});
|
|
32
|
+
});
|