Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use json string escape method to build up webhook payload body #356

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/api/src/tasks/__tests__/checkAlerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
buildAlertMessageTemplateTitle,
buildLogSearchLink,
doesExceedThreshold,
escapeJsonString,
expandToNestedObject,
getDefaultExternalAction,
processAlert,
Expand Down Expand Up @@ -129,6 +130,18 @@ describe('checkAlerts', () => {
});
});

it('escapeJsonString', () => {
expect(escapeJsonString('foo')).toBe('foo');
expect(escapeJsonString("foo'")).toBe("foo'");
expect(escapeJsonString('foo"')).toBe('foo\\"');
expect(escapeJsonString('foo\\')).toBe('foo\\\\');
expect(escapeJsonString('foo\n')).toBe('foo\\n');
expect(escapeJsonString('foo\r')).toBe('foo\\r');
expect(escapeJsonString('foo\t')).toBe('foo\\t');
expect(escapeJsonString('foo\b')).toBe('foo\\b');
expect(escapeJsonString('foo\f')).toBe('foo\\f');
});

describe('Alert Templates', () => {
const defaultSearchView: any = {
alert: {
Expand Down
41 changes: 11 additions & 30 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ const handleSendSlackWebhook = async (
});
};

export const escapeJsonString = (str: string) => {
return JSON.stringify(str).slice(1, -1);
};

export const handleSendGenericWebhook = async (
webhook: IWebhook,
message: {
Expand Down Expand Up @@ -273,36 +277,13 @@ export const handleSendGenericWebhook = async (
let body = '';
if (webhook.body) {
const handlebars = Handlebars.create();
let isJsonBody = false;
try {
const jsonBody = JSON.parse(webhook.body);
isJsonBody = true;
for (const [_key, _val] of Object.entries(jsonBody)) {
jsonBody[_key] = handlebars.compile(_val, {
noEscape: true,
})({
body: message.body,
link: message.hdxLink,
title: message.title,
});
}
body = JSON.stringify(jsonBody);
} catch (e) {
logger.error({
message: 'Webhook body is not a valid JSON',
error: serializeError(e),
});
}

if (!isJsonBody) {
body = handlebars.compile(webhook.body, {
noEscape: true,
})({
body: message.body,
link: message.hdxLink,
title: message.title,
});
}
body = handlebars.compile(webhook.body, {
noEscape: true,
})({
body: escapeJsonString(message.body),
link: escapeJsonString(message.hdxLink),
title: escapeJsonString(message.title),
});
}

try {
Expand Down