Skip to content

Commit

Permalink
Restrict maximumn resize width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
agschwender committed Dec 4, 2016
1 parent 004f349 commit efe2cbe
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@
* 1.2.0: Support setting background when saving a transparent image
- *Backwards incompatible*: default background property changed to `0fff`. To restore previous behavior, set background in config to `ffff`.
* 1.2.1: Added max operations config property
* 1.2.2: Added max resize width and height config properties
9 changes: 3 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ To see a list of all available options, run
--implicit_base_url prepend protocol/host to url paths
--max_operations maximum operations to perform (default 10)
--max_requests max concurrent requests (default 40)
--max_resize_height maximum resize height (default 15000)
--max_resize_width maximum resize width (default 15000)
--operation default operation to perform
--optimize default to optimize when saving
--port run on the given port (default 8888)
Expand Down Expand Up @@ -626,9 +628,4 @@ Changelog
to ``ffff``.

- 1.2.1: Added max operations config property

TODO
====

- How to reconcile unavailable color profiles?
- Add backends (S3, file system, etc...) if necessary
- 1.2.2: Added max resize width and height config properties
5 changes: 3 additions & 2 deletions pilbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@
`0fff`. To restore previous behavior, set background in config
to `ffff`.
* 1.2.1: Added max operations config property
* 1.2.2: Added max resize width and height config properties
"""

# human-readable version number
version = "1.2.1"
version = "1.2.2"

# The first three numbers are the components of the version number.
# The fourth is zero for an official release, positive for a development
# branch, or negative for a release candidate or beta (after the base version
# number has been incremented)
version_info = (1, 2, 1, 0)
version_info = (1, 2, 2, 0)
12 changes: 10 additions & 2 deletions pilbox/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
define("allowed_hosts", help="valid hosts", default=[], multiple=True)
define("allowed_operations", help="valid ops", default=[], multiple=True)
define("max_operations", help="maximum operations to perform", default=10)
define("max_resize_height", help="maximum resize height", default=15000)
define("max_resize_width", help="maximum resize width", default=15000)

# request related settings
define("max_requests", help="max concurrent requests", type=int, default=40)
Expand Down Expand Up @@ -100,6 +102,8 @@ def __init__(self, **kwargs):
allowed_operations=set(
options.allowed_operations or ImageHandler.OPERATIONS),
max_operations=options.max_operations,
max_resize_height=options.max_resize_height,
max_resize_width=options.max_resize_width,
background=options.background,
expand=options.expand,
filter=options.filter,
Expand Down Expand Up @@ -167,8 +171,12 @@ def validate_request(self):
opts = self._get_save_options()
ops = self._get_operations()
if "resize" in ops:
Image.validate_dimensions(
self.get_argument("w"), self.get_argument("h"))
w, h = self.get_argument("w"), self.get_argument("h")
Image.validate_dimensions(w, h)
if w and int(w) > self.settings.get("max_resize_width"):
raise errors.DimensionsError("Exceeds maximum allowed width")
elif h and int(h) > self.settings.get("max_resize_height"):
raise errors.DimensionsError("Exceeds maximum allowed height")
opts.update(self._get_resize_options())
if "rotate" in ops:
Image.validate_degree(self.get_argument("deg"))
Expand Down
10 changes: 10 additions & 0 deletions pilbox/test/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,22 @@ def test_invalid_width(self):
self.assertEqual(resp.get("error_code"),
errors.DimensionsError.get_code())

qs = urlencode(dict(url="http://foo.co/x.jpg", w=15001, h=1))
resp = self.fetch_error(400, "/?%s" % qs)
self.assertEqual(resp.get("error_code"),
errors.DimensionsError.get_code())

def test_invalid_height(self):
qs = urlencode(dict(url="http://foo.co/x.jpg", w=1, h="a"))
resp = self.fetch_error(400, "/?%s" % qs)
self.assertEqual(resp.get("error_code"),
errors.DimensionsError.get_code())

qs = urlencode(dict(url="http://foo.co/x.jpg", w=1, h=15001))
resp = self.fetch_error(400, "/?%s" % qs)
self.assertEqual(resp.get("error_code"),
errors.DimensionsError.get_code())

def test_invalid_degree(self):
qs = urlencode(dict(url="http://foo.co/x.jpg", op="rotate", deg="a"))
resp = self.fetch_error(400, "/?%s" % qs)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run(self):


setup(name='pilbox',
version='1.2.1',
version='1.2.2',
description='Pilbox is an image processing application server built on the Tornado web framework using the Pillow Imaging Library',
long_description=readme,
classifiers=[
Expand Down

0 comments on commit efe2cbe

Please sign in to comment.