Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0db6a14a
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0db6a14a
编写于
10月 10, 2017
作者:
T
Tim Masliuchenko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add allow_other_host option to redirect_back method
上级
ac1ee519
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
7 deletion
+37
-7
actionpack/lib/action_controller/metal/redirecting.rb
actionpack/lib/action_controller/metal/redirecting.rb
+16
-7
actionpack/test/controller/redirect_test.rb
actionpack/test/controller/redirect_test.rb
+21
-0
未找到文件。
actionpack/lib/action_controller/metal/redirecting.rb
浏览文件 @
0db6a14a
...
...
@@ -79,15 +79,18 @@ def redirect_to(options = {}, response_status = {})
# redirect_back fallback_location: "/images/screenshot.jpg"
# redirect_back fallback_location: posts_url
# redirect_back fallback_location: proc { edit_post_url(@post) }
# redirect_back fallback_location: '/', allow_other_host: false
#
# All options that can be passed to <tt>redirect_to</tt> are accepted as
# ==== Options
# * <tt>:fallback_location</tt> - The default fallback location that will be used on missing `Referer` header.
# * <tt>:allow_other_host</tt> - Allows or dissallow redirection to the host that is different to the current host
#
# All other options that can be passed to <tt>redirect_to</tt> are accepted as
# options and the behavior is identical.
def
redirect_back
(
fallback_location
:,
**
args
)
if
referer
=
request
.
headers
[
"Referer"
]
redirect_to
referer
,
**
args
else
redirect_to
fallback_location
,
**
args
end
def
redirect_back
(
fallback_location
:,
allow_other_host:
true
,
**
args
)
referer
=
request
.
headers
[
"Referer"
]
redirect_to_referer
=
referer
&&
(
allow_other_host
||
_url_host_allowed?
(
referer
))
redirect_to
redirect_to_referer
?
referer
:
fallback_location
,
**
args
end
def
_compute_redirect_to_location
(
request
,
options
)
#:nodoc:
...
...
@@ -120,5 +123,11 @@ def _extract_redirect_to_status(options, response_status)
302
end
end
def
_url_host_allowed?
(
url
)
URI
(
url
.
to_s
).
host
==
request
.
host
rescue
ArgumentError
,
URI
::
Error
false
end
end
end
actionpack/test/controller/redirect_test.rb
浏览文件 @
0db6a14a
...
...
@@ -62,6 +62,10 @@ def redirect_back_with_status
redirect_back
(
fallback_location:
"/things/stuff"
,
status:
307
)
end
def
safe_redirect_back_with_status
redirect_back
(
fallback_location:
"/things/stuff"
,
status:
307
,
allow_other_host:
false
)
end
def
host_redirect
redirect_to
action:
"other_host"
,
only_path:
false
,
host:
"other.test.host"
end
...
...
@@ -259,6 +263,23 @@ def test_redirect_back_with_no_referer
assert_equal
"http://test.host/things/stuff"
,
redirect_to_url
end
def
test_safe_redirect_back_from_other_host
@request
.
env
[
"HTTP_REFERER"
]
=
"http://another.host/coming/from"
get
:safe_redirect_back_with_status
assert_response
307
assert_equal
"http://test.host/things/stuff"
,
redirect_to_url
end
def
test_safe_redirect_back_from_the_same_host
referer
=
"http://test.host/coming/from"
@request
.
env
[
"HTTP_REFERER"
]
=
referer
get
:safe_redirect_back_with_status
assert_response
307
assert_equal
referer
,
redirect_to_url
end
def
test_redirect_to_record
with_routing
do
|
set
|
set
.
draw
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录