Skip to content

Commit fac5489

Browse files
authored
Merge pull request #955 from rocket-admin/backend_masterpassword_error_filering
feat: add HTML escaping for primary key values
2 parents 263cd78 + 7d1c111 commit fac5489

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

backend/src/entities/table-actions/table-actions-module/table-action-activation.service.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ export class TableActionActivationService {
319319
return primaryKeyValuesArray;
320320
}
321321

322+
private escapePrimaryKeyValuesArray(array: Array<Record<string, unknown>>): Array<Record<string, unknown>> {
323+
return array.map((record) => {
324+
const escapedRecord: Record<string, unknown> = {};
325+
for (const key in record) {
326+
if (record.hasOwnProperty(key)) {
327+
const escapedKey = escapeHtml(key);
328+
// eslint-disable-next-line security/detect-object-injection
329+
const value = record[key];
330+
const escapedValue = typeof value === 'string' ? escapeHtml(value) : value;
331+
// eslint-disable-next-line security/detect-object-injection
332+
escapedRecord[escapedKey] = escapedValue;
333+
}
334+
}
335+
return escapedRecord;
336+
});
337+
}
338+
322339
private generateMessageContent(
323340
userInfo: UserInfoMessageData,
324341
triggerOperation: TableActionEventEnum,
@@ -334,8 +351,9 @@ export class TableActionActivationService {
334351
: triggerOperation === TableActionEventEnum.DELETE_ROW
335352
? 'deleted a row'
336353
: 'performed an action';
354+
primaryKeyValuesArray = this.escapePrimaryKeyValuesArray(primaryKeyValuesArray);
337355
const textContent = `${userName ? escapeHtml(userName) : 'User'} (email: ${email}, user id: ${userId}) has ${action} in the table "${escapeHtml(tableName)}".`;
338-
const testContentWithPrimaryKeys = `${textContent} Primary Keys: ${escapeHtml(JSON.stringify(primaryKeyValuesArray))}`;
356+
const testContentWithPrimaryKeys = `${textContent} Primary Keys: ${JSON.stringify(primaryKeyValuesArray)}`;
339357
const htmlContent = `<!doctype html>
340358
<html>
341359
<head>
@@ -437,7 +455,7 @@ table[class=body] .article {
437455
<tr>
438456
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top">
439457
<p style="font-size: 18px; font-weight: normal; margin: 0; margin-bottom: 15px;">${textContent}</p>
440-
<p style="font-size: 18px; font-weight: normal; margin: 0; margin-bottom: 15px;">Primary Keys: ${escapeHtml(JSON.stringify(primaryKeyValuesArray))}</p>
458+
<p style="font-size: 18px; font-weight: normal; margin: 0; margin-bottom: 15px;">Primary Keys: ${JSON.stringify(primaryKeyValuesArray)}</p>
441459
</td>
442460
</tr>
443461
<tr>

backend/src/exceptions/all-exceptions.filter.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from
22
import { Logger } from '../helpers/logging/Logger.js';
33
import { processExceptionMessage } from './utils/process-exception-message.js';
44
import Sentry from '@sentry/minimal';
5+
import { Messages } from './text/messages.js';
56

67
@Catch()
78
export class AllExceptionsFilter implements ExceptionFilter {
@@ -29,9 +30,17 @@ export class AllExceptionsFilter implements ExceptionFilter {
2930
Logger.logError(exception);
3031
}
3132

33+
const ifErrorMasterPwdMissing = text === Messages.MASTER_PASSWORD_MISSING;
34+
const ifErrorMasterPwdIncorrect = text === Messages.MASTER_PASSWORD_INCORRECT;
35+
const masterPwdErrorType = ifErrorMasterPwdMissing
36+
? 'no_master_key'
37+
: ifErrorMasterPwdIncorrect
38+
? 'invalid_master_key'
39+
: undefined;
40+
3241
response.status(status).json({
3342
message: text ? text : 'Something went wrong',
34-
type: type ? type : undefined,
43+
type: type ? type : masterPwdErrorType,
3544
statusCode: status,
3645
timestamp: new Date().toISOString(),
3746
path: request.url,

0 commit comments

Comments
 (0)