Skip to content

Commit

Permalink
feat(utils/validateResultSet): add warning for oversized result sets
Browse files Browse the repository at this point in the history
This behaviour is generally undesirable and may lead to
excessive memory use or crashes.
  • Loading branch information
thelindat committed Mar 19, 2024
1 parent 7b488f8 commit 691ee50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/database/rawExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/database/rawQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/validateResultSet.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}

0 comments on commit 691ee50

Please sign in to comment.