From da8b4b72952e8e6799614e36883ed39a4a2b9bcf Mon Sep 17 00:00:00 2001 From: Lamarcke Date: Thu, 7 Mar 2024 01:14:11 -0300 Subject: [PATCH 1/2] - Adds check for maximum allowed relations to reduce performance overhead --- .../game-repository.service.ts | 24 ++++++++++++++++++- src/notifications/notifications.controller.ts | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/game/game-repository/game-repository.service.ts b/src/game/game-repository/game-repository.service.ts index b1f8395..972b5cd 100644 --- a/src/game/game-repository/game-repository.service.ts +++ b/src/game/game-repository/game-repository.service.ts @@ -1,7 +1,7 @@ import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; import { Game } from "./entities/game.entity"; -import { DataSource, In, Repository } from "typeorm"; +import { DataSource, FindOptionsRelations, In, Repository } from "typeorm"; import { GameGenre } from "./entities/game-genre.entity"; import { GamePlatform } from "./entities/game-platform.entity"; import { GameTheme } from "./entities/game-theme.entity"; @@ -29,6 +29,7 @@ export type TAllowedResource = keyof typeof resourceToEntityMap; @Injectable() export class GameRepositoryService { private readonly logger = new Logger(GameRepositoryService.name); + private readonly maximumAllowedRelationsQuery = 2; /** * @param dataSource @@ -40,10 +41,29 @@ export class GameRepositoryService { private readonly gameRepository: Repository, ) {} + private validateMaximumRelations( + relations: FindOptionsRelations | undefined, + ) { + if (!relations) return; + const totalQueriedEntries = Object.entries(relations).filter( + ([key, value]) => { + // E.g.: genres: true + return key != undefined && value; + }, + ).length; + if (totalQueriedEntries > this.maximumAllowedRelationsQuery) { + throw new HttpException( + "For performance reasons, queries with more than 2 relations are not allowed.", + HttpStatus.BAD_REQUEST, + ); + } + } + async findOneById( id: number, dto?: GameRepositoryFindOneDto, ): Promise { + this.validateMaximumRelations(dto?.relations); const game = await this.gameRepository.findOne({ where: { id, @@ -64,6 +84,8 @@ export class GameRepositoryService { ) { throw new HttpException("Invalid query.", HttpStatus.BAD_REQUEST); } + this.validateMaximumRelations(dto?.relations); + const games = await this.gameRepository.find({ where: { id: In(dto?.gameIds), diff --git a/src/notifications/notifications.controller.ts b/src/notifications/notifications.controller.ts index b93155a..44f6ec4 100644 --- a/src/notifications/notifications.controller.ts +++ b/src/notifications/notifications.controller.ts @@ -24,7 +24,7 @@ import { fromPromise } from "rxjs/internal/observable/innerFrom"; import { PaginationInterceptor } from "../interceptor/pagination.interceptor"; import { PaginatedNotificationAggregationDto } from "./dto/paginated-notification-aggregation.dto"; -const NOTIFICATIONS_CHECK_INTERVAL = 5000; +const NOTIFICATIONS_CHECK_INTERVAL = 20000; @Controller("notifications") @ApiTags("notifications") From 4d7d23ef2e20f99b7b71975f2b9dbcc6ac5363a6 Mon Sep 17 00:00:00 2001 From: Lamarcke Date: Thu, 7 Mar 2024 01:29:35 -0300 Subject: [PATCH 2/2] - Increased maximum allowed relations for game-repository.service.ts --- src/game/game-repository/game-repository.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/game/game-repository/game-repository.service.ts b/src/game/game-repository/game-repository.service.ts index 972b5cd..52a0917 100644 --- a/src/game/game-repository/game-repository.service.ts +++ b/src/game/game-repository/game-repository.service.ts @@ -29,7 +29,7 @@ export type TAllowedResource = keyof typeof resourceToEntityMap; @Injectable() export class GameRepositoryService { private readonly logger = new Logger(GameRepositoryService.name); - private readonly maximumAllowedRelationsQuery = 2; + private readonly maximumAllowedRelationsQuery = 3; /** * @param dataSource @@ -53,7 +53,7 @@ export class GameRepositoryService { ).length; if (totalQueriedEntries > this.maximumAllowedRelationsQuery) { throw new HttpException( - "For performance reasons, queries with more than 2 relations are not allowed.", + `For performance reasons, queries with more than ${this.maximumAllowedRelationsQuery} relations are not allowed.`, HttpStatus.BAD_REQUEST, ); }