-
-
Notifications
You must be signed in to change notification settings - Fork 23.4k
Add MIME type and extension validation for file uploads #5596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fca9d0b
3a4bb9c
bac553a
998c6b5
24d9e3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,14 @@ | ||
| import { OTLPTraceExporter as ProtoOTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' | ||
| import { getPhoenixTracer } from './handler' | ||
|
|
||
| jest.mock('@opentelemetry/exporter-trace-otlp-proto', () => { | ||
| return { | ||
| ProtoOTLPTraceExporter: jest.fn().mockImplementation((args) => { | ||
| OTLPTraceExporter: jest.fn().mockImplementation((args) => { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the exported function from the library is |
||
| return { args } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| import { OTLPTraceExporter as ProtoOTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move import to the top of the file |
||
|
|
||
| describe('URL Handling For Phoenix Tracer', () => { | ||
| const apiKey = 'test-api-key' | ||
| const projectName = 'test-project-name' | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EnvironmentOS: WIN Result
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for catching this. was using github to apply suggested changes from gemini but forgot to update test (should be reflected now) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { validateMimeTypeAndExtensionMatch } from './validator' | ||
|
|
||
| describe('validateMimeTypeAndExtensionMatch', () => { | ||
| describe('valid cases', () => { | ||
| it.each([ | ||
| ['document.txt', 'text/plain'], | ||
| ['page.html', 'text/html'], | ||
| ['data.json', 'application/json'], | ||
| ['document.pdf', 'application/pdf'], | ||
| ['script.js', 'text/javascript'], | ||
| ['script.js', 'application/javascript'], | ||
| ['readme.md', 'text/markdown'], | ||
| ['readme.md', 'text/x-markdown'], | ||
| ['DOCUMENT.TXT', 'text/plain'], | ||
| ['Document.TxT', 'text/plain'], | ||
| ['my.document.txt', 'text/plain'] | ||
| ])('should pass validation for matching MIME type and extension - %s with %s', (filename, mimetype) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename, mimetype) | ||
| }).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('invalid filename', () => { | ||
| it.each([ | ||
| ['empty filename', ''], | ||
| ['null filename', null], | ||
| ['undefined filename', undefined], | ||
| ['non-string filename (number)', 123], | ||
| ['object filename', {}] | ||
| ])('should throw error for %s', (_description, filename) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename as any, 'text/plain') | ||
| }).toThrow('Invalid filename: filename is required and must be a string') | ||
| }) | ||
| }) | ||
|
|
||
| describe('invalid MIME type', () => { | ||
| it.each([ | ||
| ['empty MIME type', ''], | ||
| ['null MIME type', null], | ||
| ['undefined MIME type', undefined], | ||
| ['non-string MIME type (number)', 123] | ||
| ])('should throw error for %s', (_description, mimetype) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch('file.txt', mimetype as any) | ||
| }).toThrow('Invalid MIME type: MIME type is required and must be a string') | ||
| }) | ||
| }) | ||
|
|
||
| describe('path traversal detection', () => { | ||
| it.each([ | ||
| ['filename with ..', '../file.txt'], | ||
| ['filename with .. in middle', 'path/../file.txt'], | ||
| ['filename with multle levels of ..', '../../../etc/passwd.txt'], | ||
| ['filename with ..\\..\\..', '..\\..\\..\\windows\\system32\\file.txt'], | ||
| ['filename with ....//....//', '....//....//etc/passwd.txt'], | ||
| ['filename starting with /', '/etc/passwd.txt'], | ||
| ['Windows absolute path', 'C:\\file.txt'], | ||
| ['URL encoded path traversal', '%2e%2e/file.txt'], | ||
| ['URL encoded path traversal multiple levels', '%2e%2e%2f%2e%2e%2f%2e%2e%2ffile.txt'], | ||
| ['null byte', 'file\0.txt'] | ||
| ])('should throw error for %s', (_description, filename) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename, 'text/plain') | ||
| }).toThrow(`Invalid filename: unsafe characters or path traversal attempt detected in filename "${filename}"`) | ||
| }) | ||
| }) | ||
|
|
||
| describe('files without extensions', () => { | ||
| it.each([ | ||
| ['filename without extension', 'file'], | ||
| ['filename ending with dot', 'file.'] | ||
| ])('should throw error for %s', (_description, filename) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename, 'text/plain') | ||
| }).toThrow('File type not allowed: files must have a valid file extension') | ||
| }) | ||
| }) | ||
|
|
||
| describe('unsupported MIME types', () => { | ||
| it.each([ | ||
| ['application/octet-stream', 'file.txt'], | ||
| ['invalid-mime-type', 'file.txt'], | ||
| ['application/x-msdownload', 'malware.exe'], | ||
| ['application/x-executable', 'script.exe'], | ||
| ['application/x-msdownload', 'program.EXE'], | ||
| ['application/octet-stream', 'script.js'] | ||
| ])('should throw error for unsupported MIME type %s with %s', (mimetype, filename) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename, mimetype) | ||
| }).toThrow(`MIME type "${mimetype}" is not supported or does not have a valid file extension mapping`) | ||
| }) | ||
| }) | ||
|
|
||
| describe('MIME type and extension mismatches', () => { | ||
| it.each([ | ||
| // [filename, mimetype, actualExt, expectedExt] | ||
| ['file.txt', 'application/json', 'txt', 'json'], | ||
| ['script.js', 'application/pdf', 'js', 'pdf'], | ||
| ['page.html', 'text/plain', 'html', 'txt'], | ||
| ['document.pdf', 'application/json', 'pdf', 'json'], | ||
| ['data.json', 'text/plain', 'json', 'txt'], | ||
| ['malware.exe', 'text/plain', 'exe', 'txt'], | ||
| ['script.js', 'application/json', 'js', 'json'] | ||
| ])('should throw error when extension does not match MIME type - %s with %s', (filename, mimetype, actualExt, expectedExt) => { | ||
| expect(() => { | ||
| validateMimeTypeAndExtensionMatch(filename, mimetype) | ||
| }).toThrow( | ||
| `MIME type mismatch: file extension "${actualExt}" does not match declared MIME type "${mimetype}". Expected: ${expectedExt}` | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import { Request, Response, NextFunction } from 'express' | ||
| import { StatusCodes } from 'http-status-codes' | ||
| import { validateMimeTypeAndExtensionMatch } from 'flowise-components' | ||
| import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
| import openAIAssistantVectorStoreService from '../../services/openai-assistants-vector-store' | ||
| import { getErrorMessage } from '../../errors/utils' | ||
|
|
||
| const getAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => { | ||
| try { | ||
|
|
@@ -142,6 +144,14 @@ const uploadFilesToAssistantVectorStore = async (req: Request, res: Response, ne | |
| for (const file of files) { | ||
| // Address file name with special characters: https://github.com/expressjs/multer/issues/1104 | ||
| file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8') | ||
|
|
||
| // Validate file extension, MIME type, and content to prevent security vulnerabilities | ||
| try { | ||
| validateMimeTypeAndExtensionMatch(file.originalname, file.mimetype) | ||
| } catch (error) { | ||
| throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, getErrorMessage(error)) | ||
| } | ||
|
Comment on lines
+149
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This For example, you could create a function like this in a utility file: import { StatusCodes } from 'http-status-codes';
import { validateMimeTypeAndExtensionMatch } from 'flowise-components';
import { InternalFlowiseError } from '../../errors/internalFlowiseError';
import { getErrorMessage } from '../../errors/utils';
export const validateFileOrThrow = (filename: string, mimetype: string): void => {
try {
validateMimeTypeAndExtensionMatch(filename, mimetype);
} catch (error) {
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, getErrorMessage(error));
}
};Then, you can replace this block with a single call: |
||
|
|
||
| uploadFiles.push({ | ||
| filePath: file.path ?? file.key, | ||
| fileName: file.originalname | ||
|
|
||
yau-wd marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests in
components/srcwas never run, need to expose the directory as roots