Skip to content

Commit

Permalink
Improve sanitization of model output
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin committed Jul 12, 2024
1 parent d67b280 commit 4dff5d7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@sveltejs/adapter-node": "^1.3.1",
"@sveltejs/kit": "^1.30.4",
"@tailwindcss/typography": "^0.5.9",
"@types/dompurify": "^3.0.5",
"@types/express": "^4.17.21",
"@types/js-yaml": "^4.0.9",
"@types/jsdom": "^21.1.1",
Expand All @@ -31,6 +32,7 @@
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^6.x",
"@typescript-eslint/parser": "^6.x",
"dompurify": "^3.1.6",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte": "^2.30.0",
Expand Down
6 changes: 4 additions & 2 deletions src/lib/components/CodeBlock.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { afterUpdate } from "svelte";
import CopyToClipBoardBtn from "./CopyToClipBoardBtn.svelte";
import DOMPurify from "dompurify";
export let code = "";
export let lang = "";
Expand All @@ -19,8 +20,9 @@
<!-- eslint-disable svelte/no-at-html-tags -->
<pre
class="scrollbar-custom overflow-auto px-5 scrollbar-thumb-gray-500 hover:scrollbar-thumb-gray-400 dark:scrollbar-thumb-white/10 dark:hover:scrollbar-thumb-white/20"><code
class="language-{lang}">{@html highlightedCode || code.replaceAll("<", "&lt;")}</code
></pre>
class="language-{lang}">
{@html DOMPurify.sanitize(highlightedCode || code)}
</code></pre>
<CopyToClipBoardBtn
classNames="absolute top-2 right-2 invisible opacity-0 group-hover:visible group-hover:opacity-100"
value={code}
Expand Down
13 changes: 6 additions & 7 deletions src/lib/components/chat/ChatMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import Modal from "../Modal.svelte";
import ToolUpdate from "./ToolUpdate.svelte";
import { useSettingsStore } from "$lib/stores/settings";
import DOMPurify from "dompurify";
function sanitizeMd(md: string) {
let ret = md
Expand All @@ -53,9 +54,6 @@
return ret;
}
function unsanitizeMd(md: string) {
return md.replaceAll("&lt;", "<");
}
export let model: Model;
export let id: Message["id"];
Expand Down Expand Up @@ -106,7 +104,6 @@
marked.use(
markedKatex({
throwOnError: false,
// output: "html",
})
);
Expand Down Expand Up @@ -301,10 +298,12 @@
{/if}
{#each tokens as token}
{#if token.type === "code"}
<CodeBlock lang={token.lang} code={unsanitizeMd(token.text)} />
<CodeBlock lang={token.lang} code={token.text} />
{:else}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html marked.parse(token.raw, options)}
{#await marked.parse(token.raw, options) then parsed}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html DOMPurify.sanitize(parsed)}
{/await}
{/if}
{/each}
</div>
Expand Down

0 comments on commit 4dff5d7

Please sign in to comment.