-
Notifications
You must be signed in to change notification settings - Fork 3
feat(backend): add security check #163
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { FileUpload } from 'graphql-upload-minimal'; | ||
import path from 'path'; | ||
|
||
/** Maximum allowed file size (5MB) */ | ||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB | ||
|
||
/** Allowed image MIME types */ | ||
const ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp']; | ||
|
||
/** Allowed file extensions */ | ||
const ALLOWED_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.webp']; | ||
|
||
/** | ||
* Validates a file upload (size, type) and returns a Buffer. | ||
* @param file - FileUpload object from GraphQL | ||
* @returns Promise<Buffer> - The file data in buffer format | ||
* @throws BadRequestException - If validation fails | ||
*/ | ||
export async function validateAndBufferFile( | ||
file: FileUpload, | ||
): Promise<{ buffer: Buffer; mimetype: string }> { | ||
const { filename, createReadStream, mimetype } = await file; | ||
|
||
// Extract the file extension | ||
const extension = path.extname(filename).toLowerCase(); | ||
|
||
// Validate MIME type | ||
if (!ALLOWED_MIME_TYPES.includes(mimetype)) { | ||
throw new BadRequestException( | ||
`Invalid file type: ${mimetype}. Only JPEG, PNG, and WebP are allowed.`, | ||
); | ||
} | ||
|
||
// Validate file extension | ||
if (!ALLOWED_EXTENSIONS.includes(extension)) { | ||
throw new BadRequestException( | ||
`Invalid file extension: ${extension}. Only .jpg, .jpeg, .png, and .webp are allowed.`, | ||
); | ||
} | ||
|
||
const chunks: Buffer[] = []; | ||
let fileSize = 0; | ||
|
||
// Read file stream and check size | ||
for await (const chunk of createReadStream()) { | ||
fileSize += chunk.length; | ||
if (fileSize > MAX_FILE_SIZE) { | ||
throw new BadRequestException( | ||
'File size exceeds the maximum allowed limit (5MB).', | ||
); | ||
} | ||
chunks.push(chunk); | ||
} | ||
|
||
return { buffer: Buffer.concat(chunks), mimetype }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Add error handling for stream operations.
The file stream reading code should have appropriate error handling to prevent unhandled exceptions.
Add a try/catch block around the stream processing:
📝 Committable suggestion