Repository navigation
fix: check the permission matching each Starship API request method - #201
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #201 +/- ##
==========================================
+ Coverage 37.06% 40.29% +3.23%
==========================================
Files 26 28 +2
Lines 2439 2675 +236
==========================================
+ Hits 904 1078 +174
- Misses 1535 1597 +62 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
2356c48 to
61d5312
Compare
m1racoli
left a comment
There was a problem hiding this comment.
dismissing my approval. will follow up with details
Every Starship API route checked only read access to the Airflow configuration, including the routes that create or delete connections, variables, pools, Dag runs, task instances, and XComs. On Airflow 3 with the simple auth manager, the Viewer role has configuration read access. Configuration read stays as the baseline for every route. Each route also checks the Airflow permission for its resource and request method. For example, creating a connection needs connection POST on Airflow 3 and can_create on Connections on Airflow 2. The env vars route needs connection read, because environment variables can hold connection secrets.
| `requires_access_by_method(GET=requires_access_pool("GET"), POST=requires_access_pool("POST"))`. | ||
| """ | ||
|
|
||
| async def inner(request: Request, user: GetUserDep) -> None: |
There was a problem hiding this comment.
Per-DAG checks can be bypassed (Airflow 3)
requires_access_dag takes the dag_id from the path or query string only (request.path_params.get("dag_id") or request.query_params.get("dag_id")). The handlers read it through get_from_request, which is json.get(key, args.get(key)), so the JSON body wins.
With an auth manager that scopes access per DAG (FAB per-DAG permissions, multi-team, or a custom manager), a user allowed on DAG A only can send:
DELETE /dag_runs?dag_id=A
{"dag_id": "B"}
The check passes for A, and delete_dag_runs(dag_id="B") runs. The same applies to PATCH /dags?dag_id=A (pauses B) and to POST on /dag_runs, /task_instances, /task_instance_history and /xcom, where each body record names its own DAG.
Airflow 2 is not affected here. FAB's _has_access_fab collects dag_id from kwargs, args, form and JSON, and returns 403 when they differ.
Suggested fix: reject requests where the query and body dag_id differ, and for bulk POSTs, authorize every distinct dag_id in the body (or require an all-DAGs permission for writes).
There was a problem hiding this comment.
Fixed in ecac81a. Every DAG route now uses a new requires_access_dags(method, access_entity) dependency:
- It collects every
dag_idthe request names: the query string, the body'sdag_id, and thedag_idof each record in bulk bodies ({"dag_runs": [...]},{"task_instances": [...]}). - It authorizes all of them in one
batch_is_authorized_dagcall, the same approach as Airflow's ownrequires_access_dag_run_bulk. - Without any
dag_id, the user still needs access to all DAGs. - A
dag_idthat isn't a string, or an invalid JSON body, returns 400. - Team names are passed when
[core] multi_teamis on (Airflow 3.1+).
This also fixes Airflow 3.0 and 3.1, where Airflow's requires_access_dag only reads dag_id from the path, so ?dag_id= was ignored and every request was checked against all DAGs.
New tests in tests/af3_api_permissions_test.py use an auth manager that allows DAG a only. They cover query a + body b → 403, bulk bodies that include b → 403, and malformed input → 400.
| """ | ||
|
|
||
| async def inner(request: Request, user: GetUserDep) -> None: | ||
| result = checks[request.method](request, user) |
There was a problem hiding this comment.
Blocking DB calls on the event loop
inner is async, so FastAPI runs it on the event loop. It then calls the synchronous requires_access_* checks directly. requires_access_dag calls DagModel.get_team_name(dag_id), which is a DB query, and the FAB auth manager also queries the DB for permissions. Those calls now block the API server's event loop for every request on these routes. When FastAPI runs the same checks as sync dependencies, it puts them in its threadpool.
Under migration traffic (lots of /dag_runs, /task_instances and /xcom calls), this stalls every other request on that api-server worker.
Suggested fix: make inner a plain def so FastAPI threadpools it. None of the Airflow checks used here are awaitable, so the inspect.isawaitable branch can go. Alternatively, wrap the call in starlette.concurrency.run_in_threadpool.
There was a problem hiding this comment.
Fixed in ecac81a. requires_access_by_method and _access_by_method are gone. Each route is now registered once per HTTP method with its own Depends(...), the same way Airflow's REST API does it (e.g. routes/public/xcom.py):
@router.get("/pools", dependencies=[Depends(requires_access_pool("GET"))])
@router.post("/pools", dependencies=[Depends(requires_access_pool("POST"))])
@router.delete("/pools", dependencies=[Depends(requires_access_pool("DELETE"))])FastAPI now runs Airflow's sync checks in its threadpool. The new DAG dependency awaits the body and then runs the auth manager call with run_in_threadpool. A test asserts that the DAG check runs on an AnyIO worker thread. As a side effect, unregistered methods now return 405 instead of a KeyError.
| @router.get("/env_vars") | ||
| @router.get( | ||
| "/env_vars", | ||
| dependencies=[Depends(requires_access_connection("GET"))], |
There was a problem hiding this comment.
/env_vars only requires connection read
get_env_vars returns dict(os.environ). That includes AIRFLOW_VAR_* (variables), AIRFLOW_CONN_* (connections), AIRFLOW__* config such as the Fernet key, DB connection string and secret key, and any cloud credentials in the environment.
Gating it only on connection read lets a role with config read and connection read but no variable read pull variable values through this route, bypassing the variable check that /variables now enforces. The same applies to the Airflow 2 mirror.
Suggested fix: require at least connection read and variable read here, and arguably config read in full, since the route exposes config secrets. Better still, filter the response to the AIRFLOW_VAR_* / AIRFLOW_CONN_* keys that the Env Vars page actually needs.
There was a problem hiding this comment.
Fixed in ecac81a. /env_vars now requires both connection read and variable read. I didn't filter the response, because the Env Vars page migrates every key and reads ASTRO_ORGANIZATION_ID / ASTRO_DEPLOYMENT_ID. New tests deny either permission and assert 403.
| return starship_route(get=starship_compat.get_info) | ||
|
|
||
| @requires_config_read | ||
| @requires_access(GET=[(READ, permissions.RESOURCE_CONNECTION)]) |
There was a problem hiding this comment.
Same issue as on the Airflow 3 side: /env_vars returns the full os.environ, including AIRFLOW_VAR_* and AIRFLOW__* secrets, but only requires connection read. It should also require variable read, or the response should be filtered.
| "/xcom", | ||
| methods=["GET", "POST", "DELETE"], | ||
| dependencies=[ | ||
| Depends(_access_by_method(requires_access_dag, "GET", "POST", "DELETE", access_entity=DagAccessEntity.XCOM)) |
There was a problem hiding this comment.
XCom create permission differs between versions
Here, XCom POST requires DagAccessEntity.XCOM + POST. That matches Airflow 3's own public XCom API, so it's defensible on its own. The Airflow 2 mirror deliberately uses (can_create, Task Instances) because FAB 2.x has no XCom create permission.
So the same custom (non-Admin) role behaves differently depending on the Airflow version. On Airflow 3 with FAB, a role that can create task instances but has no can_create on XComs passes /dag_runs and /task_instances, then gets 403 on /xcom. The DAG history migration is left half done: runs and task instances are copied, XComs are not.
Pick one rule and apply it on both sides, or document the difference. One option is to use DagAccessEntity.TASK_INSTANCE here to match Airflow 2. The other is to keep XCom here and note in the docs that Airflow 3 roles need XCom create.
There was a problem hiding this comment.
Kept as is: XCom create on Airflow 3 matches Airflow's own XCom API. In ecac81a I added a "Permissions" section to docs/faq.md. It notes that custom roles on Airflow 3 need XCom create, while Airflow 2 checks Task Instance create because FAB has no XCom create permission.
Co-Authored-By: Claude <noreply@anthropic.com>
61d5312 to
7f62f71
Compare
- Airflow 3: check each dag_id in the query string and JSON body, including bulk records, in one batch_is_authorized_dag call - Airflow 3: register one route per HTTP method with its own access dependency, so FastAPI runs sync checks in its threadpool - /env_vars requires both connection and variable read - Document the permissions Starship needs Co-Authored-By: Claude <noreply@anthropic.com>
followed up with comments and addressed them with additional commits
Summary
Every Starship API route checked only one permission: read access to the Airflow configuration. That included the routes that create or delete connections, variables, pools, Dag runs, task instances, and XComs. On Airflow 3 with
SimpleAuthManager, the Viewer role has configuration read access.Configuration read stays as the baseline for every route. Each route also checks the Airflow permission for its resource and request method:
/connections/variables/pools/dags/dag_runs/task_instances,/task_instance_history/task_log/xcom/env_varsAIRFLOW_CONN_*secrets.On Airflow 3, the routes use the
requires_access_*dependencies fromairflow.api_fastapi.core_api.security, the same ones the Airflow REST API uses. A smallrequires_access_by_methoddependency applies them per request method.On Airflow 2, a
requires_access(**permissions_by_method)decorator callsauth.has_accesswith configuration read plus the FAB permissions for the request method.Affected roles
SimpleAuthManagerViewer and User roles: lose access to connections, variables, pools, and environment variables, and to any write that Airflow doesn't otherwise allow them.This PR doesn't cover
/telescopeor filtering secrets out of/env_vars. Separate PRs handle those.Test plan
tests/af3_api_permissions_test.py, usingSimpleAuthManageron Airflow 3: the Viewer role gets 403 on connection read, create, and delete, variable create, pool delete, Dag run delete, XCom create, and environment variables, but still reaches/health. The Op role can read and create connections. Eight of these cases fail againstmain.main, a role with onlycan read on Configurationscould read and create connections, delete Dag runs, and read environment variables. On this branch, Airflow refuses those calls, and the Op and Admin roles behave as before.validation_test.py::test_docker_pytest, which needs Docker and fails the same way onmain.just dev2 startandjust dev3 startas an Admin user.