fix unsafe annotation copying in llm_router decorator#266
Draft
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
Draft
fix unsafe annotation copying in llm_router decorator#266semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
semgrep-code-rootflo[bot] wants to merge 1 commit intodevelopfrom
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prevent potential code injection via arbitrary type annotations in the llm_router decorator.
Changes
func.__annotations__.copy()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_hintsare evaluated inglobalsandlocalsnamespaces. Make sure that no arbitrary value can be written as the annotation and passed totyping.get_type_hintsfunction.@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.