fix: run MWAA CLI commands without a shell (#4750)

* fix: run MWAA CLI commands without a shell

The MWAA CLI endpoint passed the request body to sh -c inside the Airflow
container, so shell syntax such as ; | $(...) ran arbitrary commands.
Split the command with POSIX quoting rules and exec airflow directly.

* fix: follow POSIX word splitting for MWAA CLI arguments

Only spaces, tabs and newlines separate arguments, and a backslash-newline
pair is removed instead of kept.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-30 14:32:06 -04:00
1 parent 49501acb5a
commit efbd990ef7
2 files changed
+148 -4

No files matched your search

@@ -528,7 +528,7 @@ class MwaaEnvironmentManagerTest {
}
@Test
void runAirflowCliRunsTheCommandThroughAShellInTheAirflowContainer() throws Exception {
void runAirflowCliRunsTheAirflowCommandInTheAirflowContainer() throws Exception {
DockerClient dockerClient = Mockito.mock(DockerClient.class);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, "airflow-container-id", 2,
@@ -536,9 +536,73 @@ class MwaaEnvironmentManagerTest {
ContainerExec.Result result = manager.runAirflowCli("airflow-container-id", "dags list");
assertEquals(List.of(List.of("sh", "-c", "airflow dags list")), commands);
assertEquals(List.of(List.of("airflow", "dags", "list")), commands);
assertEquals(2, result.exitCode());
assertEquals("dag1\n", result.stdout());
assertEquals("warning\n", result.stderr());
}
@Nested
class AirflowCli {
private static final String AIRFLOW_ID = "airflow-container-id";
private DockerClient dockerClient;
@BeforeEach
void stubDocker() {
dockerClient = Mockito.mock(DockerClient.class);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
}
@Test
void shellSyntaxInACliCommandReachesAirflowAsPlainArguments() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
manager.runAirflowCli(AIRFLOW_ID, "dags list; touch /tmp/pwned $(id) `id` | cat > /tmp/out");
assertEquals(List.of(List.of("airflow", "dags", "list;", "touch", "/tmp/pwned", "$(id)", "`id`",
"|", "cat", ">", "/tmp/out")), commands);
}
@Test
void quotedCliArgumentsReachAirflowWhole() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
manager.runAirflowCli(AIRFLOW_ID, "dags trigger -c '{\"key\": \"a b\"}' my_dag");
manager.runAirflowCli(AIRFLOW_ID, "variables set greeting \"say \\\"hi\\\" to $USER\" ''");
manager.runAirflowCli(AIRFLOW_ID, "variables get two\\ words");
assertEquals(List.of(
List.of("airflow", "dags", "trigger", "-c", "{\"key\": \"a b\"}", "my_dag"),
List.of("airflow", "variables", "set", "greeting", "say \"hi\" to $USER", ""),
List.of("airflow", "variables", "get", "two words")), commands);
}
@Test
void backslashNewlineJoinsTheTextOnEitherSide() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
manager.runAirflowCli(AIRFLOW_ID, "variables set greeting hel\\\nlo \"wor\\\nld\"");
assertEquals(List.of(List.of("airflow", "variables", "set", "greeting", "hello", "world")), commands);
}
@Test
void onlySpacesTabsAndNewlinesSeparateArguments() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
manager.runAirflowCli(AIRFLOW_ID, "variables\tset\ngreeting hello\u2003world");
assertEquals(List.of(List.of("airflow", "variables", "set", "greeting", "hello\u2003world")), commands);
}
@Test
void unterminatedQuoteIsRejectedWithoutRunningAnything() {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
assertThrows(IllegalArgumentException.class,
() -> manager.runAirflowCli(AIRFLOW_ID, "dags trigger -c '{\"key\": 1}"));
assertEquals(List.of(), commands);
}
}
}