bito-code-review[bot] commented on code in PR #39795: URL: https://github.com/apache/superset/pull/39795#discussion_r3307822879
########## superset/utils/rison_filters.py: ########## @@ -0,0 +1,274 @@ +# 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. +""" +Parser for Rison URL filters that converts simplified filter syntax +to Superset's adhoc_filters format. +""" + +from __future__ import annotations + +import logging +import re +from typing import Any, Optional, Union + +import prison +from flask import has_request_context, request + +logger = logging.getLogger(__name__) + +# SQL identifiers permitted in URL-supplied filter columns. Restricted to +# alphanumeric/underscore (optionally schema-qualified) so OR-path SQL +# generation never interpolates an attacker-controlled identifier verbatim. +_SAFE_IDENTIFIER_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?$") + + +def _safe_identifier(name: Any) -> Optional[str]: + if isinstance(name, str) and _SAFE_IDENTIFIER_RE.match(name): + return name + return None + + +def _quote_sql_literal(value: Any) -> Optional[str]: + """Quote a scalar value safely for SQL string interpolation. + + - Strings: single-quoted with `'` escaped as `''` (SQL standard). + - Numeric / bool: rendered bare. + - Anything else (None, dict, list, etc.): rejected. + """ + if isinstance(value, bool): + return "TRUE" if value else "FALSE" + if isinstance(value, (int, float)): + return str(value) + if isinstance(value, str): + return "'" + value.replace("'", "''") + "'" + return None Review Comment: <div> <div id="suggestion"> <div id="issue"><b>CWE-89: Missing security tests</b></div> <div id="fix"> The new `_safe_identifier()` and `_quote_sql_literal()` helpers lack unit test coverage. Per BITO.md rule 6262, tests should verify actual behavior logic directly — security hardening code is especially critical to test since bugs allow SQL injection. Add tests for rejection of malicious column names and values, and for correct SQL generation from valid inputs. (See also: [CWE-89](https://cwe.mitre.org/data/definitions/89.html)) </div> </div> <small><i>Code Review Run #90ab2f</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- 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]
