|
| 1 | +// Gateway plugin icon HTTP tests cover authenticated identity lookup, bounded |
| 2 | +// remote loading, SVG normalization, caching, and failure fallback behavior. |
| 3 | +import { createServer } from "node:http"; |
| 4 | +import type { AddressInfo } from "node:net"; |
| 5 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + authorize: vi.fn(), |
| 9 | + encodeImage: vi.fn(), |
| 10 | + readImageMetadata: vi.fn(), |
| 11 | + readRemoteMediaBuffer: vi.fn(), |
| 12 | + resolveIconUrl: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("./http-utils.js", () => ({ |
| 16 | + authorizeGatewayHttpRequestOrReply: (...args: unknown[]) => mocks.authorize(...args), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../media/fetch.js", () => ({ |
| 20 | + readRemoteMediaBuffer: (...args: unknown[]) => mocks.readRemoteMediaBuffer(...args), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("../media/image-ops.js", () => ({ |
| 24 | + createImageProcessor: () => ({ |
| 25 | + encode: (...args: unknown[]) => mocks.encodeImage(...args), |
| 26 | + }), |
| 27 | + MAX_IMAGE_INPUT_PIXELS: 25_000_000, |
| 28 | + readImageMetadataFromHeader: (...args: unknown[]) => mocks.readImageMetadata(...args), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("../plugins/management-service.js", () => ({ |
| 32 | + resolveManagedPluginIconUrl: (...args: unknown[]) => mocks.resolveIconUrl(...args), |
| 33 | +})); |
| 34 | + |
| 35 | +const { |
| 36 | + clearPluginIconCacheForTest, |
| 37 | + handlePluginIconHttpRequest, |
| 38 | + PLUGIN_ICON_CACHE_TTL_MS, |
| 39 | + PLUGIN_ICON_MAX_BYTES, |
| 40 | + PLUGIN_ICON_MAX_REDIRECTS, |
| 41 | + PLUGIN_ICON_REQUEST_TIMEOUT_MS, |
| 42 | +} = await import("./plugin-icon-http.js"); |
| 43 | + |
| 44 | +const PNG_BYTES = Buffer.from( |
| 45 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9Zb0YAAAAASUVORK5CYII=", |
| 46 | + "base64", |
| 47 | +); |
| 48 | +const NORMALIZED_PNG_BYTES = Buffer.from("normalized-png"); |
| 49 | + |
| 50 | +let port = 0; |
| 51 | +let server: ReturnType<typeof createServer>; |
| 52 | +const testConfig = {}; |
| 53 | +let configForRequest = () => testConfig; |
| 54 | + |
| 55 | +beforeAll(async () => { |
| 56 | + server = createServer((req, res) => { |
| 57 | + void handlePluginIconHttpRequest(req, res, { |
| 58 | + auth: { mode: "token", token: "test-token", allowTailscale: false }, |
| 59 | + config: configForRequest(), |
| 60 | + }).then((handled) => { |
| 61 | + if (!handled) { |
| 62 | + res.statusCode = 404; |
| 63 | + res.end("unhandled"); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + await new Promise<void>((resolve, reject) => { |
| 68 | + server.once("error", reject); |
| 69 | + server.listen(0, "127.0.0.1", () => { |
| 70 | + port = (server.address() as AddressInfo).port; |
| 71 | + resolve(); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +afterAll(async () => { |
| 77 | + await new Promise<void>((resolve, reject) => { |
| 78 | + server.close((error) => (error ? reject(error) : resolve())); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +beforeEach(() => { |
| 83 | + clearPluginIconCacheForTest(); |
| 84 | + vi.clearAllMocks(); |
| 85 | + configForRequest = () => testConfig; |
| 86 | + mocks.authorize.mockResolvedValue({ |
| 87 | + authMethod: "token", |
| 88 | + trustDeclaredOperatorScopes: false, |
| 89 | + }); |
| 90 | + mocks.resolveIconUrl.mockResolvedValue("https://cdn.example.test/plugin.svg"); |
| 91 | + mocks.readImageMetadata.mockReturnValue({ width: 1, height: 1 }); |
| 92 | + mocks.encodeImage.mockResolvedValue({ data: NORMALIZED_PNG_BYTES }); |
| 93 | + mocks.readRemoteMediaBuffer.mockResolvedValue({ |
| 94 | + buffer: PNG_BYTES, |
| 95 | + contentType: "image/png", |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +function request(pathname: string, options?: { token?: string; method?: string }) { |
| 100 | + const headers: Record<string, string> = {}; |
| 101 | + if (options?.token === undefined) { |
| 102 | + headers.Authorization = "Bearer test-token"; |
| 103 | + } else if (options.token) { |
| 104 | + headers.Authorization = `Bearer ${options.token}`; |
| 105 | + } |
| 106 | + return fetch(`http://127.0.0.1:${port}${pathname}`, { |
| 107 | + method: options?.method ?? "GET", |
| 108 | + headers, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +describe("GET /__openclaw__/plugin-icon/:pluginId", () => { |
| 113 | + it("requires gateway authentication before resolving plugin metadata", async () => { |
| 114 | + mocks.authorize.mockImplementationOnce(async ({ res }) => { |
| 115 | + res.statusCode = 401; |
| 116 | + res.end(); |
| 117 | + return null; |
| 118 | + }); |
| 119 | + |
| 120 | + const response = await request("/__openclaw__/plugin-icon/firecrawl", { token: "" }); |
| 121 | + |
| 122 | + expect(response.status).toBe(401); |
| 123 | + expect(mocks.resolveIconUrl).not.toHaveBeenCalled(); |
| 124 | + expect(mocks.readRemoteMediaBuffer).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + |
| 127 | + it("resolves by plugin identity and ignores arbitrary remote URL parameters", async () => { |
| 128 | + const response = await request( |
| 129 | + "/__openclaw__/plugin-icon/firecrawl?url=http%3A%2F%2F127.0.0.1%2Fsecret", |
| 130 | + ); |
| 131 | + |
| 132 | + expect(response.status).toBe(200); |
| 133 | + expect(response.headers.get("content-type")).toBe("image/png"); |
| 134 | + expect(response.headers.get("cache-control")).toBe("private, max-age=3600"); |
| 135 | + expect(response.headers.get("content-disposition")).toBe('attachment; filename="plugin-icon"'); |
| 136 | + expect(response.headers.get("x-content-type-options")).toBe("nosniff"); |
| 137 | + expect(Buffer.from(await response.arrayBuffer())).toEqual(NORMALIZED_PNG_BYTES); |
| 138 | + expect(mocks.resolveIconUrl).toHaveBeenCalledWith({ |
| 139 | + config: testConfig, |
| 140 | + pluginId: "firecrawl", |
| 141 | + }); |
| 142 | + expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledWith({ |
| 143 | + url: "https://cdn.example.test/plugin.svg", |
| 144 | + maxBytes: PLUGIN_ICON_MAX_BYTES, |
| 145 | + maxRedirects: PLUGIN_ICON_MAX_REDIRECTS, |
| 146 | + timeoutMs: PLUGIN_ICON_REQUEST_TIMEOUT_MS, |
| 147 | + responseHeaderTimeoutMs: PLUGIN_ICON_REQUEST_TIMEOUT_MS, |
| 148 | + readIdleTimeoutMs: PLUGIN_ICON_REQUEST_TIMEOUT_MS, |
| 149 | + requestInit: { |
| 150 | + headers: { |
| 151 | + Accept: "image/avif,image/webp,image/png,image/jpeg,image/gif,image/svg+xml", |
| 152 | + }, |
| 153 | + }, |
| 154 | + }); |
| 155 | + expect(mocks.encodeImage).toHaveBeenCalledWith(PNG_BYTES, { |
| 156 | + format: "png", |
| 157 | + compressionLevel: 9, |
| 158 | + resize: { |
| 159 | + fit: "inside", |
| 160 | + maxSide: 256, |
| 161 | + enlarge: false, |
| 162 | + }, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("serves SVG only as a sandboxed attachment for browser-side rasterization", async () => { |
| 167 | + const svg = "<svg xmlns='http://www.w3.org/2000/svg'></svg>"; |
| 168 | + mocks.readRemoteMediaBuffer.mockResolvedValueOnce({ |
| 169 | + buffer: Buffer.from(svg), |
| 170 | + contentType: "image/svg+xml", |
| 171 | + }); |
| 172 | + |
| 173 | + const response = await request("/__openclaw__/plugin-icon/simple-icons"); |
| 174 | + |
| 175 | + expect(response.status).toBe(200); |
| 176 | + expect(response.headers.get("content-type")).toBe("image/svg+xml"); |
| 177 | + expect(response.headers.get("content-disposition")).toBe('attachment; filename="plugin-icon"'); |
| 178 | + expect(response.headers.get("content-security-policy")).toContain("sandbox"); |
| 179 | + expect(response.headers.get("cross-origin-resource-policy")).toBe("same-origin"); |
| 180 | + expect(Buffer.from(await response.arrayBuffer()).toString()).toBe(svg); |
| 181 | + }); |
| 182 | + |
| 183 | + it("reuses successful icon bytes from the bounded process cache", async () => { |
| 184 | + configForRequest = () => ({}); |
| 185 | + const first = await request("/__openclaw__/plugin-icon/firecrawl"); |
| 186 | + const second = await request("/__openclaw__/plugin-icon/firecrawl"); |
| 187 | + |
| 188 | + expect(first.status).toBe(200); |
| 189 | + expect(second.status).toBe(200); |
| 190 | + expect(mocks.resolveIconUrl).toHaveBeenCalledTimes(2); |
| 191 | + expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(1); |
| 192 | + }); |
| 193 | + |
| 194 | + it("accepts one canonical scoped plugin id encoded as a single path segment", async () => { |
| 195 | + const response = await request( |
| 196 | + `/__openclaw__/plugin-icon/${encodeURIComponent("@expediagroup/expedia-openclaw")}`, |
| 197 | + ); |
| 198 | + |
| 199 | + expect(response.status).toBe(200); |
| 200 | + expect(mocks.resolveIconUrl).toHaveBeenCalledWith({ |
| 201 | + config: testConfig, |
| 202 | + pluginId: "@expediagroup/expedia-openclaw", |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + it("refreshes cached icon bytes after the cache lifetime", async () => { |
| 207 | + const now = vi.spyOn(Date, "now").mockReturnValue(1_000); |
| 208 | + try { |
| 209 | + const first = await request("/__openclaw__/plugin-icon/firecrawl"); |
| 210 | + const cached = await request("/__openclaw__/plugin-icon/firecrawl"); |
| 211 | + now.mockReturnValue(1_000 + PLUGIN_ICON_CACHE_TTL_MS + 1); |
| 212 | + const refreshed = await request("/__openclaw__/plugin-icon/firecrawl"); |
| 213 | + |
| 214 | + expect(first.status).toBe(200); |
| 215 | + expect(cached.status).toBe(200); |
| 216 | + expect(refreshed.status).toBe(200); |
| 217 | + expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(2); |
| 218 | + } finally { |
| 219 | + now.mockRestore(); |
| 220 | + } |
| 221 | + }); |
| 222 | + |
| 223 | + it("returns not found when metadata is absent or remote image validation fails", async () => { |
| 224 | + mocks.resolveIconUrl.mockResolvedValueOnce(undefined); |
| 225 | + const missing = await request("/__openclaw__/plugin-icon/missing"); |
| 226 | + expect(missing.status).toBe(404); |
| 227 | + |
| 228 | + mocks.readRemoteMediaBuffer.mockResolvedValueOnce({ |
| 229 | + buffer: Buffer.from("<html>nope</html>"), |
| 230 | + contentType: "text/html", |
| 231 | + }); |
| 232 | + const invalid = await request("/__openclaw__/plugin-icon/not-an-image"); |
| 233 | + expect(invalid.status).toBe(404); |
| 234 | + |
| 235 | + mocks.readRemoteMediaBuffer.mockResolvedValueOnce({ |
| 236 | + buffer: Buffer.from("<html>still nope</html>"), |
| 237 | + contentType: "image/png", |
| 238 | + }); |
| 239 | + const mislabeled = await request("/__openclaw__/plugin-icon/mislabeled"); |
| 240 | + expect(mislabeled.status).toBe(404); |
| 241 | + |
| 242 | + mocks.readImageMetadata.mockReturnValueOnce({ width: 10_000, height: 10_000 }); |
| 243 | + mocks.readRemoteMediaBuffer.mockResolvedValueOnce({ |
| 244 | + buffer: PNG_BYTES, |
| 245 | + contentType: "image/png", |
| 246 | + }); |
| 247 | + const oversized = await request("/__openclaw__/plugin-icon/oversized"); |
| 248 | + expect(oversized.status).toBe(404); |
| 249 | + |
| 250 | + mocks.readRemoteMediaBuffer.mockRejectedValueOnce(new Error("upstream failed")); |
| 251 | + const failed = await request("/__openclaw__/plugin-icon/broken"); |
| 252 | + expect(failed.status).toBe(404); |
| 253 | + }); |
| 254 | + |
| 255 | + it("rejects non-GET methods without loading metadata", async () => { |
| 256 | + const response = await request("/__openclaw__/plugin-icon/firecrawl", { method: "POST" }); |
| 257 | + |
| 258 | + expect(response.status).toBe(405); |
| 259 | + expect(response.headers.get("allow")).toBe("GET"); |
| 260 | + expect(mocks.resolveIconUrl).not.toHaveBeenCalled(); |
| 261 | + }); |
| 262 | + |
| 263 | + it("matches the configured Control UI base path", async () => { |
| 264 | + const handledServer = createServer((req, res) => { |
| 265 | + void handlePluginIconHttpRequest(req, res, { |
| 266 | + auth: { mode: "token", token: "test-token", allowTailscale: false }, |
| 267 | + config: {}, |
| 268 | + basePath: "/openclaw", |
| 269 | + }).then((handled) => { |
| 270 | + if (!handled) { |
| 271 | + res.statusCode = 404; |
| 272 | + res.end("unhandled"); |
| 273 | + } |
| 274 | + }); |
| 275 | + }); |
| 276 | + await new Promise<void>((resolve, reject) => { |
| 277 | + handledServer.once("error", reject); |
| 278 | + handledServer.listen(0, "127.0.0.1", resolve); |
| 279 | + }); |
| 280 | + try { |
| 281 | + const handledPort = (handledServer.address() as AddressInfo).port; |
| 282 | + const response = await fetch( |
| 283 | + `http://127.0.0.1:${handledPort}/openclaw/__openclaw__/plugin-icon/firecrawl`, |
| 284 | + { headers: { Authorization: "Bearer test-token" } }, |
| 285 | + ); |
| 286 | + expect(response.status).toBe(200); |
| 287 | + } finally { |
| 288 | + await new Promise<void>((resolve, reject) => { |
| 289 | + handledServer.close((error) => (error ? reject(error) : resolve())); |
| 290 | + }); |
| 291 | + } |
| 292 | + }); |
| 293 | +}); |
0 commit comments