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

pierrejeambrun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new ed5836773e1 Authorize the backfill list on the Dag it lists (#77795)
ed5836773e1 is described below

commit ed5836773e1165866737c8013a35143c2e6a0afc
Author: Pierre Jeambrun <[email protected]>
AuthorDate: Tue Dec 1 16:42:26 2026 +0100

    Authorize the backfill list on the Dag it lists (#77795)
    
    * Authorize the backfill list on the Dag it lists
    
    The list route reads its Dag from the query string, but the access check
    took the Dag from a request body when one was sent. Only the create and
    dry-run routes take a body, so the body is now read for them only.
    
    * Merge the backfill authorization tests that differ only by route
    
    Both check where the Dag authorized comes from when the path has no 
backfill_id, so one parametrized test keeps the two routes side by side.
    
    ---------
    
    Co-authored-by: Amogh Desai <[email protected]>
---
 .../src/airflow/api_fastapi/core_api/security.py   | 17 +++++++-----
 .../unit/api_fastapi/core_api/test_security.py     | 31 +++++++++++++---------
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/airflow-core/src/airflow/api_fastapi/core_api/security.py 
b/airflow-core/src/airflow/api_fastapi/core_api/security.py
index da20a350c46..8afe5af6497 100644
--- a/airflow-core/src/airflow/api_fastapi/core_api/security.py
+++ b/airflow-core/src/airflow/api_fastapi/core_api/security.py
@@ -613,13 +613,16 @@ def requires_access_backfill(
         # Left: the routes naming their Dag in the body (create, dry run) or 
in the query string
         # (list), and ids the handler's own parser will reject.
         dag_id = None
-        # An unreadable body names no Dag, the same state as no body, so it 
falls through to the
-        # authorization below. Broad because ``json.loads`` also raises 
UnicodeDecodeError, bare
-        # ValueError and RecursionError, none of them JSONDecodeError.
-        with suppress(Exception):
-            body = await request.json()
-            if isinstance(body, dict):
-                dag_id = body.get("dag_id")
+        # Only the POST routes take a body. A GET handler reads its Dag from 
the query string, so a
+        # body sent along must not decide the authorization.
+        if method == "POST":
+            # An unreadable body names no Dag, the same state as no body, so 
it falls through to
+            # the authorization below. Broad because ``json.loads`` also 
raises UnicodeDecodeError,
+            # bare ValueError and RecursionError, none of them JSONDecodeError.
+            with suppress(Exception):
+                body = await request.json()
+                if isinstance(body, dict):
+                    dag_id = body.get("dag_id")
         if dag_id is not None and not isinstance(dag_id, str):
             # Fail closed: reject non-string dag_id before authz decision.
             raise HTTPException(
diff --git a/airflow-core/tests/unit/api_fastapi/core_api/test_security.py 
b/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
index 9b27f4d67cb..2f409a44744 100644
--- a/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
+++ b/airflow-core/tests/unit/api_fastapi/core_api/test_security.py
@@ -489,12 +489,19 @@ class TestFastApiSecurity:
 
     @pytest.mark.db_test
     @pytest.mark.asyncio
+    @pytest.mark.parametrize(
+        ("method", "expected_dag_id"),
+        [
+            pytest.param("POST", "body_dag_id", id="create-reads-the-body"),
+            pytest.param("GET", "query_dag_id", id="list-ignores-the-body"),
+        ],
+    )
     @patch.object(DagModel, "get_team_name_async", autospec=True)
     @patch("airflow.api_fastapi.core_api.security.get_auth_manager")
-    async def test_requires_access_backfill_authorized_from_body(
-        self, mock_get_auth_manager, mock_get_team_name, 
backfill_lookup_session
+    async def test_requires_access_backfill_authorized_without_backfill_id(
+        self, mock_get_auth_manager, mock_get_team_name, 
backfill_lookup_session, method, expected_dag_id
     ):
-        """With no backfill_id in the path, dag_id comes from the request body 
(POST backfill)."""
+        """With no backfill_id in the path, POST routes read the Dag from the 
body, the list from the query string."""
         auth_manager = Mock()
         auth_manager.is_authorized_dag.return_value = True
         mock_get_auth_manager.return_value = auth_manager
@@ -502,17 +509,16 @@ class TestFastApiSecurity:
 
         request = Mock()
         request.path_params = {}
+        request.query_params = {"dag_id": "query_dag_id"}
         request.json = AsyncMock(return_value={"dag_id": "body_dag_id"})
-
         user = Mock()
 
-        inner = requires_access_backfill("POST")
-        await inner(request, user)
+        await requires_access_backfill(method)(request, user)
 
         auth_manager.is_authorized_dag.assert_called_once_with(
-            method="POST",
+            method=method,
             access_entity=DagAccessEntity.RUN,
-            details=DagDetails(id="body_dag_id", team_name="team1"),
+            details=DagDetails(id=expected_dag_id, team_name="team1"),
             user=user,
         )
 
@@ -643,10 +649,10 @@ class TestFastApiSecurity:
     @pytest.mark.db_test
     @pytest.mark.asyncio
     @pytest.mark.parametrize(
-        ("path_params", "body"),
+        ("method", "path_params", "body"),
         [
-            pytest.param({"backfill_id": "1"}, {}, id="backfill-in-path"),
-            pytest.param({}, {"dag_id": "body_dag"}, id="dag-in-body"),
+            pytest.param("GET", {"backfill_id": "1"}, {}, 
id="backfill-in-path"),
+            pytest.param("POST", {}, {"dag_id": "body_dag"}, id="dag-in-body"),
         ],
     )
     @patch.object(DagModel, "get_team_name_async", autospec=True)
@@ -655,6 +661,7 @@ class TestFastApiSecurity:
         self,
         mock_get_auth_manager,
         mock_get_team_name,
+        method,
         path_params,
         body,
         backfill_lookup_session,
@@ -677,7 +684,7 @@ class TestFastApiSecurity:
         request.query_params = {}
         request.json = AsyncMock(return_value=body)
 
-        await requires_access_backfill("GET")(request, Mock(spec=BaseUser))
+        await requires_access_backfill(method)(request, Mock(spec=BaseUser))
 
         assert backfill_session_events == ["open", "close", "authorize"]
 

Reply via email to