Skip to content

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

Time safe comparison

Chocobozzz9f07e129ff4f
diff --git a/server/core/helpers/peertube-crypto.ts b/server/core/helpers/peertube-crypto.ts
index 03fef266db..311f14cd37 100644
--- a/server/core/helpers/peertube-crypto.ts
+++ b/server/core/helpers/peertube-crypto.ts
@@ -1,6 +1,6 @@
import { ParsedDraftSignature, parseRequestSignature, verifyDraftSignature } from '@misskey-dev/node-http-message-signatures'
import { sha256 } from '@peertube/peertube-node-utils'
-import { createCipheriv, createDecipheriv } from 'crypto'
+import { createCipheriv, createDecipheriv, timingSafeEqual } from 'crypto'
import { Request } from 'express'
import { BCRYPT_SALT_SIZE, ENCRYPTION, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants.js'
import { MActor } from '../types/models/index.js'
@@ -37,6 +37,22 @@ async function cryptPassword (password: string) {
return hash(password, salt)
}
+// ---------------------------------------------------------------------------
+// Secret comparison
+// ---------------------------------------------------------------------------
+
+// Prevent timing attacks when comparing secret tokens (email verification, password reset etc.)
+function isSecretEqual (a: string, b: string) {
+ if (typeof a !== 'string' || typeof b !== 'string') return false
+
+ const aBuffer = Buffer.from(a)
+ const bBuffer = Buffer.from(b)
+
+ if (aBuffer.length !== bBuffer.length) return false
+
+ return timingSafeEqual(aBuffer, bBuffer)
+}
+
// ---------------------------------------------------------------------------
// HTTP Signature
// ---------------------------------------------------------------------------
@@ -136,5 +152,6 @@ export {
encrypt,
isHTTPSignatureDigestValid,
isHTTPSignatureVerified,
+ isSecretEqual,
parseHTTPSignature
}
diff --git a/server/core/middlewares/validators/users/user-email-verification.ts b/server/core/middlewares/validators/users/user-email-verification.ts
index a19928bb6b..d178dd05b6 100644
--- a/server/core/middlewares/validators/users/user-email-verification.ts
+++ b/server/core/middlewares/validators/users/user-email-verification.ts
@@ -5,6 +5,7 @@ import { getByEmailPermissive } from '@server/lib/user.js'
import { UserModel } from '@server/models/user/user.js'
import express from 'express'
import { body, param } from 'express-validator'
+import { isSecretEqual } from '../../../helpers/peertube-crypto.js'
import { logger } from '../../../helpers/logger.js'
import { Redis } from '../../../lib/redis.js'
import { areValidationErrors, checkUserIdExist } from '../shared/index.js'
@@ -95,7 +96,7 @@ export const usersVerifyEmailValidator = [
const user = res.locals.user
const redisVerificationString = await Redis.Instance.getUserVerifyEmailLink(user.id)
- if (redisVerificationString !== req.body.verificationString) {
+ if (!isSecretEqual(redisVerificationString, req.body.verificationString)) {
return res.fail({ status: HttpStatusCode.FORBIDDEN_403, message: 'Invalid verification string.' })
}
@@ -119,7 +120,7 @@ export const registrationVerifyEmailValidator = [
const registration = res.locals.userRegistration
const redisVerificationString = await Redis.Instance.getRegistrationVerifyEmailLink(registration.id)
- if (redisVerificationString !== req.body.verificationString) {
+ if (!isSecretEqual(redisVerificationString, req.body.verificationString)) {
return res.fail({ status: HttpStatusCode.FORBIDDEN_403, message: 'Invalid verification string.' })
}
diff --git a/server/core/middlewares/validators/users/users.ts b/server/core/middlewares/validators/users/users.ts
index 5401f9774d..4447d25787 100644
--- a/server/core/middlewares/validators/users/users.ts
+++ b/server/core/middlewares/validators/users/users.ts
@@ -32,6 +32,7 @@ import {
} from '../../../helpers/custom-validators/users.js'
import { isVideoChannelUsernameValid } from '../../../helpers/custom-validators/video-channels.js'
import { logger } from '../../../helpers/logger.js'
+import { isSecretEqual } from '../../../helpers/peertube-crypto.js'
import { isThemeRegistered } from '../../../lib/plugins/theme-utils.js'
import { Redis } from '../../../lib/redis.js'
import {
@@ -434,7 +435,7 @@ export const usersResetPasswordValidator = [
const user = res.locals.user
const redisVerificationString = await Redis.Instance.getResetPasswordVerificationString(user.id)
- if (redisVerificationString !== req.body.verificationString) {
+ if (!isSecretEqual(redisVerificationString, req.body.verificationString)) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Invalid verification string.'