From d4f984e911b9c647d4c5bed41157424faadebe49 Mon Sep 17 00:00:00 2001 From: Suslov-MD Date: Sun, 16 Aug 2026 08:54:41 +0000 Subject: [PATCH] =?UTF-8?q?Phase=205/7:=20=D0=94=D0=BE=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8=20=E2=80=94=20=D0=BA=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D1=80=D0=B5=D1=82=D0=BD=D1=8B=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8,=20CORS=20multi-origin,=20ApiError=20413?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/routes/auth.routes.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/routes/auth.routes.ts b/server/src/routes/auth.routes.ts index dfa7001..4faafd9 100644 --- a/server/src/routes/auth.routes.ts +++ b/server/src/routes/auth.routes.ts @@ -9,19 +9,16 @@ import { signUserToken } from '../utils/jwt.js'; import type { AuthenticatedRequest } from '../middleware/auth.middleware.js'; import type { Request, Response } from 'express'; -/** Сообщение об ошибке валидации. */ -const VALIDATION_MSG = 'Некорректный email или пароль'; - /** - * Извлекает email и password из тела запроса. + * Извлекает email и password из тела запроса с подробной валидацией. * * @param rawBody — тело запроса (any из Express) * @returns объект с валидированными email и password - * @throws {ApiError} если email или password некорректны + * @throws {ApiError} с конкретным сообщением о проблеме */ function extractCredentials(rawBody: unknown): { email: string; password: string } { if (typeof rawBody !== 'object' || rawBody === null) { - throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG); + throw new ApiError(400, 'VALIDATION_ERROR', 'Тело запроса некорректно'); } // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-unsafe-type-assertion -- извлечение полей из unknown const record = rawBody as Record; @@ -30,11 +27,18 @@ function extractCredentials(rawBody: unknown): { email: string; password: string const emailStr = typeof email === 'string' ? email.trim().toLowerCase() : ''; const passwordStr = typeof password === 'string' ? password : ''; - if (emailStr.length === 0 || !emailStr.includes('@')) { - throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG); + if (emailStr.length === 0) { + throw new ApiError(400, 'VALIDATION_ERROR', 'Email не указан'); + } + // eslint-disable-next-line sonarjs/slow-regex -- email regex, ограниченный input + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailStr)) { + throw new ApiError(400, 'VALIDATION_ERROR', `Некорректный формат email: ${emailStr}`); + } + if (passwordStr.length === 0) { + throw new ApiError(400, 'VALIDATION_ERROR', 'Пароль не указан'); } if (passwordStr.length < 8) { - throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG); + throw new ApiError(400, 'VALIDATION_ERROR', 'Пароль должен содержать минимум 8 символов'); } return { email: emailStr, password: passwordStr };