Skip to content

Commit

Permalink
Merge pull request #55207 from niflostancu/fix-cherrypy-cors
Browse files Browse the repository at this point in the history
Fix CherryPy CORS on Python3
  • Loading branch information
dwoz authored Dec 7, 2019
2 parents 9d1d3b1 + 213b1ea commit 825a3b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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')

0 comments on commit 825a3b0

Please sign in to comment.