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

new policy protect email or sms api #592

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [20.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:10-alpine as build-stage
FROM node:20-alpine as build-stage

WORKDIR /app

Expand Down Expand Up @@ -26,7 +26,7 @@ RUN yarn build
RUN yarn install --non-interactive --frozen-lockfile --production

# build and test admin interface
FROM node:10-alpine as admin-stage
FROM node:20-alpine as admin-stage
COPY admin /app/admin
WORKDIR /app/admin
ENV REACT_APP_SERVER_ADDRESS /admin
Expand All @@ -35,7 +35,7 @@ RUN JOBS=max yarn install --non-interactive --frozen-lockfile && \
yarn build

# copy built application to runtime image
FROM node:10-alpine
FROM node:20-alpine
WORKDIR /app
COPY --from=build-stage /app/app.js app.js
COPY --from=build-stage /app/constants.js constants.js
Expand Down
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
Loading
Loading