bito-code-review[bot] commented on code in PR #40350: URL: https://github.com/apache/superset/pull/40350#discussion_r3327353678
########## superset/mcp_service/css_template/schemas.py: ########## @@ -0,0 +1,108 @@ +# 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. + +"""Pydantic schemas for CSS template MCP tools.""" + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + template_name: str = Field( + ..., + min_length=1, + max_length=250, + description="Name for the CSS template.", + ) + css: str = Field( + ..., + description="CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str) -> str: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + + +class CreateCssTemplateResponse(BaseModel): + """Response schema for create_css_template.""" + + id: int | None = Field( + None, + description="ID of the created CSS template. None if creation failed.", + ) + template_name: str | None = Field( + None, + description="Name of the created CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the created template.", + ) + error: str | None = Field( + None, + description="Error message if creation failed, otherwise null.", + ) Review Comment: <div> <div id="suggestion"> <div id="issue"><b>CWE-79: Error Sanitization Missing</b></div> <div id="fix"> The `error` field in `CreateCssTemplateResponse` lacks LLM-context sanitization. Errors from failed template creation may echo user-supplied content (template_name) and could be exploited for prompt injection when the response is included in LLM context. Apply the same `sanitize_error_for_llm_context` validator pattern used in `dashboard/schemas.py`. ([CWE-79](https://cwe.mitre.org/data/definitions/79.html)) </div> <details> <summary> <b>Code suggestion</b> </summary> <blockquote>Check the AI-generated fix before applying</blockquote> <div id="code"> ``` --- a/superset/mcp_service/css_template/schemas.py +++ b/superset/mcp_service/css_template/schemas.py @@ -17,7 +17,11 @@ """Pydantic schemas for CSS template MCP tools.""" -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from superset.mcp_service.utils import ( + sanitize_for_llm_context, +) class CreateCssTemplateRequest(BaseModel): @@ -61,6 +65,19 @@ class CreateCssTemplateResponse(BaseModel): None, description="Error message if creation failed, otherwise null.", ) + + @field_validator("error") + @classmethod + def sanitize_error_for_llm_context(cls, value: str | None) -> str | None: + """Wrap error text before it is exposed to LLM context. + + The error may echo user-supplied template_name or css content. + """ + if value is None: + return value + return sanitize_for_llm_context(value, field_path=("error",)) ``` </div> </details> </div> <small><i>Code Review Run #d496f2</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 ########## superset/commands/css/update.py: ########## @@ -0,0 +1,53 @@ +# 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. + +import logging +from functools import partial +from typing import Any + +from superset.commands.base import BaseCommand +from superset.commands.css.exceptions import ( + CssTemplateInvalidError, + CssTemplateNotFoundError, + CssTemplateUpdateFailedError, +) +from superset.daos.css import CssTemplateDAO +from superset.models.core import CssTemplate +from superset.utils.decorators import on_error, transaction + +logger = logging.getLogger(__name__) + + +class UpdateCssTemplateCommand(BaseCommand): + def __init__(self, model_id: int, properties: dict[str, Any]): + self._model_id = model_id + self._properties = properties + self._model: CssTemplate | None = None + + @transaction(on_error=partial(on_error, reraise=CssTemplateUpdateFailedError)) + def run(self) -> CssTemplate: + self.validate() + assert self._model + return CssTemplateDAO.update(self._model, attributes=self._properties) + + def validate(self) -> None: + self._model = CssTemplateDAO.find_by_id(self._model_id) + if not self._model: + raise CssTemplateNotFoundError() + template_name = self._properties.get("template_name") + if template_name is not None and not template_name.strip(): + raise CssTemplateInvalidError() Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Inconsistent validation between create/update</b></div> <div id="fix"> The `validate()` method does not check for 'css' property presence, unlike `CreateCssTemplateCommand.validate()` which requires it. This allows updating a CSS template to have no CSS content, which is likely invalid. Add: `if "css" not in self._properties: raise CssTemplateInvalidError()` </div> <details> <summary> <b>Code suggestion</b> </summary> <blockquote>Check the AI-generated fix before applying</blockquote> <div id="code"> ``` --- a/superset/commands/css/update.py +++ b/superset/commands/css/update.py @@ -50,4 +50,6 @@ class UpdateCssTemplateCommand(BaseCommand): template_name = self._properties.get("template_name") if template_name is not None and not template_name.strip(): raise CssTemplateInvalidError() + if "css" not in self._properties: + raise CssTemplateInvalidError() ``` </div> </details> </div> <small><i>Code Review Run #d496f2</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 ########## superset/mcp_service/css_template/schemas.py: ########## @@ -0,0 +1,108 @@ +# 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. + +"""Pydantic schemas for CSS template MCP tools.""" + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + template_name: str = Field( + ..., + min_length=1, + max_length=250, + description="Name for the CSS template.", + ) + css: str = Field( + ..., + description="CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str) -> str: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + + +class CreateCssTemplateResponse(BaseModel): + """Response schema for create_css_template.""" + + id: int | None = Field( + None, + description="ID of the created CSS template. None if creation failed.", + ) + template_name: str | None = Field( + None, + description="Name of the created CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the created template.", + ) + error: str | None = Field( + None, + description="Error message if creation failed, otherwise null.", + ) + + +class UpdateCssTemplateRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + id: int = Field(..., description="ID of the CSS template to update.") + template_name: str | None = Field( + None, + max_length=250, + description="New name for the CSS template.", + ) + css: str | None = Field( + None, + description="New CSS content for the template.", + ) + + @field_validator("template_name") + @classmethod + def template_name_must_not_be_empty(cls, v: str | None) -> str | None: + if v is not None: + if not v.strip(): + raise ValueError("template_name must not be empty") + return v.strip() + return v + + +class UpdateCssTemplateResponse(BaseModel): + """Response schema for update_css_template.""" + + id: int | None = Field( + None, + description="ID of the updated CSS template. None if update failed.", + ) + template_name: str | None = Field( + None, + description="Name of the updated CSS template.", + ) + css: str | None = Field( + None, + description="CSS content of the updated template.", + ) + error: str | None = Field( + None, + description="Error message if update failed, otherwise null.", + ) Review Comment: <div> <div id="suggestion"> <div id="issue"><b>CWE-79: Error Sanitization Missing</b></div> <div id="fix"> The `error` field in `UpdateCssTemplateResponse` lacks LLM-context sanitization. Errors from failed template updates may echo user-supplied content and could be exploited for prompt injection when the response is included in LLM context. Apply the same `sanitize_error_for_llm_context` validator pattern used in `dashboard/schemas.py`. ([CWE-79](https://cwe.mitre.org/data/definitions/79.html)) </div> </div> <small><i>Code Review Run #d496f2</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]
