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

feat: sanitize content of basic_output #24

Merged
merged 1 commit into from
Nov 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions server/parsers/basic_input_output_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ describe('BasicInputOutputParser', () => {
},
]);
});

it('sanitizes markdown outputs', async () => {
const outputs = await BasicInputOutputParser.parserProvider({
input: 'test question',
response:
'normal text<b onmouseover=alert("XSS testing!")></b> <img src="image.jpg" alt="image" width="500" height="600"> !!!!!!![](http://evil.com/) ![image](http://evil.com/) [good link](https://link)',
conversation_id: 'test-session',
interaction_id: 'interaction_id',
create_time: '',
});

expect(outputs).toEqual([
{
type: 'input',
contentType: 'text',
content: 'test question',
},
{
content:
'normal text<b></b> [](http://evil.com/) [image](http://evil.com/) [good link](https://link)',
contentType: 'markdown',
traceId: 'interaction_id',
type: 'output',
},
]);
});
});
10 changes: 9 additions & 1 deletion server/parsers/basic_input_output_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
import { IInput, IOutput, Interaction } from '../../common/types/chat_saved_object_attributes';

const sanitize = (content: string) => {
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify((window as unknown) as Window);
return DOMPurify.sanitize(content, { FORBID_TAGS: ['img'] }).replace(/!+\[/g, '[');
};

export const BasicInputOutputParser = {
order: 0,
id: 'output_message',
Expand All @@ -18,7 +26,7 @@ export const BasicInputOutputParser = {
{
type: 'output',
contentType: 'markdown',
content: interaction.response,
content: sanitize(interaction.response),
traceId: interaction.interaction_id,
},
];
Expand Down
Loading