Skip to content

Commit

Permalink
Fix: Frame Injection (azul-private#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsotirho-ucsc committed Mar 9, 2023
1 parent c78fd8b commit 9521984
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/azul/chalice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import (
Iterable,
)
import html
import json
from json import (
JSONEncoder,
Expand Down Expand Up @@ -28,6 +29,9 @@
from furl import (
furl,
)
from werkzeug.http import (
parse_accept_header,
)

from azul import (
config,
Expand Down Expand Up @@ -95,6 +99,7 @@ def __init__(self,
self._specs: Optional[MutableJSON] = None
super().__init__(app_name, debug=config.debug > 0, configure_logs=False)
# Middleware is invoked in order of registration
self.register_middleware(self._wrapping_middleware, 'http')
self.register_middleware(self._logging_middleware, 'http')
self.register_middleware(self._lambda_context_middleware, 'all')
self.register_middleware(self._authentication_middleware, 'http')
Expand All @@ -116,6 +121,21 @@ def patched_event_source_handler(self_, event, context):
if old_handler.__code__ != patched_event_source_handler.__code__:
chalice.app.EventSourceHandler.__call__ = patched_event_source_handler

def _wrapping_middleware(self, event, get_response):
response = get_response(event)
if response.status_code >= 400:
parsed = parse_accept_header(event.headers.get('accept'))
text_html = parsed.find('text/html')
star_star = parsed.find('*/*')
if -1 < text_html and (star_star == -1 or text_html < star_star):
response.body = (
'<html>'
f'<head>Status {response.status_code}</head>'
f'<body><pre>{html.escape(str(response.body), quote=False)}</pre></body>'
'</html>'
)
return response

def _logging_middleware(self, event, get_response):
self._log_request()
response = get_response(event)
Expand Down
28 changes: 28 additions & 0 deletions test/service/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,34 @@ def test_version(self):
}
self.assertEqual(expected_json, response.json()['git'])

def test_response_error_escaping(self):
expected = {
'unescaped': json.dumps({
'Code': 'NotFoundError',
'Message': "Unable to find file 'foo', version None in catalog 'test'"
}, separators=(',', ':')),
'escaped': (
"<html><head>Status 404</head><body><pre>{"
"'Code': 'NotFoundError', "
"'Message': \"Unable to find file 'foo', version None in catalog 'test'\""
"}</pre></body></html>"
)
}
test_data = [
(None, 'unescaped'),
('*/*', 'unescaped'),
('*/*,text/html', 'unescaped'),
('text/html', 'escaped'),
('text/html,*/*', 'escaped'),
]
url = self.base_url.set(path='repository/files/foo',
args=dict(catalog=self.catalog))
for accept, response_type in test_data:
headers = {'accept': accept}
with self.subTest(headers=headers):
response = requests.get(str(url), headers=headers)
self.assertEqual(expected[response_type], response.text)


class TestFileTypeSummaries(DCP1TestCase, WebServiceTestCase):

Expand Down

0 comments on commit 9521984

Please sign in to comment.