Merge pull request #50201 from Ch3LL/_2018.3.3

[2018.3] Ensure that tokens are hex to avoid hanging/errors in cherrypy
This commit is contained in:
Nicole Thomas 2018-10-24 15:44:23 -04:00 committed by GitHub
commit 8ad9a1bd61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 1 deletions

View file

@ -1167,6 +1167,13 @@ class LowDataAdapter(object):
if token:
chunk['token'] = token
if 'token' in chunk:
# Make sure that auth token is hex
try:
int(chunk['token'], 16)
except (TypeError, ValueError):
raise cherrypy.HTTPError(401, 'Invalid token')
if client:
chunk['client'] = client
@ -2167,7 +2174,11 @@ class Events(object):
:return bool: True if valid, False if not valid.
'''
if auth_token is None:
# Make sure that auth token is hex. If it's None, or something other
# than hex, this will raise a ValueError.
try:
int(auth_token, 16)
except (TypeError, ValueError):
return False
# First check if the given token is in our session table; if so it's a

View file

@ -2,6 +2,7 @@
# Import python libs
from __future__ import absolute_import
import os
# Import salt libs
import salt.utils.json
@ -124,6 +125,71 @@ class TestRun(cptc.BaseRestCherryPyTest):
})
self.assertEqual(response.status, '401 Unauthorized')
def test_run_empty_token(self):
'''
Test the run URL with empty token
'''
cmd = dict(self.low, **{'token': ''})
body = urlencode(cmd)
request, response = self.request('/run', method='POST', body=body,
headers={
'content-type': 'application/x-www-form-urlencoded'
})
assert response.status == '401 Unauthorized'
def test_run_empty_token_upercase(self):
'''
Test the run URL with empty token with upercase characters
'''
cmd = dict(self.low, **{'ToKen': ''})
body = urlencode(cmd)
request, response = self.request('/run', method='POST', body=body,
headers={
'content-type': 'application/x-www-form-urlencoded'
})
assert response.status == '401 Unauthorized'
def test_run_wrong_token(self):
'''
Test the run URL with incorrect token
'''
cmd = dict(self.low, **{'token': 'bad'})
body = urlencode(cmd)
request, response = self.request('/run', method='POST', body=body,
headers={
'content-type': 'application/x-www-form-urlencoded'
})
assert response.status == '401 Unauthorized'
def test_run_pathname_token(self):
'''
Test the run URL with path that exists in token
'''
cmd = dict(self.low, **{'token': os.path.join('etc', 'passwd')})
body = urlencode(cmd)
request, response = self.request('/run', method='POST', body=body,
headers={
'content-type': 'application/x-www-form-urlencoded'
})
assert response.status == '401 Unauthorized'
def test_run_pathname_not_exists_token(self):
'''
Test the run URL with path that does not exist in token
'''
cmd = dict(self.low, **{'token': os.path.join('tmp', 'doesnotexist')})
body = urlencode(cmd)
request, response = self.request('/run', method='POST', body=body,
headers={
'content-type': 'application/x-www-form-urlencoded'
})
assert response.status == '401 Unauthorized'
class TestWebhookDisableAuth(cptc.BaseRestCherryPyTest):