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

Drop OwnTracks bad packets #19161

Merged
merged 1 commit into from
Dec 10, 2018
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
22 changes: 15 additions & 7 deletions homeassistant/components/owntracks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ async def async_handle_mqtt_message(topic, payload, qos):


async def handle_webhook(hass, webhook_id, request):
"""Handle webhook callback."""
"""Handle webhook callback.
iOS sets the "topic" as part of the payload.
Android does not set a topic but adds headers to the request.
"""
context = hass.data[DOMAIN]['context']
message = await request.json()

try:
message = await request.json()
except ValueError:
_LOGGER.warning('Received invalid JSON from OwnTracks')
return json_response([])

# Android doesn't populate topic
if 'topic' not in message:
Expand All @@ -129,11 +138,10 @@ async def handle_webhook(hass, webhook_id, request):
device = headers.get('X-Limit-D', user)

if user is None:
_LOGGER.warning('Set a username in Connection -> Identification')
return json_response(
{'error': 'You need to supply username.'},
status=400
)
_LOGGER.warning('No topic or user found in message. If on Android,'
' set a username in Connection -> Identification')
# Keep it as a 200 response so the incorrect packet is discarded
return json_response([])

topic_base = re.sub('/#$', '', context.mqtt_topic)
message['topic'] = '{}/{}/{}'.format(topic_base, user, device)
Expand Down
25 changes: 22 additions & 3 deletions tests/components/owntracks/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_handle_value_error(mock_client):


@asyncio.coroutine
def test_returns_error_missing_username(mock_client):
def test_returns_error_missing_username(mock_client, caplog):
"""Test that an error is returned when username is missing."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
Expand All @@ -120,10 +120,29 @@ def test_returns_error_missing_username(mock_client):
}
)

assert resp.status == 400
# Needs to be 200 or OwnTracks keeps retrying bad packet.
assert resp.status == 200
json = yield from resp.json()
assert json == []
assert 'No topic or user found' in caplog.text


@asyncio.coroutine
def test_returns_error_incorrect_json(mock_client, caplog):
"""Test that an error is returned when username is missing."""
resp = yield from mock_client.post(
'/api/webhook/owntracks_test',
data='not json',
headers={
'X-Limit-d': 'Pixel',
}
)

# Needs to be 200 or OwnTracks keeps retrying bad packet.
assert resp.status == 200
json = yield from resp.json()
assert json == {'error': 'You need to supply username.'}
assert json == []
assert 'invalid JSON' in caplog.text


@asyncio.coroutine
Expand Down