Skip to content

Commit c254b39

Browse files
committed
fix: consistent email check in confirm-email
1 parent c959ef3 commit c254b39

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

src/backend/src/routers/confirm-email.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,25 @@ router.post('/confirm-email', auth, express.json(), async (req, res, next)=>{
4949
kv.expire(`confirm-email|${req.ip}|${req.body.email ?? req.body.username}`, 60 * 10, 'NX')
5050

5151
// Scenario: email was confirmed on another account already
52-
const rows = await db.read(
53-
'SELECT `id` FROM `user` WHERE `email` = ? AND `email_confirmed` = 1',
54-
[req.body.email],
55-
);
56-
if ( rows.length > 0 ) {
57-
APIError.create('email_already_in_use').write(res);
58-
return;
52+
{
53+
const svc_cleanEmail = req.services.get('clean-email');
54+
const clean_email = svc_cleanEmail.clean(req.body.email);
55+
56+
if ( ! await svc_cleanEmail.validate(clean_email) ) {
57+
APIError.create('field_invalid', null, {
58+
key: 'email',
59+
expected: 'valid email',
60+
got: req.body.email,
61+
})
62+
}
63+
const rows = await db.read(
64+
`SELECT EXISTS(
65+
SELECT 1 FROM user WHERE (email=? OR clean_email=?) AND email_confirmed=1 AND password IS NOT NULL
66+
) AS email_exists`, [req.body.email, clean_email]);
67+
if ( rows[0].email_exists ) {
68+
APIError.create('email_already_in_use').write(res);
69+
return;
70+
}
5971
}
6072

6173
// If other users have the same unconfirmed email, revoke it

0 commit comments

Comments
 (0)