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

feat: added rate limiter and stronger validation #5

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const makeConnectionWithDB = async () => {
try {
await sequelize.authenticate();
// if (process.env.NODE_ENV === "development") {
// await sequelize.sync({
// force: true,
// logging: (sql) => console.log(sql),
// });
await sequelize.sync({
force: true,
logging: (sql) => console.log(sql),
});
// }
console.log("Connection has been established successfully.");
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import routes from "./routes";
import { makeConnectionWithDB } from "./db";
import loadData from "./insertData";
import { loadA2ZSheet } from "./script";
import { limiter } from "./src/utils/rateLimiter";

dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;

app.use(limiter);
app.use(helmet());
app.use(xss());

Expand Down
10 changes: 9 additions & 1 deletion src/schema/revision.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ export const userIdSchema = object({
query: object({
userId: string({ required_error: "userId is required" })
.nonempty({ message: "userId can't be empty" })
.trim(),
.trim()
.refine(
async (userId) => {
const user = await User.findByPk(userId);
if (user) return true;
else return false;
},
{ message: "The user does not exists" }
),
}),
});

Expand Down
57 changes: 48 additions & 9 deletions src/schema/submission.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,28 @@ export const createSubmissionSchema = object({
required_error: "questionId is required",
})
.nonempty({ message: "questionId can't be empty" })
.trim(),
.trim()
.refine(
async (questionId) => {
const topic = await Question.findByPk(questionId);
if (topic) return true;
else return false;
},
{ message: "questionId does not exist" }
),
topicId: string({
required_error: "topicId is required",
})
.nonempty({ message: "topicId can't be empty" })
.trim(),
.trim()
.refine(
async (questionTopic) => {
const topic = await TopicUnderSubject.findByPk(questionTopic);
if (topic) return true;
else return false;
},
{ message: "topicId does not exist" }
),
completionDate: dateSchema,
revisionDate: dateSchema.optional(),
submittedBy: string({
Expand All @@ -34,28 +50,51 @@ export const userIdSchema = object({
query: object({
userId: string({ required_error: "userId is required" })
.nonempty({ message: "userId can't be empty" })
.trim(),
.trim()
.refine(
async (userId) => {
const user = await User.findByPk(userId);
if (user) return true;
else return false;
},
{ message: "The user does not exists" }
),
}),
});

export const userIdAndQuestionIdSchema = object({
query: object({
userId: string({ required_error: "userId is required" })
.nonempty({ message: "emauuserIdserIdil can't be empty" })
.trim(),
.trim()
.refine(
async (userId) => {
const user = await User.findByPk(userId);
if (user) return true;
else return false;
},
{ message: "The user does not exists" }
),
questionId: string({
required_error: "questionId is required",
})
.nonempty({ message: "questionId can't be empty" })
.trim(),
.trim()
.refine(
async (questionId) => {
const topic = await Question.findByPk(questionId);
if (topic) return true;
else return false;
},
{ message: "questionId does not exist" }
),
}),
});

export const idSchema = object({
body: object({
id: string({ required_error: "id is required" })
.nonempty({
message: "id can't be empty",
}),
id: string({ required_error: "id is required" }).nonempty({
message: "id can't be empty",
}),
}),
});
15 changes: 15 additions & 0 deletions src/utils/rateLimiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import rateLimiter from "express-rate-limit";
import { Request, Response, NextFunction } from "express";

export const limiter = rateLimiter({
max: 30, // limit each ip to 30 request per window(1 minute)
windowMs: 1 * 60 * 1000, // 1 minute
standardHeaders: true,
legacyHeaders: false,
handler: function (req: Request, res: Response, next: NextFunction) {
return res.status(429).json({
error:
"You sent too many requests. Please wait for a minute then try again",
});
},
});
2 changes: 0 additions & 2 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
change all models add id instead of uuid
work on all controllers aas models have changed
do stronger validation
postman collection
check any missing todos
Expand Down
Loading