简体   繁体   English

担心Django的内容类型安全性

[英]Worries About Django's Content-Type Security

I'm starting to use Django's Content-Type to allow the decoupling of the various Apps in my project and I've been liking a lot. 我开始使用Django的Content-Type来允许项目中各种Apps的分离,而我一直很喜欢。 But, something worries me: security. 但是,让我担心的是:安全性。

The way I'm using is passing via GET, the parameters of content_type and object_id . 我使用的方式是通过GET传递的,这些参数是content_typeobject_id For now, it's enough, since everything is public available in my site, anyway. 现在,这已经足够了,因为无论如何,所有内容都可以在我的网站上公开获得。

But worries me, when thinking about the private section of my site,the possibility of one user, changing the URL access information of another's. 但是让我担心的是,当考虑到我网站的私有部分时,一个用户可能会更改另一个用户的URL访问信息。 I thought many options to try to avoid this problem, but I'm not sure. 我想过很多选择可以避免此问题,但是我不确定。 Passing this in POST rather then GET seems the first thing that could be done, but that would only circumvent the real problem. 在POST中而不是在GET中传递它似乎是可以做的第一件事,但这只会绕开真正的问题。 Also, I thought in test the logged in user for permissions, but since I'm dealing with generic relationships it is not obvious the type of test needed to authenticate the permission. 另外,我还考虑过测试登录用户的权限,但是由于我正在处理通用关系,因此认证权限所需的测试类型并不明显。 Maybe something using cookies, or context variables... 也许是使用Cookie或上下文变量的东西...

So, I thought in asking how you guys use Content-Type for these cases. 因此,我想问一下在这些情况下你们如何使用Content-Type。 I'm really missing some good examples of how to use this awesome feature in a proper manner. 我确实缺少一些很好的示例,说明了如何以适当的方式使用此出色功能。

Any help? 有什么帮助吗?

The answer greatly depends on your model and application. 答案很大程度上取决于您的模型和应用程序。 How exactly are you using the content-type framework? 您究竟如何使用内容类型框架?

Generally speaking, it's recommended to have an additional abstraction layer which controls the use of the content-type framework. 一般而言,建议有一个附加的抽象层来控制内容类型框架的使用。

Example

Let me construct an example. 让我构造一个例子。 Let's say you have these models: Portal , Cube and Cake . 假设您有以下模型: PortalCubeCake Portal and Cube are public, whereas Cake is private for users with specific permissions. PortalCube是公共的,而Cake对于具有特定权限的用户是私有的。

As far as i understood you, your approach is something like this: 据我了解,您的方法是这样的:

# gets called via GET with parameters content_type_id and object_id
def modify_object(request, content_type_id, object_id)
    content_type = ContentType.objects.get_for_id(content_type_id)
    model_class = content_type.model_class()
    instance = model_class.objects.get(pk=object_id)
    # modify instance - could also be a "Cake"
    instance.save()

This is vulnerable if you want to allow only certain types of object to be modified. 如果只想允许某些类型的对象被修改,则这很容易受到攻击。 You could add a check for the content_type, but that does not seem very sophisticated and cleverly designed. 您可以添加一个对content_type的检查,但这似乎并不十分复杂且设计得很巧妙。

Instead, i would go for a less generic approach. 相反,我会选择一种不太通用的方法。 Define methods for each of the different tasks on your models you want to allow your users: 为要允许用户的模型上的每个不同任务定义方法:

def create_portal(request, object_id):
    portal = Portal.objects.get(pk=object_id)
    # create the portal
    portal.save()

def carry_cube(request, object_id):
    # load, move the cube and save

@permission_required('cake.can_eat')
def eat_cake(request, object_id):
    # this will only be performed if the current user has the required permissions
    # load, eat the delicious cake and save

Hopefully that information is helpful. 希望这些信息对您有所帮助。 With more input from your side it's easier to give a more detailed answer. 有了您身边的更多意见,更容易给出更详细的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM