Fli*_*imm 5 django redirect iri
在Django 3.0 发行说明中,有这样的评论url_has_allowed_host_and_scheme:
\n\n\n为了避免有效范围可能出现的混淆,私有内部实用程序
\nis_safe_url()被重命名为url_has_allowed_host_and_scheme(). URL 具有允许的主机和方案通常并不意味着它\xe2\x80\x99s \xe2\x80\x9csafe\xe2\x80\x9d。例如,它可能仍然被错误地引用。确保同时使用iri_to_uri()不受信任的 URL 的路径组件。
我明白这样做的目的url_has_allowed_host_and_scheme是什么。以提供查询参数的常见用例next为例:http://example.com/foobar?next=http%3A%2F%2Fexample2.com%2Fhello。您可以对处理此路径的视图进行编程,以重定向到参数提供的 URL next,在本例中为: \n http://example2.com/hello。如果 URL 未经验证,则这是一个“开放重定向”漏洞。恶意行为者可能会利用开放重定向将恶意 URL 隐藏在看似可信的 URL 后面。
您可以使用它url_has_allowed_host_and_scheme来确保 URL 具有预期的主机名和方案。
我的问题是关于iri_to_uri. 该文档暗示您还需要使用此功能。我什么时候需要使用它?
以下是实现安全重定向的方法:
\nfrom django.utils.http import url_has_allowed_host_and_scheme\nfrom django.utils.encoding import iri_to_uri\nfrom django.shortcuts import redirect\n\ndef example_view(request):\n if url_has_allowed_host_and_scheme(request.GET[\'next\'], None):\n url = iri_to_uri(request.GET[\'next\'])\n return redirect(url)\n else:\n raise\nRun Code Online (Sandbox Code Playgroud)\n该iri_to_uri部分对于确保正确引用最终结果 URL 是必需的。例如:
request.GET[\'next\']等于\'/caf\xc3\xa9/\'iri_to_uri(request.GET[\'next\'])等于\'/caf%C3%A9/\'HTTP 请求中的第一行需要采用如下格式:
\nGET /caf%C3%A9/ HTTP/1.0\nRun Code Online (Sandbox Code Playgroud)\nURL 需要在那里进行转义,因为如果它包含空格之类的内容,就会破坏 HTTP 协议。
\n老实说,我仍然不完全确定为什么iri_to_uri需要它,因为 Django 的实用程序redirect会在到达 HTTP 请求中的线路之前根据需要自动转义 URL。
| 归档时间: |
|
| 查看次数: |
2740 次 |
| 最近记录: |