Skip to content

Commit 6f2409b

Browse files
authored
fix(nodes): validate photo payloads before publishing captures (#116927)
1 parent 5e81a34 commit 6f2409b

6 files changed

Lines changed: 120 additions & 18 deletions

File tree

‎src/agents/openclaw-tools.camera.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,55 @@ describe("nodes photos_latest", () => {
420420
photo: { ...PHOTOS_LATEST_PAYLOAD.photos[0], format: "webp" },
421421
expectedError: /unsupported photos\.latest format/i,
422422
},
423+
{
424+
name: "malformed base64",
425+
photo: { format: "jpeg", base64: "not-base64!", width: 1, height: 1 },
426+
expectedError: /invalid base64/i,
427+
},
428+
{
429+
name: "insecure URL",
430+
photo: { format: "jpeg", url: "http://198.51.100.42/photo.jpg", width: 1, height: 1 },
431+
remoteIp: "198.51.100.42",
432+
expectedError: /only https/i,
433+
},
434+
{
435+
name: "mismatched URL host",
436+
photo: { format: "jpeg", url: "https://198.51.100.43/photo.jpg", width: 1, height: 1 },
437+
remoteIp: "198.51.100.42",
438+
expectedError: /must match node host/i,
439+
},
440+
{
441+
name: "missing URL node host",
442+
photo: { format: "jpeg", url: "https://198.51.100.42/photo.jpg", width: 1, height: 1 },
443+
expectedError: /node remoteip/i,
444+
},
445+
{
446+
name: "valid URL with malformed base64",
447+
photo: {
448+
format: "jpeg",
449+
url: "https://198.51.100.42/photo.jpg",
450+
base64: "not-base64!",
451+
width: 1,
452+
height: 1,
453+
},
454+
remoteIp: "198.51.100.42",
455+
expectedError: /invalid base64/i,
456+
},
423457
])("rejects a second photo with $name before writing the first", async (testCase) => {
458+
stubFetchTextResponse("url-image");
424459
setupNodeInvokeMock({
460+
...(testCase.remoteIp ? { remoteIp: testCase.remoteIp } : {}),
425461
invokePayload: { photos: [PHOTOS_LATEST_PAYLOAD.photos[0], testCase.photo] },
426462
});
427463
const firstPhotoId = "00000000-0000-4000-8000-000000000022";
464+
const secondPhotoId = "00000000-0000-4000-8000-000000000033";
428465
const firstPhotoPath = cameraTempPath({ kind: "snap", ext: "jpg", id: firstPhotoId });
466+
const secondPhotoPath = cameraTempPath({ kind: "snap", ext: "jpg", id: secondPhotoId });
429467
const randomUUID = vi
430468
.spyOn(crypto, "randomUUID")
431469
.mockReturnValueOnce("00000000-0000-4000-8000-000000000001")
432-
.mockReturnValueOnce(firstPhotoId);
470+
.mockReturnValueOnce(firstPhotoId)
471+
.mockReturnValueOnce(secondPhotoId);
433472

434473
try {
435474
await expect(
@@ -446,6 +485,7 @@ describe("nodes photos_latest", () => {
446485
} finally {
447486
randomUUID.mockRestore();
448487
await fs.unlink(firstPhotoPath).catch(() => undefined);
488+
await fs.unlink(secondPhotoPath).catch(() => undefined);
449489
}
450490
});
451491

‎src/agents/tools/nodes-tool-media.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async function executeCameraSnap({
198198
},
199199
idempotencyKey: crypto.randomUUID(),
200200
});
201-
const payload = parseCameraSnapPayload(raw?.payload);
201+
const payload = parseCameraSnapPayload(raw?.payload, { expectedHost: resolvedNode.remoteIp });
202202
const normalizedFormat = normalizeLowercaseStringOrEmpty(payload.format);
203203
if (normalizedFormat !== "jpg" && normalizedFormat !== "jpeg" && normalizedFormat !== "png") {
204204
throw new Error(`unsupported camera.snap format: ${payload.format}`);
@@ -281,7 +281,7 @@ async function executePhotosLatest({
281281

282282
// Reject every malformed batch member before creating any capture artifact.
283283
const photos = payload.photos.map((photoRaw) => {
284-
const photo = parseCameraSnapPayload(photoRaw);
284+
const photo = parseCameraSnapPayload(photoRaw, { expectedHost: resolvedNode.remoteIp });
285285
const normalizedFormat = normalizeLowercaseStringOrEmpty(photo.format);
286286
if (normalizedFormat !== "jpg" && normalizedFormat !== "jpeg" && normalizedFormat !== "png") {
287287
throw new Error(`unsupported photos.latest format: ${photo.format}`);

‎src/cli/nodes-camera.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,47 @@ describe("nodes camera helpers", () => {
156156
);
157157
});
158158

159+
it.each([
160+
{
161+
name: "malformed base64",
162+
payload: { format: "jpg", base64: "not-base64!", width: 1, height: 1 },
163+
expectedError: /invalid base64/i,
164+
},
165+
{
166+
name: "insecure URL",
167+
payload: { format: "jpg", url: "http://198.51.100.42/photo.jpg", width: 1, height: 1 },
168+
expectedHost: "198.51.100.42",
169+
expectedError: /only https/i,
170+
},
171+
{
172+
name: "mismatched URL host",
173+
payload: { format: "jpg", url: "https://198.51.100.43/photo.jpg", width: 1, height: 1 },
174+
expectedHost: "198.51.100.42",
175+
expectedError: /must match node host/i,
176+
},
177+
{
178+
name: "missing URL node host",
179+
payload: { format: "jpg", url: "https://198.51.100.42/photo.jpg", width: 1, height: 1 },
180+
expectedError: /node remoteip/i,
181+
},
182+
{
183+
name: "valid URL with malformed base64",
184+
payload: {
185+
format: "jpg",
186+
url: "https://198.51.100.42/photo.jpg",
187+
base64: "not-base64!",
188+
width: 1,
189+
height: 1,
190+
},
191+
expectedHost: "198.51.100.42",
192+
expectedError: /invalid base64/i,
193+
},
194+
])("rejects $name while parsing a camera.snap payload", (testCase) => {
195+
expect(() =>
196+
parseCameraSnapPayload(testCase.payload, { expectedHost: testCase.expectedHost }),
197+
).toThrow(testCase.expectedError);
198+
});
199+
159200
it.each([undefined, "front", "back", "both"] as const)(
160201
"collapses Linux facing=%s into one unknown-position capture",
161202
(facing) => {

‎src/cli/nodes-camera.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ type CameraClipPayload = {
8282
hasAudio: boolean;
8383
};
8484

85-
/** Validate and normalize an unknown camera still-image payload. */
86-
export function parseCameraSnapPayload(value: unknown): CameraSnapPayload {
85+
/** Validate a complete still-image payload before any capture can be published. */
86+
export function parseCameraSnapPayload(
87+
value: unknown,
88+
opts: { expectedHost?: string } = {},
89+
): CameraSnapPayload {
8790
const obj = asRecord(value);
8891
const format = readStringValue(obj.format);
8992
const base64 = readStringValue(obj.base64);
@@ -93,6 +96,12 @@ export function parseCameraSnapPayload(value: unknown): CameraSnapPayload {
9396
if (!format || (!base64 && !url) || width === undefined || height === undefined) {
9497
throw new Error("invalid camera.snap payload");
9598
}
99+
if (url) {
100+
validateCameraPayloadUrl(url, requireNodeRemoteIp(opts.expectedHost));
101+
}
102+
if (base64) {
103+
validateCameraPayloadBase64(base64, MAX_CAMERA_BASE64_BYTES);
104+
}
96105
return { format, ...(base64 ? { base64 } : {}), ...(url ? { url } : {}), width, height };
97106
}
98107

@@ -128,21 +137,26 @@ export function cameraTempPath(opts: {
128137
return path.join(tmpDir, `${cliName}-camera-${opts.kind}${facingPart}-${id}${ext}`);
129138
}
130139

131-
/** Download a node-hosted media URL to disk after HTTPS, host, redirect, and size checks. */
132-
async function writeUrlToFile(filePath: string, url: string, opts: { expectedHost: string }) {
140+
function validateCameraPayloadUrl(url: string, expectedNodeHost: string): string {
133141
const parsed = new URL(url);
134142
if (parsed.protocol !== "https:") {
135143
throw new Error(`writeUrlToFile: only https URLs are allowed, got ${parsed.protocol}`);
136144
}
137-
const expectedHost = normalizeHostname(opts.expectedHost);
145+
const expectedHost = normalizeHostname(expectedNodeHost);
138146
if (!expectedHost) {
139147
throw new Error("writeUrlToFile: expectedHost is required");
140148
}
141149
if (normalizeHostname(parsed.hostname) !== expectedHost) {
142150
throw new Error(
143-
`writeUrlToFile: url host ${parsed.hostname} must match node host ${opts.expectedHost}`,
151+
`writeUrlToFile: url host ${parsed.hostname} must match node host ${expectedNodeHost}`,
144152
);
145153
}
154+
return expectedHost;
155+
}
156+
157+
/** Download a node-hosted media URL to disk after HTTPS, host, redirect, and size checks. */
158+
async function writeUrlToFile(filePath: string, url: string, opts: { expectedHost: string }) {
159+
const expectedHost = validateCameraPayloadUrl(url, opts.expectedHost);
146160

147161
// The node host is allowed even when private because the RPC response supplied its remote IP.
148162
const policy = {
@@ -234,20 +248,25 @@ async function writeUrlToFile(filePath: string, url: string, opts: { expectedHos
234248
return { path: filePath, bytes };
235249
}
236250

237-
/** Decode a base64 media payload to disk with preflight and post-decode size checks. */
238-
export async function writeBase64ToFile(
239-
filePath: string,
240-
base64: string,
241-
opts: { maxBytes?: number } = {},
242-
) {
243-
const maxBytes = opts.maxBytes ?? MAX_CAMERA_BASE64_BYTES;
251+
function validateCameraPayloadBase64(base64: string, maxBytes: number): string {
244252
if (estimateBase64DecodedBytes(base64) > maxBytes) {
245253
throw new Error(`writeBase64ToFile: decoded payload exceeds max ${maxBytes}`);
246254
}
247255
const canonicalBase64 = canonicalizeBase64(base64);
248256
if (!canonicalBase64) {
249257
throw new Error("writeBase64ToFile: invalid base64 payload");
250258
}
259+
return canonicalBase64;
260+
}
261+
262+
/** Decode a base64 media payload to disk with preflight and post-decode size checks. */
263+
export async function writeBase64ToFile(
264+
filePath: string,
265+
base64: string,
266+
opts: { maxBytes?: number } = {},
267+
) {
268+
const maxBytes = opts.maxBytes ?? MAX_CAMERA_BASE64_BYTES;
269+
const canonicalBase64 = validateCameraPayloadBase64(base64, maxBytes);
251270
const buf = Buffer.from(canonicalBase64, "base64");
252271
if (buf.length > maxBytes) {
253272
throw new Error(`writeBase64ToFile: decoded ${buf.length} bytes, exceeds max ${maxBytes}`);

‎src/cli/nodes-cli/register.camera.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ vi.mock("./rpc.js", async () => {
4646
return {
4747
payload: {
4848
format: "jpg",
49-
base64: "redacted-base64",
49+
base64: "cmVkYWN0ZWQtYmFzZTY0",
5050
width: 1600,
5151
height: 1200,
5252
},

‎src/cli/nodes-cli/register.camera.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ export function registerNodesCameraCommands(nodes: Command) {
187187
});
188188

189189
const raw = await callNodesGatewayCli("node.invoke", opts, invokeParams);
190-
const payload = parseCameraSnapPayload(getGatewayInvokePayload(raw));
190+
const payload = parseCameraSnapPayload(getGatewayInvokePayload(raw), {
191+
expectedHost: node.remoteIp,
192+
});
191193
const filePath = cameraTempPath({
192194
kind: "snap",
193195
facing: target.artifactFacing,

0 commit comments

Comments
 (0)