From 0e7b746e85ae193dc381c159f1dc188445221291 Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Mon, 2 Dec 2013 23:59:19 +0000 Subject: [PATCH] Deny API requests where context doesn't match path We shouldn't overwrite the context tenant_id (which comes from the scope of the auth_token) with that from the path, instead raise a HTTPForbidden exception if the path-provided tenant_id doesn't match the context. Change-Id: Ib6fb9881103312f7492081a20178f12309f35d81 Closes-Bug: #1256983 --- heat/api/openstack/v1/util.py | 5 ++-- heat/tests/test_api_openstack_v1.py | 6 ----- heat/tests/test_api_openstack_v1_util.py | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 heat/tests/test_api_openstack_v1_util.py diff --git a/heat/api/openstack/v1/util.py b/heat/api/openstack/v1/util.py index f3aebc1..3d4716f 100644 --- a/heat/api/openstack/v1/util.py +++ b/heat/api/openstack/v1/util.py @@ -21,12 +21,13 @@ from heat.common import identifier def tenant_local(handler): ''' - Decorator for a handler method that sets the correct tenant_id in the + Decorator for a handler method that checks the path matches the request context. ''' @wraps(handler) def handle_stack_method(controller, req, tenant_id, **kwargs): - req.context.tenant_id = tenant_id + if req.context.tenant_id != tenant_id: + raise exc.HTTPForbidden() return handler(controller, req, **kwargs) return handle_stack_method diff --git a/heat/tests/test_api_openstack_v1.py b/heat/tests/test_api_openstack_v1.py index dc28d0b..2b63322 100644 --- a/heat/tests/test_api_openstack_v1.py +++ b/heat/tests/test_api_openstack_v1.py @@ -628,12 +628,6 @@ class StackControllerTest(ControllerTest, unittest.TestCase): req = self._get('/stacks/%(stack_name)s/%(stack_id)s' % identity) - self.m.StubOutWithMock(rpc, 'call') - rpc.call(req.context, self.topic, - {'method': 'show_stack', - 'args': {'stack_identity': dict(identity)}, - 'version': self.api_version}, - None).AndRaise(rpc_common.RemoteError("InvalidTenant")) self.m.ReplayAll() self.assertRaises(webob.exc.HTTPForbidden, diff --git a/heat/tests/test_api_openstack_v1_util.py b/heat/tests/test_api_openstack_v1_util.py new file mode 100644 index 0000000..e8c1e65 --- /dev/null +++ b/heat/tests/test_api_openstack_v1_util.py @@ -0,0 +1,45 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest +import mox +from nose.plugins.attrib import attr +from webob import exc + +from heat.api.openstack.v1 import util +from heat.common import context +from heat.common.wsgi import Request + + +@attr(tag=['unit', 'api-openstack-v1-util']) +@attr(speed='fast') +class TestTenantLocal(unittest.TestCase): + def setUp(self): + self.m = mox.Mox() + self.req = Request({}) + self.req.context = context.RequestContext(tenant_id='foo') + + def tearDown(self): + self.m.UnsetStubs() + + def test_tenant_local(self): + @util.tenant_local + def an_action(controller, req): + return 'woot' + + self.assertEqual('woot', + an_action(None, self.req, tenant_id='foo')) + + self.assertRaises(exc.HTTPForbidden, + an_action, None, self.req, tenant_id='bar') -- 1.8.3.1