Skip to content

Commit abc3a20

Browse files
Rishabh00bshahar1
andauthored
Escape server and user text in airflowctl command messages (#76265)
* Escape server and user text in airflowctl command messages Several airflowctl commands interpolated server error text, Dag run ids and logical dates into Rich markup unescaped. A value containing a tag-like fragment such as "[/x]" made rich.print raise MarkupError, so the user saw a traceback instead of the actual error. Escape the interpolated text in the dags, tasks, connections, auth and config commands and in the Dag run lookup helpers. * Escape variables import output and cover the escaped airflowctl messages variables import printed the bulk result unescaped, and variable keys can contain "[" and "/", so a key such as "a[/x]" crashed the success message with MarkupError after the import had already been applied. Escape it, and add tests for the task, connections, auth and config messages escaped earlier in this PR so removing the escapes fails them. * Escape Dag run ids in the clear prompt and cover remaining escapes The server-provided dag_run_id was printed as Rich markup before the confirmation prompt, so ids such as "[/x]" raised MarkupError and "run[abc]" was listed as "run". Escape it. Add tests so removing any of the existing escapes (next-execution error, tasks states-for-dag-run, logical-date not found, Dag pause error, run id not found) fails a test, and give the mocked clients added by the PR a Client spec. Claude-Session: https://claude.ai/code/session_01BVx6g9eNzpyiubnaPgUz9o --------- Co-authored-by: Shahar Epstein <[email protected]>
1 parent 335fd71 commit abc3a20

12 files changed

Lines changed: 262 additions & 18 deletions

File tree

‎airflow-ctl/src/airflowctl/ctl/commands/auth_command.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import keyring
2828
import rich
2929
from keyring.errors import NoKeyringError
30+
from rich.markup import escape
3031

3132
from airflowctl.api.client import (
3233
NEW_API_CLIENT,
@@ -93,7 +94,7 @@ def login(args, api_client=NEW_API_CLIENT) -> None:
9394
rich.print(success_message)
9495
return
9596
except Exception as e:
96-
rich.print(f"[red]Login failed: {e}")
97+
rich.print(f"[red]Login failed: {escape(str(e))}")
9798
sys.exit(1)
9899

99100
# Token-based login
@@ -156,7 +157,7 @@ def get_token(args, api_client=NEW_API_CLIENT) -> None:
156157
)
157158
print(login_response.access_token)
158159
except Exception as e:
159-
rich.print(f"[red]Token generation failed: {e}[/red]", file=sys.stderr)
160+
rich.print(f"[red]Token generation failed: {escape(str(e))}[/red]", file=sys.stderr)
160161
sys.exit(1)
161162

162163

‎airflow-ctl/src/airflowctl/ctl/commands/config_command.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import TYPE_CHECKING, Any, NamedTuple
2424

2525
import rich
26+
from rich.markup import escape
2627

2728
from airflowctl.api.client import NEW_API_CLIENT, ClientKind, provide_api_client
2829

@@ -838,5 +839,5 @@ def lint(args, api_client=NEW_API_CLIENT) -> None:
838839
rich.print("[green]No issues found in your airflow.cfg. It is ready for Airflow 3![/green]")
839840

840841
except Exception as e:
841-
rich.print(f"[red]Lint configs failed: {e}")
842+
rich.print(f"[red]Lint configs failed: {escape(str(e))}")
842843
sys.exit(1)

‎airflow-ctl/src/airflowctl/ctl/commands/connection_command.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import rich
2525
from pydantic import ValidationError
26+
from rich.markup import escape
2627

2728
from airflowctl.api.client import NEW_API_CLIENT, ClientKind, provide_api_client
2829
from airflowctl.api.datamodels.generated import (
@@ -91,9 +92,9 @@ def import_(args, api_client=NEW_API_CLIENT) -> None:
9192
)
9293
response = api_client.connections.bulk(BulkBodyConnectionBody(actions=[connection_create_action]))
9394
if response.create.errors:
94-
rich.print(f"[red]Failed to import connections: {response.create.errors}[/red]")
95+
rich.print(f"[red]Failed to import connections: {escape(str(response.create.errors))}[/red]")
9596
raise SystemExit(1)
9697
rich.print(f"[green]Successfully imported {response.create.success} connection(s)[/green]")
9798
except Exception as e:
98-
rich.print(f"[red]Failed to import connections: {e}[/red]")
99+
rich.print(f"[red]Failed to import connections: {escape(str(e))}[/red]")
99100
raise SystemExit(1)

‎airflow-ctl/src/airflowctl/ctl/commands/dag_command.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import Literal
2424

2525
import rich
26+
from rich.markup import escape
2627
from rich.text import Text
2728

2829
from airflowctl.api.client import (
@@ -55,7 +56,10 @@ def update_dag_state(
5556
try:
5657
response = api_client.dags.update(dag_id=dag_id, dag_body=dag_body)
5758
except ServerResponseError as e:
58-
rich.print(f"[red]Error while trying to {operation} Dag {dag_id}: {e}[/red]", file=sys.stderr)
59+
rich.print(
60+
f"[red]Error while trying to {operation} Dag {escape(dag_id)}: {escape(str(e))}[/red]",
61+
file=sys.stderr,
62+
)
5963
sys.exit(1)
6064

6165
response_dict = response.model_dump(mode="json")
@@ -116,7 +120,7 @@ def next_execution(args, api_client=NEW_API_CLIENT) -> dict | None:
116120
try:
117121
response = api_client.dags.get(dag_id=args.dag_id)
118122
except ServerResponseError as e:
119-
rich.print(f"[red]Error retrieving Dag {args.dag_id}: {e}[/red]")
123+
rich.print(f"[red]Error retrieving Dag {escape(args.dag_id)}: {escape(str(e))}[/red]")
120124
sys.exit(1)
121125

122126
response_dict = response.model_dump(mode="json")
@@ -244,7 +248,7 @@ def _print_dag_runs_to_clear(dag_id: str, dag_runs: list) -> None:
244248
partition_date = getattr(dag_run, "partition_date", None)
245249
partition_date_display = partition_date.isoformat() if partition_date is not None else "-"
246250
rich.print(
247-
f" - {dag_run.dag_run_id} (logical date: {logical_date}, partition date: {partition_date_display})"
251+
f" - {escape(dag_run.dag_run_id)} (logical date: {logical_date}, partition date: {partition_date_display})"
248252
)
249253

250254

‎airflow-ctl/src/airflowctl/ctl/commands/task_command.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import TYPE_CHECKING
2222

2323
import rich
24+
from rich.markup import escape
2425

2526
from airflowctl.api.client import NEW_API_CLIENT, ClientKind, ServerResponseError, provide_api_client
2627
from airflowctl.api.datamodels.generated import StructuredLogMessage, TaskInstanceState
@@ -99,7 +100,7 @@ def failed_deps(args, api_client=NEW_API_CLIENT) -> None:
99100
except ServerResponseError as e:
100101
if e.response.status_code == 404:
101102
rich.print(
102-
f"[red]{_task_instance_not_found_message(args.dag_id, run_id, args.task_id, args.map_index, e)}[/red]"
103+
f"[red]{escape(_task_instance_not_found_message(args.dag_id, run_id, args.task_id, args.map_index, e))}[/red]"
103104
)
104105
sys.exit(1)
105106
raise
@@ -122,7 +123,9 @@ def states_for_dag_run(args, api_client=NEW_API_CLIENT) -> None:
122123
task_instances = api_client.task_instances.list(dag_id=args.dag_id, dag_run_id=run_id).task_instances
123124
except ServerResponseError as e:
124125
if e.response.status_code == 404:
125-
rich.print(f"[red]Dag run {run_id!r} of Dag {args.dag_id!r} not found[/red]")
126+
rich.print(
127+
f"[red]Dag run {escape(repr(run_id))} of Dag {escape(repr(args.dag_id))} not found[/red]"
128+
)
126129
sys.exit(1)
127130
raise
128131

@@ -150,7 +153,7 @@ def state(args, api_client=NEW_API_CLIENT) -> None:
150153
except ServerResponseError as e:
151154
if e.response.status_code == 404:
152155
rich.print(
153-
f"[red]{_task_instance_not_found_message(args.dag_id, run_id, args.task_id, args.map_index, e)}[/red]"
156+
f"[red]{escape(_task_instance_not_found_message(args.dag_id, run_id, args.task_id, args.map_index, e))}[/red]"
154157
)
155158
sys.exit(1)
156159
raise
@@ -175,7 +178,7 @@ def logs(args, api_client=NEW_API_CLIENT) -> None:
175178
except ServerResponseError as e:
176179
if e.response.status_code == 404:
177180
rich.print(
178-
f"[red]{_task_instance_not_found_message(args.dag_id, args.dag_run_id, args.task_id, args.map_index, e)}[/red]"
181+
f"[red]{escape(_task_instance_not_found_message(args.dag_id, args.dag_run_id, args.task_id, args.map_index, e))}[/red]"
179182
)
180183
sys.exit(1)
181184
raise

‎airflow-ctl/src/airflowctl/ctl/utils/dag_run.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing import TYPE_CHECKING
2424

2525
import rich
26+
from rich.markup import escape
2627

2728
from airflowctl.api.client import ServerResponseError
2829

@@ -45,7 +46,7 @@ def _get_dag_run_by_run_id(api_client: Client, dag_id: str, run_id: str) -> DAGR
4546
except ServerResponseError as e:
4647
if e.response.status_code != 404:
4748
raise
48-
rich.print(f"[red]Dag run {run_id!r} of Dag {dag_id!r} not found[/red]")
49+
rich.print(f"[red]Dag run {escape(repr(run_id))} of Dag {escape(repr(dag_id))} not found[/red]")
4950
sys.exit(1)
5051

5152

@@ -54,7 +55,7 @@ def _get_dag_run_by_logical_date(api_client: Client, dag_id: str, value: str) ->
5455
try:
5556
logical_date = datetime.datetime.fromisoformat(value.replace("Z", "+00:00"))
5657
except ValueError:
57-
rich.print(f"[red]Invalid --logical-date: {value!r}[/red]")
58+
rich.print(f"[red]Invalid --logical-date: {escape(repr(value))}[/red]")
5859
sys.exit(1)
5960
if logical_date.tzinfo is None:
6061
rich.print("[red]--logical-date must include a timezone offset[/red]")
@@ -74,7 +75,9 @@ def _get_dag_run_by_logical_date(api_client: Client, dag_id: str, value: str) ->
7475
if e.response.status_code != 404:
7576
raise
7677
if not dag_runs:
77-
rich.print(f"[red]Dag run for {dag_id} with logical date {value!r} not found[/red]")
78+
rich.print(
79+
f"[red]Dag run for {escape(dag_id)} with logical date {escape(repr(value))} not found[/red]"
80+
)
7881
sys.exit(1)
7982
return dag_runs[0]
8083

‎airflow-ctl/tests/airflow_ctl/ctl/commands/test_auth_command.py‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import pytest
2626

27-
from airflowctl.api.client import ClientKind
27+
from airflowctl.api.client import Client, ClientKind
2828
from airflowctl.api.datamodels.auth_generated import LoginResponse
2929
from airflowctl.ctl import cli_parser
3030
from airflowctl.ctl.commands import auth_command
@@ -647,3 +647,53 @@ def test_list_envs_environment_name_with_json_substring(self, monkeypatch):
647647
auth_command.list_envs(args)
648648

649649
mock_get_password.assert_called_once_with("airflowctl", "api_token_prod.json.region")
650+
651+
652+
def test_login_prints_failure_containing_markup(capsys):
653+
api_client = mock.MagicMock(spec=Client)
654+
api_client.login.login_with_username_and_password.side_effect = RuntimeError("bad [/x]")
655+
656+
with pytest.raises(SystemExit) as exc_info:
657+
auth_command.login(
658+
cli_parser.get_parser().parse_args(
659+
[
660+
"auth",
661+
"login",
662+
"--api-url",
663+
"http://localhost:8080",
664+
"--username",
665+
"user",
666+
"--password",
667+
"wrong",
668+
]
669+
),
670+
api_client=api_client,
671+
)
672+
673+
assert exc_info.value.code == 1
674+
assert "bad [/x]" in capsys.readouterr().out
675+
676+
677+
def test_token_prints_failure_containing_markup(capsys):
678+
api_client = mock.MagicMock(spec=Client)
679+
api_client.login.login_with_username_and_password.side_effect = RuntimeError("bad [/x]")
680+
681+
with pytest.raises(SystemExit) as exc_info:
682+
auth_command.get_token(
683+
cli_parser.get_parser().parse_args(
684+
[
685+
"auth",
686+
"token",
687+
"--api-url",
688+
"http://localhost:8080",
689+
"--username",
690+
"user",
691+
"--password",
692+
"wrong",
693+
]
694+
),
695+
api_client=api_client,
696+
)
697+
698+
assert exc_info.value.code == 1
699+
assert "bad [/x]" in capsys.readouterr().err

‎airflow-ctl/tests/airflow_ctl/ctl/commands/test_config_command.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
from __future__ import annotations
1818

1919
import os
20+
from unittest import mock
2021
from unittest.mock import patch
2122

2223
import pytest
2324

24-
from airflowctl.api.client import ClientKind
25+
from airflowctl.api.client import Client, ClientKind
2526
from airflowctl.api.datamodels.generated import Config, ConfigOption, ConfigSection
2627
from airflowctl.ctl import cli_parser
2728
from airflowctl.ctl.commands import config_command
@@ -611,3 +612,14 @@ def test_config_list_masking_preservation(
611612
assert "fernet_key" in output_str
612613
assert "< hidden >" in output_str
613614
assert "sql_alchemy_conn" in output_str
615+
616+
617+
def test_lint_prints_failure_containing_markup(capsys):
618+
api_client = mock.MagicMock(spec=Client)
619+
api_client.configs.list.side_effect = RuntimeError("bad [/x]")
620+
621+
with pytest.raises(SystemExit) as exc_info:
622+
config_command.lint(cli_parser.get_parser().parse_args(["config", "lint"]), api_client=api_client)
623+
624+
assert exc_info.value.code == 1
625+
assert "bad [/x]" in capsys.readouterr().out

‎airflow-ctl/tests/airflow_ctl/ctl/commands/test_connections_command.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,28 @@ def test_import_action_on_existing_key(self, tmp_path, action_on_existing_key, e
323323
bulk_body = mock_client.connections.bulk.call_args[0][0]
324324
action = bulk_body.actions[0]
325325
assert action.action_on_existence == expected_enum
326+
327+
328+
@pytest.mark.parametrize(
329+
"bulk_setup",
330+
[
331+
pytest.param(
332+
lambda bulk: setattr(bulk.return_value.create, "errors", ["bad [/x]"]), id="bulk-errors"
333+
),
334+
pytest.param(lambda bulk: setattr(bulk, "side_effect", RuntimeError("bad [/x]")), id="exception"),
335+
],
336+
)
337+
def test_import_prints_failure_containing_markup(bulk_setup, tmp_path, capsys):
338+
connections_file = tmp_path / "connections.json"
339+
connections_file.write_text(json.dumps({"my_conn": {"conn_type": "http"}}))
340+
api_client = mock.MagicMock(spec=Client)
341+
bulk_setup(api_client.connections.bulk)
342+
343+
with pytest.raises(SystemExit) as exc_info:
344+
connection_command.import_(
345+
cli_parser.get_parser().parse_args(["connections", "import", connections_file.as_posix()]),
346+
api_client=api_client,
347+
)
348+
349+
assert exc_info.value.code == 1
350+
assert "bad [/x]" in capsys.readouterr().out

‎airflow-ctl/tests/airflow_ctl/ctl/commands/test_dag_command.py‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import httpx
2626
import pytest
2727

28-
from airflowctl.api.client import ClientKind
28+
from airflowctl.api.client import Client, ClientKind
2929
from airflowctl.api.datamodels.generated import ClearTaskInstancesBody, DAGResponse, DagSchedulingState
3030
from airflowctl.api.operations import DagRunOperations, ServerResponseError, TasksOperations
3131
from airflowctl.ctl import cli_parser
@@ -228,6 +228,33 @@ def test_pause_fail(self, api_client_maker, monkeypatch):
228228
api_client=api_client,
229229
)
230230

231+
def test_pause_fail_prints_server_error_containing_markup(self, api_client_maker, capsys):
232+
api_client = api_client_maker(
233+
path=f"/api/v2/dags/{self.dag_id}",
234+
response_json={"detail": "bad [/x] value"},
235+
expected_http_status_code=404,
236+
kind=ClientKind.CLI,
237+
)
238+
with pytest.raises(SystemExit):
239+
dag_command.pause(
240+
self.parser.parse_args(["dags", "pause", self.dag_id]),
241+
api_client=api_client,
242+
)
243+
244+
assert "[/x]" in capsys.readouterr().err
245+
246+
def test_pause_fail_prints_dag_id_containing_markup(self, capsys):
247+
api_client = Mock(spec=Client)
248+
api_client.dags.update.side_effect = _make_server_error(404)
249+
250+
with pytest.raises(SystemExit):
251+
dag_command.pause(
252+
self.parser.parse_args(["dags", "pause", "my_dag[/y]"]),
253+
api_client=api_client,
254+
)
255+
256+
assert "my_dag[/y]" in capsys.readouterr().err
257+
231258
def test_unpause_dag(self, api_client_maker, monkeypatch):
232259
api_client = api_client_maker(
233260
path=f"/api/v2/dags/{self.dag_id}",
@@ -364,6 +391,33 @@ def test_next_execution_no_schedule(self, api_client_maker):
364391
)
365392
assert result is None
366393

394+
def test_next_execution_fail_prints_dag_id_containing_markup(self, capsys):
395+
api_client = Mock(spec=Client)
396+
api_client.dags.get.side_effect = _make_server_error(404)
397+
398+
with pytest.raises(SystemExit):
399+
dag_command.next_execution(
400+
self.parser.parse_args(["dags", "next-execution", "my_dag[/y]"]),
401+
api_client=api_client,
402+
)
403+
404+
assert "my_dag[/y]" in capsys.readouterr().out
405+
406+
def test_next_execution_fail_prints_server_error_containing_markup(self, api_client_maker, capsys):
407+
api_client = api_client_maker(
408+
path=f"/api/v2/dags/{self.dag_id}",
409+
response_json={"detail": "bad [/x] value"},
410+
expected_http_status_code=404,
411+
kind=ClientKind.CLI,
412+
)
413+
with pytest.raises(SystemExit):
414+
dag_command.next_execution(
415+
self.parser.parse_args(["dags", "next-execution", self.dag_id]),
416+
api_client=api_client,
417+
)
418+
419+
assert "[/x]" in capsys.readouterr().out
420+
367421
def test_next_execution_fail(self, api_client_maker):
368422
api_client = api_client_maker(
369423
path=f"/api/v2/dags/{self.dag_id}",
@@ -730,6 +784,19 @@ def test_clear_prompts_before_clearing(self, monkeypatch):
730784
assert result == {"dag_run_count": 1, "cleared_task_instances": 0, "cancelled": True}
731785
api_client.tasks.clear.assert_not_called()
732786

787+
@pytest.mark.parametrize("run_id", ["run_[/x]", "run[abc]"])
788+
def test_clear_prompt_lists_run_id_containing_markup(self, run_id, monkeypatch, capsys):
789+
api_client = self._api_client_mock()
790+
api_client.dag_runs.get.return_value = self._dag_run(run_id)
791+
monkeypatch.setattr("builtins.input", lambda _: "n")
792+
793+
dag_command.clear(
794+
self.parser.parse_args(["dags", "clear", self.dag_id, "--run-id", run_id]),
795+
api_client=api_client,
796+
)
797+
798+
assert f"- {run_id} (" in capsys.readouterr().out
799+
733800
@pytest.mark.parametrize(
734801
"command",
735802
[

0 commit comments

Comments
 (0)