Repository navigation
Conversation
📝 WalkthroughWalkthroughA shared ChangesMarkdown sanitization
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant ChatContent
participant preprocessLaTeX
participant sanitizeMarkdown
participant rehypeRaw
ChatContent->>preprocessLaTeX: process markdown content
preprocessLaTeX->>sanitizeMarkdown: return decoded markdown
sanitizeMarkdown->>rehypeRaw: provide sanitized markdown
rehypeRaw->>ChatContent: render parsed content
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/utils/tests/chat.test.ts`:
- Around line 49-55: Add assertions in the sanitizeMarkdown test for the custom
think and retrieving allow-list entries. Verify the sanitized output preserves
the relevant think and retrieving tags, rather than only checking the details
class and summary elements.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c5e66670-48e4-44d2-916f-a7ea0246d70b
📒 Files selected for processing (7)
web/src/components/floating-chat-widget-markdown.tsxweb/src/components/highlight-markdown/index.tsxweb/src/components/markdown-content/index.tsxweb/src/components/next-markdown-content/index.tsxweb/src/pages/next-search/markdown-content/index.tsxweb/src/utils/chat.tsweb/src/utils/tests/chat.test.ts
| test('sanitizeMarkdown keeps the tags the chat renderers rely on', () => { | ||
| const sanitized = sanitizeMarkdown( | ||
| '<details class="think"><summary>Thinking...</summary>why</details>', | ||
| ); | ||
| expect(sanitized).toContain('<details class="think">'); | ||
| expect(sanitized).toContain('<summary>'); | ||
| }); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf 'Files matching chat.test.ts:\n'
fd -a 'chat\.test\.ts$' . || true
printf '\nRelevant test file:\n'
if [ -f web/src/utils/tests/chat.test.ts ]; then
nl -ba web/src/utils/tests/chat.test.ts | sed -n '1,140p'
fi
printf '\nSearch for sanitizeMarkdown definitions/usages:\n'
rg -n "sanitizeMarkdown|<think|<retrieving|think>|retrieving>" web/src -S || trueRepository: infiniflow/ragflow
Length of output: 354
🏁 Script executed:
#!/bin/bash
set -euo pipefail
file='web/src/utils/tests/chat.test.ts'
if [ -f "$file" ]; then
echo "Relevant test file:"
awk '{printf "%6d\t%s\n", NR, $0}' "$file" | sed -n '1,160p'
fi
echo
echo "Search for sanitizeMarkdown definitions/usages:"
rg -n "sanitizeMarkdown|<think|<retrieving|think>|retrieving>" web/src -S || trueRepository: infiniflow/ragflow
Length of output: 5037
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Chat utility around sanitizeMarkdown:"
awk '{printf "%6d\t%s\n", NR, $0}' web/src/utils/chat.ts | sed -n '1,140p'
echo
echo "Behavioral probe from source definitions:"
python3 - <<'PY'
import re
from pathlib import Path
src = Path('web/src/utils/chat.ts').read_text()
m = re.search(r'export const sanitizeMarkdown = \(content: string\) =>\s*\{(?P<body>.*?)}(?:\n|$);', src, re.S)
if m:
print(m.group(0))
body = m.group('body')
print("\nContains think tag extraction:", 'extractthinkContent' in body)
print("Contains retrieving tag extraction:", '<retrieving>' in body)
else:
print("sanitizeMarkdown definition not found")
PYRepository: infiniflow/ragflow
Length of output: 5598
Exercise the custom allow-list entries.
sanitizeMarkdown also allows think and retrieving; add preservation assertions for those tags instead of relying only on <details class="think"> and <summary>.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/utils/tests/chat.test.ts` around lines 49 - 55, Add assertions in the
sanitizeMarkdown test for the custom think and retrieving allow-list entries.
Verify the sanitized output preserves the relevant think and retrieving tags,
rather than only checking the details class and summary elements.
Summary
preprocessLaTeX()inweb/src/utils/chat.tsdecodes<,>and&back into raw markup, and every chat/search renderer hands its result to<Markdown rehypePlugins={[rehypeRaw, ...]}>. Those renderers ranDOMPurify.sanitize()before that decode, so markup that arrives entity-encoded is inert text when the allow-list inspects it and live markup again by the timerehypeRawparses it — the sanitizer can be walked past.FloatingChatWidgetMarkdownpassed message content torehypeRawwith no sanitization at all.This is the issue reported privately as GHSA-jv5f-3mqh-cxwf (the advisory is not published yet).
This PR moves sanitization to the end of the render pipeline, so the allow-list applies to exactly the string that gets parsed.
What changed
web/src/utils/chat.ts— newsanitizeMarkdown()holding the allow-list in one place (think,section,details,summary,retrieving, plusclass; unchanged from the options that were inlined in the renderers).markdown-content,next-markdown-content,next-search/markdown-content— the leadingDOMPurify.sanitize()call becomes the final step of the existingpipe(...).floating-chat-widget-markdown— gains sanitization; it had none.highlight-markdown— restores the sanitize pass added in Harden closed-advisory fixes #16409. It was removed again in fix: user-setting modal fixes and DOMPurify cleanup #16449 as "ineffective after preprocessLaTeX", but the ordering is the other way round: afterpreprocessLaTeXis the only point where it is effective, which is what the explanatory comment still sitting in that file already describes.web/src/utils/tests/chat.test.ts— three tests pinning the ordering, the event-handler stripping, and the tags the chat renderers depend on.Validation
cd web && npx jest --runInBand src/utils/tests/chat.test.ts— the three new tests pass.Two pre-existing failures are untouched by this PR; both reproduce identically on
mainat 611e059:chat.test.ts—handles double-escaped inline LaTeXandhandles mixed double-escaped delimiters with HTML entitiesalready fail onmain.highlight-markdown/__tests__/index.test.tsx— both cases currently error withReferenceError: React is not definedunder theesbuild-jesttransform (classic JSX runtime, and the component has noReactimport), so the regression guard added in Harden closed-advisory fixes #16409 does not assert anything at the moment. I left that alone to keep this PR focused, happy to fix it here if you prefer.Rendering was checked in a browser against the real plugin stack (react-markdown 9, rehype-raw 7, rehype-katex 7, dompurify 3.3.2): GFM tables,
<think>/<retrieving>sections, inline and block KaTeX, and the\right]/\big)delimiter handling all render as before. One visible difference: a literal<inside an inline code span now renders escaped, because the sanitizer HTML-escapes text before the markdown parser sees it. If that trade-off is unwelcome, the alternative is sanitizing the tree rather than the string —rehype-sanitizeplaced afterrehypeRawwith a schema extended for the tags listed above — which costs a new dependency, so I went with the smaller change here.