Skip to content

Commit 0d8f4cf

Browse files
committed
fix(knowledge): reject non-alphanumeric file extensions from document names (#3816)
* fix(knowledge): reject non-alphanumeric file extensions from document names * fix(knowledge): improve error message when extension is non-alphanumeric
1 parent d944b47 commit 0d8f4cf

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

apps/sim/lib/knowledge/documents/parser-extension.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ export function resolveParserExtension(
2929
mimeType?: string,
3030
fallback?: string
3131
): string {
32-
const filenameExtension = filename.includes('.')
33-
? filename.split('.').pop()?.toLowerCase()
34-
: undefined
32+
const raw = filename.includes('.') ? filename.split('.').pop()?.toLowerCase() : undefined
33+
const filenameExtension = raw && /^[a-z0-9]+$/.test(raw) ? raw : undefined
3534

3635
if (filenameExtension && isSupportedParserExtension(filenameExtension)) {
3736
return filenameExtension

apps/sim/lib/uploads/utils/validation.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ export interface FileValidationError {
137137
* Validate if a file type is supported for document processing
138138
*/
139139
export function validateFileType(fileName: string, mimeType: string): FileValidationError | null {
140-
const extension = path.extname(fileName).toLowerCase().substring(1) as SupportedDocumentExtension
140+
const raw = path.extname(fileName).toLowerCase().substring(1)
141+
const extension = (/^[a-z0-9]+$/.test(raw) ? raw : '') as SupportedDocumentExtension
141142

142143
if (!SUPPORTED_DOCUMENT_EXTENSIONS.includes(extension)) {
143144
return {
144145
code: 'UNSUPPORTED_FILE_TYPE',
145-
message: `Unsupported file type: ${extension}. Supported types are: ${SUPPORTED_DOCUMENT_EXTENSIONS.join(', ')}`,
146+
message: `Unsupported file type${extension ? `: ${extension}` : ` for "${fileName}"`}. Supported types are: ${SUPPORTED_DOCUMENT_EXTENSIONS.join(', ')}`,
146147
supportedTypes: [...SUPPORTED_DOCUMENT_EXTENSIONS],
147148
}
148149
}
@@ -221,15 +222,16 @@ export function validateMediaFileType(
221222
fileName: string,
222223
mimeType: string
223224
): FileValidationError | null {
224-
const extension = path.extname(fileName).toLowerCase().substring(1)
225+
const raw = path.extname(fileName).toLowerCase().substring(1)
226+
const extension = /^[a-z0-9]+$/.test(raw) ? raw : ''
225227

226228
const isAudio = SUPPORTED_AUDIO_EXTENSIONS.includes(extension as SupportedAudioExtension)
227229
const isVideo = SUPPORTED_VIDEO_EXTENSIONS.includes(extension as SupportedVideoExtension)
228230

229231
if (!isAudio && !isVideo) {
230232
return {
231233
code: 'UNSUPPORTED_FILE_TYPE',
232-
message: `Unsupported media file type: ${extension}. Supported audio types: ${SUPPORTED_AUDIO_EXTENSIONS.join(', ')}. Supported video types: ${SUPPORTED_VIDEO_EXTENSIONS.join(', ')}`,
234+
message: `Unsupported media file type${extension ? `: ${extension}` : ` for "${fileName}"`}. Supported audio types: ${SUPPORTED_AUDIO_EXTENSIONS.join(', ')}. Supported video types: ${SUPPORTED_VIDEO_EXTENSIONS.join(', ')}`,
233235
supportedTypes: [...SUPPORTED_AUDIO_EXTENSIONS, ...SUPPORTED_VIDEO_EXTENSIONS],
234236
}
235237
}

0 commit comments

Comments
 (0)