Skip to content

Commit 8bd8703

Browse files
committed
fix(gateway): claim malformed plugin icon routes
1 parent 26a58bc commit 8bd8703

2 files changed

Lines changed: 57 additions & 30 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ const ICON_ROUTES = [
5656
pathname: `/__openclaw__/catalog-icon/${encodeURIComponent(CATALOG_ICON_URL)}`,
5757
},
5858
] as const;
59+
const INVALID_ICON_ROUTES = [
60+
{ label: "blank plugin id", pathname: "/__openclaw__/plugin-icon/" },
61+
{ label: "invalid plugin id", pathname: "/__openclaw__/plugin-icon/%20" },
62+
{ label: "malformed plugin id", pathname: "/__openclaw__/plugin-icon/%zz" },
63+
{ label: "nested plugin id", pathname: "/__openclaw__/plugin-icon/one/two" },
64+
{ label: "blank catalog URL", pathname: "/__openclaw__/catalog-icon/" },
65+
{ label: "malformed catalog URL", pathname: "/__openclaw__/catalog-icon/%zz" },
66+
{ label: "nested catalog URL", pathname: "/__openclaw__/catalog-icon/one/two" },
67+
] as const;
5968

6069
let port = 0;
6170
let server: ReturnType<typeof createServer>;
@@ -122,6 +131,20 @@ function request(pathname: string, options?: { token?: string; method?: string }
122131
}
123132

124133
describe("Control UI plugin and catalog icon routes", () => {
134+
it.each(INVALID_ICON_ROUTES)(
135+
"claims $label instead of falling through to the Control UI",
136+
async ({ pathname }) => {
137+
const response = await request(pathname);
138+
139+
expect(response.status).toBe(404);
140+
await expect(response.text()).resolves.toBe("Not Found");
141+
expect(mocks.authorize).toHaveBeenCalledOnce();
142+
expect(mocks.resolveIconUrl).not.toHaveBeenCalled();
143+
expect(mocks.resolveCatalogIconUrl).not.toHaveBeenCalled();
144+
expect(mocks.readRemoteMediaBuffer).not.toHaveBeenCalled();
145+
},
146+
);
147+
125148
it.each(
126149
ICON_ROUTES.flatMap(({ label, pathname }) =>
127150
(["GET", "HEAD"] as const).map((method) => ({ label, method, pathname })),

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,47 @@ export function resolvePluginIconRoutePrefix(basePath?: string): string {
6666
return `${normalizeBasePath(basePath)}${CONTROL_UI_PLUGIN_ICON_PATH_PREFIX}/`;
6767
}
6868

69-
function parsePluginIconRequest(urlRaw: string | undefined, basePath?: string): string | null {
69+
type IconRequestPathResolution = { matched: false } | { matched: true; value: string | null };
70+
71+
function parseIconRequestPath(
72+
urlRaw: string | undefined,
73+
prefix: string,
74+
isValid: (value: string) => boolean = Boolean,
75+
): IconRequestPathResolution {
7076
if (!urlRaw) {
71-
return null;
77+
return { matched: false };
7278
}
7379
const pathname = new URL(urlRaw, "http://localhost").pathname;
74-
const prefix = resolvePluginIconRoutePrefix(basePath);
7580
if (!pathname.startsWith(prefix)) {
76-
return null;
81+
return { matched: false };
7782
}
78-
const encodedPluginId = pathname.slice(prefix.length);
79-
if (!encodedPluginId || encodedPluginId.includes("/")) {
80-
return null;
83+
const encodedValue = pathname.slice(prefix.length);
84+
if (!encodedValue || encodedValue.includes("/")) {
85+
return { matched: true, value: null };
8186
}
8287
try {
83-
const pluginId = decodeURIComponent(encodedPluginId);
84-
return PLUGIN_ID_RE.test(pluginId) ? pluginId : null;
88+
const value = decodeURIComponent(encodedValue);
89+
return { matched: true, value: isValid(value) ? value : null };
8590
} catch {
86-
return null;
91+
return { matched: true, value: null };
8792
}
8893
}
8994

90-
function parseCatalogIconRequest(urlRaw: string | undefined, basePath?: string): string | null {
91-
if (!urlRaw) {
92-
return null;
93-
}
94-
const pathname = new URL(urlRaw, "http://localhost").pathname;
95+
function parsePluginIconRequest(
96+
urlRaw: string | undefined,
97+
basePath?: string,
98+
): IconRequestPathResolution {
99+
return parseIconRequestPath(urlRaw, resolvePluginIconRoutePrefix(basePath), (value) =>
100+
PLUGIN_ID_RE.test(value),
101+
);
102+
}
103+
104+
function parseCatalogIconRequest(
105+
urlRaw: string | undefined,
106+
basePath?: string,
107+
): IconRequestPathResolution {
95108
const prefix = `${normalizeBasePath(basePath)}${CONTROL_UI_CATALOG_ICON_PATH_PREFIX}/`;
96-
if (!pathname.startsWith(prefix)) {
97-
return null;
98-
}
99-
const encodedIconUrl = pathname.slice(prefix.length);
100-
if (!encodedIconUrl || encodedIconUrl.includes("/")) {
101-
return null;
102-
}
103-
try {
104-
return decodeURIComponent(encodedIconUrl) || null;
105-
} catch {
106-
return null;
107-
}
109+
return parseIconRequestPath(urlRaw, prefix);
108110
}
109111

110112
function normalizeMimeType(contentType: string | undefined): string | undefined {
@@ -259,11 +261,13 @@ export async function handlePluginIconHttpRequest(
259261
rateLimiter?: AuthRateLimiter;
260262
},
261263
): Promise<boolean> {
262-
const pluginId = parsePluginIconRequest(req.url, opts.basePath);
263-
const catalogIconUrl = parseCatalogIconRequest(req.url, opts.basePath);
264-
if (!pluginId && !catalogIconUrl) {
264+
const pluginRequest = parsePluginIconRequest(req.url, opts.basePath);
265+
const catalogRequest = parseCatalogIconRequest(req.url, opts.basePath);
266+
if (!pluginRequest.matched && !catalogRequest.matched) {
265267
return false;
266268
}
269+
const pluginId = pluginRequest.matched ? pluginRequest.value : null;
270+
const catalogIconUrl = catalogRequest.matched ? catalogRequest.value : null;
267271
const method = req.method;
268272
if (method !== "GET" && method !== "HEAD") {
269273
sendMethodNotAllowed(res, "GET, HEAD");

0 commit comments

Comments
 (0)