Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
16f8ca56
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
16f8ca56
编写于
10月 01, 2015
作者:
R
Robert Speicher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add custom protocol whitelisting to SanitizationFilter
Addresses internal
https://dev.gitlab.org/gitlab/gitlabhq/issues/2613
上级
d7eceafb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
110 addition
and
10 deletion
+110
-10
lib/gitlab/markdown/sanitization_filter.rb
lib/gitlab/markdown/sanitization_filter.rb
+19
-0
spec/lib/gitlab/markdown/sanitization_filter_spec.rb
spec/lib/gitlab/markdown/sanitization_filter_spec.rb
+91
-10
未找到文件。
lib/gitlab/markdown/sanitization_filter.rb
浏览文件 @
16f8ca56
...
...
@@ -48,6 +48,12 @@ module Gitlab
# Allow span elements
whitelist
[
:elements
].
push
(
'span'
)
# Allow any protocol in `a` elements...
whitelist
[
:protocols
].
delete
(
'a'
)
# ...but then remove links with the `javascript` protocol
whitelist
[
:transformers
].
push
(
remove_javascript_links
)
# Remove `rel` attribute from `a` elements
whitelist
[
:transformers
].
push
(
remove_rel
)
...
...
@@ -57,6 +63,19 @@ module Gitlab
whitelist
end
def
remove_javascript_links
lambda
do
|
env
|
node
=
env
[
:node
]
return
unless
node
.
name
==
'a'
return
unless
node
.
has_attribute?
(
'href'
)
if
node
[
'href'
].
start_with?
(
'javascript'
,
':javascript'
)
node
.
remove_attribute
(
'href'
)
end
end
end
def
remove_rel
lambda
do
|
env
|
if
env
[
:node_name
]
==
'a'
...
...
spec/lib/gitlab/markdown/sanitization_filter_spec.rb
浏览文件 @
16f8ca56
...
...
@@ -44,7 +44,7 @@ module Gitlab::Markdown
instance
=
described_class
.
new
(
'Foo'
)
3
.
times
{
instance
.
whitelist
}
expect
(
instance
.
whitelist
[
:transformers
].
size
).
to
eq
4
expect
(
instance
.
whitelist
[
:transformers
].
size
).
to
eq
5
end
it
'allows syntax highlighting'
do
...
...
@@ -77,19 +77,100 @@ module Gitlab::Markdown
end
it
'removes `rel` attribute from `a` elements'
do
doc
=
filter
(
%q{<a href="#" rel="nofollow">Link</a>}
)
act
=
%q{<a href="#" rel="nofollow">Link</a>}
exp
=
%q{<a href="#">Link</a>}
expect
(
doc
.
css
(
'a'
).
size
).
to
eq
1
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
'#'
expect
(
doc
.
at_css
(
'a'
)[
'rel'
]).
to
be_nil
expect
(
filter
(
act
).
to_html
).
to
eq
exp
end
it
'removes script-like `href` attribute from `a` elements'
do
html
=
%q{<a href="javascript:alert('Hi')">Hi</a>}
doc
=
filter
(
html
)
# Adapted from the Sanitize test suite: http://git.io/vczrM
protocols
=
{
'protocol-based JS injection: simple, no spaces'
=>
{
input:
'<a href="javascript:alert(\'XSS\');">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: simple, spaces before'
=>
{
input:
'<a href="javascript :alert(\'XSS\');">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: simple, spaces after'
=>
{
input:
'<a href="javascript: alert(\'XSS\');">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: simple, spaces before and after'
=>
{
input:
'<a href="javascript : alert(\'XSS\');">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: preceding colon'
=>
{
input:
'<a href=":javascript:alert(\'XSS\');">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: UTF-8 encoding'
=>
{
input:
'<a href="javascript:">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: long UTF-8 encoding'
=>
{
input:
'<a href="javascript:">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: long UTF-8 encoding without semicolons'
=>
{
input:
'<a href=javascript:alert('XSS')>foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: hex encoding'
=>
{
input:
'<a href="javascript:">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: long hex encoding'
=>
{
input:
'<a href="javascript:">foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: hex encoding without semicolons'
=>
{
input:
'<a href=javascript:alert('XSS')>foo</a>'
,
output:
'<a>foo</a>'
},
'protocol-based JS injection: null char'
=>
{
input:
"<a href=java
\0
script:alert(
\"
XSS
\"
)>foo</a>"
,
output:
'<a href="java"></a>'
},
'protocol-based JS injection: spaces and entities'
=>
{
input:
'<a href="  javascript:alert(\'XSS\');">foo</a>'
,
output:
'<a href="">foo</a>'
},
}
protocols
.
each
do
|
name
,
data
|
it
"handles
#{
name
}
"
do
doc
=
filter
(
data
[
:input
])
expect
(
doc
.
to_html
).
to
eq
data
[
:output
]
end
end
it
'allows non-standard anchor schemes'
do
exp
=
%q{<a href="irc://irc.freenode.net/git">IRC</a>}
act
=
filter
(
exp
)
expect
(
act
.
to_html
).
to
eq
exp
end
it
'allows relative links'
do
exp
=
%q{<a href="foo/bar.md">foo/bar.md</a>}
act
=
filter
(
exp
)
expect
(
doc
.
css
(
'a'
).
size
).
to
eq
1
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
be_nil
expect
(
act
.
to_html
).
to
eq
exp
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录