aminghadersohi commented on code in PR #37186: URL: https://github.com/apache/superset/pull/37186#discussion_r2757296653
########## superset/mcp_service/utils/sanitization.py: ########## @@ -0,0 +1,283 @@ +# 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. + + Decodes all layers of HTML entity encoding BEFORE passing to nh3, + so entity-encoded tags (e.g., ``<script>``) are decoded into + real tags that nh3 can detect and strip. After nh3 removes all tags, + we only restore ``&`` back to ``&`` (not a full html.unescape) + to preserve ampersands in display text without risking XSS from + re-introducing angle brackets or other HTML-significant characters. + + Args: + value: The input string that may contain HTML + + Returns: + String with all HTML tags removed and ampersands preserved + """ + # Decode all layers of HTML entity encoding to prevent bypass + # via entity-encoded tags (e.g., <script> or &lt;script&gt;) + # The loop terminates when unescape produces no change (idempotent on decoded text). + # Max iterations cap provides defense-in-depth against pathological inputs. + max_iterations = 100 + decoded = value + prev = None + iterations = 0 + while prev != decoded and iterations < max_iterations: + prev = decoded + decoded = html.unescape(decoded) + iterations += 1 + + # nh3.clean with tags=set() strips ALL HTML tags from the decoded input + # url_schemes=set() blocks all URL schemes in any remaining attributes + cleaned = nh3.clean(decoded, tags=set(), url_schemes=set()) + + # Only restore & → & to preserve ampersands in display text (e.g. "A & B"). + # Do NOT use html.unescape() here: nh3 may pass through HTML entities from + # the input (e.g. <script>), and a full unescape would re-introduce + # raw angle brackets, creating an XSS vector. + return cleaned.replace("&", "&") Review Comment: test -- 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]
