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

AI message: do not render html when it's in a code block #2205

Merged
merged 3 commits into from
Feb 25, 2025
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
35 changes: 31 additions & 4 deletions packages/host/tests/integration/components/room-message-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ module('Integration | Component | RoomMessage', function (hooks) {
isStreaming: boolean,
timeAgoForCreated: number,
timeAgoForUpdated: number,
messageContent: string,
) {
let message = {
author: { userId: '@aibot:localhost' },
message: 'Hello,',
formattedMessage: 'Hello, ',
message: messageContent,
formattedMessage: messageContent,
created: new Date(new Date().getTime() - timeAgoForCreated * 60 * 1000),
updated: new Date(new Date().getTime() - timeAgoForUpdated * 60 * 1000),
};
Expand Down Expand Up @@ -67,7 +68,7 @@ module('Integration | Component | RoomMessage', function (hooks) {
}

test('it shows an error when AI bot message streaming timeouts', async function (assert) {
let testScenario = await setupTestScenario(true, 2, 1); // Streaming, created 2 mins ago, updated 1 min ago
let testScenario = await setupTestScenario(true, 2, 1, 'Hello,'); // Streaming, created 2 mins ago, updated 1 min ago
await renderRoomMessageComponent(testScenario);

await waitUntil(
Expand All @@ -85,7 +86,7 @@ module('Integration | Component | RoomMessage', function (hooks) {
});

test('it does not show an error when last streaming chunk is still within reasonable time limit', async function (assert) {
let testScenario = await setupTestScenario(true, 2, 0.5); // Streaming, created 2 mins ago, updated 30 seconds ago
let testScenario = await setupTestScenario(true, 2, 0.5, 'Hello,'); // Streaming, created 2 mins ago, updated 30 seconds ago
await renderRoomMessageComponent(testScenario);

assert
Expand All @@ -96,4 +97,30 @@ module('Integration | Component | RoomMessage', function (hooks) {
.dom('[data-test-ai-message-content] span.streaming-text')
.includesText('Hello,');
});

test('it escapes html code that is in code tags', async function (assert) {
let testScenario = await setupTestScenario(
true,
0,
0,
`
\`\`\`typescript
<template>
<h1>Hello, world!</h1>
</template>
\`\`\`
`,
);
await renderRoomMessageComponent(testScenario);

let content = document.querySelector(
'[data-test-ai-message-content]',
)?.innerHTML;
assert.ok(
content?.includes(`&lt;template&gt;
&lt;h1&gt;Hello, world!&lt;/h1&gt;
&lt;/template&gt;`),
'rendered code snippet in a streaming message should contain escaped HTML template so that we see code, not rendered html',
);
});
});
10 changes: 9 additions & 1 deletion packages/runtime-common/marked-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function markedSync(markdown: string) {
// also note that since we are in common, we don't have ember-window-mock
// available to us.
globalThis.localStorage?.setItem(id, code);
return `<pre id="${id}" class="language-${language}" data-codeblock="${language}">${code}</pre></div>`;
return `<pre id="${id}" class="language-${language}" data-codeblock="${language}">${escapeHtmlInPreTags(code)}</pre></div>`;
},
},
})
Expand All @@ -32,3 +32,11 @@ export function markedSync(markdown: string) {
export function markdownToHtml(markdown: string | null | undefined): string {
return markdown ? sanitizeHtml(markedSync(markdown)) : '';
}

function escapeHtmlInPreTags(html: string) {
// For example, html can be <pre><code><h1>Hello</h1></code></pre>
// We want to escape the <h1>Hello</h1> so that it is rendered as
// <pre><code>&lt;h1&gt;Hello&lt;/h1&gt;</code></pre>, otherwise the h1 will
// be rendered as a real header, not code (same applies for other html tags, such as <template>, <style>, ...)
return html.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}