Skip to content

An AI-powered search engine with a generative UI

fix(upload): reject files whose contents are not a supported format (#952)

Yoshiki Miura7886eee74ffb
diff --git a/app/api/upload/__tests__/route.test.ts b/app/api/upload/__tests__/route.test.ts
index 776e4064..3cf0cf1c 100644
--- a/app/api/upload/__tests__/route.test.ts
+++ b/app/api/upload/__tests__/route.test.ts
@@ -50,12 +50,16 @@ const EXISTING = {
* The route only reads headers and formData, so a stub avoids round-tripping a
* File through multipart encoding in the test environment.
*/
-function uploadRequest(bytes = 1234) {
+function uploadRequest(
+ bytes = 1234,
+ contents = pdfUploadBytes(bytes),
+ declared = { name: 'report.pdf', type: 'application/pdf' }
+) {
const file = {
- name: 'report.pdf',
- type: 'application/pdf',
+ name: declared.name,
+ type: declared.type,
size: bytes,
- arrayBuffer: async () => new Uint8Array(bytes).buffer
+ arrayBuffer: async () => contents.buffer
}
const formData = new Map<string, unknown>([
['file', file],
@@ -68,8 +72,14 @@ function uploadRequest(bytes = 1234) {
} as any
}
+function pdfUploadBytes(bytes = 1234) {
+ const contents = new Uint8Array(bytes)
+ contents.set(Buffer.from('%PDF-', 'ascii'))
+ return contents
+}
+
function md5OfUpload(bytes = 1234) {
- return createHash('md5').update(Buffer.alloc(bytes)).digest('hex')
+ return createHash('md5').update(pdfUploadBytes(bytes)).digest('hex')
}
describe('POST /api/upload', () => {
@@ -122,6 +132,52 @@ describe('POST /api/upload', () => {
expect(send).toHaveBeenCalledOnce()
})
+ it('rejects allowed declared types with unsupported content', async () => {
+ const contents = new Uint8Array([
+ 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d
+ ])
+
+ const response = await POST(uploadRequest(contents.byteLength, contents))
+
+ expect(response.status).toBe(400)
+ expect(await response.json()).toMatchObject({
+ error: 'Unsupported file content'
+ })
+ expect(dbActions.findChatFileCandidates).not.toHaveBeenCalled()
+ expect(send).not.toHaveBeenCalled()
+ expect(dbActions.createLibraryFile).not.toHaveBeenCalled()
+ })
+
+ it('stores a mislabelled file under the type its bytes say it is', async () => {
+ // A document renamed to .jpg is reported as an image by the browser, and
+ // handing it to the provider as one fails the turn just like unreadable
+ // content does.
+ vi.mocked(dbActions.findChatFileCandidates).mockResolvedValue([])
+ const contents = pdfUploadBytes(1234)
+
+ const { file } = await (
+ await POST(
+ uploadRequest(contents.byteLength, contents, {
+ name: 'report.jpg',
+ type: 'image/jpeg'
+ })
+ )
+ ).json()
+
+ expect(file.mediaType).toBe('application/pdf')
+ expect(dbActions.findChatFileCandidates).toHaveBeenCalledWith(
+ expect.objectContaining({ mediaType: 'application/pdf' })
+ )
+ expect(send).toHaveBeenCalledWith(
+ expect.objectContaining({
+ input: expect.objectContaining({ ContentType: 'application/pdf' })
+ })
+ )
+ expect(dbActions.createLibraryFile).toHaveBeenCalledWith(
+ expect.objectContaining({ mediaType: 'application/pdf' })
+ )
+ })
+
it('uploads when the candidate holds different bytes', async () => {
// Same name, same type, same size, different content: the digest is the
// only thing standing between the user and a silently substituted file.
diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts
index 48884ee2..16d122c5 100644
--- a/app/api/upload/route.ts
+++ b/app/api/upload/route.ts
@@ -6,6 +6,11 @@ import { createHash } from 'node:crypto'
import { capture } from '@/lib/analytics/dispatch'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
import * as dbActions from '@/lib/db/actions'
+import {
+ detectFileMediaType,
+ isSupportedFileType,
+ type SupportedFileType
+} from '@/lib/storage/file-signature'
import {
getChatFileObjectKeyPrefix,
getObjectContentMd5,
@@ -16,7 +21,6 @@ import {
} from '@/lib/storage/r2-client'
const MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB
-const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'application/pdf']
export async function POST(req: NextRequest) {
try {
@@ -58,7 +62,7 @@ export async function POST(req: NextRequest) {
)
}
- if (!ALLOWED_TYPES.includes(file.type)) {
+ if (!isSupportedFileType(file.type)) {
return NextResponse.json(
{ error: 'Unsupported file type' },
{ status: 400 }
@@ -67,8 +71,30 @@ export async function POST(req: NextRequest) {
const isAnonymous = process.env.ENABLE_AUTH === 'false'
const buffer = Buffer.from(await file.arrayBuffer())
+ // The browser reports file.type from the name, so the check above says
+ // nothing about the contents. Only the bytes do, and from here on they are
+ // what the file is: sending a document to the provider as an image fails
+ // the whole turn just as surely as sending something unreadable.
+ const mediaType = detectFileMediaType(buffer)
+ if (!mediaType) {
+ return NextResponse.json(
+ {
+ error: 'Unsupported file content',
+ message:
+ 'The file contents are not a JPEG, PNG, or PDF even though the file name suggests otherwise.'
+ },
+ { status: 400 }
+ )
+ }
+
if (!isAnonymous && chatId) {
- const reused = await reuseExistingChatFile(file, buffer, userId, chatId)
+ const reused = await reuseExistingChatFile(
+ file,
+ buffer,
+ mediaType,
+ userId,
+ chatId
+ )
if (reused) {
return NextResponse.json(
{ success: true, file: reused },
@@ -77,7 +103,7 @@ export async function POST(req: NextRequest) {
}
}
- const result = await uploadFileToR2(file, buffer, userId, chatId)
+ const result = await uploadFileToR2(file, buffer, mediaType, userId, chatId)
if (isAnonymous) {
return NextResponse.json({ success: true, file: result }, { status: 200 })
}
@@ -149,6 +175,7 @@ export async function POST(req: NextRequest) {
async function reuseExistingChatFile(
file: File,
buffer: Buffer,
+ mediaType: SupportedFileType,
userId: string,
chatId: string
) {
@@ -157,7 +184,7 @@ async function reuseExistingChatFile(
userId,
chatKeyPrefix: getChatFileObjectKeyPrefix(userId, chatId),
filename: file.name,
- mediaType: file.type,
+ mediaType,
size: file.size
})
if (candidates.length === 0) return null
@@ -196,6 +223,7 @@ function sanitizeFilename(filename: string) {
async function uploadFileToR2(
file: File,
buffer: Buffer,
+ mediaType: SupportedFileType,
userId: string,
chatId: string
) {
@@ -210,7 +238,7 @@ async function uploadFileToR2(
Bucket: R2_BUCKET_NAME,
Key: filePath,
Body: buffer,
- ContentType: file.type,
+ ContentType: mediaType,
CacheControl: 'max-age=3600'
})
)
@@ -221,7 +249,7 @@ async function uploadFileToR2(
filename: file.name,
key: filePath,
url: signedUrl,
- mediaType: file.type,
+ mediaType,
type: 'file'
}
} catch (error: any) {
diff --git a/lib/storage/__tests__/file-signature.test.ts b/lib/storage/__tests__/file-signature.test.ts
new file mode 100644
index 00000000..ebee759a
--- /dev/null
+++ b/lib/storage/__tests__/file-signature.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, it } from 'vitest'
+
+import { detectFileMediaType } from '../file-signature'
+
+describe('detectFileMediaType', () => {
+ it('detects JPEG content', () => {
+ expect(detectFileMediaType(Buffer.from([0xff, 0xd8, 0xff, 0xe0]))).toBe(
+ 'image/jpeg'
+ )
+ })
+
+ it('detects PNG content', () => {
+ expect(
+ detectFileMediaType(
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+ )
+ ).toBe('image/png')
+ })
+
+ it('detects PDF content', () => {
+ expect(detectFileMediaType(Buffer.from('%PDF-1.7', 'ascii'))).toBe(
+ 'application/pdf'
+ )
+ })
+
+ it('detects a PDF header after a preamble', () => {
+ expect(
+ detectFileMediaType(Buffer.from('preamble\n%PDF-1.7', 'ascii'))
+ ).toBe('application/pdf')
+ })
+
+ it('rejects the MP4 content from the production upload', () => {
+ expect(
+ detectFileMediaType(
+ Buffer.from([
+ 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d
+ ])
+ )
+ ).toBeNull()
+ })
+
+ it('rejects empty and short content', () => {
+ expect(detectFileMediaType(Buffer.alloc(0))).toBeNull()
+ expect(detectFileMediaType(Buffer.from([0xff, 0xd8]))).toBeNull()
+ })
+})
diff --git a/lib/storage/file-signature.ts b/lib/storage/file-signature.ts
new file mode 100644
index 00000000..3771f13f
--- /dev/null
+++ b/lib/storage/file-signature.ts
@@ -0,0 +1,30 @@
+export const ALLOWED_FILE_TYPES = [
+ 'image/jpeg',
+ 'image/png',
+ 'application/pdf'
+] as const
+
+export type SupportedFileType = (typeof ALLOWED_FILE_TYPES)[number]
+
+const signatures: Record<SupportedFileType, (buffer: Buffer) => boolean> = {
+ 'image/jpeg': buffer =>
+ buffer.subarray(0, 3).equals(Buffer.from([0xff, 0xd8, 0xff])),
+ 'image/png': buffer =>
+ buffer
+ .subarray(0, 8)
+ .equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])),
+ 'application/pdf': buffer =>
+ buffer.subarray(0, 1024).indexOf(Buffer.from('%PDF-', 'ascii')) !== -1
+}
+
+export function isSupportedFileType(value: string): value is SupportedFileType {
+ return ALLOWED_FILE_TYPES.some(mediaType => mediaType === value)
+}
+
+export function detectFileMediaType(buffer: Buffer): SupportedFileType | null {
+ for (const mediaType of ALLOWED_FILE_TYPES) {
+ if (signatures[mediaType](buffer)) return mediaType
+ }
+
+ return null
+}