Fix critical error handling for ProfanityFilter initialization
- Add proper error handling to prevent undefined profanityFilter from being passed to routes - Implement fallback no-op profanity filter strategy when initialization fails - Add validation check before setupRoutes() to ensure profanityFilter is defined - Provide clear error messages and security warnings when fallback is used - Update graceful shutdown to safely handle both real and fallback profanity filters Fallback profanity filter: - Allows all content to pass through (security risk but prevents crash) - Provides proper method signatures for API compatibility - Logs prominent security warnings about disabled filtering - Returns appropriate error messages for admin operations This prevents runtime errors while maintaining service availability, with clear warnings about the security implications.
This commit is contained in:
parent
45a8d67362
commit
c0dc1f3c6d
1 changed files with 29 additions and 4 deletions
33
server.js
33
server.js
|
@ -31,7 +31,21 @@ try {
|
|||
profanityFilter = new ProfanityFilter();
|
||||
console.log('Profanity filter initialized successfully with separate database');
|
||||
} catch (error) {
|
||||
console.error('Error initializing profanity filter:', error);
|
||||
console.error('WARNING: Failed to initialize profanity filter:', error);
|
||||
console.error('Creating fallback no-op profanity filter. ALL CONTENT WILL BE ALLOWED!');
|
||||
console.error('This is a security risk - please fix the profanity filter configuration.');
|
||||
|
||||
// Create a fallback no-op profanity filter
|
||||
profanityFilter = {
|
||||
checkText: () => ({ isProfane: false, reason: null }),
|
||||
addWord: () => Promise.resolve({ success: false, error: 'Profanity filter not available' }),
|
||||
removeWord: () => Promise.resolve({ success: false, error: 'Profanity filter not available' }),
|
||||
getWords: () => Promise.resolve([]),
|
||||
testText: () => Promise.resolve({ isProfane: false, detectedWords: [], filteredText: '' }),
|
||||
close: () => {}
|
||||
};
|
||||
|
||||
console.warn('⚠️ SECURITY WARNING: Profanity filtering is DISABLED due to initialization failure!');
|
||||
}
|
||||
|
||||
// Initialize database
|
||||
|
@ -121,6 +135,13 @@ function setupRoutes() {
|
|||
});
|
||||
}
|
||||
|
||||
// Validate profanity filter is properly initialized before setting up routes
|
||||
if (!profanityFilter) {
|
||||
console.error('CRITICAL ERROR: profanityFilter is undefined after initialization attempt.');
|
||||
console.error('Cannot start server without a functional profanity filter.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Initialize routes after everything is set up
|
||||
setupRoutes();
|
||||
|
||||
|
@ -138,9 +159,13 @@ process.on('SIGINT', () => {
|
|||
console.log('\nShutting down server...');
|
||||
|
||||
// Close profanity filter database first
|
||||
if (profanityFilter) {
|
||||
profanityFilter.close();
|
||||
console.log('Profanity filter database closed.');
|
||||
if (profanityFilter && typeof profanityFilter.close === 'function') {
|
||||
try {
|
||||
profanityFilter.close();
|
||||
console.log('Profanity filter database closed.');
|
||||
} catch (error) {
|
||||
console.error('Error closing profanity filter:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Close main database
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue