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

Fix CherryPy CORS on Python3 #55207

Merged
merged 2 commits into from
Dec 7, 2019
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
6 changes: 4 additions & 2 deletions salt/netapi/rest_cherrypy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,11 @@ def cors_tool():
resp_head['Connection'] = 'keep-alive'
resp_head['Access-Control-Max-Age'] = '1400'

# CORS requests should short-circuit the other tools.
cherrypy.response.body = ''
# Note: CherryPy on Py3 uses binary objects for the response
# Python 2.6 also supports the byte prefix, so no need for conditionals
cherrypy.response.body = b''
cherrypy.response.status = 200
# CORS requests should short-circuit the other tools.
cherrypy.serving.request.handler = None

# Needed to avoid the auth_tool check.
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/netapi/test_rest_cherrypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def test_yaml_ctype(self):
))
self.assertEqual(response.status, '200 OK')
self.assertDictEqual(request.unserialized_data, data)


class TestCors(BaseToolsTest):
def __get_cp_config__(self):
return {
'tools.cors_tool.on': True,
}

def test_option_request(self):
request, response = self.request(
'/', method='OPTIONS', headers=(
('Origin', 'https://domain.com'),
))
self.assertEqual(response.status, '200 OK')
self.assertEqual(response.headers.get(
'Access-Control-Allow-Origin'), 'https://domain.com')