fix(security): sanitize HTML in chat UI to prevent XSS injection#3820
Open
xr843 wants to merge 2 commits intolm-sys:mainfrom
Open
fix(security): sanitize HTML in chat UI to prevent XSS injection#3820xr843 wants to merge 2 commits intolm-sys:mainfrom
xr843 wants to merge 2 commits intolm-sys:mainfrom
Conversation
Escape HTML entities in user messages and model responses before rendering in Gradio chatbot to prevent HTML/XSS injection attacks. Changes: - Add html.escape() to all text messages in Conversation.to_gradio_chatbot() for both user input (even indices) and model output (odd indices) - Escape image URL and filetype attributes in constructed img tags - Add sanitize_html=True to all gr.Chatbot() instances as defense-in-depth The vulnerability allowed arbitrary HTML/JavaScript execution when: 1. A user typed HTML tags (e.g. <img onerror=alert(1) src=#>) as input 2. A model returned HTML in its response Fixes lm-sys#3154 Fixes lm-sys#88 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Conversation.to_gradio_chatbot()usinghtml.escape(), preventing raw HTML/JS from being rendered in the chat UI<img>tags for uploaded imagessanitize_html=Trueto all 6gr.Chatbot()instances across all Gradio UI modules as defense-in-depthVulnerability
Both user input and model output were passed unescaped to Gradio's Chatbot component. This allowed:
<img onerror=alert(1) src=#>and it would execute JavaScriptReproduction (from #88):
Approach
The fix applies output escaping (not input sanitization), which is the correct strategy because:
Intentionally constructed
<img>tags for user-uploaded images are preserved since they use trusted data (image objects from Gradio's upload handler), but their dynamic attributes are also escaped as a precaution.Files changed
fastchat/conversation.pyhtml.escape()to text messages into_gradio_chatbot()fastchat/serve/gradio_web_server.pysanitize_html=Truetogr.Chatbot()fastchat/serve/gradio_block_arena_anony.pysanitize_html=Truetogr.Chatbot()fastchat/serve/gradio_block_arena_named.pysanitize_html=Truetogr.Chatbot()fastchat/serve/gradio_block_arena_vision.pysanitize_html=Truetogr.Chatbot()fastchat/serve/gradio_block_arena_vision_anony.pysanitize_html=Truetogr.Chatbot()fastchat/serve/gradio_block_arena_vision_named.pysanitize_html=Truetogr.Chatbot()Test plan
<img onerror=alert(1) src=#>— should display as plain text, not execute<script>alert('xss')</script>— should display as plain text$E=mc^2$)Fixes #3154
Fixes #88
🤖 Generated with Claude Code