Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling for malformed auth headers #858

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ def test_request_authorizer_no_headers(current_request, mock_get_urs_url):
assert authorizer.get_success_response_headers() == {}


def test_request_authorizer_malformed_header(current_request):
current_request.headers = {
"Authorization": "token",
"x-origin-request-id": "origin_request_id",
}
authorizer = app.RequestAuthorizer()

assert authorizer.get_profile() is None
response = authorizer.get_error_response()
assert response.status_code == 400
assert response.body == {
"status_code": 400,
"error_description": "Malformed Authorization header",
}


@mock.patch(f"{MODULE}.get_profile_with_jwt_bearer", autospec=True)
def test_request_authorizer_bearer_header(
mock_get_profile_with_jwt_bearer,
Expand Down
35 changes: 33 additions & 2 deletions thin_egress_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@
self._response = do_auth_and_return(app.current_request.context)
return None

method, token, *_ = authorization.split()
method = method.lower()
parsed_auth = self._parse_auth_header(authorization)
if parsed_auth is None:
return None

method, args = parsed_auth

if method == "bearer":
token, *_ = args
# we will deal with "bearer" auth here. "Basic" auth will be handled by do_auth_and_return()
log.debug("we got an Authorization header. %s", authorization)
user_profile, self._response = self._get_profile_and_response_from_bearer(token)
Expand All @@ -203,6 +207,33 @@
self._response = do_auth_and_return(app.current_request.context)
return None

def _parse_auth_header(self, authorization: str):
try:
method, token, *args = authorization.split()
method = method.lower()
return method, (token, *args)
except Exception:
status_code = 400
if check_for_browser(app.current_request.headers):
template_vars = {

Check warning on line 218 in thin_egress_app/app.py

View check run for this annotation

Codecov / codecov/patch

thin_egress_app/app.py#L218

Added line #L218 was not covered by tests
"title": "Bad Request",
"status_code": status_code,
"contentstring": "Malformed Authorization header",
"requestid": get_request_id(),
}

self._response = make_html_response(template_vars, {}, status_code, "error.html")

Check warning on line 225 in thin_egress_app/app.py

View check run for this annotation

Codecov / codecov/patch

thin_egress_app/app.py#L225

Added line #L225 was not covered by tests
else:
self._response = Response(
body={
"status_code": status_code,
"error_description": "Malformed Authorization header",
},
status_code=status_code,
headers={},
)
return None

@with_trace()
@cachetools.cached(
cachetools.TTLCache(maxsize=32, ttl=1 * 60),
Expand Down
Loading