Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Restrict unsupported file types in support form attachments (#52086)

Monica Khourycf5a2369101f
diff --git a/apps/studio/components/interfaces/Support/AttachmentUpload.tsx b/apps/studio/components/interfaces/Support/AttachmentUpload.tsx
index 5747b2ae3eb..5271f49aa02 100644
--- a/apps/studio/components/interfaces/Support/AttachmentUpload.tsx
+++ b/apps/studio/components/interfaces/Support/AttachmentUpload.tsx
@@ -21,6 +21,17 @@ import { uuidv4 } from '@/lib/helpers'
import { useProfile } from '@/lib/profile'
const MAX_ATTACHMENTS = 5
+const ALLOWED_FILE_EXTENSIONS = ['.txt', '.log', '.png', '.jpg', '.jpeg', '.pdf', '.json', '.har']
+const IMAGE_FILE_EXTENSIONS = ['.png', '.jpg', '.jpeg']
+
+const getFileExtension = (filename: string) => {
+ const extension = filename.split('.').pop()
+ return extension ? `.${extension.toLowerCase()}` : ''
+}
+
+const isAllowedFile = (file: File) => ALLOWED_FILE_EXTENSIONS.includes(getFileExtension(file.name))
+
+const isImageFile = (file: File) => IMAGE_FILE_EXTENSIONS.includes(getFileExtension(file.name))
const removeAttachmentButtonClassName = cn(
'absolute -top-1 -right-1 size-4 shrink-0 rounded-full bg-red-900 p-0 cursor-pointer',
@@ -41,7 +52,7 @@ const uploadAttachments = async ({ userId, files }: { userId: string; files: Fil
const filesToUpload = Array.from(files)
const uploadedFiles = await Promise.all(
filesToUpload.map(async (file) => {
- const suffix = file.name.endsWith('.har') ? 'har' : file.type.split('/')[1]
+ const suffix = file.name.split('.').pop()
const prefix = `${userId}/${uuidv4()}.${suffix}`
const options = { cacheControl: '3600' }
@@ -76,10 +87,18 @@ export function useAttachmentUpload() {
event.persist()
const items = event.target.files || (event as any).dataTransfer.items
const itemsCopied = Array.prototype.map.call(items, (item: any) => item) as File[]
- const itemsToBeUploaded = itemsCopied.slice(0, MAX_ATTACHMENTS - uploadedFiles.length)
+
+ const allowedItems = itemsCopied.filter(isAllowedFile)
+ const rejectedItems = itemsCopied.filter((item) => !isAllowedFile(item))
+ const itemsToBeUploaded = allowedItems.slice(0, MAX_ATTACHMENTS - uploadedFiles.length)
setUploadedFiles(uploadedFiles.concat(itemsToBeUploaded))
- if (items.length + uploadedFiles.length > MAX_ATTACHMENTS) {
+ if (rejectedItems.length > 0) {
+ toast.error(
+ `Unsupported file type: ${rejectedItems.map((item) => item.name).join(', ')}. Supported formats: ${ALLOWED_FILE_EXTENSIONS.join(', ')}`
+ )
+ }
+ if (allowedItems.length + uploadedFiles.length > MAX_ATTACHMENTS) {
toast(`Only up to ${MAX_ATTACHMENTS} attachments are allowed`)
}
event.target.value = ''
@@ -103,10 +122,10 @@ export function useAttachmentUpload() {
useEffect(() => {
if (!uploadedFiles) return
const objectUrls = uploadedFiles.map((file) => {
- if (file.name.endsWith('.har')) {
- return file.name
- } else {
+ if (isImageFile(file)) {
return URL.createObjectURL(file)
+ } else {
+ return file.name
}
})
setUploadedDataUrls(objectUrls)
@@ -191,21 +210,27 @@ export function AttachmentUploadDisplay({
HAR files
</InlineLink>
</p>
+ <p className="text-sm text-foreground-lighter">
+ Supported formats: {ALLOWED_FILE_EXTENSIONS.join(', ')}
+ </p>
</div>
<input
multiple
type="file"
ref={uploadButtonRef}
className="hidden"
- accept="image/png, image/jpeg, .har"
+ accept={ALLOWED_FILE_EXTENSIONS.join(',')}
onChange={handleFileUpload}
/>
<div className="flex items-center gap-x-2">
{uploadedDataUrls.map((url, idx) => {
- if (url.endsWith('.har')) {
+ const isImage = url.startsWith('blob:')
+ if (!isImage) {
+ const filename = url
+ const label = getFileExtension(filename).slice(1).toUpperCase()
return (
<div
- key={url}
+ key={filename}
className="border relative h-14 w-14 rounded-sm flex items-center justify-center"
>
<Tooltip>
@@ -213,11 +238,11 @@ export function AttachmentUploadDisplay({
<div className="flex flex-col items-center justify-center gap-y-1">
<FileCodeIcon className="text-foreground-light" size={16} />
<p className="text-[10px] font-mono text-foreground-light tracking-wide leading-none">
- HAR
+ {label}
</p>
</div>
</TooltipTrigger>
- <TooltipContent side="bottom">{url}</TooltipContent>
+ <TooltipContent side="bottom">{filename}</TooltipContent>
</Tooltip>
<button
diff --git a/apps/studio/components/interfaces/Support/__tests__/SupportFormPage.test.tsx b/apps/studio/components/interfaces/Support/__tests__/SupportFormPage.test.tsx
index f932d1f994d..b739b49fa71 100644
--- a/apps/studio/components/interfaces/Support/__tests__/SupportFormPage.test.tsx
+++ b/apps/studio/components/interfaces/Support/__tests__/SupportFormPage.test.tsx
@@ -343,9 +343,7 @@ const getSupportForm = () => {
}
const getAttachmentFileInput = () => {
- const input = getSupportForm().querySelector<HTMLInputElement>(
- 'input[type="file"][accept*="image"]'
- )
+ const input = getSupportForm().querySelector<HTMLInputElement>('input[type="file"]')
expect(input).not.toBeNull()
return input!
}