Skip to content

Commit b884373

Browse files
steipeteLZY3538
andcommitted
fix(control-ui): inline workspace assistant avatars
Project workspace-local assistant avatars into browser-safe data URLs across bootstrap and agent identity RPCs while preserving same-origin avatar routes and shared size limits. Co-authored-by: LZY3538 <[email protected]>
1 parent 8d0fd8b commit b884373

14 files changed

Lines changed: 550 additions & 264 deletions

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
195195
),
196196
publicExports: readPluginSdkSurfaceBudgetEnv(
197197
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS",
198-
10488,
198+
10489,
199199
env,
200200
),
201201
publicFunctionExports: readPluginSdkSurfaceBudgetEnv(
202202
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS",
203-
5235,
203+
5236,
204204
env,
205205
),
206206
publicDeprecatedExports: readPluginSdkSurfaceBudgetEnv(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Projects resolved agent avatars into browser-safe URLs for internal Gateway/UI payloads.
3+
*/
4+
import fs from "node:fs";
5+
import type { AgentAvatarResolution } from "./identity-avatar.js";
6+
import { openRootFileSync } from "../infra/boundary-file-read.js";
7+
import { AVATAR_MAX_BYTES, resolveAvatarMime } from "../shared/avatar-policy.js";
8+
9+
function readLocalAvatarDataUrl(
10+
resolved: Extract<AgentAvatarResolution, { kind: "local" }>,
11+
): string | undefined {
12+
try {
13+
// Keep validation and reading on the same opened descriptor. Reopening the
14+
// pathname here would let a symlink swap escape the workspace boundary.
15+
const opened = openRootFileSync({
16+
absolutePath: resolved.filePath,
17+
rootPath: resolved.workspaceRoot,
18+
rootRealPath: resolved.workspaceRoot,
19+
boundaryLabel: "workspace root",
20+
maxBytes: AVATAR_MAX_BYTES,
21+
rejectHardlinks: true,
22+
skipLexicalRootCheck: true,
23+
});
24+
if (!opened.ok) {
25+
return undefined;
26+
}
27+
try {
28+
const buffer = fs.readFileSync(opened.fd);
29+
const mime = resolveAvatarMime(opened.path);
30+
return `data:${mime};base64,${buffer.toString("base64")}`;
31+
} finally {
32+
fs.closeSync(opened.fd);
33+
}
34+
} catch {
35+
return undefined;
36+
}
37+
}
38+
39+
/** Project a verified avatar to the browser-safe value shared by Gateway identity surfaces. */
40+
export function resolveAgentAvatarUrl(resolved: AgentAvatarResolution): string | undefined {
41+
if (resolved.kind === "remote" || resolved.kind === "data") {
42+
return resolved.url;
43+
}
44+
return resolved.kind === "local" ? readLocalAvatarDataUrl(resolved) : undefined;
45+
}

‎src/agents/identity-avatar.test.ts‎

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import path from "node:path";
55
import { afterEach, describe, expect, it } from "vitest";
66
import type { OpenClawConfig } from "../config/config.js";
77
import { AVATAR_MAX_BYTES } from "../shared/avatar-policy.js";
8-
import { resolveAgentAvatar, resolvePublicAgentAvatarSource } from "./identity-avatar.js";
8+
import {
9+
resolveAgentAvatar,
10+
resolveAgentAvatarFromSource,
11+
resolvePublicAgentAvatarSource,
12+
} from "./identity-avatar.js";
13+
import { resolveAgentAvatarUrl } from "./identity-avatar-projection.js";
14+
15+
const AVATAR_MAX_DATA_URL_CHARS = 4 * Math.ceil(AVATAR_MAX_BYTES / 3) + 64;
916

1017
async function writeFile(filePath: string, contents = "avatar") {
1118
await fs.mkdir(path.dirname(filePath), { recursive: true });
@@ -212,6 +219,26 @@ describe("resolveAgentAvatar", () => {
212219
}
213220
});
214221

222+
it("projects an avatar at the exact local byte limit without exceeding the data URL cap", async () => {
223+
const root = await createTempAvatarRoot();
224+
const workspace = path.join(root, "work");
225+
const avatarPath = path.join(workspace, "avatars", "max.png");
226+
await fs.mkdir(path.dirname(avatarPath), { recursive: true });
227+
await fs.writeFile(avatarPath, Buffer.alloc(AVATAR_MAX_BYTES));
228+
const resolved = resolveAgentAvatar(
229+
{
230+
agents: {
231+
list: [{ id: "main", workspace, identity: { avatar: "avatars/max.png" } }],
232+
},
233+
},
234+
"main",
235+
);
236+
const dataUrl = resolveAgentAvatarUrl(resolved);
237+
238+
expect(dataUrl?.startsWith("data:image/png;base64,")).toBe(true);
239+
expect(dataUrl?.length).toBeLessThanOrEqual(AVATAR_MAX_DATA_URL_CHARS);
240+
});
241+
215242
it("accepts remote and data avatars", () => {
216243
const cfg: OpenClawConfig = {
217244
agents: {
@@ -335,3 +362,116 @@ describe("resolveAgentAvatar", () => {
335362
await expectLocalAvatarPath(cfg, workspace, "ui-avatar.png", { includeUiOverride: true });
336363
});
337364
});
365+
366+
describe("agent avatar browser projection", () => {
367+
it("projects local, remote, and data sources without changing their semantics", async () => {
368+
const root = await createTempAvatarRoot();
369+
const workspace = path.join(root, "work");
370+
await writeFile(path.join(workspace, "avatars", "main.png"), "avatar");
371+
const cfg: OpenClawConfig = {
372+
agents: {
373+
list: [{ id: "main", workspace, identity: { avatar: "avatars/main.png" } }],
374+
},
375+
};
376+
const expectedLocal = `data:image/png;base64,${Buffer.from("avatar").toString("base64")}`;
377+
const local = resolveAgentAvatar(cfg, "main");
378+
379+
expect(resolveAgentAvatarUrl(local)).toBe(expectedLocal);
380+
expect(
381+
resolveAgentAvatarUrl(resolveAgentAvatarFromSource(cfg, "main", "avatars/main.png")),
382+
).toBe(expectedLocal);
383+
expect(
384+
resolveAgentAvatarUrl(
385+
resolveAgentAvatarFromSource(cfg, "main", "https://example.com/avatar.png"),
386+
),
387+
).toBe("https://example.com/avatar.png");
388+
expect(
389+
resolveAgentAvatarUrl(
390+
resolveAgentAvatarFromSource(cfg, "main", "data:image/png;base64,aaaa"),
391+
),
392+
).toBe("data:image/png;base64,aaaa");
393+
});
394+
395+
it.each([
396+
["jpg", "image/jpeg"],
397+
["svg", "image/svg+xml"],
398+
] as const)("uses the shared MIME policy for .%s files", async (extension, mime) => {
399+
const root = await createTempAvatarRoot();
400+
const workspace = path.join(root, "work");
401+
await writeFile(path.join(workspace, `avatar.${extension}`), "avatar");
402+
const resolved = resolveAgentAvatar(
403+
{
404+
agents: {
405+
list: [{ id: "main", workspace, identity: { avatar: `avatar.${extension}` } }],
406+
},
407+
},
408+
"main",
409+
);
410+
411+
expect(resolveAgentAvatarUrl(resolved)).toBe(
412+
`data:${mime};base64,${Buffer.from("avatar").toString("base64")}`,
413+
);
414+
});
415+
416+
it("does not project rejected local avatar paths", async () => {
417+
const root = await createTempAvatarRoot();
418+
const workspace = path.join(root, "work");
419+
await fs.mkdir(workspace, { recursive: true });
420+
const missing = resolveAgentAvatar(
421+
{
422+
agents: {
423+
list: [{ id: "main", workspace, identity: { avatar: "avatars/missing.png" } }],
424+
},
425+
},
426+
"main",
427+
);
428+
429+
expect(resolveAgentAvatarUrl(missing)).toBeUndefined();
430+
});
431+
432+
it("rechecks the workspace boundary when a resolved path is replaced before reading", async () => {
433+
const root = await createTempAvatarRoot();
434+
const workspace = path.join(root, "work");
435+
const avatarPath = path.join(workspace, "avatar.png");
436+
const outsidePath = path.join(root, "outside.png");
437+
await writeFile(avatarPath, "avatar");
438+
await writeFile(outsidePath, "secret");
439+
const resolved = resolveAgentAvatar(
440+
{
441+
agents: { list: [{ id: "main", workspace, identity: { avatar: "avatar.png" } }] },
442+
},
443+
"main",
444+
);
445+
expect(resolved.kind).toBe("local");
446+
447+
await fs.rm(avatarPath);
448+
try {
449+
await fs.symlink(outsidePath, avatarPath);
450+
} catch {
451+
return;
452+
}
453+
expect(resolveAgentAvatarUrl(resolved)).toBeUndefined();
454+
});
455+
456+
it("rejects hardlinked avatar files at the read boundary", async () => {
457+
const root = await createTempAvatarRoot();
458+
const workspace = path.join(root, "work");
459+
const outsidePath = path.join(root, "outside.png");
460+
const avatarPath = path.join(workspace, "avatar.png");
461+
await writeFile(outsidePath, "secret");
462+
await fs.mkdir(workspace, { recursive: true });
463+
try {
464+
await fs.link(outsidePath, avatarPath);
465+
} catch {
466+
return;
467+
}
468+
const resolved = resolveAgentAvatar(
469+
{
470+
agents: { list: [{ id: "main", workspace, identity: { avatar: "avatar.png" } }] },
471+
},
472+
"main",
473+
);
474+
expect(resolved.kind).toBe("local");
475+
expect(resolveAgentAvatarUrl(resolved)).toBeUndefined();
476+
});
477+
});

‎src/agents/identity-avatar.ts‎

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { resolveAgentIdentity } from "./identity.js";
2525
// shared avatar policy limits.
2626
export type AgentAvatarResolution =
2727
| { kind: "none"; reason: string; source?: string }
28-
| { kind: "local"; filePath: string; source: string }
28+
| { kind: "local"; filePath: string; workspaceRoot: string; source: string }
2929
| { kind: "remote"; url: string; source: string }
3030
| { kind: "data"; url: string; source: string };
3131

@@ -37,7 +37,7 @@ type AgentAvatarPublicSourceInput = {
3737
const PUBLIC_AVATAR_SOURCE_MAX_CHARS = 256;
3838
const PUBLIC_DATA_AVATAR_HEADER_MAX_CHARS = 64;
3939

40-
function resolveAvatarSource(
40+
function resolveEffectiveAvatarSource(
4141
cfg: OpenClawConfig,
4242
agentId: string,
4343
opts?: { includeUiOverride?: boolean },
@@ -77,7 +77,7 @@ function resolveExistingPath(value: string): string {
7777
function resolveLocalAvatarPath(params: {
7878
raw: string;
7979
workspaceDir: string;
80-
}): { ok: true; filePath: string } | { ok: false; reason: string } {
80+
}): { ok: true; filePath: string; workspaceRoot: string } | { ok: false; reason: string } {
8181
const workspaceRoot = resolveExistingPath(params.workspaceDir);
8282
const raw = params.raw;
8383
const resolved =
@@ -104,7 +104,38 @@ function resolveLocalAvatarPath(params: {
104104
} catch {
105105
return { ok: false, reason: "missing" };
106106
}
107-
return { ok: true, filePath: realPath };
107+
return { ok: true, filePath: realPath, workspaceRoot };
108+
}
109+
110+
/** Resolve one configured source without applying UI or IDENTITY.md fallback precedence. */
111+
export function resolveAgentAvatarFromSource(
112+
cfg: OpenClawConfig,
113+
agentId: string,
114+
source: string | null | undefined,
115+
): AgentAvatarResolution {
116+
const normalized = normalizeOptionalString(source) ?? null;
117+
if (!normalized) {
118+
return { kind: "none", reason: "missing" };
119+
}
120+
if (isAvatarHttpUrl(normalized)) {
121+
return { kind: "remote", url: normalized, source: normalized };
122+
}
123+
if (isAvatarDataUrl(normalized)) {
124+
return { kind: "data", url: normalized, source: normalized };
125+
}
126+
const resolved = resolveLocalAvatarPath({
127+
raw: normalized,
128+
workspaceDir: resolveAgentWorkspaceDir(cfg, agentId),
129+
});
130+
if (!resolved.ok) {
131+
return { kind: "none", reason: resolved.reason, source: normalized };
132+
}
133+
return {
134+
kind: "local",
135+
filePath: resolved.filePath,
136+
workspaceRoot: resolved.workspaceRoot,
137+
source: normalized,
138+
};
108139
}
109140

110141
function isSafeRelativeAvatarSource(source: string): boolean {
@@ -151,20 +182,9 @@ export function resolveAgentAvatar(
151182
agentId: string,
152183
opts?: { includeUiOverride?: boolean },
153184
): AgentAvatarResolution {
154-
const source = resolveAvatarSource(cfg, agentId, opts);
155-
if (!source) {
156-
return { kind: "none", reason: "missing" };
157-
}
158-
if (isAvatarHttpUrl(source)) {
159-
return { kind: "remote", url: source, source };
160-
}
161-
if (isAvatarDataUrl(source)) {
162-
return { kind: "data", url: source, source };
163-
}
164-
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
165-
const resolved = resolveLocalAvatarPath({ raw: source, workspaceDir });
166-
if (!resolved.ok) {
167-
return { kind: "none", reason: resolved.reason, source };
168-
}
169-
return { kind: "local", filePath: resolved.filePath, source };
185+
return resolveAgentAvatarFromSource(
186+
cfg,
187+
agentId,
188+
resolveEffectiveAvatarSource(cfg, agentId, opts),
189+
);
170190
}

0 commit comments

Comments
 (0)