Compare commits
2 Commits
b1093f3cfc
...
5c0b291b4a
Author | SHA1 | Date | |
---|---|---|---|
|
5c0b291b4a | ||
|
0dea7fcaec |
@@ -1,5 +1,5 @@
|
|||||||
// The entire module is now a function that accepts 'contactController' as an argument.
|
// The entire module is now a function that accepts 'contactController' and security middleware as an argument.
|
||||||
module.exports = (contactController) => {
|
module.exports = (contactController, securityMw) => {
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const rateLimit = require('express-rate-limit');
|
const rateLimit = require('express-rate-limit');
|
||||||
@@ -12,17 +12,12 @@ module.exports = (contactController) => {
|
|||||||
message: "Too many requests from this IP, please try again after 15 minutes."
|
message: "Too many requests from this IP, please try again after 15 minutes."
|
||||||
});
|
});
|
||||||
|
|
||||||
// Define the route for form submissions
|
// Define the route for form submissions with all middleware
|
||||||
router.post('/submit-form',
|
router.post('/submit-form',
|
||||||
apiLimiter,
|
apiLimiter,
|
||||||
// In-line honeypot check
|
// The security middleware is now a separate step,
|
||||||
(req, res, next) => {
|
// containing both the honeypot check and hCaptcha verification.
|
||||||
if (req.body.url) {
|
securityMw.formSecurityCheck,
|
||||||
console.warn('Bot detected! Honeypot field was filled.');
|
|
||||||
return res.status(200).json({ success: true, message: 'Thank you for your submission.' });
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
[
|
[
|
||||||
// express-validator: sanitation and validation
|
// express-validator: sanitation and validation
|
||||||
body('firstName').trim().escape(),
|
body('firstName').trim().escape(),
|
||||||
|
@@ -37,8 +37,11 @@ const transporter = nodemailer.createTransport({
|
|||||||
|
|
||||||
const contactController = require('./controllers/contactController')(pool, transporter);
|
const contactController = require('./controllers/contactController')(pool, transporter);
|
||||||
|
|
||||||
// Import contactRoutes and contactController
|
// Import the security middleware
|
||||||
const contactRoutes = require('./routes/contactRoutes')(contactController);
|
const securityMw = require('./middleware/securityMw');
|
||||||
|
|
||||||
|
// Import contactRoutes and contactController, and pass in securityMw
|
||||||
|
const contactRoutes = require('./routes/contactRoutes')(contactController, securityMw);
|
||||||
|
|
||||||
// Use contactRoutes to connect the modular router to the main app
|
// Use contactRoutes to connect the modular router to the main app
|
||||||
app.use(contactRoutes);
|
app.use(contactRoutes);
|
||||||
|
Reference in New Issue
Block a user