feat: check the name against regex to not break the DB

This commit is contained in:
2022-10-07 11:31:52 +02:00
parent df47ba1ea2
commit a526f5ea6b

View File

@@ -16,6 +16,7 @@ const limiter = rateLimit({
legacyHeaders: false, legacyHeaders: false,
}) })
app.use(limiter) app.use(limiter)
const englishRegex = /^[A-Za-z0-9]*$/
await mongoose.connect(`${process.env.MONGODB}`).then(() => { await mongoose.connect(`${process.env.MONGODB}`).then(() => {
console.log("Connected to MongoDB!") console.log("Connected to MongoDB!")
@@ -23,7 +24,7 @@ await mongoose.connect(`${process.env.MONGODB}`).then(() => {
app.post("/sern/newTime", async (req, res, next) => { app.post("/sern/newTime", async (req, res, next) => {
if ( if (
req.body.name && englishRegex.test(req.body.name) &&
req.body.timezone && req.body.timezone &&
req.body.key === process.env.SERN_TIME && req.body.key === process.env.SERN_TIME &&
req.body.userid req.body.userid
@@ -45,7 +46,7 @@ app.post("/sern/newTime", async (req, res, next) => {
const saveToDB = new sernTime({ const saveToDB = new sernTime({
name: req.body.name, name: req.body.name,
timezone: req.body.timezone, timezone: req.body.timezone,
userid: req.body.userid userid: req.body.userid,
}) })
saveToDB.save() saveToDB.save()
res.json({ "ok": "kay done" }) res.json({ "ok": "kay done" })
@@ -56,8 +57,7 @@ app.post("/sern/newTime", async (req, res, next) => {
}) })
} else { } else {
res.status(400).json({ res.status(400).json({
"error": "error": "make sure you have the right params and english characters.",
"make sure you have name, timezone and key as a JSON post. You could also have your key wrong.",
}) })
} }
}) })