Skip to content

Commit c5faeb1

Browse files
Authorize Dag reparse against the file's Dags, not the query-string dag_id (#69471)
* Authorize Dag reparse against the file's Dags, not the query-string dag_id The PUT /parseDagFile/{file_token} endpoint authorized the caller through a route dependency that, with no dag_id path parameter, resolves the target from the query string, which is decoupled from the Dag file the signed file_token actually resolves to. Add a requires_access_dag_from_file_token dependency that decodes the token, resolves the Dags defined in that file, and authorizes the caller against exactly those Dags, so authorization always matches the Dag being reparsed regardless of any request parameter. * Update airflow-core/src/airflow/api_fastapi/core_api/security.py Co-authored-by: Amogh Desai <[email protected]> * Fix CI --------- Co-authored-by: Amogh Desai <[email protected]>
1 parent 038ece6 commit c5faeb1

4 files changed

Lines changed: 63 additions & 42 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/public/dag_parsing.py‎

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,29 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19-
from collections.abc import Sequence
20-
from typing import TYPE_CHECKING
19+
from fastapi import Depends, Request, status
20+
from itsdangerous import URLSafeSerializer
2121

22-
from fastapi import Depends, HTTPException, Request, status
23-
from itsdangerous import BadSignature, URLSafeSerializer
24-
from sqlalchemy import select
25-
26-
from airflow.api_fastapi.auth.managers.models.resource_details import DagDetails
2722
from airflow.api_fastapi.common.db.common import SessionDep
2823
from airflow.api_fastapi.common.router import AirflowRouter
2924
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
30-
from airflow.api_fastapi.core_api.security import requires_access_dag
25+
from airflow.api_fastapi.core_api.security import requires_access_dag_from_file_token
3126
from airflow.api_fastapi.logging.decorators import action_logging
32-
from airflow.models.dag import DagModel
3327
from airflow.models.dagbag import DagPriorityParsingRequest
3428

35-
if TYPE_CHECKING:
36-
from airflow.api_fastapi.auth.managers.models.batch_apis import IsAuthorizedDagRequest
37-
3829
dag_parsing_router = AirflowRouter(tags=["DAG Parsing"], prefix="/parseDagFile/{file_token}")
3930

4031

4132
@dag_parsing_router.put(
4233
"",
4334
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
4435
status_code=status.HTTP_201_CREATED,
45-
dependencies=[Depends(requires_access_dag(method="PUT")), Depends(action_logging())],
36+
dependencies=[Depends(requires_access_dag_from_file_token(method="PUT")), Depends(action_logging())],
4637
)
47-
def reparse_dag_file(
48-
file_token: str,
49-
session: SessionDep,
50-
request: Request,
51-
) -> None:
38+
def reparse_dag_file(file_token: str, session: SessionDep, request: Request) -> None:
5239
"""Request re-parsing a Dag file."""
53-
secret_key = request.app.state.secret_key
54-
auth_s = URLSafeSerializer(secret_key)
55-
try:
56-
payload = auth_s.loads(file_token)
57-
except BadSignature:
58-
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found")
59-
60-
bundle_name = payload["bundle_name"]
61-
relative_fileloc = payload["relative_fileloc"]
62-
63-
requests: Sequence[IsAuthorizedDagRequest] = [
64-
{"method": "PUT", "details": DagDetails(id=dag_id)}
65-
for dag_id in session.scalars(
66-
select(DagModel.dag_id).where(
67-
DagModel.bundle_name == bundle_name, DagModel.relative_fileloc == relative_fileloc
68-
)
69-
)
70-
]
71-
if not requests:
72-
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found")
73-
74-
parsing_request = DagPriorityParsingRequest(bundle_name=bundle_name, relative_fileloc=relative_fileloc)
40+
payload = URLSafeSerializer(request.app.state.secret_key).loads(file_token)
41+
parsing_request = DagPriorityParsingRequest(
42+
bundle_name=payload["bundle_name"], relative_fileloc=payload["relative_fileloc"]
43+
)
7544
session.add(parsing_request)

