Skip to content

Commit

Permalink
Restrict number of operations per request
Browse files Browse the repository at this point in the history
  • Loading branch information
agschwender committed Dec 4, 2016
1 parent 431c785 commit 004f349
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
9 changes: 5 additions & 4 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
* 1.1.2: Added keep JPEG quality option and set JPEG subsampling to keep
* 1.1.3: Fix auto rotation on JPEG with missing Exif data
* 1.1.4: Exception handling around invalid Exif data
* 1.1.5: Fix image requests without content types
* 1.1.5: Fixed image requests without content types
* 1.1.6: Support custom applications that need command line arguments
* 1.1.7: Support adapt resize mode
* 1.1.8: Add preserve Exif flag
* 1.1.9: Increase Pillow version to 2.8.1
* 1.1.10: Add ca_certs option
* 1.1.8: Added preserve Exif flag
* 1.1.9: Increased Pillow version to 2.8.1
* 1.1.10: Added ca_certs option
* 1.1.11: Added support for TIFF
* 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
13 changes: 8 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ To see a list of all available options, run
--filter default filter to use when resizing
--help show this help information
--implicit_base_url prepend protocol/host to url paths
--max_operations maximum operations to perform (default 10)
--max_requests max concurrent requests (default 40)
--operation default operation to perform
--optimize default to optimize when saving
Expand Down Expand Up @@ -609,21 +610,23 @@ Changelog
- 1.1.0: Proxy server support
- 1.1.1: Added JPEG auto rotation based on Exif orientation
- 1.1.2: Added keep JPEG quality option and set JPEG subsampling to keep
- 1.1.3: Fix auto rotation on JPEG with missing Exif data
- 1.1.3: Fixed auto rotation on JPEG with missing Exif data
- 1.1.4: Exception handling around invalid Exif data
- 1.1.5: Fix image requests without content types
- 1.1.5: Fixed image requests without content types
- 1.1.6: Support custom applications that need command line arguments
- 1.1.7: Support adapt resize mode
- 1.1.8: Add preserve Exif flag
- 1.1.9: Increase Pillow version to 2.8.1
- 1.1.10: Add ca_certs option
- 1.1.8: Added preserve Exif flag
- 1.1.9: Increased Pillow version to 2.8.1
- 1.1.10: Added ca_certs option
- 1.1.11: Added support for TIFF
- 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

TODO
====

Expand Down
15 changes: 8 additions & 7 deletions pilbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,27 @@
* 1.1.0: Proxy server support
* 1.1.1: Added JPEG auto rotation based on Exif orientation
* 1.1.2: Added keep JPEG quality option and set JPEG subsampling to keep
* 1.1.3: Fix auto rotation on JPEG with missing Exif data
* 1.1.3: Fixed auto rotation on JPEG with missing Exif data
* 1.1.4: Exception handling around invalid Exif data
* 1.1.5: Fix for images requests without content types
* 1.1.5: Fixed image requests without content types
* 1.1.6: Support custom applications that need command line arguments
* 1.1.7: Support adapt resize mode
* 1.1.8: Add preserve Exif flag
* 1.1.9: Increase Pillow version to 2.8.1
* 1.1.10: Add ca_certs option
* 1.1.8: Added preserve Exif flag
* 1.1.9: Increased Pillow version to 2.8.1
* 1.1.10: Added ca_certs option
* 1.1.11: Added support for TIFF
* 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
"""

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

# 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, 0, 0)
version_info = (1, 2, 1, 0)
4 changes: 4 additions & 0 deletions pilbox/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
define("client_key", help="client key")
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)

# request related settings
define("max_requests", help="max concurrent requests", type=int, default=40)
Expand Down Expand Up @@ -98,6 +99,7 @@ def __init__(self, **kwargs):
allowed_hosts=options.allowed_hosts,
allowed_operations=set(
options.allowed_operations or ImageHandler.OPERATIONS),
max_operations=options.max_operations,
background=options.background,
expand=options.expand,
filter=options.filter,
Expand Down Expand Up @@ -297,6 +299,8 @@ def _validate_operation(self):
operations = set(self._get_operations())
if not operations.issubset(self.settings.get("allowed_operations")):
raise errors.OperationError("Unsupported operation")
elif len(operations) > self.settings.get("max_operations"):
raise errors.OperationError("Too many operations")

def _validate_url(self):
url = self.get_argument("url")
Expand Down
27 changes: 27 additions & 0 deletions pilbox/test/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ def test_valid_default_operation(self):
self.assertEqual(resp.buffer.read(), expected.read(), msg)


class AppMaxOperationsTest(AsyncHTTPTestCase, _AppAsyncMixin):
def get_app(self):
return _PilboxTestApplication(max_operations=1)

def test_invalid_max_operations(self):
qs = urlencode(dict(
url=self.get_url("/test/data/test1.jpg"),
op="resize,rotate",
deg=180,
w=100,
h=100
))
resp = self.fetch_error(400, "/?%s" % qs)
self.assertEqual(resp.get("error_code"),
errors.OperationError.get_code())

def test_valid_max_operations(self):
url = self.get_url("/test/data/test1.jpg")
qs = urlencode(dict(url=url, op="noop"))
resp = self.fetch_success("/?%s" % qs)
expected_path = os.path.join(
os.path.dirname(__file__), "data", "test1.jpg")
msg = "/?%s does not match %s" % (qs, expected_path)
with open(expected_path, "rb") as expected:
self.assertEqual(resp.buffer.read(), expected.read(), msg)


class AppOverrideContentTypeTest(AsyncHTTPTestCase, _AppAsyncMixin):
def get_app(self):
return _PilboxTestApplication(content_type_from_image=True)
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.0',
version='1.2.1',
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 004f349

Please sign in to comment.