mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-04-25 11:18:22 +00:00
fix(frontend): block unsupported .app uploads (#1834)
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
parent
163121d327
commit
144c9b2464
@ -34,6 +34,7 @@ import {
|
|||||||
SelectTrigger,
|
SelectTrigger,
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
|
import { splitUnsupportedUploadFiles } from "@/core/uploads";
|
||||||
import { isIMEComposing } from "@/lib/ime";
|
import { isIMEComposing } from "@/lib/ime";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import type { ChatStatus, FileUIPart } from "ai";
|
import type { ChatStatus, FileUIPart } from "ai";
|
||||||
@ -71,6 +72,7 @@ import {
|
|||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Provider Context & Types
|
// Provider Context & Types
|
||||||
@ -107,6 +109,9 @@ const PromptInputController = createContext<PromptInputControllerProps | null>(
|
|||||||
const ProviderAttachmentsContext = createContext<AttachmentsContext | null>(
|
const ProviderAttachmentsContext = createContext<AttachmentsContext | null>(
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
const PromptInputValidationContext = createContext<
|
||||||
|
((files: File[] | FileList) => File[]) | null
|
||||||
|
>(null);
|
||||||
|
|
||||||
export const usePromptInputController = () => {
|
export const usePromptInputController = () => {
|
||||||
const ctx = useContext(PromptInputController);
|
const ctx = useContext(PromptInputController);
|
||||||
@ -134,6 +139,7 @@ export const useProviderAttachments = () => {
|
|||||||
|
|
||||||
const useOptionalProviderAttachments = () =>
|
const useOptionalProviderAttachments = () =>
|
||||||
useContext(ProviderAttachmentsContext);
|
useContext(ProviderAttachmentsContext);
|
||||||
|
const usePromptInputValidation = () => useContext(PromptInputValidationContext);
|
||||||
|
|
||||||
export type PromptInputProviderProps = PropsWithChildren<{
|
export type PromptInputProviderProps = PropsWithChildren<{
|
||||||
initialInput?: string;
|
initialInput?: string;
|
||||||
@ -451,7 +457,7 @@ export type PromptInputProps = Omit<
|
|||||||
maxFiles?: number;
|
maxFiles?: number;
|
||||||
maxFileSize?: number; // bytes
|
maxFileSize?: number; // bytes
|
||||||
onError?: (err: {
|
onError?: (err: {
|
||||||
code: "max_files" | "max_file_size" | "accept";
|
code: "max_files" | "max_file_size" | "accept" | "unsupported_package";
|
||||||
message: string;
|
message: string;
|
||||||
}) => void;
|
}) => void;
|
||||||
onSubmit: (
|
onSubmit: (
|
||||||
@ -599,6 +605,23 @@ export const PromptInput = ({
|
|||||||
? controller.attachments.openFileDialog
|
? controller.attachments.openFileDialog
|
||||||
: openFileDialogLocal;
|
: openFileDialogLocal;
|
||||||
|
|
||||||
|
const sanitizeIncomingFiles = useCallback(
|
||||||
|
(fileList: File[] | FileList) => {
|
||||||
|
const { accepted, message } = splitUnsupportedUploadFiles(fileList);
|
||||||
|
if (message) {
|
||||||
|
onError?.({
|
||||||
|
code: "unsupported_package",
|
||||||
|
message,
|
||||||
|
});
|
||||||
|
if (!onError) {
|
||||||
|
toast.error(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return accepted;
|
||||||
|
},
|
||||||
|
[onError],
|
||||||
|
);
|
||||||
|
|
||||||
// Let provider know about our hidden file input so external menus can call openFileDialog()
|
// Let provider know about our hidden file input so external menus can call openFileDialog()
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!usingProvider) return;
|
if (!usingProvider) return;
|
||||||
@ -629,7 +652,10 @@ export const PromptInput = ({
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
||||||
add(e.dataTransfer.files);
|
const accepted = sanitizeIncomingFiles(e.dataTransfer.files);
|
||||||
|
if (accepted.length > 0) {
|
||||||
|
add(accepted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
form.addEventListener("dragover", onDragOver);
|
form.addEventListener("dragover", onDragOver);
|
||||||
@ -638,7 +664,7 @@ export const PromptInput = ({
|
|||||||
form.removeEventListener("dragover", onDragOver);
|
form.removeEventListener("dragover", onDragOver);
|
||||||
form.removeEventListener("drop", onDrop);
|
form.removeEventListener("drop", onDrop);
|
||||||
};
|
};
|
||||||
}, [add, globalDrop]);
|
}, [add, globalDrop, sanitizeIncomingFiles]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!globalDrop) return;
|
if (!globalDrop) return;
|
||||||
@ -653,7 +679,10 @@ export const PromptInput = ({
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
||||||
add(e.dataTransfer.files);
|
const accepted = sanitizeIncomingFiles(e.dataTransfer.files);
|
||||||
|
if (accepted.length > 0) {
|
||||||
|
add(accepted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
document.addEventListener("dragover", onDragOver);
|
document.addEventListener("dragover", onDragOver);
|
||||||
@ -662,7 +691,7 @@ export const PromptInput = ({
|
|||||||
document.removeEventListener("dragover", onDragOver);
|
document.removeEventListener("dragover", onDragOver);
|
||||||
document.removeEventListener("drop", onDrop);
|
document.removeEventListener("drop", onDrop);
|
||||||
};
|
};
|
||||||
}, [add, globalDrop]);
|
}, [add, globalDrop, sanitizeIncomingFiles]);
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
() => () => {
|
() => () => {
|
||||||
@ -678,7 +707,10 @@ export const PromptInput = ({
|
|||||||
|
|
||||||
const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
|
const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
|
||||||
if (event.currentTarget.files) {
|
if (event.currentTarget.files) {
|
||||||
add(event.currentTarget.files);
|
const accepted = sanitizeIncomingFiles(event.currentTarget.files);
|
||||||
|
if (accepted.length > 0) {
|
||||||
|
add(accepted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Reset input value to allow selecting files that were previously removed
|
// Reset input value to allow selecting files that were previously removed
|
||||||
event.currentTarget.value = "";
|
event.currentTarget.value = "";
|
||||||
@ -778,7 +810,7 @@ export const PromptInput = ({
|
|||||||
|
|
||||||
// Render with or without local provider
|
// Render with or without local provider
|
||||||
const inner = (
|
const inner = (
|
||||||
<>
|
<PromptInputValidationContext.Provider value={sanitizeIncomingFiles}>
|
||||||
<input
|
<input
|
||||||
accept={accept}
|
accept={accept}
|
||||||
aria-label="Upload files"
|
aria-label="Upload files"
|
||||||
@ -797,7 +829,7 @@ export const PromptInput = ({
|
|||||||
>
|
>
|
||||||
<InputGroup>{children}</InputGroup>
|
<InputGroup>{children}</InputGroup>
|
||||||
</form>
|
</form>
|
||||||
</>
|
</PromptInputValidationContext.Provider>
|
||||||
);
|
);
|
||||||
|
|
||||||
return usingProvider ? (
|
return usingProvider ? (
|
||||||
@ -830,6 +862,7 @@ export const PromptInputTextarea = ({
|
|||||||
}: PromptInputTextareaProps) => {
|
}: PromptInputTextareaProps) => {
|
||||||
const controller = useOptionalPromptInputController();
|
const controller = useOptionalPromptInputController();
|
||||||
const attachments = usePromptInputAttachments();
|
const attachments = usePromptInputAttachments();
|
||||||
|
const sanitizeIncomingFiles = usePromptInputValidation();
|
||||||
const [isComposing, setIsComposing] = useState(false);
|
const [isComposing, setIsComposing] = useState(false);
|
||||||
|
|
||||||
const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
|
const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
|
||||||
@ -888,7 +921,12 @@ export const PromptInputTextarea = ({
|
|||||||
|
|
||||||
if (files.length > 0) {
|
if (files.length > 0) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
attachments.add(files);
|
const accepted = sanitizeIncomingFiles
|
||||||
|
? sanitizeIncomingFiles(files)
|
||||||
|
: files;
|
||||||
|
if (accepted.length > 0) {
|
||||||
|
attachments.add(accepted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
55
frontend/src/core/uploads/file-validation.test.mjs
Normal file
55
frontend/src/core/uploads/file-validation.test.mjs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import test from "node:test";
|
||||||
|
|
||||||
|
import {
|
||||||
|
MACOS_APP_BUNDLE_UPLOAD_MESSAGE,
|
||||||
|
isLikelyMacOSAppBundle,
|
||||||
|
splitUnsupportedUploadFiles,
|
||||||
|
} from "./file-validation.ts";
|
||||||
|
|
||||||
|
test("identifies Finder-style .app bundle uploads as unsupported", () => {
|
||||||
|
assert.equal(
|
||||||
|
isLikelyMacOSAppBundle({
|
||||||
|
name: "Vibe Island.app",
|
||||||
|
type: "application/octet-stream",
|
||||||
|
}),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("keeps normal files and reports rejected app bundles", () => {
|
||||||
|
const files = [
|
||||||
|
new File(["demo"], "Vibe Island.app", {
|
||||||
|
type: "application/octet-stream",
|
||||||
|
}),
|
||||||
|
new File(["notes"], "notes.txt", { type: "text/plain" }),
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = splitUnsupportedUploadFiles(files);
|
||||||
|
|
||||||
|
assert.equal(result.accepted.length, 1);
|
||||||
|
assert.equal(result.accepted[0]?.name, "notes.txt");
|
||||||
|
assert.equal(result.rejected.length, 1);
|
||||||
|
assert.equal(result.rejected[0]?.name, "Vibe Island.app");
|
||||||
|
assert.equal(result.message, MACOS_APP_BUNDLE_UPLOAD_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("treats empty MIME .app uploads as unsupported", () => {
|
||||||
|
const result = splitUnsupportedUploadFiles([
|
||||||
|
new File(["demo"], "Another.app", { type: "" }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.equal(result.accepted.length, 0);
|
||||||
|
assert.equal(result.rejected.length, 1);
|
||||||
|
assert.equal(result.message, MACOS_APP_BUNDLE_UPLOAD_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns no message when every file is supported", () => {
|
||||||
|
const result = splitUnsupportedUploadFiles([
|
||||||
|
new File(["notes"], "notes.txt", { type: "text/plain" }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.equal(result.accepted.length, 1);
|
||||||
|
assert.equal(result.rejected.length, 0);
|
||||||
|
assert.equal(result.message, undefined);
|
||||||
|
});
|
||||||
34
frontend/src/core/uploads/file-validation.ts
Normal file
34
frontend/src/core/uploads/file-validation.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
const MACOS_APP_BUNDLE_CONTENT_TYPES = new Set([
|
||||||
|
"",
|
||||||
|
"application/octet-stream",
|
||||||
|
]);
|
||||||
|
|
||||||
|
export const MACOS_APP_BUNDLE_UPLOAD_MESSAGE =
|
||||||
|
"macOS .app bundles can't be uploaded directly from the browser. Compress the app as a .zip or upload the .dmg instead.";
|
||||||
|
|
||||||
|
export function isLikelyMacOSAppBundle(file: Pick<File, "name" | "type">) {
|
||||||
|
return (
|
||||||
|
file.name.toLowerCase().endsWith(".app") &&
|
||||||
|
MACOS_APP_BUNDLE_CONTENT_TYPES.has(file.type)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function splitUnsupportedUploadFiles(fileList: File[] | FileList) {
|
||||||
|
const incoming = Array.from(fileList);
|
||||||
|
const accepted: File[] = [];
|
||||||
|
const rejected: File[] = [];
|
||||||
|
|
||||||
|
for (const file of incoming) {
|
||||||
|
if (isLikelyMacOSAppBundle(file)) {
|
||||||
|
rejected.push(file);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
accepted.push(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
accepted,
|
||||||
|
rejected,
|
||||||
|
message: rejected.length > 0 ? MACOS_APP_BUNDLE_UPLOAD_MESSAGE : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -3,4 +3,5 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export * from "./api";
|
export * from "./api";
|
||||||
|
export * from "./file-validation";
|
||||||
export * from "./hooks";
|
export * from "./hooks";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user