‎airflow-core/src/airflow/api_fastapi/core_api/security.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from fastapi import Depends, HTTPException, Request, status
2727
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer, OAuth2PasswordBearer
28+
from itsdangerous import BadSignature, URLSafeSerializer
2829
from jwt import ExpiredSignatureError, InvalidTokenError
2930
from sqlalchemy import or_, select
3031
from sqlalchemy.orm import Session
@@ -198,6 +199,49 @@ def inner(
198199
return inner
199200

200201

202+
def requires_access_dag_from_file_token(
203+
method: ResourceMethod,
204+
) -> Callable[[str, Request, BaseUser, Session], None]:
205+
"""
206+
Authorize the caller against the DAGs referenced by a signed ``file_token``.
207+
208+
For ``file_token`` based endpoints (such as ``reparse``), the token is resolved to its referenced file, and authorization is performed against exactly the DAGs defined in that file, never against a request parameter.
209+
"""
210+
211+
def inner(
212+
file_token: str,
213+
request: Request,
214+
user: GetUserDep,
215+
session: SessionDep,
216+
) -> None:
217+
try:
218+
payload = URLSafeSerializer(request.app.state.secret_key).loads(file_token)
219+
except BadSignature:
220+
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found")
221+
222+
dag_ids = list(
223+
session.scalars(
224+
select(DagModel.dag_id).where(
225+
DagModel.bundle_name == payload["bundle_name"],
226+
DagModel.relative_fileloc == payload["relative_fileloc"],
227+
)
228+
)
229+
)
230+
if not dag_ids:
231+
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found")
232+
233+
dag_id_to_team = DagModel.get_dag_id_to_team_name_mapping(dag_ids, session=session)
234+
requests: list[IsAuthorizedDagRequest] = [
235+
{"method": method, "details": DagDetails(id=dag_id, team_name=dag_id_to_team.get(dag_id))}
236+
for dag_id in dag_ids
237+
]
238+
_requires_access(
239+
is_authorized_callback=lambda: get_auth_manager().batch_is_authorized_dag(requests, user=user),
240+
)
241+
242+
return inner
243+
244+
201245
class PermittedDagFilter(OrmClause[set[str]]):
202246
"""A parameter that filters the permitted dags for the user."""
203247

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dag_parsing.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ def test_should_respond_401(self, unauthenticated_test_client):
7575
)
7676
assert response.status_code == 401
7777

78-
def test_should_respond_403(self, unauthorized_test_client):
79-
response = unauthorized_test_client.put("/parseDagFile/token", headers={"Accept": "application/json"})
78+
def test_should_respond_403(self, unauthorized_test_client, url_safe_serializer, session):
79+
parse_and_sync_to_db(EXAMPLE_DAG_FILE)
80+
test_dag = DBDagBag(load_op_links=False).get_latest_version_of_dag(TEST_DAG_ID, session=session)
81+
token = url_safe_serializer.dumps(
82+
{"bundle_name": "example_dags", "relative_fileloc": test_dag.relative_fileloc}
83+
)
84+
response = unauthorized_test_client.put(
85+
f"/parseDagFile/{token}", headers={"Accept": "application/json"}
86+
)
8087
assert response.status_code == 403
8188

8289
def test_bad_file_request(self, url_safe_serializer, session, test_client):

‎scripts/ci/prek/extract_permissions.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def _extract_entity_arg(call_node: ast.Call) -> str | None:
225225
# Map from requires_access_* function name → (resource base name, forced entity or None)
226226
_FN_TO_RESOURCE_INFO: dict[str, tuple[str, str | None]] = {
227227
"requires_access_dag": ("DAG", None),
228+
"requires_access_dag_from_file_token": ("DAG", None), # reparse authorizes the file_token's Dags
228229
"requires_access_backfill": ("DAG", "RUN"), # backfill is a DAG.RUN alias
229230
"requires_access_dag_run_bulk": ("DAG", "RUN"), # dag_run bulk is a DAG.RUN alias
230231
"requires_access_dag_run_clear_bulk": ("DAG", "RUN"), # dag_run clear bulk is a DAG.RUN alias

0 commit comments

Comments
 (0)