Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added middleware for subscribe API #1102

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
16 changes: 16 additions & 0 deletions middlewares/validators/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ const createChallenge = async (req, res, next) => {
}
};

const subscribeToChallenge = async (req, res, next) => {
const schema = joi.object().strict().keys({
userId: joi.string().required(),
challengeId: joi.string().required(),
khushi818 marked this conversation as resolved.
Show resolved Hide resolved
});

try {
await schema.validateAsync(req.body);
next();
} catch (error) {
logger.error(`Error validating subscribeToChallenge payload : ${error}`);
res.boom.badRequest(error.details[0].message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @arpit01923, sending the exact error message to the end user might be harmful and can expose critical information on the backend Making us prone to external threats.

We already have a response saved in constants file which can be used here to send back to the end user.

Copy link
Author

@arpit01923 arpit01923 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error message changed

}
};

module.exports = {
createChallenge,
subscribeToChallenge
};
4 changes: 2 additions & 2 deletions routes/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const express = require("express");
const router = express.Router();
const authenticate = require("../middlewares/authenticate");
const challenges = require("../controllers/challenge");
const { createChallenge } = require("../middlewares/validators/challenges");
const { createChallenge, subscribeToChallenge } = require("../middlewares/validators/challenges");

router.get("/", authenticate, challenges.fetchChallenges);
router.post("/", authenticate, createChallenge, challenges.createChallenge);
router.post("/subscribe", authenticate, challenges.subscribeToChallenge);
router.post("/subscribe", authenticate, subscribeToChallenge, challenges.subscribeToChallenge);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.

module.exports = router;