Merge lp:~blamar/nova/negative-limits-offsets into lp:~hudson-openstack/nova/trunk

Proposed by Brian Lamar
Status: Merged
Approved by: Rick Harris
Approved revision: 752
Merged at revision: 756
Proposed branch: lp:~blamar/nova/negative-limits-offsets
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 68 lines (+28/-1)
2 files modified
nova/api/openstack/common.py (+7/-1)
nova/tests/api/openstack/test_common.py (+21/-0)
To merge this branch: bzr merge lp:~blamar/nova/negative-limits-offsets
Reviewer Review Type Date Requested Status
Rick Harris (community) Approve
Devin Carlen (community) Approve
Review via email: mp+51818@code.launchpad.net

Description of the change

Very simple change checking for < 0 values in "limit" and "offset" GET parameters. If either are negative, raise a HTTPBadRequest exception. Relevant tests included.

To post a comment you must log in.
Revision history for this message
Devin Carlen (devcamcar) wrote :

lgtm

review: Approve
Revision history for this message
Rick Harris (rconradharris) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nova/api/openstack/common.py'
2--- nova/api/openstack/common.py 2011-02-09 18:30:40 +0000
3+++ nova/api/openstack/common.py 2011-03-01 21:04:33 +0000
4@@ -15,6 +15,8 @@
5 # License for the specific language governing permissions and limitations
6 # under the License.
7
8+import webob.exc
9+
10 from nova import exception
11
12
13@@ -27,7 +29,8 @@
14 GET variables. 'offset' is where to start in the list,
15 and 'limit' is the maximum number of items to return. If
16 'limit' is not specified, 0, or > max_limit, we default
17- to max_limit.
18+ to max_limit. Negative values for either offset or limit
19+ will cause exc.HTTPBadRequest() exceptions to be raised.
20 @kwarg max_limit: The maximum number of items to return from 'items'
21 """
22 try:
23@@ -40,6 +43,9 @@
24 except ValueError:
25 limit = max_limit
26
27+ if offset < 0 or limit < 0:
28+ raise webob.exc.HTTPBadRequest()
29+
30 limit = min(max_limit, limit or max_limit)
31 range_end = offset + limit
32 return items[offset:range_end]
33
34=== modified file 'nova/tests/api/openstack/test_common.py'
35--- nova/tests/api/openstack/test_common.py 2011-02-23 19:56:37 +0000
36+++ nova/tests/api/openstack/test_common.py 2011-03-01 21:04:33 +0000
37@@ -19,6 +19,7 @@
38 Test suites for 'common' code used throughout the OpenStack HTTP API.
39 """
40
41+import webob.exc
42
43 from webob import Request
44
45@@ -160,3 +161,23 @@
46 self.assertEqual(limited(items, req, max_limit=2000), items[3:])
47 req = Request.blank('/?offset=3000&limit=10')
48 self.assertEqual(limited(items, req, max_limit=2000), [])
49+
50+ def test_limiter_negative_limit(self):
51+ """
52+ Test a negative limit.
53+ """
54+ def _limit_large():
55+ limited(self.large, req, max_limit=2000)
56+
57+ req = Request.blank('/?limit=-3000')
58+ self.assertRaises(webob.exc.HTTPBadRequest, _limit_large)
59+
60+ def test_limiter_negative_offset(self):
61+ """
62+ Test a negative offset.
63+ """
64+ def _limit_large():
65+ limited(self.large, req, max_limit=2000)
66+
67+ req = Request.blank('/?offset=-30')
68+ self.assertRaises(webob.exc.HTTPBadRequest, _limit_large)