Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b120e171
Commit
b120e171
authored
Dec 02, 2020
by
Aleksei Lipniagov
Committed by
Patrick Bajao
Dec 02, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not resize when image is less than 8 bytes, add comments
parent
5b34783e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
5 deletions
+41
-5
changelogs/unreleased/alipniagov-comment-on-buffer-size.yml
changelogs/unreleased/alipniagov-comment-on-buffer-size.yml
+5
-0
internal/imageresizer/image_resizer.go
internal/imageresizer/image_resizer.go
+13
-5
internal/imageresizer/image_resizer_test.go
internal/imageresizer/image_resizer_test.go
+23
-0
No files found.
changelogs/unreleased/alipniagov-comment-on-buffer-size.yml
0 → 100644
View file @
b120e171
---
title
:
Do not resize when image is less than 8 bytes
merge_request
:
666
author
:
type
:
other
internal/imageresizer/image_resizer.go
View file @
b120e171
...
...
@@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"math"
"net"
"net/http"
"os"
...
...
@@ -272,6 +271,10 @@ func (r *Resizer) tryResizeImage(req *http.Request, f *imageFile, params *resize
return
f
.
reader
,
nil
,
fmt
.
Errorf
(
"%d bytes exceeds maximum file size of %d bytes"
,
f
.
contentLength
,
cfg
.
MaxFilesize
)
}
if
f
.
contentLength
<
maxMagicLen
{
return
f
.
reader
,
nil
,
fmt
.
Errorf
(
"file is too small to resize: %d bytes"
,
f
.
contentLength
)
}
if
!
r
.
numScalerProcs
.
tryIncrement
(
int32
(
cfg
.
MaxScalerProcs
))
{
return
f
.
reader
,
nil
,
fmt
.
Errorf
(
"too many running scaler processes (%d / %d)"
,
r
.
numScalerProcs
.
n
,
cfg
.
MaxScalerProcs
)
}
...
...
@@ -282,11 +285,16 @@ func (r *Resizer) tryResizeImage(req *http.Request, f *imageFile, params *resize
r
.
numScalerProcs
.
decrement
()
}()
// Prevents EOF if the file is smaller than 8 bytes
bufferSize
:=
int
(
math
.
Min
(
maxMagicLen
,
float64
(
f
.
contentLength
)))
buffered
:=
bufio
.
NewReaderSize
(
f
.
reader
,
bufferSize
)
// Creating buffered Reader is required for us to Peek into first bytes of the image file to detect the format
// without advancing the reader (we need to read from the file start in the Scaler binary).
// We set `8` as the minimal buffer size by the length of PNG magic bytes sequence (JPEG needs only 2).
// In fact, `NewReaderSize` will immediately override it with `16` using its `minReadBufferSize` -
// here we are just being explicit about the buffer size required for our code to operate correctly.
// Having a reader with such tiny buffer will not hurt the performance during further operations,
// because Golang `bufio.Read` avoids double copy: https://golang.org/src/bufio/bufio.go?s=1768:1804#L212
buffered
:=
bufio
.
NewReaderSize
(
f
.
reader
,
maxMagicLen
)
headerBytes
,
err
:=
buffered
.
Peek
(
bufferSize
)
headerBytes
,
err
:=
buffered
.
Peek
(
maxMagicLen
)
if
err
!=
nil
{
return
buffered
,
nil
,
fmt
.
Errorf
(
"peek stream: %v"
,
err
)
}
...
...
internal/imageresizer/image_resizer_test.go
View file @
b120e171
...
...
@@ -198,6 +198,29 @@ func TestServeOriginalImageWhenSourceImageFormatIsNotAllowed(t *testing.T) {
require
.
Equal
(
t
,
svgImage
,
responseData
,
"expected original image"
)
}
func
TestServeOriginalImageWhenSourceImageIsTooSmall
(
t
*
testing
.
T
)
{
content
:=
[]
byte
(
"PNG"
)
// 3 bytes only, invalid as PNG/JPEG image
img
,
err
:=
ioutil
.
TempFile
(
""
,
"*.png"
)
require
.
NoError
(
t
,
err
)
defer
img
.
Close
()
defer
os
.
Remove
(
img
.
Name
())
_
,
err
=
img
.
Write
(
content
)
require
.
NoError
(
t
,
err
)
cfg
:=
config
.
DefaultImageResizerConfig
params
:=
resizeParams
{
Location
:
img
.
Name
(),
ContentType
:
"image/png"
,
Width
:
64
}
resp
:=
requestScaledImage
(
t
,
nil
,
params
,
cfg
)
require
.
Equal
(
t
,
http
.
StatusOK
,
resp
.
StatusCode
)
responseData
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
content
,
responseData
,
"expected original image"
)
}
// The Rails applications sends a Base64 encoded JSON string carrying
// these parameters in an HTTP response header
func
encodeParams
(
t
*
testing
.
T
,
p
*
resizeParams
)
string
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment