Skip to content

Commit 4f7fbcb

Browse files
steipetePeter Steinberger
andauthored
fix(gateway): preserve icon metadata for HEAD requests (#117079)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent f455eb7 commit 4f7fbcb

2 files changed

Lines changed: 144 additions & 27 deletions

File tree

‎src/gateway/plugin-icon-http.test.ts‎

Lines changed: 139 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ const PNG_BYTES = Buffer.from(
4848
"base64",
4949
);
5050
const NORMALIZED_PNG_BYTES = Buffer.from("normalized-png");
51+
const CATALOG_ICON_URL = "https://cdn.example.test/setup-tool.svg";
52+
const ICON_ROUTES = [
53+
{ label: "plugin", pathname: "/__openclaw__/plugin-icon/firecrawl" },
54+
{
55+
label: "catalog",
56+
pathname: `/__openclaw__/catalog-icon/${encodeURIComponent(CATALOG_ICON_URL)}`,
57+
},
58+
] as const;
5159

5260
let port = 0;
5361
let server: ReturnType<typeof createServer>;
@@ -85,6 +93,7 @@ beforeEach(() => {
8593
clearPluginIconCacheForTest();
8694
vi.clearAllMocks();
8795
configForRequest = () => testConfig;
96+
mocks.authorize.mockReset();
8897
mocks.authorize.mockResolvedValue({
8998
authMethod: "token",
9099
trustDeclaredOperatorScopes: false,
@@ -112,20 +121,68 @@ function request(pathname: string, options?: { token?: string; method?: string }
112121
});
113122
}
114123

115-
describe("GET /__openclaw__/plugin-icon/:pluginId", () => {
116-
it("requires gateway authentication before resolving plugin metadata", async () => {
117-
mocks.authorize.mockImplementationOnce(async ({ res }) => {
118-
res.statusCode = 401;
119-
res.end();
120-
return null;
121-
});
124+
describe("Control UI plugin and catalog icon routes", () => {
125+
it.each(
126+
ICON_ROUTES.flatMap(({ label, pathname }) =>
127+
(["GET", "HEAD"] as const).map((method) => ({ label, method, pathname })),
128+
),
129+
)(
130+
"authenticates $method $label icons before resolving their metadata",
131+
async ({ method, pathname }) => {
132+
mocks.authorize.mockImplementationOnce(async ({ res }) => {
133+
res.statusCode = 401;
134+
res.end();
135+
return null;
136+
});
122137

123-
const response = await request("/__openclaw__/plugin-icon/firecrawl", { token: "" });
138+
const response = await request(pathname, { method, token: "" });
139+
140+
expect(response.status).toBe(401);
141+
expect(mocks.resolveIconUrl).not.toHaveBeenCalled();
142+
expect(mocks.resolveCatalogIconUrl).not.toHaveBeenCalled();
143+
expect(mocks.readRemoteMediaBuffer).not.toHaveBeenCalled();
144+
},
145+
);
146+
147+
it.each(
148+
ICON_ROUTES.flatMap(({ label, pathname }) =>
149+
(["image/png", "image/svg+xml"] as const).map((contentType) => ({
150+
contentType,
151+
label,
152+
pathname,
153+
})),
154+
),
155+
)(
156+
"preserves every $contentType $label GET header on a bodyless HEAD",
157+
async ({ contentType, pathname }) => {
158+
const svg = "<svg xmlns='http://www.w3.org/2000/svg'></svg>";
159+
if (contentType === "image/svg+xml") {
160+
mocks.readRemoteMediaBuffer.mockResolvedValue({ buffer: Buffer.from(svg), contentType });
161+
}
124162

125-
expect(response.status).toBe(401);
126-
expect(mocks.resolveIconUrl).not.toHaveBeenCalled();
127-
expect(mocks.readRemoteMediaBuffer).not.toHaveBeenCalled();
128-
});
163+
const get = await request(pathname);
164+
const head = await request(pathname, { method: "HEAD" });
165+
166+
expect(get.status).toBe(200);
167+
expect(head.status).toBe(200);
168+
for (const name of [
169+
"cache-control",
170+
"content-disposition",
171+
"content-length",
172+
"content-security-policy",
173+
"content-type",
174+
"cross-origin-resource-policy",
175+
"x-content-type-options",
176+
]) {
177+
expect(head.headers.get(name), name).toBe(get.headers.get(name));
178+
}
179+
const expectedBody =
180+
contentType === "image/svg+xml" ? Buffer.from(svg) : NORMALIZED_PNG_BYTES;
181+
expect(Buffer.from(await get.arrayBuffer())).toEqual(expectedBody);
182+
expect((await head.arrayBuffer()).byteLength).toBe(0);
183+
expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(1);
184+
},
185+
);
129186

130187
it("resolves by plugin identity and ignores arbitrary remote URL parameters", async () => {
131188
const response = await request(
@@ -167,7 +224,7 @@ describe("GET /__openclaw__/plugin-icon/:pluginId", () => {
167224
});
168225

169226
it("resolves encoded catalog URLs through the server-owned allowlist", async () => {
170-
const iconUrl = "https://cdn.example.test/setup-tool.svg";
227+
const iconUrl = CATALOG_ICON_URL;
171228
const response = await request(`/__openclaw__/catalog-icon/${encodeURIComponent(iconUrl)}`);
172229

173230
expect(response.status).toBe(200);
@@ -218,6 +275,53 @@ describe("GET /__openclaw__/plugin-icon/:pluginId", () => {
218275
expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(1);
219276
});
220277

278+
it.each(ICON_ROUTES)(
279+
"shares one $label icon download across concurrent GET and HEAD",
280+
async ({ pathname }) => {
281+
const [get, head] = await Promise.all([
282+
request(pathname),
283+
request(pathname, { method: "HEAD" }),
284+
]);
285+
286+
expect(get.status).toBe(200);
287+
expect(head.status).toBe(200);
288+
expect((await head.arrayBuffer()).byteLength).toBe(0);
289+
expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(1);
290+
},
291+
);
292+
293+
it.each(ICON_ROUTES)(
294+
"reuses a $label icon loaded by HEAD on a later GET",
295+
async ({ pathname }) => {
296+
const head = await request(pathname, { method: "HEAD" });
297+
const get = await request(pathname);
298+
299+
expect(head.status).toBe(200);
300+
expect(get.status).toBe(200);
301+
expect(head.headers.get("content-length")).toBe(get.headers.get("content-length"));
302+
expect((await head.arrayBuffer()).byteLength).toBe(0);
303+
expect(Buffer.from(await get.arrayBuffer())).toEqual(NORMALIZED_PNG_BYTES);
304+
expect(mocks.readRemoteMediaBuffer).toHaveBeenCalledTimes(1);
305+
},
306+
);
307+
308+
it.each(ICON_ROUTES)(
309+
"returns a bodyless 404 for an unavailable $label icon HEAD",
310+
async ({ label, pathname }) => {
311+
if (label === "plugin") {
312+
mocks.resolveIconUrl.mockResolvedValueOnce(undefined);
313+
} else {
314+
mocks.resolveCatalogIconUrl.mockReturnValueOnce(undefined);
315+
}
316+
317+
const response = await request(pathname, { method: "HEAD" });
318+
319+
expect(response.status).toBe(404);
320+
expect((await response.arrayBuffer()).byteLength).toBe(0);
321+
expect(mocks.readRemoteMediaBuffer).not.toHaveBeenCalled();
322+
},
323+
);
324+
221325
it("accepts one canonical scoped plugin id encoded as a single path segment", async () => {
222326
const response = await request(
223327
`/__openclaw__/plugin-icon/${encodeURIComponent("@expediagroup/expedia-openclaw")}`,
@@ -279,13 +383,17 @@ describe("GET /__openclaw__/plugin-icon/:pluginId", () => {
279383
expect(failed.status).toBe(404);
280384
});
281385

282-
it("rejects non-GET methods without loading metadata", async () => {
283-
const response = await request("/__openclaw__/plugin-icon/firecrawl", { method: "POST" });
386+
it.each(ICON_ROUTES)(
387+
"rejects non-read $label icon methods without loading metadata",
388+
async ({ pathname }) => {
389+
const response = await request(pathname, { method: "POST" });
284390

285-
expect(response.status).toBe(405);
286-
expect(response.headers.get("allow")).toBe("GET");
287-
expect(mocks.resolveIconUrl).not.toHaveBeenCalled();
288-
});
391+
expect(response.status).toBe(405);
392+
expect(response.headers.get("allow")).toBe("GET, HEAD");
393+
expect(mocks.resolveIconUrl).not.toHaveBeenCalled();
394+
expect(mocks.resolveCatalogIconUrl).not.toHaveBeenCalled();
395+
},
396+
);
289397

290398
it("matches the configured Control UI base path", async () => {
291399
const handledServer = createServer((req, res) => {
@@ -306,11 +414,18 @@ describe("GET /__openclaw__/plugin-icon/:pluginId", () => {
306414
});
307415
try {
308416
const handledPort = (handledServer.address() as AddressInfo).port;
309-
const response = await fetch(
310-
`http://127.0.0.1:${handledPort}/openclaw/__openclaw__/plugin-icon/firecrawl`,
311-
{ headers: { Authorization: "Bearer test-token" } },
312-
);
313-
expect(response.status).toBe(200);
417+
for (const { pathname } of ICON_ROUTES) {
418+
for (const method of ["GET", "HEAD"]) {
419+
const response = await fetch(`http://127.0.0.1:${handledPort}/openclaw${pathname}`, {
420+
headers: { Authorization: "Bearer test-token" },
421+
method,
422+
});
423+
expect(response.status).toBe(200);
424+
if (method === "HEAD") {
425+
expect((await response.arrayBuffer()).byteLength).toBe(0);
426+
}
427+
}
428+
}
314429
} finally {
315430
await new Promise<void>((resolve, reject) => {
316431
handledServer.close((error) => (error ? reject(error) : resolve()));

‎src/gateway/plugin-icon-http.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ export async function handlePluginIconHttpRequest(
269269
if (!pluginId && !catalogIconUrl) {
270270
return false;
271271
}
272-
if (req.method !== "GET") {
273-
sendMethodNotAllowed(res, "GET");
272+
const method = req.method;
273+
if (method !== "GET" && method !== "HEAD") {
274+
sendMethodNotAllowed(res, "GET, HEAD");
274275
return true;
275276
}
276277
const requestAuth = await authorizeGatewayHttpRequestOrReply({
@@ -322,6 +323,7 @@ export async function handlePluginIconHttpRequest(
322323
"default-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors 'none'; sandbox",
323324
);
324325
res.setHeader("content-disposition", 'attachment; filename="plugin-icon"');
325-
res.end(icon.body);
326+
// HEAD uses the same authenticated, cached representation; only its body is omitted.
327+
res.end(method === "HEAD" ? undefined : icon.body);
326328
return true;
327329
}

0 commit comments

Comments
 (0)