Skip to content

Commit 933bb61

Browse files
committed
fix(browser): reject malformed proxy file base64
1 parent 9d5ecc7 commit 933bb61

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎extensions/browser/src/browser/proxy-files.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ describe("persistBrowserProxyFiles", () => {
104104
).rejects.toHaveProperty("code", "ENOENT");
105105
});
106106

107+
it("rejects malformed base64 before persisting files", async () => {
108+
const error = await persistBrowserProxyFiles([
109+
{
110+
path: "/tmp/malformed.bin",
111+
base64: "aGVsbG8$",
112+
mimeType: "application/octet-stream",
113+
},
114+
]).then(
115+
() => null,
116+
(err: unknown) => err,
117+
);
118+
expect(error).toBeInstanceOf(Error);
119+
expect((error as Error).message).toBe("browser proxy file contains malformed base64 data");
120+
121+
await expect(
122+
fs.stat(path.join(tempHome.home, ".openclaw", "media", "browser")),
123+
).rejects.toHaveProperty("code", "ENOENT");
124+
});
125+
107126
it("rejects too many files before persisting any", async () => {
108127
const files = Array.from({ length: BROWSER_PROXY_MAX_FILES + 1 }, (_, index) => ({
109128
path: `/tmp/file-${index}.bin`,

‎extensions/browser/src/browser/proxy-files.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Persists files returned by node-hosted browser proxy calls and rewrites
55
* proxied result paths to local saved media paths.
66
*/
7+
import { canonicalizeBase64, estimateBase64DecodedBytes } from "openclaw/plugin-sdk/media-runtime";
78
import {
89
assertBrowserProxyFileCountWithinLimit,
910
assertBrowserProxyFileBytesWithinLimits,
@@ -13,6 +14,18 @@ import {
1314
} from "../browser-proxy-envelope.js";
1415
import { saveMediaBuffer } from "../media/store.js";
1516

17+
function decodeBrowserProxyFileBase64(file: BrowserProxyFile, totalBytes: number): Buffer {
18+
const estimatedBytes = estimateBase64DecodedBytes(file.base64);
19+
assertBrowserProxyFileBytesWithinLimits(estimatedBytes, totalBytes + estimatedBytes);
20+
const canonicalBase64 = canonicalizeBase64(file.base64);
21+
if (!canonicalBase64) {
22+
throw new Error("browser proxy file contains malformed base64 data");
23+
}
24+
const buffer = Buffer.from(canonicalBase64, "base64");
25+
assertBrowserProxyFileBytesWithinLimits(buffer.byteLength, totalBytes + buffer.byteLength);
26+
return buffer;
27+
}
28+
1629
/** Persist proxy-returned files and return a remote-path to local-path map. */
1730
export async function persistBrowserProxyFiles(files: BrowserProxyFile[] | undefined) {
1831
if (!files || files.length === 0) {
@@ -22,9 +35,8 @@ export async function persistBrowserProxyFiles(files: BrowserProxyFile[] | undef
2235
const decoded: Array<{ file: BrowserProxyFile; buffer: Buffer }> = [];
2336
let totalBytes = 0;
2437
for (const file of files) {
25-
const buffer = Buffer.from(file.base64, "base64");
38+
const buffer = decodeBrowserProxyFileBase64(file, totalBytes);
2639
totalBytes += buffer.byteLength;
27-
assertBrowserProxyFileBytesWithinLimits(buffer.byteLength, totalBytes);
2840
decoded.push({ file, buffer });
2941
}
3042

0 commit comments

Comments
 (0)