Skip to content

Commit 3312ce5

Browse files
committed
fix: support home-relative media paths
1 parent 24d0562 commit 3312ce5

7 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Docs: https://docs.openclaw.ai
1414

1515
- Plugins/externalization: keep ACPX, Google Chat, and LINE publishable plugin dist trees out of the core npm package file list.
1616
- Plugins/ClawHub: explain unavailable explicit ClawHub ClawPack artifact downloads with a temporary npm install hint while ClawHub artifact routing rolls out. Thanks @vincentkoc.
17+
- Media: accept home-relative `MEDIA:~/...` attachment paths while preserving existing file-read policy, traversal checks, and media type validation. Fixes #73796. Thanks @fabkury.
1718
- Onboarding/search: install official external web-search plugins such as Brave before saving provider config, and make doctor repair reconcile selected external search providers whose npm payload is missing. Thanks @vincentkoc.
1819
- Plugins/externalization: add official npm-first catalogs for externalized channel, provider, and generic plugins, keep unpublished ACPX/Google Chat/LINE bundled, and make missing-plugin repair honor npm-first metadata while ClawHub pack files roll out. Thanks @vincentkoc.
1920
- Plugins/update: detect tracked plugin install records whose package directories disappeared during `openclaw update`, reinstall them before normal plugin updates, and fail the update if any install record still points at missing disk payloads.

‎docs/reference/rich-output-protocol.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Remote `MEDIA:` attachments must be public `https:` URLs. Plain `http:`,
1717
loopback, link-local, private, and internal hostnames are ignored as attachment
1818
directives; server-side media fetchers still enforce their own network guards.
1919

20+
Local `MEDIA:` attachments can use absolute paths, workspace-relative paths, or
21+
home-relative `~/` paths. They still pass through the agent file-read policy and
22+
media type checks before delivery.
23+
2024
Plain Markdown image syntax stays text by default. Channels that intentionally
2125
map Markdown image replies to media attachments opt in at their outbound
2226
adapter; Telegram does this so `![alt](url)` can still become a media reply.

‎docs/start/openclaw.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Local-path behavior follows the same file-read trust model as the agent:
203203

204204
- If `tools.fs.workspaceOnly` is `true`, outbound `MEDIA:` local paths stay restricted to the OpenClaw temp root, the media cache, agent workspace paths, and sandbox-generated files.
205205
- If `tools.fs.workspaceOnly` is `false`, outbound `MEDIA:` can use host-local files the agent is already allowed to read.
206+
- Local paths can be absolute, workspace-relative, or home-relative with `~/`.
206207
- Host-local sends still only allow media and safe document types (images, audio, video, PDF, and Office documents). Plain text and secret-like files are not treated as sendable media.
207208

208209
That means generated images/files outside the workspace can now send when your fs policy already allows those reads, without reopening arbitrary host-text attachment exfiltration.

‎src/auto-reply/reply/reply-media-paths.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,38 @@ describe("createReplyMediaPathNormalizer", () => {
486486
groupSpace: undefined,
487487
});
488488
});
489+
490+
it("passes home-relative local media sources into shared outbound media access", async () => {
491+
const homeRelativePath = "~/Pictures/chart.png";
492+
const normalize = createReplyMediaPathNormalizer({
493+
cfg: { tools: { fs: { workspaceOnly: false } } },
494+
sessionKey: "session-key",
495+
workspaceDir: "/tmp/agent-workspace",
496+
});
497+
498+
const result = await normalize({
499+
mediaUrls: [homeRelativePath],
500+
});
501+
502+
expect(result).toMatchObject({
503+
mediaUrl: "/tmp/outbound-media/chart.png",
504+
mediaUrls: ["/tmp/outbound-media/chart.png"],
505+
});
506+
expect(resolveAgentScopedOutboundMediaAccess).toHaveBeenCalledWith({
507+
cfg: { tools: { fs: { workspaceOnly: false } } },
508+
agentId: expect.any(String),
509+
workspaceDir: "/tmp/agent-workspace",
510+
mediaSources: [homeRelativePath],
511+
sessionKey: "session-key",
512+
messageProvider: undefined,
513+
accountId: undefined,
514+
requesterSenderId: undefined,
515+
requesterSenderName: undefined,
516+
requesterSenderUsername: undefined,
517+
requesterSenderE164: undefined,
518+
groupId: undefined,
519+
groupChannel: undefined,
520+
groupSpace: undefined,
521+
});
522+
});
489523
});

