Skip to content

Commit

Permalink
fix: dompurify removes sandbox href
Browse files Browse the repository at this point in the history
  • Loading branch information
moeakwak committed Oct 17, 2023
1 parent 3cf101e commit f5829d7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
7 changes: 6 additions & 1 deletion frontend/src/utils/chat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import DOMPurify from 'dompurify';

import { i18n } from '@/i18n';
import { allChatModelNames } from '@/types/json_schema';
import {
Expand All @@ -16,7 +18,6 @@ import {
OpenaiWebChatMessageTextContent,
OpenaiWebChatModels,
} from '@/types/schema';

const t = i18n.global.t as any;

export const chatModelColorMap: Record<string, string> = {
Expand Down Expand Up @@ -312,3 +313,7 @@ export function replaceMathDelimiters(input: string) {
// return output;
return output;
}

export function dompurifyRenderedHtml(html: string) {
return DOMPurify.sanitize(html, {ALLOW_UNKNOWN_PROTOCOLS: true});
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
</template>

<script setup lang="ts">
import DOMPurify from 'dompurify';
import { computed, onMounted, ref, watch } from 'vue';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useAppStore } from '@/store';
import { BaseChatMessage, OpenaiWebChatMessageMultimodalTextContentImagePart } from '@/types/schema';
import { getContentRawText, getMultimodalContentImageParts, getTextMessageContent } from '@/utils/chat';
import { dompurifyRenderedHtml, getContentRawText, getMultimodalContentImageParts } from '@/utils/chat';
import md from '@/utils/markdown';
import { bindOnclick, processPreTags } from '../utils/codeblock';
Expand All @@ -62,7 +61,7 @@ const renderedContent = computed(() => {
if (!props.renderMarkdown) {
return content.value;
}
const result = DOMPurify.sanitize(md.render(content.value || ''));
const result = dompurifyRenderedHtml(md.render(content.value || ''));
return processPreTags(result, appStore.preference.codeAutoWrap);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@
</template>

<script setup lang="ts">
import DOMPurify from 'dompurify';
import { computed, onMounted, ref, watch } from 'vue';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useAppStore } from '@/store';
import { BaseChatMessage, OpenaiWebChatMessageMultimodalTextContentImagePart } from '@/types/schema';
import { getContentRawText, getMultimodalContentImageParts, getTextMessageContent } from '@/utils/chat';
import { dompurifyRenderedHtml, getContentRawText, getMultimodalContentImageParts } from '@/utils/chat';
import md from '@/utils/markdown';
import { bindOnclick, processPreTags } from '../utils/codeblock';
Expand All @@ -88,7 +87,7 @@ const renderedContent = computed(() => {
if (!props.renderMarkdown) {
return content.value;
}
const result = DOMPurify.sanitize(md.render(content.value || ''));
const result = dompurifyRenderedHtml(md.render(content.value || ''));
return processPreTags(result, appStore.preference.codeAutoWrap);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
</template>

<script setup lang="ts">
import DOMPurify from 'dompurify';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useAppStore } from '@/store';
import { BaseChatMessage } from '@/types/schema';
import { getTextMessageContent } from '@/utils/chat';
import { dompurifyRenderedHtml, getTextMessageContent } from '@/utils/chat';
import md from '@/utils/markdown';
import { bindOnclick, processPreTags } from '../utils/codeblock';
Expand All @@ -37,7 +36,7 @@ const renderedContent = computed(() => {
return content.value;
}
console.log;
const result = DOMPurify.sanitize(md.render(content.value || ''));
const result = dompurifyRenderedHtml(md.render(content.value || ''));
return processPreTags(result, appStore.preference.codeAutoWrap);
});
Expand Down

0 comments on commit f5829d7

Please sign in to comment.