Require IFTTT to send data as dictionary (#32317)

* Require IFTTT to send data as dictionary

* Update logging
This commit is contained in:
Paulus Schoutsen 2020-02-29 20:37:06 -08:00 committed by GitHub
parent 8e3492d4f5
commit c6e85cac0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -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)

View File

@ -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