fix(web): preserve XML-like tags in user messages (#4133)
Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
3 files changed
+163
-3
No files matched your search
@@ -117,6 +117,8 @@ interface ChatMarkdownProps {
|
||||
className?: string;
|
||||
/** Treat single newlines as hard breaks — chat-style user input. */
|
||||
lineBreaks?: boolean;
|
||||
/** Parse sanitized raw HTML instead of displaying its source text. */
|
||||
parseRawHtml?: boolean;
|
||||
}
|
||||
|
||||
const EMPTY_MARKDOWN_SKILLS: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">> = [];
|
||||
@@ -1360,6 +1362,7 @@ function ChatMarkdown({
|
||||
skills = EMPTY_MARKDOWN_SKILLS,
|
||||
className,
|
||||
lineBreaks = false,
|
||||
parseRawHtml = true,
|
||||
}: ChatMarkdownProps) {
|
||||
const { resolvedTheme } = useTheme();
|
||||
const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
|
||||
@@ -1622,7 +1625,7 @@ function ChatMarkdown({
|
||||
/>
|
||||
);
|
||||
},
|
||||
a({ node, href, children, ...props }) {
|
||||
a({ node, href, children, title: _title, ...props }) {
|
||||
const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : "";
|
||||
const fileLinkMeta = normalizedHref ? markdownFileLinkMetaByHref.get(normalizedHref) : null;
|
||||
if (!fileLinkMeta) {
|
||||
@@ -1707,6 +1710,9 @@ function ChatMarkdown({
|
||||
props.className,
|
||||
);
|
||||
},
|
||||
img({ node: _node, title: _title, ...props }) {
|
||||
return <img {...props} />;
|
||||
},
|
||||
code({ node, children, className, ...props }) {
|
||||
if (node?.properties?.dataInlineCode != null) {
|
||||
const codeText = nodeToPlainText(children);
|
||||
@@ -1777,6 +1783,9 @@ function ChatMarkdown({
|
||||
]);
|
||||
/* eslint-enable react/no-unstable-nested-components */
|
||||
|
||||
// react-markdown converts unparsed HTML nodes to text when skipHtml is false.
|
||||
// Keep that behavior explicit because literal mode depends on escaping the
|
||||
// complete source token instead of dropping it from the rendered message.
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
@@ -1789,7 +1798,8 @@ function ChatMarkdown({
|
||||
remarkPlugins={
|
||||
lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS
|
||||
}
|
||||
rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
|
||||
rehypePlugins={parseRawHtml ? CHAT_MARKDOWN_REHYPE_PLUGINS : undefined}
|
||||
skipHtml={false}
|
||||
components={markdownComponents}
|
||||
urlTransform={markdownUrlTransform}
|
||||
>
|
||||
|
||||
@@ -226,6 +226,17 @@ function buildUserTimelineEntry(text: string) {
|
||||
};
|
||||
}
|
||||
|
||||
function buildAssistantTimelineEntry(text: string) {
|
||||
const entry = buildUserTimelineEntry(text);
|
||||
return {
|
||||
...entry,
|
||||
message: {
|
||||
...entry.message,
|
||||
role: "assistant" as const,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("MessagesTimeline", () => {
|
||||
it("sizes expanded tool details with the configured code font size", () => {
|
||||
expect(toolCallExpandedBodyClassName).toContain("var(--font-size-code");
|
||||
@@ -470,7 +481,142 @@ describe("MessagesTimeline", () => {
|
||||
expect(markup).toContain("rounded-2xl bg-message p-3");
|
||||
});
|
||||
|
||||
it("renders inline terminal labels with the composer chip UI", () => {
|
||||
it("preserves arbitrary XML-like tags and comparisons in rendered user messages", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildUserTimelineEntry(
|
||||
[
|
||||
'Without reading a file, do you have <global-agent-instructions scope="workspace">',
|
||||
'Before <nested data-value="a&b">inside</nested> after',
|
||||
"</global-agent-instructions> in your context?",
|
||||
"Comparison: 2 < 3 and 5 > 4.",
|
||||
].join("\n"),
|
||||
),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain("<global-agent-instructions scope="workspace">");
|
||||
expect(markup).toContain(
|
||||
"Before <nested data-value="a&b">inside</nested> after",
|
||||
);
|
||||
expect(markup).toContain("</global-agent-instructions> in your context?");
|
||||
expect(markup).toContain("Comparison: 2 < 3 and 5 > 4.");
|
||||
});
|
||||
|
||||
it("preserves XML-like source inside user code spans and fences", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildUserTimelineEntry(
|
||||
[
|
||||
'Inline `<tag attr="x">`',
|
||||
"",
|
||||
"```xml",
|
||||
'<root><child enabled="true" /></root>',
|
||||
"```",
|
||||
].join("\n"),
|
||||
),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('<code data-inline-code=""><tag attr="x"></code>');
|
||||
expect(markup).toContain("<root><child enabled="true" /></root>");
|
||||
});
|
||||
|
||||
it("does not render markdown title attributes in user messages", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildUserTimelineEntry(
|
||||
'[link](https://example.com "link tip") ',
|
||||
),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('href="https://example.com"');
|
||||
expect(markup).toContain('src="https://example.com/image.png"');
|
||||
expect(markup).not.toContain('title="link tip"');
|
||||
expect(markup).not.toContain('title="image tip"');
|
||||
});
|
||||
|
||||
it("renders unsafe user HTML as inert source text", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildUserTimelineEntry(
|
||||
'<script>globalThis.__t3Xss = 1</script><img src="x" onerror="globalThis.__t3Xss = 2">',
|
||||
),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain("<script>globalThis.__t3Xss = 1</script>");
|
||||
expect(markup).toContain(
|
||||
"<img src="x" onerror="globalThis.__t3Xss = 2">",
|
||||
);
|
||||
expect(markup).not.toMatch(/<script(?:\s|>)/i);
|
||||
expect(markup).not.toMatch(/<img(?:\s|>)/i);
|
||||
});
|
||||
|
||||
it("continues to render sanitized raw HTML in assistant messages", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildAssistantTimelineEntry("<details><summary>More</summary>Details</details>"),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('data-markdown-details=""');
|
||||
expect(markup).toContain("More");
|
||||
expect(markup).not.toContain("<details>");
|
||||
});
|
||||
|
||||
it("sanitizes executable HTML while preserving supported assistant markup", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
timelineEntries={[
|
||||
buildAssistantTimelineEntry(
|
||||
[
|
||||
'<details open onclick="globalThis.__t3Xss = 1">',
|
||||
"<summary>Safe details</summary>",
|
||||
"<script>globalThis.__t3Xss = 2</script>",
|
||||
'<img src="x" onerror="globalThis.__t3Xss = 3">',
|
||||
'<a href="javascript:globalThis.__t3Xss = 4">Unsafe link</a>',
|
||||
"</details>",
|
||||
].join(""),
|
||||
),
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(markup).toContain('data-markdown-details=""');
|
||||
expect(markup).toContain("Safe details");
|
||||
expect(markup).not.toMatch(/<script(?:\s|>)/i);
|
||||
expect(markup).not.toContain("onclick=");
|
||||
expect(markup).not.toContain("onerror=");
|
||||
expect(markup).not.toContain("javascript:");
|
||||
expect(markup).not.toContain("globalThis.__t3Xss");
|
||||
});
|
||||
|
||||
it("renders inline terminal labels with the composer chip UI", async () => {
|
||||
const { MessagesTimeline } = await import("./MessagesTimeline");
|
||||
const markup = renderToStaticMarkup(
|
||||
<MessagesTimeline
|
||||
{...buildProps()}
|
||||
|
||||
@@ -1692,6 +1692,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
|
||||
skills={props.skills}
|
||||
className="text-message-foreground"
|
||||
lineBreaks
|
||||
parseRawHtml={false}
|
||||
/>
|
||||
) : null}
|
||||
{trailingWhitespace ? <span aria-hidden="true">{trailingWhitespace}</span> : null}
|
||||
@@ -1714,6 +1715,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
|
||||
skills={props.skills}
|
||||
className="text-message-foreground"
|
||||
lineBreaks
|
||||
parseRawHtml={false}
|
||||
/>
|
||||
</div>
|
||||
) : null
|
||||
@@ -1802,6 +1804,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
|
||||
skills={props.skills}
|
||||
className="text-message-foreground"
|
||||
lineBreaks
|
||||
parseRawHtml={false}
|
||||
/>,
|
||||
);
|
||||
} else if (inlinePrefix.length === 0) {
|
||||
@@ -1827,6 +1830,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
|
||||
skills={props.skills}
|
||||
className="text-message-foreground"
|
||||
lineBreaks
|
||||
parseRawHtml={false}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user