Skip to content

Commit

Permalink
Use one regex for Hass.io URL check (#16710)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli authored and balloob committed Sep 19, 2018
1 parent 7f462ba commit d376049
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
45 changes: 22 additions & 23 deletions homeassistant/components/hassio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@

X_HASSIO = 'X-HASSIO-KEY'

NO_TIMEOUT = {
re.compile(r'^homeassistant/update$'),
re.compile(r'^host/update$'),
re.compile(r'^supervisor/update$'),
re.compile(r'^addons/[^/]*/update$'),
re.compile(r'^addons/[^/]*/install$'),
re.compile(r'^addons/[^/]*/rebuild$'),
re.compile(r'^snapshots/.*/full$'),
re.compile(r'^snapshots/.*/partial$'),
re.compile(r'^snapshots/[^/]*/upload$'),
re.compile(r'^snapshots/[^/]*/download$'),
}

NO_AUTH = {
re.compile(r'^app/.*$'),
re.compile(r'^addons/[^/]*/logo$')
}
NO_TIMEOUT = re.compile(
r'^(?:'
r'|homeassistant/update'
r'|host/update'
r'|supervisor/update'
r'|addons/[^/]+/(?:update|install|rebuild)'
r'|snapshots/.+/full'
r'|snapshots/.+/partial'
r'|snapshots/[^/]+/(?:upload|download)'
r')$'
)

NO_AUTH = re.compile(
r'^(?:'
r'|app/.*'
r'|addons/[^/]+/logo'
r')$'
)


class HassIOView(HomeAssistantView):
Expand Down Expand Up @@ -128,15 +129,13 @@ def _create_response_log(client, data):

def _get_timeout(path):
"""Return timeout for a URL path."""
for re_path in NO_TIMEOUT:
if re_path.match(path):
return 0
if NO_TIMEOUT.match(path):
return 0
return 300


def _need_auth(path):
"""Return if a path need authentication."""
for re_path in NO_AUTH:
if re_path.match(path):
return False
if NO_AUTH.match(path):
return False
return True
8 changes: 6 additions & 2 deletions tests/components/hassio/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ def test_forward_request(hassio_client):


@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."""
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
assert resp.status == 401
Expand Down

0 comments on commit d376049

Please sign in to comment.