惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
H
Help Net Security
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
博客园 - 司徒正美
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(media): centralize inbound media reference resolution...
steipete · 2026-04-25 · via Recent Commits to openclaw:main

@@ -0,0 +1,142 @@

1+

import fs from "node:fs/promises";

2+

import path from "node:path";

3+

import { describe, expect, it } from "vitest";

4+

import { resolveStateDir } from "../config/paths.js";

5+

import {

6+

classifyMediaReferenceSource,

7+

MediaReferenceError,

8+

normalizeMediaReferenceSource,

9+

resolveInboundMediaReference,

10+

resolveMediaReferenceLocalPath,

11+

} from "./media-reference.js";

12+13+

describe("media reference helpers", () => {

14+

it("normalizes outbound MEDIA tags without changing canonical media URIs", () => {

15+

expect(normalizeMediaReferenceSource(" MEDIA: ./out.png")).toBe("./out.png");

16+

expect(normalizeMediaReferenceSource("media://inbound/a.png")).toBe("media://inbound/a.png");

17+

});

18+19+

it("classifies supported and unsupported media reference schemes", () => {

20+

expect(classifyMediaReferenceSource("media://inbound/a.png")).toMatchObject({

21+

isMediaStoreUrl: true,

22+

hasUnsupportedScheme: false,

23+

});

24+

expect(classifyMediaReferenceSource("data:image/png;base64,cG5n")).toMatchObject({

25+

isDataUrl: true,

26+

hasUnsupportedScheme: false,

27+

});

28+

expect(

29+

classifyMediaReferenceSource("data:image/png;base64,cG5n", { allowDataUrl: false }),

30+

).toMatchObject({

31+

isDataUrl: true,

32+

hasUnsupportedScheme: true,

33+

});

34+

expect(classifyMediaReferenceSource("ftp://example.test/a.png")).toMatchObject({

35+

hasUnsupportedScheme: true,

36+

});

37+

expect(classifyMediaReferenceSource("C:\\Users\\pete\\image.png")).toMatchObject({

38+

looksLikeWindowsDrivePath: true,

39+

hasUnsupportedScheme: false,

40+

});

41+

});

42+43+

it("resolves canonical inbound media URIs", async () => {

44+

const stateDir = resolveStateDir();

45+

const id = `ref-${Date.now()}-${Math.random().toString(36).slice(2)}.png`;

46+

const filePath = path.join(stateDir, "media", "inbound", id);

47+

await fs.mkdir(path.dirname(filePath), { recursive: true });

48+

await fs.writeFile(filePath, Buffer.from("png"));

49+50+

try {

51+

await expect(resolveInboundMediaReference(`media://inbound/${id}`)).resolves.toMatchObject({

52+

id,

53+

normalizedSource: `media://inbound/${id}`,

54+

physicalPath: filePath,

55+

sourceType: "uri",

56+

});

57+

} finally {

58+

await fs.rm(filePath, { force: true });

59+

}

60+

});

61+62+

it("maps canonical inbound media URIs to local paths for direct file readers", async () => {

63+

const stateDir = resolveStateDir();

64+

const id = `ref-local-path-${Date.now()}-${Math.random().toString(36).slice(2)}.png`;

65+

const filePath = path.join(stateDir, "media", "inbound", id);

66+

await fs.mkdir(path.dirname(filePath), { recursive: true });

67+

await fs.writeFile(filePath, Buffer.from("png"));

68+69+

try {

70+

await expect(resolveMediaReferenceLocalPath(`media://inbound/${id}`)).resolves.toBe(filePath);

71+

await expect(resolveMediaReferenceLocalPath(" MEDIA: ./out.png")).resolves.toBe("./out.png");

72+

} finally {

73+

await fs.rm(filePath, { force: true });

74+

}

75+

});

76+77+

it("resolves direct absolute paths only for first-level inbound media files", async () => {

78+

const stateDir = resolveStateDir();

79+

const id = `ref-path-${Date.now()}-${Math.random().toString(36).slice(2)}.png`;

80+

const filePath = path.join(stateDir, "media", "inbound", id);

81+

await fs.mkdir(path.dirname(filePath), { recursive: true });

82+

await fs.writeFile(filePath, Buffer.from("png"));

83+84+

try {

85+

await expect(resolveInboundMediaReference(filePath)).resolves.toMatchObject({

86+

id,

87+

physicalPath: filePath,

88+

sourceType: "path",

89+

});

90+

await expect(

91+

resolveInboundMediaReference(path.join(stateDir, "media", "inbound", "nested", id)),

92+

).resolves.toBeNull();

93+

await expect(

94+

resolveInboundMediaReference(path.join(stateDir, "media", "outbound", id)),

95+

).resolves.toBeNull();

96+

} finally {

97+

await fs.rm(filePath, { force: true });

98+

}

99+

});

100+101+

it("rejects inbound media URIs with unsupported locations or unsafe ids", async () => {

102+

await expect(resolveInboundMediaReference("media://outbound/a.png")).rejects.toMatchObject({

103+

code: "path-not-allowed",

104+

});

105+

await expect(

106+

resolveInboundMediaReference("media://inbound/nested%2Fa.png"),

107+

).rejects.toBeInstanceOf(MediaReferenceError);

108+

await expect(

109+

resolveInboundMediaReference("media://inbound/nested%2Fa.png"),

110+

).rejects.toMatchObject({ code: "invalid-path" });

111+

await expect(resolveInboundMediaReference("media://inbound/")).rejects.toMatchObject({

112+

code: "invalid-path",

113+

});

114+

await expect(resolveInboundMediaReference("media://inbound/%00.png")).rejects.toMatchObject({

115+

code: "invalid-path",

116+

});

117+

});

118+119+

it("rejects symlinked inbound media files", async () => {

120+

const stateDir = resolveStateDir();

121+

const targetDir = path.join(stateDir, "media-reference-test-target");

122+

const targetPath = path.join(targetDir, "target.png");

123+

const id = `ref-link-${Date.now()}-${Math.random().toString(36).slice(2)}.png`;

124+

const linkPath = path.join(stateDir, "media", "inbound", id);

125+

await fs.mkdir(targetDir, { recursive: true });

126+

await fs.mkdir(path.dirname(linkPath), { recursive: true });

127+

await fs.writeFile(targetPath, Buffer.from("png"));

128+

await fs.symlink(targetPath, linkPath);

129+130+

try {

131+

await expect(resolveInboundMediaReference(`media://inbound/${id}`)).rejects.toMatchObject({

132+

code: "invalid-path",

133+

});

134+

await expect(resolveInboundMediaReference(linkPath)).rejects.toMatchObject({

135+

code: "invalid-path",

136+

});

137+

} finally {

138+

await fs.rm(linkPath, { force: true });

139+

await fs.rm(targetDir, { recursive: true, force: true });

140+

}

141+

});

142+

});