From 691ee508d174acb3452ddd7b4e3e4dc7fad0927e Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:07:04 +1100 Subject: [PATCH] feat(utils/validateResultSet): add warning for oversized result sets This behaviour is generally undesirable and may lead to excessive memory use or crashes. --- src/database/rawExecute.ts | 3 +++ src/database/rawQuery.ts | 3 +++ src/utils/validateResultSet.ts | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/utils/validateResultSet.ts diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index 6e22733..5ae92af 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -5,6 +5,7 @@ import { executeType, parseExecute } from '../utils/parseExecute'; import { getPoolConnection } from './connection'; import { setCallback } from '../utils/setCallback'; import { performance } from 'perf_hooks'; +import validateResultSet from 'utils/validateResultSet'; export const rawExecute = async ( invokingResource: string, @@ -60,6 +61,8 @@ export const rawExecute = async ( } else if (startTime) { logQuery(invokingResource, query, performance.now() - startTime, values); } + + validateResultSet(invokingResource, query, result); } if (!cb) return response.length === 1 ? response[0] : response; diff --git a/src/database/rawQuery.ts b/src/database/rawQuery.ts index 6571b16..0f257e3 100644 --- a/src/database/rawQuery.ts +++ b/src/database/rawQuery.ts @@ -7,6 +7,7 @@ import type { QueryType } from '../types'; import { getPoolConnection } from './connection'; import { RowDataPacket } from 'mysql2'; import { performance } from 'perf_hooks'; +import validateResultSet from 'utils/validateResultSet'; export const rawQuery = async ( type: QueryType, @@ -43,6 +44,8 @@ export const rawQuery = async ( logQuery(invokingResource, query, performance.now() - startTime, parameters); } + validateResultSet(invokingResource, query, result); + if (!cb) return parseResponse(type, result); try { diff --git a/src/utils/validateResultSet.ts b/src/utils/validateResultSet.ts new file mode 100644 index 0000000..07f0246 --- /dev/null +++ b/src/utils/validateResultSet.ts @@ -0,0 +1,22 @@ +import { OkPacket, ProcedureCallPacket, ResultSetHeader, RowDataPacket } from 'mysql2/promise'; + +const oversizedResultSet = GetConvarInt('mysql_resultset_warning', 1000); + +export default function ( + invokingResource: string, + query: string, + rows: + | OkPacket + | ResultSetHeader + | ResultSetHeader[] + | RowDataPacket[] + | RowDataPacket[][] + | OkPacket[] + | ProcedureCallPacket +) { + const length = Array.isArray(rows) ? rows.length : 0; + + if (length < oversizedResultSet) return; + + console.warn(`${invokingResource} executed a query with an oversized result set (${length} results)!\n${query}`); +}