Skip to content

Commit

Permalink
new policy protect email or sms api
Browse files Browse the repository at this point in the history
  • Loading branch information
ety001 committed Feb 11, 2024
1 parent ceb1503 commit b2b378c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ HIGH_FREQUENCY_TIME_RANGE=2
HIGH_FREQUENCY_COUNT=10
CREATOR_INFO=steem|steemcurator01|steemcurator02|booming01|booming02|booming03|booming04
GOOGLE_ANALYTICS_ID=
SMS_SEND_TIME_WINDOW=600
SMS_SEND_THRESHOLD_IN_TIME_WINDOW=1
EMAIL_SEND_TIME_WINDOW=600
EMAIL_SEND_THRESHOLD_IN_TIME_WINDOW=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addIndex('actions', ['created_at'], {
name: 'idx_created_at',
})
},
down: queryInterface => queryInterface.removeIndex('actions', 'idx_created_at'),
};
28 changes: 28 additions & 0 deletions helpers/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ async function actionLimitNew(ip, action = 'default', ipLimit = 32) {
}
}

/**
* protect email or sms api
* in {timeWindow}, only {limitCount} are allowed.
* -- action = 'request_email_code' | 'send_sms'
* -- timeWindow
* -- limitCount
*/
async function emailOrSmsActionLimit(action, timeWindow = 300, limitCount = 1) {
const created_at = {
[Op.gte]: moment()
.subtract(timeWindow, 's')
.toDate(),
};
const promises = [
db.actions.count({
where: { created_at, action: { [Op.eq]: action } },
}),
];
const [actions] = await Promise.all(promises);
if (actions > limitCount) {
throw new ApiError({
type: 'error_api_auth_code_request_frequently',
field: { action },
});
}
}

/**
* find last send sms action by country number
*/
Expand Down Expand Up @@ -209,4 +236,5 @@ module.exports = {
deleteEmailRecord,
findLastSendSmsByCountryNumber,
countTryNumber,
emailOrSmsActionLimit,
};
21 changes: 21 additions & 0 deletions routes/apiHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,18 @@ async function handleRequestEmailCode(ip, email, log, locale) {

await database.actionLimitNew(ip, 'request_email_code');

const timeWindow = process.env.EMAIL_SEND_TIME_WINDOW
? parseInt(process.env.EMAIL_SEND_TIME_WINDOW, 10)
: 300;
const limitCount = process.env.EMAIL_SEND_THRESHOLD_IN_TIME_WINDOW
? parseInt(process.env.EMAIL_SEND_THRESHOLD_IN_TIME_WINDOW, 10)
: 1;
await database.emailOrSmsActionLimit(
'request_email_code',
timeWindow,
limitCount
);

await database.logAction({
action: 'request_email_code',
ip,
Expand Down Expand Up @@ -1361,6 +1373,14 @@ async function handleRequestSmsNew(req) {
}
}

const timeWindow = process.env.SMS_SEND_TIME_WINDOW
? parseInt(process.env.SMS_SEND_TIME_WINDOW, 10)
: 300;
const limitCount = process.env.SMS_SEND_THRESHOLD_IN_TIME_WINDOW
? parseInt(process.env.SMS_SEND_THRESHOLD_IN_TIME_WINDOW, 10)
: 1;
await database.emailOrSmsActionLimit('send_sms', timeWindow, limitCount);

await database.logAction({
action: 'send_sms',
ip: req.ip,
Expand All @@ -1369,6 +1389,7 @@ async function handleRequestSmsNew(req) {
countryNumber,
},
});

services.recordSmsTracker({
sendType: 'before_send_sms',
countryCode: req.body.prefix,
Expand Down

0 comments on commit b2b378c

Please sign in to comment.