Skip to content

Commit

Permalink
feat: sanitize content of basic_output (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Dec 5, 2023
1 parent b82e17f commit 491a111
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit 491a111

Please sign in to comment.