Skip to content

Commit 4dc1e01

Browse files
committed
fix: continue work on blocked_email_domains (2)
1 parent 161be7f commit 4dc1e01

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

src/backend/src/api/APIError.js

+4
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ module.exports = class APIError {
306306
status: 409,
307307
message: ({ email }) => `Email ${quot(email)} is already in use.`,
308308
},
309+
'email_not_allowed': {
310+
status: 400,
311+
message: ({ email }) => `The email ${quot(email)} is not allowed.`,
312+
},
309313
'username_already_in_use': {
310314
status: 409,
311315

src/backend/src/routers/save_account.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ router.post('/save_account', auth, express.json(), async (req, res, next)=>{
7373

7474
const svc_cleanEmail = req.services.get('clean-email')
7575
const clean_email = svc_cleanEmail.clean(req.body.email);
76-
77-
if ( can(config.blocked_email_domains, 'iterate') ) {
78-
for ( const suffix of config.blocked_email_domains ) {
79-
if ( clean_email.endsWith(suffix) ) {
80-
return res.status(400).send('This email domain is not allowed.');
81-
}
82-
}
76+
77+
if ( ! svc_cleanEmail.validate(clean_email) ) {
78+
return res.status(400).send('This email domain is not allowed.');
8379
}
8480

8581
const svc_edgeRateLimit = req.services.get('edge-rate-limit');

src/backend/src/routers/signup.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,8 @@ module.exports = eggspress(['/signup'], {
147147
const svc_cleanEmail = req.services.get('clean-email');
148148
const clean_email = svc_cleanEmail.clean(req.body.email);
149149

150-
if ( can(config.blocked_email_domains, 'iterate') ) {
151-
for ( const suffix of config.blocked_email_domains ) {
152-
if ( clean_email.endsWith(suffix) ) {
153-
return res.status(400).send('This email domain is not allowed.');
154-
}
155-
}
150+
if ( ! svc_cleanEmail.validate(clean_email) ) {
151+
return res.status(400).send('This email domain is not allowed');
156152
}
157153

158154
// duplicate username check

src/backend/src/routers/user-protected/change-email.js

+20
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ module.exports = {
4949
const svc_cleanEmail = req.services.get('clean-email');
5050
const clean_email = svc_cleanEmail.clean(new_email);
5151

52+
if ( ! svc_cleanEmail.validate(clean_email) ) {
53+
throw APIError.create('email_not_allowed', undefined, {
54+
email: clean_email,
55+
});
56+
}
57+
5258
// check if email is already in use
5359
const db = req.services.get('database').get(DB_WRITE, 'auth');
5460
const rows = await db.read(
5561
'SELECT COUNT(*) AS `count` FROM `user` WHERE (`email` = ? OR `clean_email` = ?) AND `email_confirmed` = 1',
5662
[new_email, clean_email]
5763
);
64+
65+
// TODO: DRY: signup.js, save_account.js
5866
if ( rows[0].count > 0 ) {
5967
throw APIError.create('email_already_in_use', null, { email: new_email });
6068
}
@@ -84,6 +92,18 @@ module.exports = {
8492
[new_email, token, user.id]
8593
);
8694

95+
// Update email change audit table
96+
await db.write(
97+
'INSERT INTO `user_update_audit` ' +
98+
'(`user_id`, `user_id_keep`, `old_email`, `new_email`, `reason`) ' +
99+
'VALUES (?, ?, ?, ?, ?)',
100+
[
101+
req.user.id, req.user.id,
102+
old_email, new_email,
103+
'change_username'
104+
]
105+
);
106+
87107
res.send({ success: true });
88108
}
89109
};

src/backend/src/services/CleanEmailService.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { can } = require("../util/langutil");
12
const BaseService = require("./BaseService");
23

34
class CleanEmailService extends BaseService {
@@ -99,6 +100,21 @@ class CleanEmailService extends BaseService {
99100

100101
return eml.local + '@' + eml.domain;
101102
}
103+
104+
validate (email) {
105+
email = this.clean(email);
106+
const config = this.global_config;
107+
108+
if ( can(config.blocked_email_domains, 'iterate') ) {
109+
for ( const suffix of config.blocked_email_domains ) {
110+
if ( email.endsWith(suffix) ) {
111+
return false;
112+
}
113+
}
114+
}
115+
116+
return true;
117+
}
102118

103119
_test ({ assert }) {
104120
const cases = [

0 commit comments

Comments
 (0)