feat: complete scheduled publishing, security remediation and DEV_TICKETS implementation
Deploy Canina / deploy (push) Successful in 2m29s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s

This commit is contained in:
parsa committed 2026-09-29 01:15:33 +03:30
1 parent d6d4158cbe
commit efb3c473c4
35 files changed
+685 -185

No files matched your search

+38 -8
View File
@@ -63,7 +63,11 @@ export class AuthController {
return this.authService.sendOtp(sendOtpDto);
}
private setAuthCookies(res?: Response, accessToken?: string, refreshToken?: string) {
private setAuthCookies(
res?: Response,
accessToken?: string,
refreshToken?: string,
) {
if (!res || typeof res.cookie !== 'function') return;
const isProd = process.env.NODE_ENV === 'production';
if (accessToken) {
@@ -117,7 +121,11 @@ export class AuthController {
) {
const result = await this.authService.verifyOtp(verifyOtpDto);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
@@ -136,7 +144,11 @@ export class AuthController {
) {
const result = await this.authService.register(registerDto);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
@@ -153,7 +165,11 @@ export class AuthController {
) {
const result = await this.authService.login(loginDto);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
@@ -170,7 +186,11 @@ export class AuthController {
) {
const result = await this.authService.adminLogin(body);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
@@ -178,7 +198,9 @@ export class AuthController {
@Throttle({ default: { limit: 10, ttl: 60000 } })
@Post('refresh-token')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'دریافت جفت توکن جدید با استفاده از توکن تازه‌سازی' })
@ApiOperation({
summary: 'دریافت جفت توکن جدید با استفاده از توکن تازه‌سازی',
})
@ApiOkResponse({ description: 'توکن‌های جدید با موفقیت صادر شدند' })
@ApiBadRequestResponse({ description: 'توکن نامعتبر یا منقضی است' })
async refreshToken(
@@ -193,7 +215,11 @@ export class AuthController {
}
const result = await this.authService.refreshToken(token);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
@@ -213,7 +239,11 @@ export class AuthController {
) {
const result = await this.authService.refresh(req.user);
if (result?.data) {
this.setAuthCookies(res, result.data.accessToken, result.data.refreshToken);
this.setAuthCookies(
res,
result.data.accessToken,
result.data.refreshToken,
);
}
return result;
}
+8 -2
View File
@@ -93,7 +93,10 @@ describe('AuthService', () => {
});
it('should delete OTP, find or create user and sign JWT', async () => {
mockRedis.get.mockResolvedValue('12345');
mockRedis.get.mockImplementation(async (key: string) => {
if (key.startsWith('otp_attempts:')) return null;
return '12345';
});
const mockUser = { id: 'user-id', mobile: '09123456789' };
mockPrisma.user.findUnique.mockResolvedValue(mockUser);
@@ -116,7 +119,10 @@ describe('AuthService', () => {
});
it('should create new user if user does not exist', async () => {
mockRedis.get.mockResolvedValue('12345');
mockRedis.get.mockImplementation(async (key: string) => {
if (key.startsWith('otp_attempts:')) return null;
return '12345';
});
mockPrisma.user.findUnique.mockResolvedValue(null);
const newUser = { id: 'new-user-id', mobile: '09123456789' };
mockPrisma.user.create.mockResolvedValue(newUser);
+38 -9
View File
@@ -150,17 +150,41 @@ export class AuthService {
async verifyOtp(verifyOtpDto: VerifyOtpDto) {
const phoneNumber = normalizeMobile(verifyOtpDto.phoneNumber);
const code = String(verifyOtpDto.code).trim();
const attemptsKey = `otp_attempts:${phoneNumber}`;
// SEC-007: Check failed attempts to prevent brute-forcing OTP
const currentAttempts = await this.redisService.get(attemptsKey);
if (currentAttempts && Number(currentAttempts) >= 5) {
await this.redisService.del(`otp:${phoneNumber}`);
throw new BadRequestException({
message:
'تعداد تلاش‌های ناموفق بیش از حد مجاز است. لطفاً پس از چند دقیقه دوباره کد جدید دریافت کنید.',
error: 'TOO_MANY_OTP_ATTEMPTS',
});
}
const savedCode = await this.redisService.get(`otp:${phoneNumber}`);
if (!savedCode || savedCode !== code) {
const attempts = await this.redisService.incr(attemptsKey, 300);
const remaining = Math.max(0, 5 - attempts);
if (remaining === 0) {
await this.redisService.del(`otp:${phoneNumber}`);
throw new BadRequestException({
message:
'کد تایید به دلیل ۵ بار تلاش ناموفق باطل شد. لطفاً کد جدید دریافت کنید.',
error: 'TOO_MANY_OTP_ATTEMPTS',
});
}
throw new BadRequestException({
message: 'کد وارد شده اشتباه یا منقضی شده است',
message: `کد وارد شده اشتباه یا منقضی شده است. (${remaining} تلاش باقی‌مانده)`,
error: 'INVALID_OTP',
});
}
// Success - clear OTP and attempts
await this.redisService.del(`otp:${phoneNumber}`);
await this.redisService.del(attemptsKey);
let user = await this.prisma.user.findUnique({
where: { mobile: phoneNumber },
@@ -346,11 +370,10 @@ export class AuthService {
Buffer.from(crypto.createHash('sha256').update(adminEmail).digest()),
);
const passwordMatches =
crypto.timingSafeEqual(
Buffer.from(crypto.createHash('sha256').update(body.password).digest()),
Buffer.from(crypto.createHash('sha256').update(adminPassword).digest()),
);
const passwordMatches = crypto.timingSafeEqual(
Buffer.from(crypto.createHash('sha256').update(body.password).digest()),
Buffer.from(crypto.createHash('sha256').update(adminPassword).digest()),
);
if (emailMatches && passwordMatches) {
const payload = {
@@ -391,7 +414,9 @@ export class AuthService {
secret: getJwtRefreshSecret(),
});
} catch {
throw new UnauthorizedException('توکن تازه‌سازی نامعتبر یا منقضی شده است');
throw new UnauthorizedException(
'توکن تازه‌سازی نامعتبر یا منقضی شده است',
);
}
const userId = decoded.sub;
@@ -405,7 +430,9 @@ export class AuthService {
const tokenKey = `refresh_token:${userId}:${tokenId}`;
const tokenValid = await this.redisService.get(tokenKey);
if (!tokenValid) {
throw new UnauthorizedException('توکن تازه‌سازی باطل شده است. لطفاً مجدداً وارد شوید');
throw new UnauthorizedException(
'توکن تازه‌سازی باطل شده است. لطفاً مجدداً وارد شوید',
);
}
// Invalidate old token (Rotation)
@@ -492,7 +519,9 @@ export class AuthService {
// If token payload has jti and sub, also invalidate the refresh token in Redis
if (decoded && decoded.sub && decoded.jti) {
await this.redisService.del(`refresh_token:${decoded.sub}:${decoded.jti}`);
await this.redisService.del(
`refresh_token:${decoded.sub}:${decoded.jti}`,
);
}
} catch {
await this.redisService.set(`bl_token:${token}`, '1', 15 * 60);
+3 -1
View File
@@ -49,7 +49,9 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
if (rawToken) {
const isBlacklisted = await this.redisService.get(`bl_token:${rawToken}`);
if (isBlacklisted) {
throw new UnauthorizedException('این توکن باطل شده است. لطفاً مجدداً وارد شوید');
throw new UnauthorizedException(
'این توکن باطل شده است. لطفاً مجدداً وارد شوید',
);
}
}