core: security: add mail_domain blacklist

This commit is contained in:
undefined committed 2021-04-21 12:59:51 +08:00
1 parent b902b54409
commit 869c3131ca
4 files changed
+13 -7

No files matched your search

+5 -2
View File
@@ -3,7 +3,7 @@ import yaml from 'js-yaml';
import { import {
VerifyPasswordError, UserAlreadyExistError, InvalidTokenError, VerifyPasswordError, UserAlreadyExistError, InvalidTokenError,
NotFoundError, UserNotFoundError, PermissionError, NotFoundError, UserNotFoundError, PermissionError,
DomainAlreadyExistsError, ValidationError, DomainAlreadyExistsError, ValidationError, BlacklistedError,
} from '../error'; } from '../error';
import { Mdoc, Setting } from '../interface'; import { Mdoc, Setting } from '../interface';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
@@ -25,6 +25,7 @@ import * as discussion from '../model/discussion';
import token from '../model/token'; import token from '../model/token';
import * as training from '../model/training'; import * as training from '../model/training';
import { PERM, PRIV } from '../model/builtin'; import { PERM, PRIV } from '../model/builtin';
import BlackListModel from '../model/blacklist';
const { geoip, useragent } = global.Hydro.lib; const { geoip, useragent } = global.Hydro.lib;
@@ -153,10 +154,12 @@ class HomeSecurityHandler extends Handler {
@param('password', Types.String) @param('password', Types.String)
@param('mail', Types.Name, isEmail) @param('mail', Types.Name, isEmail)
async postChangeMail(domainId: string, current: string, email: string) { async postChangeMail(domainId: string, current: string, email: string) {
await this.limitRate('send_mail', 3600, 30); const mailDomain = email.split('@')[1];
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
this.user.checkPassword(current); this.user.checkPassword(current);
const udoc = await user.getByEmail(domainId, email); const udoc = await user.getByEmail(domainId, email);
if (udoc) throw new UserAlreadyExistError(email); if (udoc) throw new UserAlreadyExistError(email);
await this.limitRate('send_mail', 3600, 30);
const [code] = await token.add( const [code] = await token.add(
token.TYPE_CHANGEMAIL, token.TYPE_CHANGEMAIL,
system.get('session.unsaved_expire_seconds'), system.get('session.unsaved_expire_seconds'),
+3
View File
@@ -16,6 +16,7 @@ import problem, { Pdoc } from '../model/problem';
import task from '../model/task'; import task from '../model/task';
import * as system from '../model/system'; import * as system from '../model/system';
import { PERM, PRIV } from '../model/builtin'; import { PERM, PRIV } from '../model/builtin';
import BlackListModel from '../model/blacklist';
import { isEmail, isPassword, isUname } from '../lib/validator'; import { isEmail, isPassword, isUname } from '../lib/validator';
import { sendMail } from '../lib/mail'; import { sendMail } from '../lib/mail';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
@@ -67,6 +68,8 @@ export class UserRegisterHandler extends Handler {
async post(domainId: string, mail: string, phoneNumber: string) { async post(domainId: string, mail: string, phoneNumber: string) {
if (mail) { if (mail) {
if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail); if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail);
const mailDomain = mail.split('@')[1];
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
await this.limitRate('send_mail', 3600, 30); await this.limitRate('send_mail', 3600, 30);
const t = await token.add( const t = await token.add(
token.TYPE_REGISTRATION, token.TYPE_REGISTRATION,
+4 -4
View File
@@ -6,10 +6,10 @@ const coll = db.collection('blacklist');
class BlackListModel { class BlackListModel {
@ArgMethod @ArgMethod
static async add(ip: string) { static async add(id: string) {
const expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000); const expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
const res = await coll.findOneAndUpdate( const res = await coll.findOneAndUpdate(
{ _id: ip }, { _id: id },
{ $set: { expireAt } }, { $set: { expireAt } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
@@ -17,8 +17,8 @@ class BlackListModel {
} }
@ArgMethod @ArgMethod
static async get(ip: string) { static async get(id: string) {
return await coll.findOne({ _id: ip }); return await coll.findOne({ _id: id });
} }
@ArgMethod @ArgMethod
+1 -1
View File
@@ -477,7 +477,7 @@ export class Handler extends HandlerCommon {
} }
async getBdoc() { async getBdoc() {
const bdoc = await blacklist.get(this.request.ip); const bdoc = await blacklist.get(`ip::${this.request.ip}`);
if (bdoc) throw new BlacklistedError(this.request.ip); if (bdoc) throw new BlacklistedError(this.request.ip);
} }