Skip to content

Commit be6934b

Browse files
author
Rebecka Gulliksson
committed
Handle unknown request paths gracefully.
Closes issue #1.
1 parent 88d45e9 commit be6934b

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/satosa/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from satosa.logging_util import satosa_logging
1414
from satosa.plugin_loader import load_backends, load_frontends, load_micro_services
1515
from satosa.response import Response
16-
from satosa.routing import ModuleRouter
16+
from satosa.routing import ModuleRouter, SATOSANoBoundEndpointError
1717
from satosa.state import cookie_to_state, SATOSAStateError, State, state_to_cookie
1818

1919
__author__ = 'mathiashedstrom'
@@ -273,6 +273,8 @@ def run(self, context):
273273
spec = self.module_router.endpoint_routing(context)
274274
resp = self._run_bound_endpoint(context, spec)
275275
self._save_state(resp, context)
276+
except SATOSANoBoundEndpointError:
277+
raise
276278
except SATOSAError:
277279
satosa_logging(LOGGER, logging.ERROR, "Uncaught SATOSA error", context.state,
278280
exc_info=True)

src/satosa/proxy_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def run_server(self, environ, start_response, debug=False):
4343
if ".." in path:
4444
resp = Unauthorized()
4545
return resp(environ, start_response)
46+
elif path == "":
47+
resp = NotFound("Couldn't find the side you asked for!")
48+
return resp(environ, start_response)
49+
4650

4751
context = Context()
4852
context.path = path

src/satosa/routing.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ def endpoint_routing(self, context):
136136

137137
if backend in self.backends:
138138
context.target_backend = backend
139-
else:
140-
satosa_logging(LOGGER, logging.DEBUG, "Unknown backend %s" % backend, context.state)
141139

142140
# Search for frontend endpoint
143141
for frontend in self.frontends.keys():
@@ -151,15 +149,19 @@ def endpoint_routing(self, context):
151149
satosa_logging(LOGGER, logging.INFO, msg, context.state)
152150
return spec
153151

154-
# Search for backend endpoint
155-
for regex, spec in self.backends[backend]["endpoints"]:
156-
match = re.search(regex, context.path)
157-
if match is not None:
158-
msg = "Backend request. Module name:'{name}', endpoint: {endpoint}".format(
159-
name=backend,
160-
endpoint=context.path)
161-
satosa_logging(LOGGER, logging.INFO, msg, context.state)
162-
return spec
163-
satosa_logging(LOGGER, logging.DEBUG, "%s not bound to any function" % context.path,
164-
context.state)
152+
if backend in self.backends:
153+
# Search for backend endpoint
154+
for regex, spec in self.backends[backend]["endpoints"]:
155+
match = re.search(regex, context.path)
156+
if match is not None:
157+
msg = "Backend request. Module name:'{name}', endpoint: {endpoint}".format(
158+
name=backend,
159+
endpoint=context.path)
160+
satosa_logging(LOGGER, logging.INFO, msg, context.state)
161+
return spec
162+
satosa_logging(LOGGER, logging.DEBUG, "%s not bound to any function" % context.path,
163+
context.state)
164+
else:
165+
satosa_logging(LOGGER, logging.DEBUG, "Unknown backend %s" % backend, context.state)
166+
165167
raise SATOSANoBoundEndpointError("'{}' not bound to any function".format(context.path))

tests/test_wsgi_flow.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111
from saml2 import BINDING_HTTP_REDIRECT, BINDING_HTTP_POST
1212
from saml2.config import SPConfig, IdPConfig
13+
from saml2.httputil import NotFound
1314
from werkzeug.test import Client
1415
from werkzeug.wrappers import BaseResponse
1516

@@ -303,3 +304,10 @@ def test_flow(self):
303304

304305
identity = resp.ava
305306
assert identity["displayName"][0] == "Test Testsson"
307+
308+
def test_unknown_request_path(self):
309+
app = WsgiApplication(config=TestConfiguration.get_instance().proxy_config)
310+
test_client = Client(app.run_server, BaseResponse)
311+
312+
resp = test_client.get('/unknown')
313+
assert resp.status == NotFound._status

0 commit comments

Comments
 (0)