From c6e85cac0bb796e457f4d9cb2d29489f39fe8937 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 29 Feb 2020 20:37:06 -0800 Subject: [PATCH] Require IFTTT to send data as dictionary (#32317) * Require IFTTT to send data as dictionary * Update logging --- homeassistant/components/ifttt/__init__.py | 15 ++++++++++++--- tests/components/ifttt/test_init.py | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/ifttt/__init__.py b/homeassistant/components/ifttt/__init__.py index 3011f5a2a0a..72c905497c0 100644 --- a/homeassistant/components/ifttt/__init__.py +++ b/homeassistant/components/ifttt/__init__.py @@ -93,10 +93,19 @@ async def handle_webhook(hass, webhook_id, request): try: data = json.loads(body) if body else {} except ValueError: - return None + _LOGGER.error( + "Received invalid data from IFTTT. Data needs to be formatted as JSON: %s", + body, + ) + return - if isinstance(data, dict): - data["webhook_id"] = webhook_id + if not isinstance(data, dict): + _LOGGER.error( + "Received invalid data from IFTTT. Data needs to be a dictionary: %s", data + ) + return + + data["webhook_id"] = webhook_id hass.bus.async_fire(EVENT_RECEIVED, data) diff --git a/tests/components/ifttt/test_init.py b/tests/components/ifttt/test_init.py index 74d12ba44f4..ab5aa7ea1ad 100644 --- a/tests/components/ifttt/test_init.py +++ b/tests/components/ifttt/test_init.py @@ -33,3 +33,11 @@ async def test_config_flow_registers_webhook(hass, aiohttp_client): assert len(ifttt_events) == 1 assert ifttt_events[0].data["webhook_id"] == webhook_id assert ifttt_events[0].data["hello"] == "ifttt" + + # Invalid JSON + await client.post("/api/webhook/{}".format(webhook_id), data="not a dict") + assert len(ifttt_events) == 1 + + # Not a dict + await client.post("/api/webhook/{}".format(webhook_id), json="not a dict") + assert len(ifttt_events) == 1