mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Forbid float NaN in JSON (#18757)
This commit is contained in:
parent
5c3a4e3d10
commit
a2386f871d
@ -45,8 +45,9 @@ class HomeAssistantView:
|
|||||||
"""Return a JSON response."""
|
"""Return a JSON response."""
|
||||||
try:
|
try:
|
||||||
msg = json.dumps(
|
msg = json.dumps(
|
||||||
result, sort_keys=True, cls=JSONEncoder).encode('UTF-8')
|
result, sort_keys=True, cls=JSONEncoder, allow_nan=False
|
||||||
except TypeError as err:
|
).encode('UTF-8')
|
||||||
|
except (ValueError, TypeError) as err:
|
||||||
_LOGGER.error('Unable to serialize to JSON: %s\n%s', err, result)
|
_LOGGER.error('Unable to serialize to JSON: %s\n%s', err, result)
|
||||||
raise HTTPInternalServerError
|
raise HTTPInternalServerError
|
||||||
response = web.Response(
|
response = web.Response(
|
||||||
|
@ -13,11 +13,12 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.helpers.json import JSONEncoder
|
from homeassistant.helpers.json import JSONEncoder
|
||||||
|
|
||||||
from .const import MAX_PENDING_MSG, CANCELLATION_ERRORS, URL
|
from .const import MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR
|
||||||
from .auth import AuthPhase, auth_required_message
|
from .auth import AuthPhase, auth_required_message
|
||||||
from .error import Disconnect
|
from .error import Disconnect
|
||||||
|
from .messages import error_message
|
||||||
|
|
||||||
JSON_DUMP = partial(json.dumps, cls=JSONEncoder)
|
JSON_DUMP = partial(json.dumps, cls=JSONEncoder, allow_nan=False)
|
||||||
|
|
||||||
|
|
||||||
class WebsocketAPIView(HomeAssistantView):
|
class WebsocketAPIView(HomeAssistantView):
|
||||||
@ -58,9 +59,12 @@ class WebSocketHandler:
|
|||||||
self._logger.debug("Sending %s", message)
|
self._logger.debug("Sending %s", message)
|
||||||
try:
|
try:
|
||||||
await self.wsock.send_json(message, dumps=JSON_DUMP)
|
await self.wsock.send_json(message, dumps=JSON_DUMP)
|
||||||
except TypeError as err:
|
except (ValueError, TypeError) as err:
|
||||||
self._logger.error('Unable to serialize to JSON: %s\n%s',
|
self._logger.error('Unable to serialize to JSON: %s\n%s',
|
||||||
err, message)
|
err, message)
|
||||||
|
await self.wsock.send_json(error_message(
|
||||||
|
message['id'], ERR_UNKNOWN_ERROR,
|
||||||
|
'Invalid JSON in response'))
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _send_message(self, message):
|
def _send_message(self, message):
|
||||||
|
@ -10,6 +10,6 @@ async def test_invalid_json(caplog):
|
|||||||
view = HomeAssistantView()
|
view = HomeAssistantView()
|
||||||
|
|
||||||
with pytest.raises(HTTPInternalServerError):
|
with pytest.raises(HTTPInternalServerError):
|
||||||
view.json(object)
|
view.json(float("NaN"))
|
||||||
|
|
||||||
assert str(object) in caplog.text
|
assert str(float("NaN")) in caplog.text
|
||||||
|
@ -300,3 +300,19 @@ async def test_states_filters_visible(hass, hass_admin_user, websocket_client):
|
|||||||
|
|
||||||
assert len(msg['result']) == 1
|
assert len(msg['result']) == 1
|
||||||
assert msg['result'][0]['entity_id'] == 'test.entity'
|
assert msg['result'][0]['entity_id'] == 'test.entity'
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_states_not_allows_nan(hass, websocket_client):
|
||||||
|
"""Test get_states command not allows NaN floats."""
|
||||||
|
hass.states.async_set('greeting.hello', 'world', {
|
||||||
|
'hello': float("NaN")
|
||||||
|
})
|
||||||
|
|
||||||
|
await websocket_client.send_json({
|
||||||
|
'id': 5,
|
||||||
|
'type': commands.TYPE_GET_STATES,
|
||||||
|
})
|
||||||
|
|
||||||
|
msg = await websocket_client.receive_json()
|
||||||
|
assert not msg['success']
|
||||||
|
assert msg['error']['code'] == const.ERR_UNKNOWN_ERROR
|
||||||
|
Loading…
x
Reference in New Issue
Block a user