bito-code-review[bot] commented on code in PR #40814:
URL: https://github.com/apache/superset/pull/40814#discussion_r3392358691
##########
superset/mcp_service/__main__.py:
##########
@@ -25,21 +25,37 @@
import logging
import os
import sys
-from typing import Any
+from typing import Any, Callable
# Must redirect click output BEFORE importing anything that uses it
import click
# Monkey-patch click to redirect output to stderr in stdio mode
if os.environ.get("FASTMCP_TRANSPORT", "stdio") == "stdio":
+ original_echo = click.echo
original_secho = click.secho
- def secho_to_stderr(*args: Any, **kwargs: Any) -> Any:
- kwargs["file"] = sys.stderr
- return original_secho(*args, **kwargs)
+ def redirect_to_stderr(
+ original_func: Callable[..., None],
+ *args: Any,
+ **kwargs: Any,
+ ) -> None:
+ if len(args) >= 2:
+ args = (args[0], sys.stderr, *args[2:])
+ kwargs.pop("file", None)
+ else:
+ kwargs["file"] = sys.stderr
+
+ original_func(*args, **kwargs)
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>CWE-682: Incorrect Calculation in Redirect</b></div>
<div id="fix">
The `redirect_to_stderr` function has a critical bug: it blindly treats any
second positional argument as `file` and replaces it with `sys.stderr`. If a
caller uses `click.echo('msg', nl=False)` (with `nl` as the second positional),
the code incorrectly interprets the boolean as a file object, redirects to
stderr, and silently loses the `nl=False` parameter. This changes output
destination and newline behavior. The original one-liner `click.echo = lambda
*args, **kwargs: click.echo(*args, file=sys.stderr, **kwargs)` was correct
because it preserves all positional args while forcing `file=sys.stderr` in
kwargs. (See also: [CWE-682](https://cwe.mitre.org/data/definitions/682.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/__main__.py
+++ b/superset/mcp_service/__main__.py
@@ -35,24 +35,15 @@ if os.environ.get("FASTMCP_TRANSPORT", "stdio") ==
"stdio"::
original_echo = click.echo
original_secho = click.secho
- def redirect_to_stderr(
- original_func: Callable[..., None],
- *args: Any,
- **kwargs: Any,
- ) -> None:
- if len(args) >= 2:
- args = (args[0], sys.stderr, *args[2:])
- kwargs.pop("file", None)
- else:
- kwargs["file"] = sys.stderr
-
- original_func(*args, **kwargs)
-
def echo_to_stderr(*args: Any, **kwargs: Any) -> None:
- redirect_to_stderr(original_echo, *args, **kwargs)
+ original_echo(*args, file=sys.stderr, **kwargs)
def secho_to_stderr(*args: Any, **kwargs: Any) -> None:
- redirect_to_stderr(original_secho, *args, **kwargs)
+ original_secho(*args, file=sys.stderr, **kwargs)
click.echo = echo_to_stderr
click.secho = secho_to_stderr
```
</div>
</details>
</div>
<small><i>Code Review Run #dd6149</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]