‎src/media/parse.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ describe("splitMediaFromOutput", () => {
5151
["./screenshots/image.png", "MEDIA:./screenshots/image.png"],
5252
["media/inbound/image.png", "MEDIA:media/inbound/image.png"],
5353
["./screenshot.png", " MEDIA:./screenshot.png"],
54+
["~/Pictures/My File.png", "MEDIA:~/Pictures/My File.png"],
55+
["~/.openclaw/media/browser/snap.png", "MEDIA:~/.openclaw/media/browser/snap.png"],
5456
["C:\\Users\\pete\\Pictures\\snap.png", "MEDIA:C:\\Users\\pete\\Pictures\\snap.png"],
5557
["/tmp/tts-fAJy8C/voice-1770246885083.opus", "MEDIA:/tmp/tts-fAJy8C/voice-1770246885083.opus"],
5658
["image.png", "MEDIA:image.png"],
@@ -70,10 +72,10 @@ describe("splitMediaFromOutput", () => {
7072
it.each([
7173
"MEDIA:../../../etc/passwd",
7274
"MEDIA:../../.env",
73-
"MEDIA:~/.ssh/id_rsa",
74-
"MEDIA:~/Pictures/My File.png",
75+
"MEDIA:~user/Pictures/My File.png",
76+
"MEDIA:~/Pictures/../../.ssh/id_rsa",
7577
"MEDIA:./foo/../../../etc/shadow",
76-
] as const)("rejects traversal and home-dir path: %s", (input) => {
78+
] as const)("rejects traversal and unsupported home-dir path: %s", (input) => {
7779
expectRejectedMediaPathCase(input);
7880
});
7981

‎src/media/parse.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ const HAS_FILE_EXT = /\.\w{1,10}$/;
4949
// Matches ".." as a standalone path segment (start, middle, or end).
5050
const TRAVERSAL_SEGMENT_RE = /(?:^|[/\\])\.\.(?:[/\\]|$)/;
5151

52-
function hasTraversalOrHomeDirPrefix(candidate: string): boolean {
52+
function isSupportedHomeRelativePath(candidate: string): boolean {
53+
return candidate.startsWith("~/") || candidate.startsWith("~\\");
54+
}
55+
56+
function hasTraversalOrUnsupportedHomeDirPrefix(candidate: string): boolean {
5357
return (
5458
candidate.startsWith("../") ||
5559
candidate === ".." ||
56-
candidate.startsWith("~") ||
60+
(candidate.startsWith("~") && !isSupportedHomeRelativePath(candidate)) ||
5761
TRAVERSAL_SEGMENT_RE.test(candidate)
5862
);
5963
}
@@ -73,14 +77,15 @@ function looksLikeLocalFilePath(candidate: string): boolean {
7377
}
7478

7579
// Recognize safe local file path patterns for media approval, rejecting
76-
// traversal and home-dir paths so they never reach downstream load/send logic.
80+
// traversal and unsupported home-dir paths so they never reach downstream load/send logic.
7781
function isLikelyLocalPath(candidate: string): boolean {
78-
if (hasTraversalOrHomeDirPrefix(candidate)) {
82+
if (hasTraversalOrUnsupportedHomeDirPrefix(candidate)) {
7983
return false;
8084
}
8185
return (
8286
candidate.startsWith("/") ||
8387
candidate.startsWith("./") ||
88+
isSupportedHomeRelativePath(candidate) ||
8489
WINDOWS_DRIVE_RE.test(candidate) ||
8590
candidate.startsWith("\\\\") ||
8691
(!SCHEME_RE.test(candidate) && (candidate.includes("/") || candidate.includes("\\")))
@@ -171,9 +176,9 @@ function isValidMedia(
171176
return true;
172177
}
173178

174-
// Hard reject traversal/home-dir patterns before the bare-filename fallback
179+
// Hard reject traversal/unsupported home-dir patterns before the bare-filename fallback
175180
// to prevent path traversal bypasses (e.g. "../../.env" matching HAS_FILE_EXT).
176-
if (hasTraversalOrHomeDirPrefix(candidate)) {
181+
if (hasTraversalOrUnsupportedHomeDirPrefix(candidate)) {
177182
return false;
178183
}
179184

‎src/media/web-media.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ describe("loadWebMedia", () => {
173173
expect(result.buffer.length).toBeGreaterThan(0);
174174
});
175175

176+
it("resolves home-relative local media paths through allowed local roots", async () => {
177+
vi.stubEnv("OPENCLAW_HOME", fixtureRoot);
178+
try {
179+
const result = await loadWebMedia("~/workspace/chart.png", {
180+
maxBytes: 1024 * 1024,
181+
localRoots: [workspaceDir],
182+
});
183+
expect(result.kind).toBe("image");
184+
expect(result.buffer.length).toBeGreaterThan(0);
185+
} finally {
186+
vi.unstubAllEnvs();
187+
}
188+
});
189+
176190
it("rejects host-read text files outside local roots", async () => {
177191
const secretFile = path.join(fixtureRoot, "secret.txt");
178192
await fs.writeFile(secretFile, "secret", "utf8");

0 commit comments

Comments
 (0)