Drop OwnTracks bad packets (#19161)

This commit is contained in:
Paulus Schoutsen 2018-12-10 12:24:56 +01:00 committed by GitHub
parent fe2d24c240
commit fdea9cb426
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 11 deletions

View file

@ -118,9 +118,18 @@ async def async_connect_mqtt(hass, component):
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:
@ -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)

View file

@ -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',
@ -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 == {'error': 'You need to supply username.'}
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 == []
assert 'invalid JSON' in caplog.text
@asyncio.coroutine