Skip to content

fix unsafe annotation copying in llm_router decorator#266

Draft
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
semgrep-autofix/1775116929
Draft

fix unsafe annotation copying in llm_router decorator#266
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
semgrep-autofix/1775116929

Conversation

@semgrep-code-rootflo
Copy link
Copy Markdown

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 from the detection rule python.lang.security.audit.dangerous-annotations-usage.dangerous-annotations-usage.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

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

0 participants