Skip to content

Python: visit function parameter and return annotations in new CFG#21943

Merged
yoff merged 0 commit into
yoff/python-shared-cfg-dataflow-flipfrom
yoff/python-cfg-parameter-annotations
Jun 29, 2026
Merged

Python: visit function parameter and return annotations in new CFG#21943
yoff merged 0 commit into
yoff/python-shared-cfg-dataflow-flipfrom
yoff/python-cfg-parameter-annotations

Conversation

@yoff

@yoff yoff commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #21937 (and the broader new-CFG stack: #21921, #21923, #21925).

Root cause. The new control flow graph (semmle.python.controlflow.internal.Cfg, introduced in #21921) did not emit CFG nodes for parameter type annotations or for the return-type annotation on function definitions. The legacy CFG emitted both. LocalSources::annotatedInstance (and a small handful of framework models) walk the parameter-annotation expression via its CFG node to identify that a parameter receives an instance of the annotated class, so after the dataflow flip (#21925) annotation-based parameter tracking was silently broken.

Fix. Extend FunctionDefExpr.getChild to visit each annotation as a child of the function-def expression, in CPython evaluation order: positional parameter annotations, *args annotation, keyword-only parameter annotations, **kwargs annotation, then the return annotation. Lambda expressions have no annotations in Python syntax, so LambdaExpr is unchanged. PEP 695 type parameters remain out of scope (they belong to the inner annotation scope, not the enclosing CFG).

Results.

  • ✅ FastAPI Depends() receivers, Pydantic request bodies, Starlette WebSocket handlers, and the experimental call-graph type-annotation test all detect again.
  • CWE-022-PathInjection regains the lost fastapi_path_injection.py:26-27 flow path through file_handler: FileHandler = Depends().
  • ✅ Two FastAPI list-comprehension MISSING markers become positive (taint_test.py:41,55).
  • ✅ CPython CFG consistency overview remains empty (all 11 checks pass).
  • ✅ Full Python test suite: 973/975 passing — same 2 pre-existing extractor failures as the cfg-exc baseline.

Stack: #21921 (P3) → #21923 (P4) → #21925 (Flip) → #21937 (CFG-Exc) → this.

@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from c34dc45 to f79f239 Compare June 4, 2026 22:13
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 8a5ed32 to 96099ce Compare June 4, 2026 22:13
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from f79f239 to b783ed6 Compare June 5, 2026 08:12
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 96099ce to 709566e Compare June 5, 2026 08:12
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from b783ed6 to 158fb5b Compare June 18, 2026 13:17
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 038c537 to 537d704 Compare June 18, 2026 13:17
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from 158fb5b to 9c1d64b Compare June 18, 2026 15:12
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 537d704 to 19436d2 Compare June 18, 2026 15:12
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from 9c1d64b to cada7e9 Compare June 18, 2026 15:18
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 19436d2 to 08e2f37 Compare June 18, 2026 15:18
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from cada7e9 to 7d5fd6b Compare June 22, 2026 11:56
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 08e2f37 to 8a0f3e9 Compare June 22, 2026 11:56
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from 7d5fd6b to 8e79ca5 Compare June 22, 2026 12:28
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch 2 times, most recently from 59c80eb to e842264 Compare June 22, 2026 12:45
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from 8e79ca5 to 3e5256d Compare June 22, 2026 12:45
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from e842264 to 8933010 Compare June 22, 2026 13:47
@yoff yoff force-pushed the yoff/python-cfg-modelling-exceptions branch from 3e5256d to 92e0331 Compare June 22, 2026 13:47
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 8933010 to a0a9ddf Compare June 24, 2026 08:04
@github-actions github-actions Bot added C# C++ Go Rust Pull requests that update Rust code labels Jun 24, 2026
@yoff yoff changed the base branch from yoff/python-cfg-modelling-exceptions to yoff/python-shared-cfg-dataflow-flip June 24, 2026 08:05
@yoff yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch from 93cae5f to acb4a58 Compare June 24, 2026 08:18
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from a0a9ddf to c6ed878 Compare June 24, 2026 08:18
@github-actions github-actions Bot removed Go Rust Pull requests that update Rust code labels Jun 24, 2026
@yoff yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch from acb4a58 to 5081d81 Compare June 24, 2026 08:46
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from c6ed878 to 8189cd2 Compare June 24, 2026 08:46
@yoff yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch from 5081d81 to 4cbe7ae Compare June 25, 2026 22:20
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch 2 times, most recently from 683b96a to d3d6a3d Compare June 25, 2026 23:08
@yoff yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch 2 times, most recently from 62f34d5 to 44b8aad Compare June 29, 2026 11:40
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from d3d6a3d to 158147b Compare June 29, 2026 11:40
@yoff yoff closed this Jun 29, 2026
@yoff yoff force-pushed the yoff/python-cfg-parameter-annotations branch from 158147b to 9ce906b Compare June 29, 2026 13:25
@yoff yoff merged commit 9ce906b into yoff/python-shared-cfg-dataflow-flip Jun 29, 2026
@yoff yoff force-pushed the yoff/python-shared-cfg-dataflow-flip branch from 44b8aad to 9ce906b Compare June 29, 2026 13:25
@yoff yoff deleted the yoff/python-cfg-parameter-annotations branch June 29, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant