pyre-check icon indicating copy to clipboard operation
pyre-check copied to clipboard

How to add source in path('admin/', admin.site.urls)

Open Peteling opened this issue 3 years ago • 3 comments

Pysa Feature Request

Is your feature request related to a problem? Please describe. hello, I am learning how to use pysa and I come across a question. Here is the problem. when using Django to initialize the project.The code automatic generated

from django.contrib import admin
from django.urls import path


urlpatterns = [
    path('admin/', admin.site.urls),
]

Compare to the exercise5, it just use the path which import from django.urls. But if I write the generate_models.py like

from django.urls import path

def main() -> None:
    # Here, specify all the generators that you might want to call.
    generators = {
        "django_path_params": generate_taint_models.RESTApiSourceGenerator(
            django_urls=view_generator.DjangoUrls(
                urls_module="urls",
                url_pattern_type=UrlPattern,
                url_resolver_type=Ignore,
            )
        ),
    }
    generate_taint_models.run_generators(
        generators,
        default_modes=[
            "django_path_params",
        ],
    )

it will be a error show out.

Traceback (most recent call last):
  File "/root/pyre-check/tools/generate_taint_models/__init__.py", line 186, in run_from_parsed_arguments
    generated_models[mode] = set(generator_options[mode].generate_models())
  File "/root/pyre-check/tools/generate_taint_models/model_generator.py", line 46, in generate_models
    return self.compute_models(self.gather_functions_to_model())
  File "/root/pyre-check/tools/generate_taint_models/get_REST_api_sources.py", line 41, in gather_functions_to_model
    return get_all_views(self.django_urls)
  File "/root/pyre-check/tools/generate_taint_models/view_generator.py", line 53, in get_all_views
    visit_all_patterns(imported_urls_module.urlpatterns)
  File "/root/pyre-check/tools/generate_taint_models/view_generator.py", line 38, in visit_all_patterns
    elif isinstance(pattern, django_urls.url_pattern_type):
TypeError: isinstance() arg 2 must be a type or tuple of types

And I try to use Model DSL, but I think DSL have not the ability to support the case I met. Please show me how to solve the problem, thanks a lot!

Describe the solution you'd like

Describe alternatives you've considered

Additional context

Peteling avatar Oct 11 '22 06:10 Peteling

I copy the wrong code before, Here is my code.

from django.urls import path

def main() -> None:
    # Here, specify all the generators that you might want to call.
    generators = {
        "django_path_params": generate_taint_models.RESTApiSourceGenerator(
            django_urls=view_generator.DjangoUrls(
                urls_module="urls",
                url_pattern_type=path,
                url_resolver_type=Ignore,
            )
        ),
    }
    generate_taint_models.run_generators(
        generators,
        default_modes=[
            "django_path_params",
        ],
    )

Peteling avatar Oct 11 '22 06:10 Peteling

Hey @PeteLing, looks like we missed this issue. If you look at the error you posted, it's saying:

    elif isinstance(pattern, django_urls.url_pattern_type):
TypeError: isinstance() arg 2 must be a type or tuple of types

The url_pattern_type argument that you passed in (url_pattern_type=path) is not a type, it's the path function. You should instead be passing in a type as you see in the Exercise 5 example:

https://github.com/facebook/pyre-check/blob/10c375bea52db5d10b71cb5206fac7da9549eb0c/documentation/pysa_tutorial/exercise5/generate_models.py#L40

gbleaney avatar Nov 01 '22 21:11 gbleaney

hi @gbleaney thanks for your reply~ But I still have no idea how to cover the example I mentioned before.

from django.contrib import admin
from django.urls import path
from views import testurl


urlpatterns = [
    path('admin/', admin.site.urls),
    path('abc/', testurl),
]

and the views.py

def testurl(request):
    result = eval(f"2 {request.operator} 2")  # noqa: P204
    return result

the path in django.urls is a function defined in file django/urls.conf.py. the Exercise5 is a example where the url_pattern_type is a class(type), but in django original code, it is a function. I have read the doc in https://pyre-check.org/docs/pysa-model-generators/ and still don't know how to handle the case. I try to change the generators as

from django.urls.resolvers import URLPattern
...
        "django_path_params": generate_taint_models.RESTApiSourceGenerator(
            django_urls=view_generator.DjangoUrls(
                urls_module="django.urls.resolvers",
                url_pattern_type=UrlPattern,
                url_resolver_type=Ignore,
            )
        ),

but got a error

2022-11-02 14:05:49 ERROR module 'django.urls.resolvers' has no attribute 'urlpatterns'
Traceback (most recent call last):
  File "/root/pyre-check/tools/generate_taint_models/__init__.py", line 186, in run_from_parsed_arguments
    generated_models[mode] = set(generator_options[mode].generate_models())
  File "/root/pyre-check/tools/generate_taint_models/model_generator.py", line 46, in generate_models
    return self.compute_models(self.gather_functions_to_model())
  File "/root/pyre-check/tools/generate_taint_models/get_REST_api_sources.py", line 41, in gather_functions_to_model
    return get_all_views(self.django_urls)
  File "/root/pyre-check/tools/generate_taint_models/view_generator.py", line 53, in get_all_views
    visit_all_patterns(imported_urls_module.urlpatterns)

In Django original comment

Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')

Can you give me some tips about how to solved the common case in Django? Still hope to receive your reply, thanks~

Peteling avatar Nov 02 '22 14:11 Peteling