Commit 9deae53
Semgrep Autofix
fix unsafe annotation copying in llm_router decorator
Prevent potential code injection via arbitrary type annotations in the llm_router decorator.
## Changes
- Removed blanket copying of all annotations from decorated functions via `func.__annotations__.copy()`
- Now only copies the return annotation when needed for validation
- Added validation to ensure the annotation is an actual type object, not a string forward reference
## Why
The previous code copied all annotations from user-provided decorated functions to the wrapper. String annotations (forward references) can be evaluated by `typing.get_type_hints()` in the function's globals/locals namespace, potentially executing arbitrary code.
The fix restricts annotation copying to only the return type (which is actually used for routing validation) and rejects string annotations that could be dangerous when evaluated. The wrapper function already has proper parameter annotations defined in its signature, so those don't need to be copied.
## Semgrep Finding Details
Annotations passed to `typing.get_type_hints` are evaluated in `globals` and `locals` namespaces. Make sure that no arbitrary value can be written as the annotation and passed to `typing.get_type_hints` function.
@18578539 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/rootflo_ai/findings/683091385) from the detection rule [python.lang.security.audit.dangerous-annotations-usage.dangerous-annotations-usage](https://semgrep.dev/r/python.lang.security.audit.dangerous-annotations-usage.dangerous-annotations-usage).1 parent ceda045 commit 9deae53
1 file changed
Lines changed: 7 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1038 | 1038 | | |
1039 | 1039 | | |
1040 | 1040 | | |
1041 | | - | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
1045 | 1044 | | |
1046 | | - | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
1047 | 1049 | | |
1048 | 1050 | | |
1049 | 1051 | | |
| |||
0 commit comments