Skip to content

fix: check the permission matching each Starship API request method - #201

Merged
m1racoli merged 3 commits into
mainfrom
fix/write-permission-checks
Oct 30, 2026
Merged

m1racoli merged 3 commits into
mainfrom
fix/write-permission-checks

Conversation

@odaneau-astro

@odaneau-astro odaneau-astro commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

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:

Route GET POST PATCH DELETE
/connections Connection read Connection create Connection delete
/variables Variable read Variable create Variable delete
/pools Pool read Pool create Pool delete
/dags Dag read Dag edit
/dag_runs Dag run read Dag run create Dag run delete
/task_instances, /task_instance_history Task instance read Task instance create
/task_log Task log read Task instance create (1) Task instance delete (1)
/xcom XCom read XCom create on Airflow 3, task instance create on Airflow 2 (1) XCom delete
/env_vars Connection read (2)
  1. Flask AppBuilder (FAB) has no create or delete permission for task logs, and no create permission for XComs. These routes use the task instance permissions instead.
  2. Environment variables can hold AIRFLOW_CONN_* secrets.

On Airflow 3, the routes use the requires_access_* dependencies from airflow.api_fastapi.core_api.security, the same ones the Airflow REST API uses. A small requires_access_by_method dependency applies them per request method.

On Airflow 2, a requires_access(**permissions_by_method) decorator calls auth.has_access with configuration read plus the FAB permissions for the request method.

Affected roles

  • FAB Admin and Op roles: no change, because they already have all of these permissions.
  • Airflow 3 SimpleAuthManager Viewer and User roles: lose access to connections, variables, pools, and environment variables, and to any write that Airflow doesn't otherwise allow them.
  • Custom roles with only configuration read: need the matching resource permissions.

This PR doesn't cover /telescope or filtering secrets out of /env_vars. Separate PRs handle those.

Test plan

  • tests/af3_api_permissions_test.py, using SimpleAuthManager on 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 against main.
  • Manual check on Airflow 2.10.5 with a real webserver app, because CI only installs Airflow 3. On main, a role with only can read on Configurations could 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.
  • Full pytest suite on Airflow 3.3.2: 133 passed, 30 skipped. The only error is validation_test.py::test_docker_pytest, which needs Docker and fails the same way on main.
  • ruff 0.14.5 check and format.
  • Manual: run a full UI migration with just dev2 start and just dev3 start as an Admin user.

@codecov-commenter

codecov-commenter commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.92308% with 112 lines in your changes missing coverage. Please review.
✅ Project coverage is 40.29%. Comparing base (15b24da) to head (ecac81a).

Files with missing lines Patch % Lines
tests/af2_api_permissions_test.py 5.68% 83 Missing ⚠️
astronomer_starship/_af2/starship_api.py 0.00% 27 Missing ⚠️
tests/af3_api_permissions_test.py 97.64% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@odaneau-astro
odaneau-astro force-pushed the fix/write-permission-checks branch from 2356c48 to 61d5312 Compare October 22, 2026 13:56

@m1racoli m1racoli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ecac81a. Every DAG route now uses a new requires_access_dags(method, access_entity) dependency:

  • It collects every dag_id the request names: the query string, the body's dag_id, and the dag_id of each record in bulk bodies ({"dag_runs": [...]}, {"task_instances": [...]}).
  • It authorizes all of them in one batch_is_authorized_dag call, the same approach as Airflow's own requires_access_dag_run_bulk.
  • Without any dag_id, the user still needs access to all DAGs.
  • A dag_id that isn't a string, or an invalid JSON body, returns 400.
  • Team names are passed when [core] multi_team is 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"))],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ecac81a: /env_vars now requires (can_read, Connections) and (can_read, Variables). Covered by test_env_vars_needs_connection_and_variable_read in tests/af2_api_permissions_test.py (added in 7f62f71 along with the other Airflow 2 permission tests).

"/xcom",
methods=["GET", "POST", "DELETE"],
dependencies=[
Depends(_access_by_method(requires_access_dag, "GET", "POST", "DELETE", access_entity=DagAccessEntity.XCOM))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@m1racoli
m1racoli force-pushed the fix/write-permission-checks branch from 61d5312 to 7f62f71 Compare October 22, 2026 20:15
- 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>
@m1racoli
m1racoli dismissed their stale review October 22, 2026 20:36

followed up with comments and addressed them with additional commits

@m1racoli
m1racoli merged commit 7ec8ad6 into main Oct 30, 2026
6 checks passed
@m1racoli
m1racoli deleted the fix/write-permission-checks branch October 30, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants