This is an automated email from the ASF dual-hosted git repository.

shahar1 pushed a commit to branch airflow-ctl/v0-1-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/airflow-ctl/v0-1-test by this 
push:
     new c66d45a5a04 [airflow-ctl/v0-1-test] Escape error text in airflowctl 
safe_call_command messages (#76264) (#76385)
c66d45a5a04 is described below

commit c66d45a5a04ba21e3cfb57ca0db8ba0b5823873c
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Nov 3 17:34:19 2026 +0000

    [airflow-ctl/v0-1-test] Escape error text in airflowctl safe_call_command 
messages (#76264) (#76385)
    
    safe_call_command interpolated exception and server error text straight
    into Rich markup. A message containing a tag-like fragment such as
    "[/x]" made rich.print raise MarkupError, so the user saw a traceback
    instead of the real failure and the exit code was no longer 1.
    
    Escape the text in the AirflowCtl, protocol, timeout, server response
    and HTTP status handlers.
    (cherry picked from commit ab39e9569698a9eaf9e21ed31c86ddf1c1a98f93)
    
    Co-authored-by: Rishabh <[email protected]>
---
 airflow-ctl/src/airflowctl/ctl/cli_config.py       | 12 ++++-----
 .../tests/airflow_ctl/ctl/test_cli_config.py       | 31 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/airflow-ctl/src/airflowctl/ctl/cli_config.py 
b/airflow-ctl/src/airflowctl/ctl/cli_config.py
index bb2a7c4f809..4351c332bbf 100755
--- a/airflow-ctl/src/airflowctl/ctl/cli_config.py
+++ b/airflow-ctl/src/airflowctl/ctl/cli_config.py
@@ -95,20 +95,20 @@ def safe_call_command(function: Callable, args: 
Iterable[Arg]) -> None:
         function(args)
     except AirflowCtlException as e:
         # The base class, so a new AirflowCtl error never regresses to a raw 
traceback.
-        rich.print(f"command failed due to {e}", file=sys.stderr)
+        rich.print(f"command failed due to {escape(str(e))}", file=sys.stderr)
         sys.exit(1)
     except (httpx.RemoteProtocolError, httpx.ReadError, httpx.WriteError) as e:
-        rich.print(f"[red]Remote protocol error: {e}[/red]", file=sys.stderr)
+        rich.print(f"[red]Remote protocol error: {escape(str(e))}[/red]", 
file=sys.stderr)
         if "Server disconnected without sending a response." in str(e):
             rich.print(
-                f"[red]Server response error: {e}. "
+                f"[red]Server response error: {escape(str(e))}. "
                 "Please check if the server is running and the API URL is 
correct.[/red]",
                 file=sys.stderr,
             )
         _warn_if_write_may_have_applied(e)
         sys.exit(1)
     except httpx.TimeoutException as e:
-        rich.print(f"[red]Request timeout error: {e}[/red]", file=sys.stderr)
+        rich.print(f"[red]Request timeout error: {escape(str(e))}[/red]", 
file=sys.stderr)
         if "timed out" in str(e):
             rich.print(
                 "[red]Please check if the server is running and the API ready 
to accept calls.[/red]",
@@ -117,7 +117,7 @@ def safe_call_command(function: Callable, args: 
Iterable[Arg]) -> None:
         _warn_if_write_may_have_applied(e)
         sys.exit(1)
     except ServerResponseError as e:
-        rich.print(f"Server response error: {e}", file=sys.stderr)
+        rich.print(f"Server response error: {escape(str(e))}", file=sys.stderr)
         if "Client error message:" in str(e):
             rich.print(
                 "[red]Client error, [/red] "
@@ -131,7 +131,7 @@ def safe_call_command(function: Callable, args: 
Iterable[Arg]) -> None:
     # turn into a ``ServerResponseError`` -- a 3xx, or a 4xx/5xx whose body is 
not JSON -- reach us
     # as the bare httpx error.
     except httpx.HTTPStatusError as e:
-        rich.print(f"[red]Server response error: {e}[/red]", file=sys.stderr)
+        rich.print(f"[red]Server response error: {escape(str(e))}[/red]", 
file=sys.stderr)
         if e.response.is_redirect:
             rich.print(
                 "[red]The server answered with a redirect, which airflowctl 
does not follow. "
diff --git a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py 
b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
index 42534ede70e..3b27bffb289 100644
--- a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
+++ b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
@@ -777,6 +777,37 @@ class TestCliConfigMethods:
         for text in expected:
             assert text in err
 
+    @pytest.mark.parametrize(
+        "raised_exception",
+        [
+            AirflowCtlNotFoundException("not found [/bogus]"),
+            httpx.RemoteProtocolError("protocol error [/bogus]"),
+            httpx.ReadTimeout("timed out [/bogus]"),
+            httpx.HTTPStatusError(
+                "status error [/bogus]",
+                request=httpx.Request("GET", 
"http://localhost:8080/api/v2/dags";),
+                response=httpx.Response(302),
+            ),
+            ServerResponseError(
+                "server error [/bogus]",
+                request=httpx.Request("GET", 
"http://localhost:8080/api/v2/dags";),
+                response=httpx.Response(500, json={"detail": "boom"}),
+            ),
+        ],
+        ids=["airflowctl", "protocol", "timeout", "http-status", 
"server-response"],
+    )
+    def test_safe_call_command_prints_error_text_containing_markup(self, 
raised_exception, capsys):
+        def raise_error(_args):
+            raise raised_exception
+
+        with pytest.raises(SystemExit) as ctx:
+            safe_call_command(raise_error, args=argparse.Namespace())
+
+        assert ctx.value.code == 1
+        captured = capsys.readouterr()
+        assert captured.out == ""
+        assert "[/bogus]" in captured.err
+
     @pytest.mark.parametrize(
         ("response", "hint_expected"),
         [

Reply via email to