Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Prevent email check replay

Chocobozzz0100bffe6c33
diff --git a/packages/tests/src/api/server/email.ts b/packages/tests/src/api/server/email.ts
index e17dc914cf..21d3579277 100644
--- a/packages/tests/src/api/server/email.ts
+++ b/packages/tests/src/api/server/email.ts
@@ -337,6 +337,14 @@ describe('Test emails', function () {
it('Should verify the email', async function () {
await server.users.verifyEmail({ userId, verificationString })
})
+
+ it('Should not verify the email again with the same verification string', async function () {
+ await server.users.verifyEmail({
+ userId,
+ verificationString,
+ expectedStatus: HttpStatusCode.FORBIDDEN_403
+ })
+ })
})
describe('When verifying a registration email', function () {
@@ -392,6 +400,14 @@ describe('Test emails', function () {
it('Should verify the email', async function () {
await server.registrations.verifyEmail({ registrationId: registrationIdEmail, verificationString })
})
+
+ it('Should not verify the email again with the same verification string', async function () {
+ await server.registrations.verifyEmail({
+ registrationId: registrationIdEmail,
+ verificationString,
+ expectedStatus: HttpStatusCode.FORBIDDEN_403
+ })
+ })
})
describe('Email config', function () {
diff --git a/packages/tests/src/api/users/users-email-verification.ts b/packages/tests/src/api/users/users-email-verification.ts
index 0a16bf0cf8..0e5ab0e359 100644
--- a/packages/tests/src/api/users/users-email-verification.ts
+++ b/packages/tests/src/api/users/users-email-verification.ts
@@ -89,6 +89,10 @@ describe('Test users email verification', function () {
expect(user.emailVerified).to.be.true
})
+ it('Should not verify the email again with the same verification string', async function () {
+ await server.users.verifyEmail({ userId, verificationString, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
+ })
+
it('Should be able to change the user email', async function () {
let updateVerificationString: string
diff --git a/server/core/controllers/api/users/email-verification.ts b/server/core/controllers/api/users/email-verification.ts
index c9856a661b..0d35c7ee9e 100644
--- a/server/core/controllers/api/users/email-verification.ts
+++ b/server/core/controllers/api/users/email-verification.ts
@@ -1,4 +1,5 @@
import { HttpStatusCode } from '@peertube/peertube-models'
+import { Redis } from '@server/lib/redis.js'
import express from 'express'
import { CONFIG } from '../../../initializers/config.js'
import { sendVerifyRegistrationEmail, sendVerifyRegistrationRequestEmail, sendVerifyUserChangeEmail } from '../../../lib/user.js'
@@ -31,7 +32,11 @@ emailVerificationRouter.post(
asyncMiddleware(reSendRegistrationVerifyUserEmail)
)
-emailVerificationRouter.post('/:id/verify-email', asyncMiddleware(usersVerifyEmailValidator), asyncMiddleware(verifyUserEmail))
+emailVerificationRouter.post(
+ '/:id/verify-email',
+ asyncMiddleware(usersVerifyEmailValidator),
+ asyncMiddleware(verifyUserEmail)
+)
emailVerificationRouter.post(
'/registrations/:registrationId/verify-email',
@@ -72,6 +77,8 @@ async function verifyUserEmail (req: express.Request, res: express.Response) {
await user.save()
+ await Redis.Instance.deleteUserVerifyEmailLink(user.id)
+
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
@@ -81,5 +88,7 @@ async function verifyRegistrationEmail (req: express.Request, res: express.Respo
await registration.save()
+ await Redis.Instance.deleteRegistrationVerifyEmailLink(registration.id)
+
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
diff --git a/server/core/lib/redis.ts b/server/core/lib/redis.ts
index 7ff3ae4cce..9f6c27fade 100644
--- a/server/core/lib/redis.ts
+++ b/server/core/lib/redis.ts
@@ -204,6 +204,10 @@ class Redis {
return this.getValue(this.generateUserVerifyEmailKey(userId))
}
+ deleteUserVerifyEmailLink (userId: number) {
+ return this.removeValue(this.generateUserVerifyEmailKey(userId))
+ }
+
async setRegistrationVerifyEmailVerificationString (registrationId: number) {
const generatedString = await generateRandomString(32)
@@ -216,6 +220,10 @@ class Redis {
return this.getValue(this.generateRegistrationVerifyEmailKey(registrationId))
}
+ deleteRegistrationVerifyEmailLink (registrationId: number) {
+ return this.removeValue(this.generateRegistrationVerifyEmailKey(registrationId))
+ }
+
/* ************ Contact form per IP ************ */
async setContactFormIp (ip: string) {