Skip to content

Commit 61f1d4b

Browse files
authored
Resolve a trigger's XCom requests as its deferred task instance (#74772)
* Resolve a trigger's XCom requests as its deferred task instance Note: _None_ of this is a security boundary, and should not be treated as such. The Execution API resolves XCom reads and writes for a task inside a loop to the caller's own iteration, using the task identity in the caller's JWT. Triggers have no such identity: the triggerer talks to an in-process API that treats every request as the all-zero task instance. A trigger's XCom requests therefore resolve as an anonymous caller rather than as the deferred task it runs for, so it can neither read (a 400) nor write the XComs of a task inside a loop. Triggers can also share one runner coroutine and one comms channel (the "shared streams" idea), so only the asyncio task running a trigger's code knows which trigger it is; a context variable carries that trigger's task instance id to each request frame. Shared-stream groups are started from whichever trigger subscribes first but serve several triggers, so they start with it cleared rather than inheriting that trigger's id. This isn't ideal, but will do "for now". The trigger runner process (the one running async code) sets the task instance id, and since that process runs user code a trigger can claim to be any task instance. The (in-process) API therefore checks the claim. A write acts as the task instance only when it is deferred and its trigger is already assigned to the calling triggerer. A read acts as it whenever that try is still live, because after a user marks a deferred task failed or successful the trigger row is gone and the triggerer still runs the trigger's on_kill(). The most a misbehaving trigger can do is write as another task that is deferred on the same triggerer, or read as any live task, and it can already read and write every task's XComs by coordinates through the in-process API. As stated, this is _not_ a security boundary we can rely on Requests that fail the check are rejected rather than falling back to the anonymous identity, deliberately: a trigger whose task instance is no longer deferred, or whose trigger was reassigned, should not still be writing its task's XComs, and falling back would only work for tasks outside loops. Clearing a deferred task archives its try before on_kill() runs, so on_kill() after a clear cannot read its task's XComs; a trigger that needs a job id there should keep it on the trigger, as the in-tree triggers already do. Only the in-process API that the triggerer creates knows which triggerer is calling, so every other API rejects the header rather than trusting it. A triggerer that eventually talks to a real API server will need its own credential before this can work there. Only the XCom routes honour the trigger's task instance id for now. get_previous_task_instance also resolves relative to the caller, so a trigger asking for its previous task instance still gets the latest pass rather than the one at its own loop position. Every other request from the triggerer keeps its current identity, so access rules and the live-attempt check on those routes are unchanged. * fixup! Resolve a trigger's XCom requests as its deferred task instance Pin the HEAD and DELETE rules in the trigger claim check and assert which check denied each 403, so loosening the read-method test no longer passes. * fixup! Resolve a trigger's XCom requests as its deferred task instance Ignore a trigger failure reported by a triggerer that no longer owns the trigger. The XCom write check now answers 403 to a stale copy on its previous owner, which raises before yielding and would otherwise have Trigger.submit_failure send the task to __fail__ while the new owner is still running it.
1 parent 098d017 commit 61f1d4b

10 files changed

Lines changed: 563 additions & 75 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/execution_api/in_process.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
logger = structlog.get_logger(logger_name=__name__)
3737

38+
TRIGGER_TI_ID_HEADER = "Airflow-Trigger-Ti-Id"
39+
3840

