codeant-ai-for-open-source[bot] commented on code in PR #37186: URL: https://github.com/apache/superset/pull/37186#discussion_r2710816414
########## superset/mcp_service/utils/sanitization.py: ########## @@ -0,0 +1,267 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Centralized sanitization utilities for MCP service user inputs. + +This module uses the nh3 library (Rust-based HTML sanitizer) to strip malicious +HTML tags and protocols from user inputs. nh3 is faster and safer than manual +regex-based sanitization. + +Key features: +- Strips all HTML tags using nh3.clean() with no allowed tags +- Blocks dangerous URL schemes (javascript:, vbscript:, data:) +- Preserves safe text content (e.g., '&' stays as '&', not '&') +- Additional SQL injection protection for database-facing inputs +""" + +import html +import re + +import nh3 + + +def _strip_html_tags(value: str) -> str: + """ + Strip all HTML tags from the input using nh3, then unescape HTML entities. + + Uses nh3's Rust-based sanitizer with no allowed tags, which is + faster and safer than manual regex-based tag stripping. + + Since nh3.clean() produces HTML-safe output (escaping & to &, etc.), + we use html.unescape() afterward to restore the original characters. + This ensures text like "A & B" stays as "A & B", not "A & B". + + Args: + value: The input string that may contain HTML + + Returns: + String with all HTML tags removed and entities unescaped + """ + # nh3.clean with tags=set() strips ALL HTML tags + # url_schemes=set() blocks all URL schemes in any remaining attributes + cleaned = nh3.clean(value, tags=set(), url_schemes=set()) + + # Unescape HTML entities so & doesn't become & + return html.unescape(cleaned) Review Comment: **Suggestion:** Security bug: calling html.unescape() on nh3's cleaned output can reintroduce HTML tags and scripts that the sanitizer removed/escaped (e.g., "<script>...</script>" -> "<script>...<\/script>"), effectively undoing sanitization and creating an XSS vector; return the sanitizer output directly instead of unescaping it. [security] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ MCP chart creation XSS risk in titles - ❌ Stored chart metadata may contain scripts - ⚠️ Superset UI rendering could execute injected scripts ``` </details> ```suggestion # Return the sanitizer output directly to avoid reintroducing escaped tags/entities. return cleaned ``` <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Call sanitize_user_input() in superset/mcp_service/utils/sanitization.py (function at ~lines 135-193). The function calls _strip_html_tags(value) at line 181: `value = _strip_html_tags(value)`. 2. Execution enters _strip_html_tags() at superset/mcp_service/utils/sanitization.py:38-60. At line 57 it runs `cleaned = nh3.clean(value, tags=set(), url_schemes=set())`. 3. nh3.clean() will escape unsafe characters into entities (e.g. "<script>" -> "<script>"). The current code then calls html.unescape() at line 60 which converts "<script>...</script>" back to "<script>...</script>" and returns it to sanitize_user_input(). 4. If a downstream caller stores this returned value (for example MCP chart title via schemas validators like UpdateChartRequest.sanitize_chart_name in superset/mcp_service/schemas.py) it can reach the Superset UI via JSON APIs and be rendered by React, reintroducing an executable <script> tag (XSS). Observe returned string contains raw "<script>" when inspecting the value returned from sanitize_user_input() at superset/mcp_service/utils/sanitization.py:181 after calling _strip_html_tags(). ``` </details> <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/mcp_service/utils/sanitization.py **Line:** 58:60 **Comment:** *Security: Security bug: calling html.unescape() on nh3's cleaned output can reintroduce HTML tags and scripts that the sanitizer removed/escaped (e.g., "<script>...</script>" -> "<script>...<\/script>"), effectively undoing sanitization and creating an XSS vector; return the sanitizer output directly instead of unescaping it. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. ``` </details> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
