Use one regex for Hass.io URL check (#16710)

This commit is contained in:
Pascal Vizeli 2018-09-19 12:57:55 +02:00 committed by Paulus Schoutsen
parent 7f462ba0ec
commit d376049a3f
2 changed files with 27 additions and 24 deletions

View File

@ -22,23 +22,24 @@ _LOGGER = logging.getLogger(__name__)
X_HASSIO = 'X-HASSIO-KEY' X_HASSIO = 'X-HASSIO-KEY'
NO_TIMEOUT = { NO_TIMEOUT = re.compile(
re.compile(r'^homeassistant/update$'), r'^(?:'
re.compile(r'^host/update$'), r'|homeassistant/update'
re.compile(r'^supervisor/update$'), r'|host/update'
re.compile(r'^addons/[^/]*/update$'), r'|supervisor/update'
re.compile(r'^addons/[^/]*/install$'), r'|addons/[^/]+/(?:update|install|rebuild)'
re.compile(r'^addons/[^/]*/rebuild$'), r'|snapshots/.+/full'
re.compile(r'^snapshots/.*/full$'), r'|snapshots/.+/partial'
re.compile(r'^snapshots/.*/partial$'), r'|snapshots/[^/]+/(?:upload|download)'
re.compile(r'^snapshots/[^/]*/upload$'), r')$'
re.compile(r'^snapshots/[^/]*/download$'), )
}
NO_AUTH = { NO_AUTH = re.compile(
re.compile(r'^app/.*$'), r'^(?:'
re.compile(r'^addons/[^/]*/logo$') r'|app/.*'
} r'|addons/[^/]+/logo'
r')$'
)
class HassIOView(HomeAssistantView): class HassIOView(HomeAssistantView):
@ -128,15 +129,13 @@ def _create_response_log(client, data):
def _get_timeout(path): def _get_timeout(path):
"""Return timeout for a URL path.""" """Return timeout for a URL path."""
for re_path in NO_TIMEOUT: if NO_TIMEOUT.match(path):
if re_path.match(path):
return 0 return 0
return 300 return 300
def _need_auth(path): def _need_auth(path):
"""Return if a path need authentication.""" """Return if a path need authentication."""
for re_path in NO_AUTH: if NO_AUTH.match(path):
if re_path.match(path):
return False return False
return True return True

View File

@ -36,9 +36,13 @@ def test_forward_request(hassio_client):
@asyncio.coroutine @asyncio.coroutine
def test_auth_required_forward_request(hassio_client): @pytest.mark.parametrize(
'build_type', [
'supervisor/info', 'homeassistant/update', 'host/info'
])
def test_auth_required_forward_request(hassio_client, build_type):
"""Test auth required for normal request.""" """Test auth required for normal request."""
resp = yield from hassio_client.post('/api/hassio/beer') resp = yield from hassio_client.post("/api/hassio/{}".format(build_type))
# Check we got right response # Check we got right response
assert resp.status == 401 assert resp.status == 401