3941
# Finalizer arguments must not reference the transport, or it cannot be garbage collected.
4042
def _shutdown_loop(
@@ -51,12 +53,16 @@ def _shutdown_loop(
5153
thread.join(timeout=5)
5254

5355

54-
def _configure_in_process_auth(app: FastAPI) -> None:
56+
def _configure_in_process_auth(app: FastAPI, *, triggerer_id: int | None = None) -> None:
5557
from airflow.api_fastapi.execution_api.datamodels.token import TIClaims, TIToken
5658
from airflow.api_fastapi.execution_api.routes.connections import has_connection_access
5759
from airflow.api_fastapi.execution_api.routes.variables import has_variable_access
5860
from airflow.api_fastapi.execution_api.routes.xcoms import has_xcom_access
59-
from airflow.api_fastapi.execution_api.security import _IN_PROCESS_NON_TI_CALLER, _jwt_bearer
61+
from airflow.api_fastapi.execution_api.security import (
62+
_IN_PROCESS_NON_TI_CALLER,
63+
_IN_PROCESS_TRIGGERER_ID,
64+
_jwt_bearer,
65+
)
6066

6167
async def always_allow(request: Request):
6268
from uuid import UUID
@@ -71,6 +77,8 @@ async def always_allow(request: Request):
7177
"task_instance_id" not in request.path_params
7278
and "X-Airflow-In-Process-Attempt-Id" not in request.headers
7379
)
80+
if triggerer_id is not None:
81+
request.scope[_IN_PROCESS_TRIGGERER_ID] = triggerer_id
7482
claims = TIClaims(scope="execution")
7583
return TIToken(id=ti_id, claims=claims)
7684

@@ -90,6 +98,7 @@ class InProcessExecutionAPI:
9098
"""
9199

92100
_app: FastAPI | None = None
101+
triggerer_id: int | None = None
93102

94103
@cached_property
95104
def app(self):
@@ -105,7 +114,7 @@ def app(self):
105114
self._app = create_task_execution_api_app(lifespan=attrs.evolve(lifespan, registry=registry))
106115
# Auth dependency overrides bypass validation, so the stub skips validator setup in lifespan().
107116
registry.register_value(JWTValidator, None)
108-
_configure_in_process_auth(self._app)
117+
_configure_in_process_auth(self._app, triggerer_id=self.triggerer_id)
109118

110119
return self._app
111120

‎airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py‎

Lines changed: 111 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from uuid import UUID
2525

2626
import attrs
27+
from cadwyn import current_dependency_solver
2728
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, Request, Response, status
2829
from pydantic import JsonValue
2930
from sqlalchemy import select
@@ -33,18 +34,20 @@
3334
from airflow.api_fastapi.common.db.common import SessionDep
3435
from airflow.api_fastapi.core_api.base import BaseModel
3536
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
36-
from airflow.api_fastapi.execution_api.datamodels.token import DagParseToken, ExecutionToken
37+
from airflow.api_fastapi.execution_api.datamodels.token import DagParseToken, ExecutionToken, TIToken
3738
from airflow.api_fastapi.execution_api.datamodels.xcom import (
3839
XComResponse,
3940
XComSequenceIndexResponse,
4041
XComSequenceSliceResponse,
4142
)
43+
from airflow.api_fastapi.execution_api.in_process import TRIGGER_TI_ID_HEADER
4244
from airflow.api_fastapi.execution_api.security import (
45+
_IN_PROCESS_TRIGGERER_ID,
4346
CurrentExecutionToken,
44-
CurrentTIToken,
4547
DagInGrantedBundle,
4648
ExecutionAPIRoute,
4749
ExecutionOrDagParseToken,
50+
require_task_token,
4851
)
4952
from airflow.api_fastapi.execution_api.versions.v2026_10_30 import IdentifyArchivedTaskStateUpdates
5053
from airflow.exceptions import TaskNotFound
@@ -62,6 +65,7 @@
6265
enclosing_loop,
6366
)
6467
from airflow.models.taskinstance import TaskInstance
68+
from airflow.models.trigger import Trigger
6569
from airflow.models.xcom import XCOM_RETURN_KEY, XComModel, xcom_entity
6670
from airflow.utils.db import get_query_count
6771
from airflow.utils.state import TaskInstanceState
@@ -252,29 +256,86 @@ def _build_xcom_read(
252256
raise HTTPException(status.HTTP_400_BAD_REQUEST, str(error)) from error
253257

254258

255-
def xcom_query(
256-
dag_id: str,
257-
run_id: str,
258-
task_id: str,
259-
key: str,
259+
def _resolve_trigger_token(
260+
request: Request,
260261
session: SessionDep,
261-
dag_bag: DagBagDep,
262-
map_index: Annotated[int | None, Query()] = None,
263-
previous_iteration: bool = False,
264262
token: ExecutionToken = CurrentExecutionToken,
265-
) -> Select:
266-
return _build_xcom_read(
267-
dag_id=dag_id,
268-
run_id=run_id,
269-
task_id=task_id,
270-
key=key,
271-
session=session,
272-
dag_bag=dag_bag,
273-
token=token,
274-
map_index=map_index,
275-
previous_iteration=previous_iteration,
276-
sequence=True,
277-
).statement
263+
dependency_solver=Depends(current_dependency_solver),
264+
) -> ExecutionToken:
265+
"""
266+
Return the identity an XCom request acts as.
267+
268+
A request made by a trigger's code carries its task instance's id in ``TRIGGER_TI_ID_HEADER``. The
269+
triggerer subprocess can forge the header, so it is only honoured on the triggerer's in-process API.
270+
271+
A write acts as the claimed task instance only while it is deferred on a trigger assigned to the calling
272+
triggerer. A read acts as it while that try is live, in any state, because a trigger's ``on_kill`` runs
273+
after the task instance leaves ``deferred`` and the trigger row is gone; reads are not a security
274+
boundary on this API, which already serves every XCom. Anything else gets a 403.
275+
"""
276+
header = request.headers.get(TRIGGER_TI_ID_HEADER)
277+
# The endpoint runs with Cadwyn's re-solved dependencies, so checking there reuses the endpoint's session.
278+
if header is None or dependency_solver != "cadwyn":
279+
return token
280+
281+
triggerer_id = request.scope.get(_IN_PROCESS_TRIGGERER_ID)
282+
if triggerer_id is None:
283+
raise HTTPException(
284+
status_code=status.HTTP_403_FORBIDDEN,
285+
detail={
286+
"reason": "access_denied",
287+
"message": f"{TRIGGER_TI_ID_HEADER} is only accepted from the triggerer",
288+
},
289+
)
290+
291+
try:
292+
claimed = UUID(header)
293+
except ValueError:
294+
raise HTTPException(
295+
status_code=status.HTTP_400_BAD_REQUEST,
296+
detail={"reason": "invalid_request", "message": f"{TRIGGER_TI_ID_HEADER} must be a UUID"},
297+
)
298+
299+
if request.method in ("GET", "HEAD"):
300+
permitted = (
301+
session.scalar(
302+
select(TaskInstance.working_set)
303+
.where(TaskInstance.id == claimed)
304+
.execution_options(include_all_attempts=True)
305+
)
306+
is True
307+
)
308+
denial = "Task instance is not a live attempt"
309+
else:
310+
permitted = (
311+
session.scalar(
312+
select(TaskInstance.id)
313+
.join(Trigger, Trigger.id == TaskInstance.trigger_id)
314+
.where(
315+
TaskInstance.id == claimed,
316+
Trigger.triggerer_id == triggerer_id,
317+
TaskInstance.state == TaskInstanceState.DEFERRED,
318+
)
319+
)
320+
is not None
321+
)
322+
denial = "Task instance is not deferred on a trigger assigned to this triggerer"
323+
if not permitted:
324+
raise HTTPException(
325+
status_code=status.HTTP_403_FORBIDDEN,
326+
detail={"reason": "access_denied", "message": denial},
327+
)
328+
return token.model_copy(update={"id": claimed})
329+
330+
331+
_TriggerAwareToken = Depends(_resolve_trigger_token)
332+
333+
334+
def _resolve_trigger_ti_token(token: ExecutionToken = _TriggerAwareToken) -> TIToken:
335+
return require_task_token(token)
336+
337+
338+
_TriggerAwareTIToken = Depends(_resolve_trigger_ti_token)
278339

279340

280341
@router.get(
@@ -297,7 +358,7 @@ def get_mapped_xcom_by_index(
297358
session: SessionDep,
298359
dag_bag: DagBagDep,
299360
previous_iteration: bool = False,
300-
token: ExecutionToken = CurrentExecutionToken,
361+
token: ExecutionToken = _TriggerAwareToken,
301362
) -> XComSequenceIndexResponse:
302363
xcom_read = _build_xcom_read(
303364
dag_id=dag_id,
@@ -364,7 +425,7 @@ def get_mapped_xcom_by_slice(
364425
params: Annotated[GetXComSliceFilterParams, Query()],
365426
session: SessionDep,
366427
dag_bag: DagBagDep,
367-
token: ExecutionToken = CurrentExecutionToken,
428+
token: ExecutionToken = _TriggerAwareToken,
368429
) -> XComSequenceSliceResponse:
369430
xcom_read = _build_xcom_read(
370431
dag_id=dag_id,
@@ -448,7 +509,10 @@ def get_mapped_xcom_by_slice(
448509
dependencies=[ExecutionOrDagParseToken, DagInGrantedBundle],
449510
responses={
450511
**create_openapi_http_exception_doc(
451-
[(status.HTTP_400_BAD_REQUEST, "map_index cannot be specified in a HEAD request")]
512+
[
513+
(status.HTTP_400_BAD_REQUEST, "map_index cannot be specified in a HEAD request"),
514+
(status.HTTP_409_CONFLICT, "Several live task instances produce the requested value"),
515+
]
452516
),
453517
status.HTTP_200_OK: {
454518
"description": "Metadata about the number of matching XCom values",
@@ -463,10 +527,16 @@ def get_mapped_xcom_by_slice(
463527
description="Returns the count of mapped XCom values found in the `Content-Range` response header",
464528
)
465529
def head_xcom(
530+
dag_id: str,
531+
run_id: str,
532+
task_id: str,
533+
key: str,
466534
response: Response,
467535
session: SessionDep,
468-
xcom_query: Annotated[Select, Depends(xcom_query)],
536+
dag_bag: DagBagDep,
469537
map_index: Annotated[int | None, Query()] = None,
538+
previous_iteration: bool = False,
539+
token: ExecutionToken = _TriggerAwareToken,
470540
) -> None:
471541
"""Get the count of XComs from database - not other XCom Backends."""
472542
if map_index is not None:
@@ -475,6 +545,17 @@ def head_xcom(
475545
detail={"reason": "invalid_request", "message": "Cannot specify map_index in a HEAD request"},
476546
)
477547

548+
xcom_query = _build_xcom_read(
549+
dag_id=dag_id,
550+
run_id=run_id,
551+
task_id=task_id,
552+
key=key,
553+
session=session,
554+
dag_bag=dag_bag,
555+
token=token,
556+
previous_iteration=previous_iteration,
557+
sequence=True,
558+
).statement
478559
count = get_query_count(xcom_query, session=session)
479560
# Tell the caller how many items in this query. We define a custom range unit (HTTP spec only defines
480561
# "bytes" but we can add our own)
@@ -509,7 +590,7 @@ def get_xcom(
509590
session: SessionDep,
510591
params: Annotated[GetXcomFilterParams, Query()],
511592
dag_bag: DagBagDep,
512-
token: ExecutionToken = CurrentExecutionToken,
593+
token: ExecutionToken = _TriggerAwareToken,
513594
) -> XComResponse:
514595
"""Get an Airflow XCom from database - not other XCom Backends."""
515596
owner = None
@@ -643,7 +724,7 @@ def set_xcom(
643724
mapped_length: Annotated[
644725
int | None, Query(ge=0, description="Number of mapped tasks this value expands into")
645726
] = None,
646-
token=CurrentTIToken,
727+
token=_TriggerAwareTIToken,
647728
):
648729
"""Set an Airflow XCom."""
649730
from airflow.configuration import conf
@@ -773,7 +854,7 @@ def delete_xcom(
773854
task_id: str,
774855
key: Annotated[str, Path(min_length=1)],
775856
map_index: Annotated[int, Query()] = -1,
776-
token=CurrentTIToken,
857+
token=_TriggerAwareTIToken,
777858
):
778859
"""Delete a single XCom Value."""
779860
owner = _find_writer_id(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
_REQUEST_SCOPE_LIVE_ATTEMPT_KEY = "live_attempt_checked"
142142
_REQUEST_SCOPE_JOB_KEY = "dag_processor_job_id"
143143
_IN_PROCESS_NON_TI_CALLER = "airflow_in_process_non_ti_caller"
144+
_IN_PROCESS_TRIGGERER_ID = "airflow_in_process_triggerer_id"
144145
_SKIP_AUTO_TI_ATTEMPT_LIVE = "skip_auto_ti_attempt_live"
145146
_P = ParamSpec("_P")
146147
_R = TypeVar("_R")

0 commit comments

Comments
 (0)