From 51891d8edd80fcef74c09cce5eaea27808439a3d Mon Sep 17 00:00:00 2001 From: elsapet Date: Thu, 16 May 2024 15:28:37 +0200 Subject: [PATCH] fix(python): shared input rules to use import wherever possible (#402) --- rules/python/shared/django/user_input.yml | 15 +++++++-- rules/python/shared/lang/dynamic_input.yml | 39 ++++++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/rules/python/shared/django/user_input.yml b/rules/python/shared/django/user_input.yml index e8aa0bac..3601047f 100644 --- a/rules/python/shared/django/user_input.yml +++ b/rules/python/shared/django/user_input.yml @@ -19,7 +19,8 @@ patterns: - pattern: $
.$[$<_>] filters: - variable: FORM - regex: \A(un)?(bound_)?form\z + detection: python_shared_django_user_input_form + scope: result - variable: METHOD values: - data @@ -28,9 +29,19 @@ patterns: - pattern: $[$<_>].value() filters: - variable: FORM - regex: \A(un)?(bound_)?form\z + detection: python_shared_django_user_input_form + scope: result auxiliary: + - id: python_shared_django_user_input_form + patterns: + - pattern: $ + # from import + # form = FormName(request.POST) + filters: + - variable: FORM + regex: \A(un)?(bound_)?form\z - id: python_shared_django_user_input_request + # passed to a view method e.g. def my_view(request) patterns: - request - req diff --git a/rules/python/shared/lang/dynamic_input.yml b/rules/python/shared/lang/dynamic_input.yml index 8bd08721..d145e559 100644 --- a/rules/python/shared/lang/dynamic_input.yml +++ b/rules/python/shared/lang/dynamic_input.yml @@ -1,6 +1,8 @@ type: shared languages: - python +imports: + - python_shared_lang_import1 patterns: - sys.argv[$<_>] - pattern: $.parse_args($<...>) @@ -8,36 +10,29 @@ patterns: - variable: PARSER detection: python_shared_lang_dynamic_input_parser scope: result - - pattern: $.getopt($<...>) + - pattern: $($<...>) filters: - variable: GETOPT - detection: python_shared_lang_dynamic_input_getopt - scope: result + detection: python_shared_lang_import1 + scope: cursor + filters: + - variable: MODULE1 + values: [getopt] + - variable: NAME + values: [getopt] auxiliary: - id: python_shared_lang_dynamic_input_parser patterns: - pattern: $() filters: - variable: ARG_PARSER - detection: python_shared_lang_dynamic_input_argument_parser - - id: python_shared_lang_dynamic_input_argument_parser - patterns: - - ArgumentParser # fallback - - from argparse import ArgumentParser as $$<_> - - pattern: $.ArgumentParser - filters: - - variable: ARGPARSE - detection: python_shared_lang_dynamic_input_argparse_import - - id: python_shared_lang_dynamic_input_argparse_import - patterns: - - import $argparse - - import argparse as $$<_> - - argparse # fallback - - id: python_shared_lang_dynamic_input_getopt - patterns: - - import $getopt - - import getopt as $$<_> - - getopt # fallback + detection: python_shared_lang_import1 + scope: cursor + filters: + - variable: MODULE1 + values: [argparse] + - variable: NAME + values: [ArgumentParser] metadata: description: "Python dynamic input." id: python_shared_lang_dynamic_input