Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

feat: allow dangerous html flag (CT-000) #88

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Changes from 1 commit
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
Next Next commit
feat: allow dangerous html flag (CT-000)
DecathectZero committed Nov 25, 2023
commit 5c978450177cc3a1d86dd35c1ca4f2025966b6d5
6 changes: 3 additions & 3 deletions packages/react-chat/package.json
Original file line number Diff line number Diff line change
@@ -21,9 +21,9 @@
"dayjs": "^1.11.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-markdown": "^8.0.7",
"rehype-raw": "^6.1.1",
"remark-gfm": "^3.0.1",
"react-markdown": "9.0.0",
"rehype-raw": "7.0.0",
"remark-gfm": "4.0.0",
"remeda": "^1.0.1",
"slate": "^0.94.1",
"ts-pattern": "^4.0.5",
2 changes: 2 additions & 0 deletions packages/react-chat/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ export interface RuntimeOptions<Verify extends AuthVerify | PublicVerify = AuthV
userID?: string;
versionID?: string | undefined;
launch?: LaunchOptions;

allowDangerousHTML?: boolean;
}

export enum SessionStatus {
8 changes: 7 additions & 1 deletion packages/react-chat/src/components/Text/Default.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { serializeToMarkdown } from '@voiceflow/slate-serializer/markdown';
import React from 'react';
import rehypeRaw from 'rehype-raw';

import Message from '@/components/Message';
import type { TextMessageProps } from '@/components/SystemResponse/types';
import { RuntimeStateAPIContext } from '@/contexts';

import Markdown from './Markdown';

@@ -14,9 +16,13 @@ export interface DefaultTextProps {
}

const DefaultText: React.FC<DefaultTextProps> = ({ text }) => {
const api = React.useContext(RuntimeStateAPIContext);

return (
<Message from="system">
<Markdown>{typeof text === 'string' ? text : serializeToMarkdown(text)}</Markdown>
<Markdown rehypePlugins={api?.config?.allowDangerousHTML ? [rehypeRaw] : []}>
{typeof text === 'string' ? text : serializeToMarkdown(text)}
</Markdown>
</Message>
);
};
11 changes: 7 additions & 4 deletions packages/react-chat/src/components/Text/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ReactMarkdown from 'react-markdown';
import rehypeRaw from 'rehype-raw';
import remarkGfm from 'remark-gfm';

import { styled } from '@/styles';
@@ -35,14 +34,18 @@ const MarkdownText = styled(ReactMarkdown, {
});

MarkdownText.defaultProps = {
rehypePlugins: [rehypeRaw],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rehypeRaw is the dangerous one - this is now gone by default.

remarkPlugins: [remarkGfm],
linkTarget: '_blank',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no longer supported:
remarkjs/react-markdown#761

components: {
a: ({ node, href, children, ...props }) => (
<a href={href} target="_blank" rel="noopener noreferrer" {...props}>
{children}
</a>
),
},
remarkRehypeOptions: {
handlers: {
break: () => [{ type: 'text', value: '\n' }],
},
allowDangerousHtml: false,
},
};

5 changes: 4 additions & 1 deletion packages/react-chat/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ const tryDecodeURIComponent = (str: string) => {

export const sanitizeConfig = (config: unknown): Partial<ChatConfig> & Pick<ChatConfig, 'verify' | 'url'> => {
const ref = isObject(config) ? config : {};
const { url, user, userID, versionID, verify, assistant, launch } = ref;
const { url, user, userID, versionID, verify, assistant, launch, allowDangerousHTML } = ref;

if (!validateVerify(verify)) {
throw new Error('no projectID on load');
@@ -38,5 +38,8 @@ export const sanitizeConfig = (config: unknown): Partial<ChatConfig> & Pick<Chat
}),
...(isObject(assistant) && ({ assistant } as Partial<Pick<ChatConfig, 'assistant'>>)),
...(isObject(launch) && ({ launch } as Pick<ChatConfig, 'launch'>)),

// default to true during migration period, turn off later
...(typeof allowDangerousHTML === 'boolean' ? { allowDangerousHTML } : { allowDangerousHTML: true }),
};
};
Loading