aminghadersohi commented on code in PR #37186:
URL: https://github.com/apache/superset/pull/37186#discussion_r2756346242


##########
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:
   Claude on behalf of Amin commenting:
   
   This was a valid catch. I confirmed the issue by testing: 
`nh3.clean('<script>', tags=set())` passes entities through unchanged, 
and `html.unescape()` on that output produces `<script>` - a real XSS vector.
   
   Fixed in d50d600946 by replacing `html.unescape(cleaned)` with 
`cleaned.replace("&amp;", "&")`. This only restores ampersands (so "A & B" 
displays correctly) without risking angle bracket reintroduction. Added 
defense-in-depth tests to verify.



-- 
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]

Reply via email to