Skip to content

Commit

Permalink
fix(python): shared input rules to use import wherever possible (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet authored May 16, 2024
1 parent 0dfba82 commit 51891d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
15 changes: 13 additions & 2 deletions rules/python/shared/django/user_input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ patterns:
- pattern: $<FORM>.$<METHOD>[$<_>]
filters:
- variable: FORM
regex: \A(un)?(bound_)?form\z
detection: python_shared_django_user_input_form
scope: result
- variable: METHOD
values:
- data
Expand All @@ -28,9 +29,19 @@ patterns:
- pattern: $<FORM>[$<_>].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: $<FORM>
# from <some_form_module> import <SomeFormName>
# 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
Expand Down
39 changes: 17 additions & 22 deletions rules/python/shared/lang/dynamic_input.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
type: shared
languages:
- python
imports:
- python_shared_lang_import1
patterns:
- sys.argv[$<_>]
- pattern: $<PARSER>.parse_args($<...>)
filters:
- variable: PARSER
detection: python_shared_lang_dynamic_input_parser
scope: result
- pattern: $<GETOPT>.getopt($<...>)
- pattern: $<GETOPT>($<...>)
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: $<ARG_PARSER>()
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: $<ARGPARSE>.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

0 comments on commit 51891d8

Please sign in to comment.