Handle parameterized base64 image data URLs
- accept `data:image/...;...;base64,...` headers while still rejecting invalid formats - add `imageMime` tests for mime params, non-base64 URLs, and missing mime types - fork branch-generation prompt effect at call site in `ProviderCommandReactor`
This commit is contained in:
1 parent
e64ad4b2c7
commit
e1a8ee189a
3 files changed
+42
-4
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseBase64DataUrl } from "./imageMime.ts";
|
||||
|
||||
describe("imageMime", () => {
|
||||
it("parses base64 data URL with mime type", () => {
|
||||
expect(parseBase64DataUrl("data:image/png;base64,SGVsbG8=")).toEqual({
|
||||
mimeType: "image/png",
|
||||
base64: "SGVsbG8=",
|
||||
});
|
||||
});
|
||||
|
||||
it("parses base64 data URL with mime parameters", () => {
|
||||
expect(parseBase64DataUrl("data:image/png;charset=utf-8;base64,SGVsbG8=")).toEqual({
|
||||
mimeType: "image/png",
|
||||
base64: "SGVsbG8=",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects non-base64 data URL", () => {
|
||||
expect(parseBase64DataUrl("data:image/png;charset=utf-8,hello")).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects missing mime type", () => {
|
||||
expect(parseBase64DataUrl("data:;base64,SGVsbG8=")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -32,10 +32,22 @@ export const SAFE_IMAGE_FILE_EXTENSIONS = new Set([
|
||||
export function parseBase64DataUrl(
|
||||
dataUrl: string,
|
||||
): { readonly mimeType: string; readonly base64: string } | null {
|
||||
const match = /^data:([^;,]+);base64,([a-z0-9+/=\r\n]+)$/i.exec(dataUrl.trim());
|
||||
const match = /^data:([^,]+),([a-z0-9+/=\r\n]+)$/i.exec(dataUrl.trim());
|
||||
if (!match) return null;
|
||||
|
||||
const mimeType = match[1]?.trim().toLowerCase();
|
||||
const headerParts = (match[1] ?? "")
|
||||
.split(";")
|
||||
.map((part) => part.trim())
|
||||
.filter((part) => part.length > 0);
|
||||
if (headerParts.length < 2) {
|
||||
return null;
|
||||
}
|
||||
const trailingToken = headerParts.at(-1)?.toLowerCase();
|
||||
if (trailingToken !== "base64") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const mimeType = headerParts[0]?.toLowerCase();
|
||||
const base64 = match[2]?.replace(/\s+/g, "");
|
||||
if (!mimeType || !base64) return null;
|
||||
|
||||
|
||||
@@ -421,7 +421,6 @@ const make = Effect.gen(function* () {
|
||||
{ threadId: input.threadId, cwd, oldBranch, cause: Cause.pretty(cause) },
|
||||
),
|
||||
),
|
||||
Effect.forkScoped,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -464,7 +463,7 @@ const make = Effect.gen(function* () {
|
||||
messageId: message.id,
|
||||
messageText: message.text,
|
||||
...(message.attachments !== undefined ? { attachments: branchGenerationAttachments } : {}),
|
||||
});
|
||||
}).pipe(Effect.forkScoped);
|
||||
|
||||
yield* sendTurnForThread({
|
||||
threadId: event.payload.threadId,
|
||||
|
||||
Reference in new issue
Block a user