This repository hosts a fortified and secured version of the Smart Waste Management System. Following a comprehensive security audit using the Snyk tool, our team identified and remediated critical vulnerabilities aligned with the OWASP Top 10 framework. The goal was to significantly enhance the project's security posture, ensuring data integrity, secure authentication, and resilience against common web attacks.
The original, vulnerable project can be found here: Smart-Waste-Management-System.
This project addresses several critical vulnerability categories. The following is a summary of the fixes implemented based on the security audit.
To prevent unauthorized access and credential theft, we implemented:
- Strong Password Policies: Enforced complex password requirements (length, uppercase, lowercase, number, special character) using Joi schema validation.
- Secure Password Hashing: Integrated
bcryptto hash and salt user passwords before storing them in the database, preventing plaintext credential exposure. - Safe Password Comparison: Used
bcrypt.compare()for password verification to protect against timing attacks.
We hardened the application's configuration to minimize attack surfaces:
- Secure HTTP Headers: Added the
helmetmiddleware to set various security-related HTTP headers, protecting against common attacks like clickjacking and XSS. - Strict CORS Policy: Configured a strict Cross-Origin Resource Sharing (CORS) policy to only allow requests from whitelisted frontend URLs, preventing unauthorized cross-origin access.
- Payload Size Limiting: Set a
10kbrequest body size limit to prevent Denial of Service (DoS) attacks via large payloads. - Global Error Handling: Implemented a global error handler that prevents leaking sensitive stack traces and internal error details to the client in a production environment.
To protect against query manipulation and parameter-based attacks:
- NoSQL Injection Prevention: Used the
express-mongo-sanitizemiddleware to strip any user-supplied data containing prohibited characters (like$or.) from request bodies, query strings, and parameters. - HTTP Parameter Pollution (HPP) Prevention: Integrated the
hppmiddleware to prevent attackers from overriding parameters by creating a whitelist of parameters that are allowed to appear multiple times.
The initial database schema design was improved to enforce data integrity and security from the ground up:
- Robust Schema Validation: Enforced
requiredfields, email format validation using regex, and string normalization (trim,lowercase) in the Mongoose User schema. - Uniqueness Constraints: Ensured the email field is unique to prevent duplicate account creation.
- Secure Defaults: Set a default non-privileged role (
isAdmin: false) to prevent unauthorized privilege escalation.
To ensure application availability and protect against resource exhaustion attacks:
- Rate Limiting: Implemented
express-rate-limitto throttle requests. This includes a global limit for all endpoints and a stricter limit specifically for authentication routes to prevent brute-force attacks.
We addressed risks associated with third-party dependencies:
- Dependency Management: Updated the MongoDB connection logic to use a modern, singleton pattern, improving performance, reliability, and compatibility with the latest driver versions.
- Code Modernization: Refactored database connection code to use modern JavaScript class syntax for better maintainability and error handling.
To improve application stability and ensure graceful failure:
- Graceful Shutdown: Implemented listeners for
SIGTERM,unhandledRejection, anduncaughtExceptionto ensure the server shuts down gracefully without crashing abruptly, preventing data loss and service downtime.
The vulnerabilities in the original project were identified using Snyk, a developer security platform that helps scan and fix vulnerabilities in code, dependencies, and containers.
Follow these instructions to get a local copy of the project up and running.
- Node.js (v14 or higher)
- npm
- MongoDB instance (local or cloud)
-
Clone the Repository
git clone [https://github.com/DhananjayaLakshan/SSD_Assignment.git](https://github.com/DhananjayaLakshan/SSD_Assignment.git)
-
Install Backend Dependencies
cd SSD_Assignment npm install -
Install Frontend Dependencies
cd frontend npm install -
Configure Environment Variables Create a
.envfile in the root directory and add the following variables:PORT=5001 MONGO_URL=<YOUR_MONGODB_CONNECTION_STRING> FRONTEND_URL=http://localhost:5173 JWT_SECRET=<YOUR_JWT_SECRET_KEY> -
Run the Application
- Start the Backend Server (from the root directory):
npm run dev
- Start the Frontend Server (from the
frontenddirectory):npm run dev
- Start the Backend Server (from the root directory):
-
Access the Application Open your browser and navigate to
http://localhost:5173/.
- Frontend: React, Flowbite, Tailwind CSS, Chart.js, Axios, SweetAlert2
- Backend: Node.js, Express.js
- Database: MongoDB, Mongoose
- Security: Helmet, Express-Rate-Limit, HPP, Express-Mongo-Sanitize, Bcrypt
- This project was completed as part of the Secure Software Development (SE4030) module at the Sri Lanka Institute of Information Technology (SLIIT).
- Credit to the original author of the Smart Waste Management System.