Mirror to GitHub / mirror (push) Successful in 26s
Test and Lint / backend-test (push) Successful in 1m11s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m28s
Version and Release / version-bump (push) Successful in 32s
Version and Release / trigger-drone (push) Has been skipped
Original: feat: enhance security logging and ensure rate limit blocks are properly tracked - Add comprehensive logging for rate limit blocks with full request details - IP address (with proper proxy detection), user agent, headers, timestamps - Rate limit info (current count, limit, remaining, reset time) - Separate tracking for auth vs general endpoints - Enhance authentication failure logging - JWT validation failures with detailed error info - Admin auth attempts without token - Failed token validation with user context - All events include IP, path, method, user agent - Improve Winston logger configuration for production - Add automatic log rotation (10MB errors, 50MB combined) - Create separate security.log for auth/rate limit events - Ensure logs directory exists automatically - Add structured JSON format for log aggregation - Support container logging with LOG_TO_CONSOLE env var - Create comprehensive documentation - Security logging guide with examples - Monitoring recommendations - Configuration reference - Add test script to verify logging functionality All rate limit settings remain configurable via admin panel: - Window duration, max requests, auth limits - Skip authenticated requests option - Public endpoints only option 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
const sharp = require('sharp');
|
|
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* Validate uploaded file is complete and not corrupted
|
|
*/
|
|
async function validateUploadedFile(filePath) {
|
|
try {
|
|
// Check file exists and has size
|
|
const stats = await fs.stat(filePath);
|
|
if (stats.size === 0) {
|
|
throw new Error('File is empty');
|
|
}
|
|
|
|
// For image files, verify they can be read by Sharp
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
|
|
|
|
if (imageExtensions.includes(ext)) {
|
|
// Try to read metadata - this will fail if image is corrupted
|
|
let metadata;
|
|
try {
|
|
metadata = await sharp(filePath, {
|
|
failOnError: false, // Don't fail on recoverable errors
|
|
limitInputPixels: 268402689 // ~16k x 16k max
|
|
}).metadata();
|
|
} catch (metadataError) {
|
|
// If metadata reading fails, the file is likely incomplete
|
|
throw new Error(`Invalid image file: ${metadataError.message}`);
|
|
}
|
|
|
|
if (!metadata || !metadata.width || !metadata.height) {
|
|
throw new Error('Invalid image dimensions - file may be incomplete');
|
|
}
|
|
|
|
// Check for reasonable dimensions
|
|
if (metadata.width < 10 || metadata.height < 10) {
|
|
throw new Error('Image dimensions too small');
|
|
}
|
|
|
|
// Additional check: verify we can actually decode a small portion of the image
|
|
try {
|
|
await sharp(filePath, {
|
|
failOnError: false,
|
|
limitInputPixels: 268402689
|
|
})
|
|
.resize(10, 10) // Try to resize to very small size
|
|
.toBuffer();
|
|
} catch (decodeError) {
|
|
throw new Error(`Image decode failed - file may be corrupted: ${decodeError.message}`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
logger.error(`File validation failed for ${filePath}:`, error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Middleware to validate uploaded files after multer processing
|
|
*/
|
|
async function validateUploadedFiles(req, res, next) {
|
|
if (!req.files || req.files.length === 0) {
|
|
return next();
|
|
}
|
|
|
|
const validFiles = [];
|
|
const invalidFiles = [];
|
|
|
|
// Validate each file
|
|
for (const file of req.files) {
|
|
try {
|
|
await validateUploadedFile(file.path);
|
|
validFiles.push(file);
|
|
} catch (error) {
|
|
logger.warn(`Removing invalid upload ${file.originalname}: ${error.message}`);
|
|
invalidFiles.push({
|
|
filename: file.originalname,
|
|
error: error.message
|
|
});
|
|
|
|
// Delete the invalid file
|
|
try {
|
|
await fs.unlink(file.path);
|
|
} catch (unlinkErr) {
|
|
logger.error(`Failed to delete invalid file ${file.path}:`, unlinkErr.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update req.files to only include valid files
|
|
req.files = validFiles;
|
|
|
|
// Store invalid files info for response
|
|
if (invalidFiles.length > 0) {
|
|
req.invalidFiles = invalidFiles;
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = {
|
|
validateUploadedFile,
|
|
validateUploadedFiles
|
|
}; |