【问题标题】:Securing Django OAuth Toolkit Views保护 Django OAuth 工具包视图
【发布时间】:2017-04-01 15:58:13
【问题描述】:

我们希望在后端实现 Django OAuth,以便集成 Alexa 和其他 3rd 方 API。我们一直在关注他们网站 (http://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial.html) 上的教程,但遇到了一个迄今为止我们没有发现的安全问题:

是否存在任何用户都可以访问https://<oursite.com>/o/applications 的安全问题?如果是这样,需要采取哪些步骤来阻止用户访问这些视图?

关于 SO 的唯一相关问题并不是特别有帮助:

Secure creation of new applications in Django OAuth Toolkit

Disable or restrict /o/applications (django rest framework, oauth2)

【问题讨论】:

    标签: python django oauth alexa django-oauth


    【解决方案1】:

    我正在做类似的事情,我相信任何人都可以看到 /o/applications 是一个安全问题 - 据我所知,该页面旨在成为开发实用程序,而不是生产页面。事实上,在the django-oauth-toolkit documentation 中,他们有一个代码示例,对视图的访问权限更加受限。

    from django.conf.urls import url
    import oauth2_provider.views as oauth2_views
    from django.conf import settings
    from .views import ApiEndpoint
    
    # OAuth2 provider endpoints
    oauth2_endpoint_views = [
        url(r'^authorize/$', oauth2_views.AuthorizationView.as_view(), name="authorize"),
        url(r'^token/$', oauth2_views.TokenView.as_view(), name="token"),
        url(r'^revoke-token/$', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
    ]
    
    if settings.DEBUG:
        # OAuth2 Application Management endpoints
        oauth2_endpoint_views += [
            url(r'^applications/$', oauth2_views.ApplicationList.as_view(), name="list"),
            url(r'^applications/register/$', oauth2_views.ApplicationRegistration.as_view(), name="register"),
            url(r'^applications/(?P<pk>\d+)/$', oauth2_views.ApplicationDetail.as_view(), name="detail"),
            url(r'^applications/(?P<pk>\d+)/delete/$', oauth2_views.ApplicationDelete.as_view(), name="delete"),
            url(r'^applications/(?P<pk>\d+)/update/$', oauth2_views.ApplicationUpdate.as_view(), name="update"),
        ]
    
        # OAuth2 Token Management endpoints
        oauth2_endpoint_views += [
            url(r'^authorized-tokens/$', oauth2_views.AuthorizedTokensListView.as_view(), name="authorized-token-list"),
            url(r'^authorized-tokens/(?P<pk>\d+)/delete/$', oauth2_views.AuthorizedTokenDeleteView.as_view(),
                name="authorized-token-delete"),
        ]
    
    urlpatterns = [
        # OAuth 2 endpoints:
        url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")),
    
        url(r'^admin/', include(admin.site.urls)),
        url(r'^api/hello', ApiEndpoint.as_view()),  # an example resource endpoint
    ]
    

    revoke token view is part of the RFC,所以需要一个。我在我的应用中采用了类似的方法,只包括 AuthorizationView、TokenView 和 RevokeTokenView。

    希望有帮助!

    【讨论】:

    • docs.djangoproject.com/en/2.0/howto/deployment/checklist 说“你绝不能在生产中启用调试。”因此,我相信他们给了我们一个提示,我们应该对这些敏感端点实施我们自己的访问控制。
    • 是的,敏感端点完全是可选的,不应该在生产中使用。事实上,就我而言,将它们完全排除在外并通过 Django Admin 管理授权的应用程序更容易。
    【解决方案2】:

    这是一个安全问题,我建议仅将访问权限限制为具有活动帐户的超级用户,如 urls.py 中的以下代码所示:

    from django.contrib.auth.decorators import user_passes_test
    import oauth2_provider.views as oauth2_views
    
    def is_super(user):
        return user.is_superuser and user.is_active
    
    oauth2_endpoint_views = [
        url(r'^authorize/$', oauth2_views.AuthorizationView.as_view(), name="authorize"),
        url(r'^token/$', oauth2_views.TokenView.as_view(), name="token"),
        url(r'^revoke-token/$', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
        # the above are public but we restrict the following:
        url(r'^applications/$', user_passes_test(is_super)(oauth2_views.ApplicationList.as_view()), name="list"),
        ...
    ]
    urlpatterns = [url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider"))]
    

    【讨论】:

    • 嘿,我不完全确定这到底是如何相关的,尤其是因为没有上下文说明它所达到的效果。
    • 明白了。说得通。谢谢。
    【解决方案3】:

    要排除 'applications/' 端点,只需导入所需的 url,而不是使用整个 oauth2_provider.urls

    from oauth2_provider.urls import app_name, base_urlpatterns, management_urlpatterns
    
    urlpatterns = [
        ...
        # oauth2
        path('oauth2/', include((base_urlpatterns, app_name), namespace='oauth2_provider'))
    ]
    

    只会添加客户端应用授权所需的 url:

    oauth2/ ^authorize/$ [name='authorize']
    oauth2/ ^token/$ [name='token']
    oauth2/ ^revoke_token/$ [name='revoke-token']
    oauth2/ ^introspect/$ [name='introspect'] 
    

    要添加/删除应用程序,您可以使用 Django 管理站点,或者允许管理员用户使用 management_urlpatterns,如 @David Chander 回答:https://stackoverflow.com/a/49210935/7709003

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2015-01-19
      • 2014-04-02
      • 2019-04-16
      • 2018-08-08
      • 1970-01-01
      • 2018-04-28
      • 2020-02-06
      相关资源
      最近更新 更多