Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tails
mat2
Commits
6a832a41
Commit
6a832a41
authored
6 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Prevent exiftool-based parameter-injection
parent
fa6c06ed
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
libmat2/images.py
+23
-16
23 additions, 16 deletions
libmat2/images.py
tests/test_libmat2.py
+10
-0
10 additions, 0 deletions
tests/test_libmat2.py
with
33 additions
and
16 deletions
libmat2/images.py
+
23
−
16
View file @
6a832a41
import
subprocess
import
json
import
os
import
shutil
import
tempfile
import
cairo
...
...
@@ -11,7 +13,26 @@ from gi.repository import GdkPixbuf
from
.
import
abstract
class
PNGParser
(
abstract
.
AbstractParser
):
class
__ImageParser
(
abstract
.
AbstractParser
):
def
get_meta
(
self
):
"""
There is no way to escape the leading(s) dash(es) of the current
self.filename to prevent parameter injections, so we do have to copy it
"""
fname
=
self
.
filename
tmpdirname
=
""
if
self
.
filename
.
startswith
(
'
-
'
):
tmpdirname
=
tempfile
.
mkdtemp
()
fname
=
os
.
path
.
join
(
tmpdirname
,
self
.
filename
)
shutil
.
copy
(
self
.
filename
,
fname
)
out
=
subprocess
.
check_output
([
'
/usr/bin/exiftool
'
,
'
-json
'
,
fname
])
if
self
.
filename
.
startswith
(
'
-
'
):
shutil
.
rmtree
(
tmpdirname
)
meta
=
json
.
loads
(
out
.
decode
(
'
utf-8
'
))[
0
]
for
key
in
self
.
meta_whitelist
:
meta
.
pop
(
key
,
None
)
return
meta
class
PNGParser
(
__ImageParser
):
mimetypes
=
{
'
image/png
'
,
}
meta_whitelist
=
{
'
SourceFile
'
,
'
ExifToolVersion
'
,
'
FileName
'
,
'
Directory
'
,
'
FileSize
'
,
'
FileModifyDate
'
,
...
...
@@ -28,30 +49,16 @@ class PNGParser(abstract.AbstractParser):
except
MemoryError
:
raise
ValueError
def
get_meta
(
self
):
out
=
subprocess
.
check_output
([
'
/usr/bin/exiftool
'
,
'
-json
'
,
self
.
filename
])
meta
=
json
.
loads
(
out
.
decode
(
'
utf-8
'
))[
0
]
for
key
in
self
.
meta_whitelist
:
meta
.
pop
(
key
,
None
)
return
meta
def
remove_all
(
self
):
surface
=
cairo
.
ImageSurface
.
create_from_png
(
self
.
filename
)
surface
.
write_to_png
(
self
.
output_filename
)
return
True
class
GdkPixbufAbstractParser
(
abstract
.
Abstract
Parser
):
class
GdkPixbufAbstractParser
(
__Image
Parser
):
"""
GdkPixbuf can handle a lot of surfaces, so we
'
re rending images on it,
this has the side-effect of removing metadata completely.
"""
def
get_meta
(
self
):
out
=
subprocess
.
check_output
([
'
/usr/bin/exiftool
'
,
'
-json
'
,
self
.
filename
])
meta
=
json
.
loads
(
out
.
decode
(
'
utf-8
'
))[
0
]
for
key
in
self
.
meta_whitelist
:
meta
.
pop
(
key
,
None
)
return
meta
def
remove_all
(
self
):
_
,
extension
=
os
.
path
.
splitext
(
self
.
filename
)
pixbuf
=
GdkPixbuf
.
Pixbuf
.
new_from_file
(
self
.
filename
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
10
−
0
View file @
6a832a41
...
...
@@ -17,6 +17,16 @@ class TestParserFactory(unittest.TestCase):
self
.
assertEqual
(
parser
.
__class__
,
audio
.
MP3Parser
)
class
TestParameterInjection
(
unittest
.
TestCase
):
def
test_ver_injection
(
self
):
shutil
.
copy
(
'
./tests/data/dirty.png
'
,
'
./-ver
'
)
p
=
images
.
PNGParser
(
'
-ver
'
)
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
Comment
'
],
'
This is a comment, be careful!
'
)
self
.
assertEqual
(
meta
[
'
ModifyDate
'
],
"
2018:03:20 21:59:25
"
)
os
.
remove
(
'
-ver
'
)
class
TestUnsupportedFiles
(
unittest
.
TestCase
):
def
test_pdf
(
self
):
shutil
.
copy
(
'
./tests/test_libmat2.py
'
,
'
./tests/clean.py
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment