Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
1a4235b3
未验证
提交
1a4235b3
编辑于
3 years ago
作者:
charlie ablett
提交者:
Yorick Peterse
3 years ago
浏览文件
操作
下载
补丁
差异文件
Detect file MIME type before checking exif headers
Before running exiftool from rake task, file's MIME type is checked.
上级
1b6d9e45
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
3
隐藏空白变更内容
行内
左右并排
显示
3 个更改的文件
changelogs/unreleased/security-327154-only-jpeg-tiff.yml
+5
-0
5 个添加, 0 个删除
changelogs/unreleased/security-327154-only-jpeg-tiff.yml
lib/gitlab/sanitizers/exif.rb
+15
-3
15 个添加, 3 个删除
lib/gitlab/sanitizers/exif.rb
spec/lib/gitlab/sanitizers/exif_spec.rb
+28
-7
28 个添加, 7 个删除
spec/lib/gitlab/sanitizers/exif_spec.rb
有
48 个添加
和
10 个删除
changelogs/unreleased/security-327154-only-jpeg-tiff.yml
0 → 100644
+
5
−
0
浏览文件 @
1a4235b3
---
title
:
Clean only legitimate JPG and TIFF files
merge_request
:
author
:
type
:
security
此差异已折叠。
点击以展开。
lib/gitlab/sanitizers/exif.rb
+
15
−
3
浏览文件 @
1a4235b3
...
@@ -45,6 +45,7 @@ class Exif
...
@@ -45,6 +45,7 @@ class Exif
ALLOWED_TAGS
=
WHITELISTED_TAGS
+
IGNORED_TAGS
ALLOWED_TAGS
=
WHITELISTED_TAGS
+
IGNORED_TAGS
EXCLUDE_PARAMS
=
WHITELISTED_TAGS
.
map
{
|
tag
|
"-
#{
tag
}
"
}
EXCLUDE_PARAMS
=
WHITELISTED_TAGS
.
map
{
|
tag
|
"-
#{
tag
}
"
}
ALLOWED_MIME_TYPES
=
%w(image/jpeg image/tiff)
.
freeze
attr_reader
:logger
attr_reader
:logger
...
@@ -96,12 +97,12 @@ def clean(uploader, dry_run: true)
...
@@ -96,12 +97,12 @@ def clean(uploader, dry_run: true)
end
end
end
end
private
def
extra_tags
(
path
)
def
extra_tags
(
path
)
exif_tags
(
path
).
keys
-
ALLOWED_TAGS
exif_tags
(
path
).
keys
-
ALLOWED_TAGS
end
end
private
def
remove_and_store
(
tmpdir
,
src_path
,
uploader
)
def
remove_and_store
(
tmpdir
,
src_path
,
uploader
)
exec_remove_exif!
(
src_path
)
exec_remove_exif!
(
src_path
)
logger
.
info
"
#{
upload_ref
(
uploader
.
upload
)
}
: exif removed, storing"
logger
.
info
"
#{
upload_ref
(
uploader
.
upload
)
}
: exif removed, storing"
...
@@ -133,15 +134,26 @@ def fetch_upload_to_file(uploader, dir)
...
@@ -133,15 +134,26 @@ def fetch_upload_to_file(uploader, dir)
# upload is stored into the file with the original name - this filename
# upload is stored into the file with the original name - this filename
# is used by carrierwave when storing the file back to the storage
# is used by carrierwave when storing the file back to the storage
filename
=
File
.
join
(
dir
,
uploader
.
filename
)
filename
=
File
.
join
(
dir
,
uploader
.
filename
)
contents
=
uploader
.
read
check_for_allowed_types
(
contents
)
File
.
open
(
filename
,
'w'
)
do
|
file
|
File
.
open
(
filename
,
'w'
)
do
|
file
|
file
.
binmode
file
.
binmode
file
.
write
uploader
.
read
file
.
write
contents
end
end
filename
filename
end
end
def
check_for_allowed_types
(
contents
)
mime_type
=
Gitlab
::
Utils
::
MimeType
.
from_string
(
contents
)
unless
ALLOWED_MIME_TYPES
.
include?
(
mime_type
)
raise
"File type
#{
mime_type
}
not supported. Only supports
#{
ALLOWED_MIME_TYPES
.
join
(
", "
)
}
."
end
end
def
upload_ref
(
upload
)
def
upload_ref
(
upload
)
"
#{
upload
.
id
}
:
#{
upload
.
path
}
"
"
#{
upload
.
id
}
:
#{
upload
.
path
}
"
end
end
...
...
此差异已折叠。
点击以展开。
spec/lib/gitlab/sanitizers/exif_spec.rb
+
28
−
7
浏览文件 @
1a4235b3
...
@@ -4,6 +4,11 @@
...
@@ -4,6 +4,11 @@
RSpec
.
describe
Gitlab
::
Sanitizers
::
Exif
do
RSpec
.
describe
Gitlab
::
Sanitizers
::
Exif
do
let
(
:sanitizer
)
{
described_class
.
new
}
let
(
:sanitizer
)
{
described_class
.
new
}
let
(
:mime_type
)
{
'image/jpeg'
}
before
do
allow
(
Gitlab
::
Utils
::
MimeType
).
to
receive
(
:from_string
).
and_return
(
mime_type
)
end
describe
'#batch_clean'
do
describe
'#batch_clean'
do
context
'with image uploads'
do
context
'with image uploads'
do
...
@@ -43,7 +48,7 @@
...
@@ -43,7 +48,7 @@
end
end
end
end
it
'filters only jpg/tiff images'
do
it
'filters only jpg/tiff images
by filename
'
do
create
(
:upload
,
path:
'filename.jpg'
)
create
(
:upload
,
path:
'filename.jpg'
)
create
(
:upload
,
path:
'filename.jpeg'
)
create
(
:upload
,
path:
'filename.jpeg'
)
create
(
:upload
,
path:
'filename.JPG'
)
create
(
:upload
,
path:
'filename.JPG'
)
...
@@ -53,12 +58,16 @@
...
@@ -53,12 +58,16 @@
create
(
:upload
,
path:
'filename.txt'
)
create
(
:upload
,
path:
'filename.txt'
)
expect
(
sanitizer
).
to
receive
(
:clean
).
exactly
(
5
).
times
expect
(
sanitizer
).
to
receive
(
:clean
).
exactly
(
5
).
times
sanitizer
.
batch_clean
sanitizer
.
batch_clean
end
end
end
end
describe
'#clean'
do
describe
'#clean'
do
let
(
:uploader
)
{
create
(
:upload
,
:with_file
,
:issuable_upload
).
retrieve_uploader
}
let
(
:uploader
)
{
create
(
:upload
,
:with_file
,
:issuable_upload
).
retrieve_uploader
}
let
(
:dry_run
)
{
false
}
subject
{
sanitizer
.
clean
(
uploader
,
dry_run:
dry_run
)
}
context
"no dry run"
do
context
"no dry run"
do
it
"removes exif from the image"
do
it
"removes exif from the image"
do
...
@@ -76,7 +85,7 @@
...
@@ -76,7 +85,7 @@
[
expected_args
,
0
]
[
expected_args
,
0
]
end
end
s
anitizer
.
clean
(
uploader
,
dry_run:
false
)
s
ubject
expect
(
uploader
.
upload
.
id
).
not_to
eq
(
original_upload
.
id
)
expect
(
uploader
.
upload
.
id
).
not_to
eq
(
original_upload
.
id
)
expect
(
uploader
.
upload
.
path
).
to
eq
(
original_upload
.
path
)
expect
(
uploader
.
upload
.
path
).
to
eq
(
original_upload
.
path
)
...
@@ -89,23 +98,35 @@
...
@@ -89,23 +98,35 @@
expect
(
sanitizer
).
not_to
receive
(
:exec_remove_exif!
)
expect
(
sanitizer
).
not_to
receive
(
:exec_remove_exif!
)
expect
(
uploader
).
not_to
receive
(
:store!
)
expect
(
uploader
).
not_to
receive
(
:store!
)
s
anitizer
.
clean
(
uploader
,
dry_run:
false
)
s
ubject
end
end
it
"raises an error if the exiftool fails with an error"
do
it
"raises an error if the exiftool fails with an error"
do
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
"error"
,
1
])
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
"error"
,
1
])
expect
{
sanitizer
.
clean
(
uploader
,
dry_run:
false
)
}.
to
raise_exception
(
RuntimeError
,
"failed to get exif tags: error"
)
expect
{
subject
}.
to
raise_exception
(
RuntimeError
,
"failed to get exif tags: error"
)
end
context
'for files that do not have the correct MIME type'
do
let
(
:mime_type
)
{
'text/plain'
}
it
'cleans only jpg/tiff images with the correct mime types'
do
expect
(
sanitizer
).
not_to
receive
(
:extra_tags
)
expect
{
subject
}.
to
raise_error
(
RuntimeError
,
/File type text\/plain not supported/
)
end
end
end
end
end
context
"dry run"
do
context
"dry run"
do
let
(
:dry_run
)
{
true
}
it
"doesn't change the image"
do
it
"doesn't change the image"
do
expect
(
sanitizer
).
to
receive
(
:extra_tags
).
and_return
({
'foo'
=>
'bar'
})
expect
(
sanitizer
).
to
receive
(
:extra_tags
).
and_return
({
'foo'
=>
'bar'
})
expect
(
sanitizer
).
not_to
receive
(
:exec_remove_exif!
)
expect
(
sanitizer
).
not_to
receive
(
:exec_remove_exif!
)
expect
(
uploader
).
not_to
receive
(
:store!
)
expect
(
uploader
).
not_to
receive
(
:store!
)
s
anitizer
.
clean
(
uploader
,
dry_run:
true
)
s
ubject
end
end
end
end
end
end
...
@@ -119,7 +140,7 @@
...
@@ -119,7 +140,7 @@
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
tags
,
0
])
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
tags
,
0
])
expect
(
sanitizer
.
extra_tags
(
'filename'
)).
not_to
be_empty
expect
(
sanitizer
.
send
(
:
extra_tags
,
'filename'
)).
not_to
be_empty
end
end
it
"returns an empty list for file with only whitelisted and ignored tags"
do
it
"returns an empty list for file with only whitelisted and ignored tags"
do
...
@@ -130,7 +151,7 @@
...
@@ -130,7 +151,7 @@
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
tags
,
0
])
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
and_return
([
tags
,
0
])
expect
(
sanitizer
.
extra_tags
(
'some file'
)).
to
be_empty
expect
(
sanitizer
.
send
(
:
extra_tags
,
'some file'
)).
to
be_empty
end
end
end
end
end
end
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录